Commit 79de2a5d authored by Matthew Holt's avatar Matthew Holt

Stubbed out basic code to obtain Let's Encrypt cert

parent ca1f1362
package config package config
import ( import (
"crypto/rand"
"crypto/rsa"
"errors"
"fmt" "fmt"
"io" "io"
"log" "log"
...@@ -11,6 +14,7 @@ import ( ...@@ -11,6 +14,7 @@ import (
"github.com/mholt/caddy/config/setup" "github.com/mholt/caddy/config/setup"
"github.com/mholt/caddy/middleware" "github.com/mholt/caddy/middleware"
"github.com/mholt/caddy/server" "github.com/mholt/caddy/server"
"github.com/xenolf/lego/acme"
) )
const ( const (
...@@ -73,10 +77,60 @@ func Load(filename string, input io.Reader) (Group, error) { ...@@ -73,10 +77,60 @@ func Load(filename string, input io.Reader) (Group, error) {
// restore logging settings // restore logging settings
log.SetFlags(flags) 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 // Group by address/virtualhosts
return arrangeBindings(configs) 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 // serverBlockToConfig makes a config for the server block
// by executing the tokens that were parsed. The returned // by executing the tokens that were parsed. The returned
// config is shared among all hosts/addresses for the server // config is shared among all hosts/addresses for the server
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment