From 79de2a5de2187b2682bf98c643d9857de3263afb Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Sat, 26 Sep 2015 18:11:53 -0600 Subject: [PATCH] Stubbed out basic code to obtain Let's Encrypt cert --- config/config.go | 54 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/config/config.go b/config/config.go index 82a29585..a763d3c2 100644 --- a/config/config.go +++ b/config/config.go @@ -1,6 +1,9 @@ package config import ( + "crypto/rand" + "crypto/rsa" + "errors" "fmt" "io" "log" @@ -11,6 +14,7 @@ import ( "github.com/mholt/caddy/config/setup" "github.com/mholt/caddy/middleware" "github.com/mholt/caddy/server" + "github.com/xenolf/lego/acme" ) const ( @@ -73,10 +77,60 @@ func Load(filename string, input io.Reader) (Group, error) { // restore logging settings log.SetFlags(flags) + privateKey, err := rsa.GenerateKey(rand.Reader, 2048) + if err != nil { + return Group{}, errors.New("Error Generating Key:" + err.Error()) + } + + for _, cfg := range configs { + // TODO: && hostname does not resolve to localhost (?) && TLS is not force-disabled + if !cfg.TLS.Enabled { + // Initiate Let's Encrypt + user := LetsEncryptUser{ + Email: "example@mail.com", + Key: privateKey, + } + client := acme.NewClient("http://192.168.99.100:4000", &user, 2048, "5001") + reg, err := client.Register() + if err != nil { + return Group{}, errors.New("Error Registering: " + err.Error()) + } + user.Registration = reg + + err = client.AgreeToTos() + if err != nil { + return Group{}, errors.New("Error Agreeing to ToS: " + err.Error()) + } + + certs, err := client.ObtainCertificates([]string{"caddy.dev"}) + if err != nil { + return Group{}, errors.New("Error Obtaining Certs: " + err.Error()) + } + + fmt.Printf("%#v\n", certs) + } + } + // Group by address/virtualhosts return arrangeBindings(configs) } +type LetsEncryptUser struct { + Email string + Registration *acme.RegistrationResource + Key *rsa.PrivateKey +} + +func (u LetsEncryptUser) GetEmail() string { + return u.Email +} +func (u LetsEncryptUser) GetRegistration() *acme.RegistrationResource { + return u.Registration +} +func (u LetsEncryptUser) GetPrivateKey() *rsa.PrivateKey { + return u.Key +} + // serverBlockToConfig makes a config for the server block // by executing the tokens that were parsed. The returned // config is shared among all hosts/addresses for the server