97 lines
2.3 KiB
Go
97 lines
2.3 KiB
Go
package main
|
|
|
|
import (
|
|
"crypto"
|
|
"crypto/ecdsa"
|
|
"crypto/elliptic"
|
|
"crypto/rand"
|
|
"fmt"
|
|
"github.com/go-acme/lego/v4/providers/dns/cloudflare"
|
|
"log"
|
|
"os"
|
|
|
|
"github.com/go-acme/lego/v4/certcrypto"
|
|
"github.com/go-acme/lego/v4/certificate"
|
|
"github.com/go-acme/lego/v4/lego"
|
|
"github.com/go-acme/lego/v4/registration"
|
|
)
|
|
|
|
type User struct {
|
|
Email string
|
|
Registration *registration.Resource
|
|
key crypto.PrivateKey
|
|
}
|
|
|
|
func (u *User) GetEmail() string {
|
|
return u.Email
|
|
}
|
|
func (u User) GetRegistration() *registration.Resource {
|
|
return u.Registration
|
|
}
|
|
func (u *User) GetPrivateKey() crypto.PrivateKey {
|
|
return u.key
|
|
}
|
|
|
|
func mainexample() {
|
|
|
|
// Create a user. New accounts need an email and private key to start.
|
|
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
user := User{
|
|
Email: config.GetAsString("Certificates.email"),
|
|
key: privateKey,
|
|
}
|
|
|
|
configLE := lego.NewConfig(&user)
|
|
|
|
// This CA URL is configured for a local dev instance of Boulder running in Docker in a VM.
|
|
configLE.CADirURL = "http://192.168.99.100:4000/directory"
|
|
configLE.Certificate.KeyType = certcrypto.RSA2048
|
|
|
|
// A client facilitates communication with the CA server.
|
|
client, err := lego.NewClient(configLE)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
dnsConfig := cloudflare.NewDefaultConfig()
|
|
dnsConfig.AuthEmail = "" //TODO Pull from config
|
|
dnsConfig.AuthKey = "" //TODO Pull from config
|
|
|
|
provider, err := cloudflare.NewDNSProviderConfig(dnsConfig)
|
|
if err != nil {
|
|
fmt.Printf("Error creating DNS provider: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
err = client.Challenge.SetDNS01Provider(provider)
|
|
if err != nil {
|
|
fmt.Printf("Error setting dns provider: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
// New users will need to register
|
|
reg, err := client.Registration.Register(registration.RegisterOptions{TermsOfServiceAgreed: true})
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
user.Registration = reg
|
|
|
|
request := certificate.ObtainRequest{
|
|
Domains: []string{"mydomain.com"},
|
|
Bundle: true,
|
|
}
|
|
certificates, err := client.Certificate.Obtain(request)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
// Each certificate comes back with the cert bytes, the bytes of the client's
|
|
// private key, and a certificate URL. SAVE THESE TO DISK.
|
|
fmt.Printf("%#v\n", certificates)
|
|
|
|
// ... all done.
|
|
}
|