2015-06-08 23:54:15 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2015-10-19 00:40:59 +02:00
|
|
|
"encoding/json"
|
2015-06-13 03:56:34 +02:00
|
|
|
"io/ioutil"
|
2015-06-08 23:54:15 +02:00
|
|
|
"os"
|
2015-06-13 03:56:34 +02:00
|
|
|
"path"
|
2015-06-08 23:54:15 +02:00
|
|
|
"strings"
|
2015-12-06 23:35:52 +02:00
|
|
|
"time"
|
2015-06-08 23:54:15 +02:00
|
|
|
|
|
|
|
"github.com/codegangsta/cli"
|
|
|
|
"github.com/xenolf/lego/acme"
|
|
|
|
)
|
|
|
|
|
|
|
|
func checkFolder(path string) error {
|
|
|
|
if _, err := os.Stat(path); os.IsNotExist(err) {
|
|
|
|
return os.MkdirAll(path, 0700)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-10-19 00:40:59 +02:00
|
|
|
func setup(c *cli.Context) (*Configuration, *Account, *acme.Client) {
|
2015-06-12 23:34:49 +02:00
|
|
|
err := checkFolder(c.GlobalString("path"))
|
2015-06-08 23:54:15 +02:00
|
|
|
if err != nil {
|
2015-12-07 17:52:59 +02:00
|
|
|
logger().Fatalf("Cound not check/create path: %s", err.Error())
|
2015-06-08 23:54:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
conf := NewConfiguration(c)
|
2015-12-15 20:21:46 +02:00
|
|
|
if len(c.GlobalString("email")) == 0 {
|
2015-06-08 23:54:15 +02:00
|
|
|
logger().Fatal("You have to pass an account (email address) to the program using --email or -m")
|
|
|
|
}
|
|
|
|
|
2015-10-19 00:40:59 +02:00
|
|
|
//TODO: move to account struct? Currently MUST pass email.
|
2015-06-08 23:54:15 +02:00
|
|
|
acc := NewAccount(c.GlobalString("email"), conf)
|
2015-10-28 01:05:40 +02:00
|
|
|
|
2015-12-27 19:30:04 +02:00
|
|
|
client, err := acme.NewClient(c.GlobalString("server"), acc, conf.RsaBits())
|
2015-10-28 01:05:40 +02:00
|
|
|
if err != nil {
|
2015-12-07 17:52:59 +02:00
|
|
|
logger().Fatalf("Could not create client: %s", err.Error())
|
2015-10-28 01:05:40 +02:00
|
|
|
}
|
|
|
|
|
2015-12-27 19:30:04 +02:00
|
|
|
if len(c.GlobalStringSlice("exclude")) > 0 {
|
|
|
|
client.ExcludeChallenges(conf.ExcludedSolvers())
|
|
|
|
}
|
|
|
|
|
2016-01-08 09:05:07 +02:00
|
|
|
if c.GlobalIsSet("http") {
|
|
|
|
client.SetHTTPAddress(c.GlobalString("http"))
|
2015-12-27 19:30:04 +02:00
|
|
|
}
|
|
|
|
|
2016-01-08 09:05:07 +02:00
|
|
|
if c.GlobalIsSet("tls") {
|
|
|
|
client.SetTLSAddress(c.GlobalString("tls"))
|
2015-12-27 19:30:04 +02:00
|
|
|
}
|
|
|
|
|
2016-01-30 03:40:57 +02:00
|
|
|
if c.GlobalIsSet("dns") {
|
|
|
|
var err error
|
|
|
|
var provider acme.ChallengeProvider
|
|
|
|
switch c.GlobalString("dns") {
|
|
|
|
case "cloudflare":
|
|
|
|
provider, err = acme.NewDNSProviderCloudFlare("", "")
|
|
|
|
case "digitalocean":
|
|
|
|
authToken := os.Getenv("DO_AUTH_TOKEN")
|
|
|
|
|
|
|
|
provider, err = acme.NewDNSProviderDigitalOcean(authToken)
|
|
|
|
case "dnsimple":
|
|
|
|
provider, err = acme.NewDNSProviderDNSimple("", "")
|
|
|
|
case "route53":
|
|
|
|
awsRegion := os.Getenv("AWS_REGION")
|
|
|
|
provider, err = acme.NewDNSProviderRoute53("", "", awsRegion)
|
|
|
|
case "rfc2136":
|
|
|
|
nameserver := os.Getenv("RFC2136_NAMESERVER")
|
|
|
|
zone := os.Getenv("RFC2136_ZONE")
|
|
|
|
tsigKey := os.Getenv("RFC2136_TSIG_KEY")
|
|
|
|
tsigSecret := os.Getenv("RFC2136_TSIG_SECRET")
|
|
|
|
|
|
|
|
provider, err = acme.NewDNSProviderRFC2136(nameserver, zone, tsigKey, tsigSecret)
|
|
|
|
case "manual":
|
|
|
|
provider, err = acme.NewDNSProviderManual()
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
logger().Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
client.SetChallengeProvider(acme.DNS01, provider)
|
|
|
|
}
|
|
|
|
|
2015-10-28 01:05:40 +02:00
|
|
|
return conf, acc, client
|
2015-10-19 00:40:59 +02:00
|
|
|
}
|
|
|
|
|
2015-10-19 03:18:06 +02:00
|
|
|
func saveCertRes(certRes acme.CertificateResource, conf *Configuration) {
|
2015-10-19 00:40:59 +02:00
|
|
|
// We store the certificate, private key and metadata in different files
|
|
|
|
// as web servers would not be able to work with a combined file.
|
|
|
|
certOut := path.Join(conf.CertPath(), certRes.Domain+".crt")
|
|
|
|
privOut := path.Join(conf.CertPath(), certRes.Domain+".key")
|
|
|
|
metaOut := path.Join(conf.CertPath(), certRes.Domain+".json")
|
|
|
|
|
|
|
|
err := ioutil.WriteFile(certOut, certRes.Certificate, 0600)
|
|
|
|
if err != nil {
|
2015-12-07 17:52:59 +02:00
|
|
|
logger().Fatalf("Unable to save Certificate for domain %s\n\t%s", certRes.Domain, err.Error())
|
2015-10-19 00:40:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err = ioutil.WriteFile(privOut, certRes.PrivateKey, 0600)
|
|
|
|
if err != nil {
|
2015-12-07 17:52:59 +02:00
|
|
|
logger().Fatalf("Unable to save PrivateKey for domain %s\n\t%s", certRes.Domain, err.Error())
|
2015-10-19 00:40:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
jsonBytes, err := json.MarshalIndent(certRes, "", "\t")
|
|
|
|
if err != nil {
|
2015-12-07 17:52:59 +02:00
|
|
|
logger().Fatalf("Unable to marshal CertResource for domain %s\n\t%s", certRes.Domain, err.Error())
|
2015-10-19 00:40:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err = ioutil.WriteFile(metaOut, jsonBytes, 0600)
|
|
|
|
if err != nil {
|
2015-12-07 17:52:59 +02:00
|
|
|
logger().Fatalf("Unable to save CertResource for domain %s\n\t%s", certRes.Domain, err.Error())
|
2015-10-19 00:40:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func run(c *cli.Context) {
|
|
|
|
conf, acc, client := setup(c)
|
2015-06-08 23:54:15 +02:00
|
|
|
if acc.Registration == nil {
|
|
|
|
reg, err := client.Register()
|
|
|
|
if err != nil {
|
2015-12-07 17:52:59 +02:00
|
|
|
logger().Fatalf("Could not complete registration\n\t%s", err.Error())
|
2015-06-08 23:54:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
acc.Registration = reg
|
|
|
|
acc.Save()
|
|
|
|
|
|
|
|
logger().Print("!!!! HEADS UP !!!!")
|
|
|
|
logger().Printf(`
|
2015-09-26 20:00:19 +02:00
|
|
|
Your account credentials have been saved in your Let's Encrypt
|
|
|
|
configuration directory at "%s".
|
|
|
|
You should make a secure backup of this folder now. This
|
|
|
|
configuration directory will also contain certificates and
|
|
|
|
private keys obtained from Let's Encrypt so making regular
|
|
|
|
backups of this folder is ideal.`, conf.AccountPath(c.GlobalString("email")))
|
2015-06-08 23:54:15 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if acc.Registration.Body.Agreement == "" {
|
2015-09-26 20:00:19 +02:00
|
|
|
reader := bufio.NewReader(os.Stdin)
|
|
|
|
logger().Printf("Please review the TOS at %s", acc.Registration.TosURL)
|
|
|
|
|
|
|
|
for {
|
|
|
|
logger().Println("Do you accept the TOS? Y/n")
|
|
|
|
text, err := reader.ReadString('\n')
|
|
|
|
if err != nil {
|
2015-12-07 17:52:59 +02:00
|
|
|
logger().Fatalf("Could not read from console -> %s", err.Error())
|
2015-09-26 20:00:19 +02:00
|
|
|
}
|
2015-06-08 23:54:15 +02:00
|
|
|
|
2015-09-26 20:00:19 +02:00
|
|
|
text = strings.Trim(text, "\r\n")
|
2015-06-08 23:54:15 +02:00
|
|
|
|
2015-09-26 20:00:19 +02:00
|
|
|
if text == "n" {
|
|
|
|
logger().Fatal("You did not accept the TOS. Unable to proceed.")
|
|
|
|
}
|
2015-06-08 23:54:15 +02:00
|
|
|
|
2015-09-26 20:00:19 +02:00
|
|
|
if text == "Y" || text == "y" || text == "" {
|
2015-10-23 10:23:06 +02:00
|
|
|
err = client.AgreeToTOS()
|
2015-09-26 20:00:19 +02:00
|
|
|
if err != nil {
|
2015-12-07 17:52:59 +02:00
|
|
|
logger().Fatalf("Could not agree to tos -> %s", err)
|
2015-06-08 23:54:15 +02:00
|
|
|
}
|
2015-09-26 20:00:19 +02:00
|
|
|
acc.Save()
|
|
|
|
break
|
2015-06-08 23:54:15 +02:00
|
|
|
}
|
2015-09-26 20:00:19 +02:00
|
|
|
|
|
|
|
logger().Println("Your input was invalid. Please answer with one of Y/y, n or by pressing enter.")
|
2015-06-08 23:54:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-15 20:21:46 +02:00
|
|
|
if len(c.GlobalStringSlice("domains")) == 0 {
|
2015-12-15 20:18:51 +02:00
|
|
|
logger().Fatal("Please specify --domains or -d")
|
2015-06-08 23:54:15 +02:00
|
|
|
}
|
|
|
|
|
2016-01-08 11:14:41 +02:00
|
|
|
cert, failures := client.ObtainCertificate(c.GlobalStringSlice("domains"), true, nil)
|
2015-11-02 02:01:00 +02:00
|
|
|
if len(failures) > 0 {
|
|
|
|
for k, v := range failures {
|
2015-12-07 17:52:59 +02:00
|
|
|
logger().Printf("[%s] Could not obtain certificates\n\t%s", k, v.Error())
|
2015-11-02 02:01:00 +02:00
|
|
|
}
|
2015-12-15 20:18:51 +02:00
|
|
|
|
2015-12-07 17:52:59 +02:00
|
|
|
// Make sure to return a non-zero exit code if ObtainSANCertificate
|
|
|
|
// returned at least one error. Due to us not returning partial
|
|
|
|
// certificate we can just exit here instead of at the end.
|
|
|
|
os.Exit(1)
|
2015-06-13 03:56:34 +02:00
|
|
|
}
|
|
|
|
|
2015-11-02 02:01:00 +02:00
|
|
|
err := checkFolder(conf.CertPath())
|
2015-06-13 03:56:34 +02:00
|
|
|
if err != nil {
|
2015-12-07 17:52:59 +02:00
|
|
|
logger().Fatalf("Cound not check/create path: %s", err.Error())
|
2015-06-13 03:56:34 +02:00
|
|
|
}
|
|
|
|
|
2015-11-11 19:05:09 +02:00
|
|
|
saveCertRes(cert, conf)
|
2015-06-08 23:54:15 +02:00
|
|
|
}
|
2015-09-27 14:51:44 +02:00
|
|
|
|
|
|
|
func revoke(c *cli.Context) {
|
|
|
|
|
2015-10-19 00:40:59 +02:00
|
|
|
conf, _, client := setup(c)
|
2015-09-27 14:51:44 +02:00
|
|
|
|
2015-10-19 00:40:59 +02:00
|
|
|
err := checkFolder(conf.CertPath())
|
2015-09-27 14:51:44 +02:00
|
|
|
if err != nil {
|
2015-12-07 17:52:59 +02:00
|
|
|
logger().Fatalf("Cound not check/create path: %s", err.Error())
|
2015-09-27 14:51:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, domain := range c.GlobalStringSlice("domains") {
|
|
|
|
logger().Printf("Trying to revoke certificate for domain %s", domain)
|
|
|
|
|
|
|
|
certPath := path.Join(conf.CertPath(), domain+".crt")
|
|
|
|
certBytes, err := ioutil.ReadFile(certPath)
|
|
|
|
|
|
|
|
err = client.RevokeCertificate(certBytes)
|
|
|
|
if err != nil {
|
2015-12-07 17:52:59 +02:00
|
|
|
logger().Fatalf("Error while revoking the certificate for domain %s\n\t%s", domain, err.Error())
|
2015-09-27 14:51:44 +02:00
|
|
|
} else {
|
|
|
|
logger().Print("Certificate was revoked.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-19 00:42:04 +02:00
|
|
|
|
|
|
|
func renew(c *cli.Context) {
|
|
|
|
conf, _, client := setup(c)
|
2015-12-24 10:57:09 +02:00
|
|
|
|
2015-12-15 22:10:47 +02:00
|
|
|
if len(c.GlobalStringSlice("domains")) <= 0 {
|
|
|
|
logger().Fatal("Please specify at least one domain.")
|
|
|
|
}
|
2015-12-24 10:57:09 +02:00
|
|
|
|
2015-12-15 22:10:47 +02:00
|
|
|
domain := c.GlobalStringSlice("domains")[0]
|
2015-10-19 00:42:04 +02:00
|
|
|
|
2015-12-15 22:10:47 +02:00
|
|
|
// load the cert resource from files.
|
|
|
|
// We store the certificate, private key and metadata in different files
|
|
|
|
// as web servers would not be able to work with a combined file.
|
|
|
|
certPath := path.Join(conf.CertPath(), domain+".crt")
|
|
|
|
privPath := path.Join(conf.CertPath(), domain+".key")
|
|
|
|
metaPath := path.Join(conf.CertPath(), domain+".json")
|
2015-12-06 23:35:52 +02:00
|
|
|
|
2015-12-15 22:10:47 +02:00
|
|
|
certBytes, err := ioutil.ReadFile(certPath)
|
|
|
|
if err != nil {
|
|
|
|
logger().Fatalf("Error while loading the certificate for domain %s\n\t%s", domain, err.Error())
|
|
|
|
}
|
2015-12-06 23:35:52 +02:00
|
|
|
|
2015-12-15 22:10:47 +02:00
|
|
|
if c.IsSet("days") {
|
|
|
|
expTime, err := acme.GetPEMCertExpiration(certBytes)
|
2015-10-19 00:42:04 +02:00
|
|
|
if err != nil {
|
2015-12-15 22:10:47 +02:00
|
|
|
logger().Printf("Could not get Certification expiration for domain %s", domain)
|
2015-10-19 00:42:04 +02:00
|
|
|
}
|
|
|
|
|
2015-12-24 10:57:09 +02:00
|
|
|
if int(expTime.Sub(time.Now()).Hours()/24.0) > c.Int("days") {
|
2015-12-15 22:10:47 +02:00
|
|
|
return
|
2015-10-19 00:42:04 +02:00
|
|
|
}
|
2015-12-15 22:10:47 +02:00
|
|
|
}
|
2015-10-19 00:42:04 +02:00
|
|
|
|
2015-12-15 22:10:47 +02:00
|
|
|
metaBytes, err := ioutil.ReadFile(metaPath)
|
|
|
|
if err != nil {
|
|
|
|
logger().Fatalf("Error while loading the meta data for domain %s\n\t%s", domain, err.Error())
|
|
|
|
}
|
2015-10-19 00:42:04 +02:00
|
|
|
|
2015-12-15 22:10:47 +02:00
|
|
|
var certRes acme.CertificateResource
|
|
|
|
err = json.Unmarshal(metaBytes, &certRes)
|
|
|
|
if err != nil {
|
|
|
|
logger().Fatalf("Error while marshalling the meta data for domain %s\n\t%s", domain, err.Error())
|
|
|
|
}
|
2015-10-19 00:42:04 +02:00
|
|
|
|
2016-01-08 11:14:41 +02:00
|
|
|
if c.Bool("reuse-key") {
|
|
|
|
keyBytes, err := ioutil.ReadFile(privPath)
|
|
|
|
if err != nil {
|
|
|
|
logger().Fatalf("Error while loading the private key for domain %s\n\t%s", domain, err.Error())
|
|
|
|
}
|
|
|
|
certRes.PrivateKey = keyBytes
|
|
|
|
}
|
|
|
|
|
2015-12-15 22:10:47 +02:00
|
|
|
certRes.Certificate = certBytes
|
|
|
|
|
2015-12-27 18:38:49 +02:00
|
|
|
newCert, err := client.RenewCertificate(certRes, true)
|
2015-12-15 22:10:47 +02:00
|
|
|
if err != nil {
|
|
|
|
logger().Fatalf("%s", err.Error())
|
2015-10-19 00:42:04 +02:00
|
|
|
}
|
2015-12-15 22:10:47 +02:00
|
|
|
|
|
|
|
saveCertRes(newCert, conf)
|
2015-10-19 00:42:04 +02:00
|
|
|
}
|