traefik/provider/acme/account.go

81 lines
1.8 KiB
Go
Raw Normal View History

2018-03-05 19:54:04 +00:00
package acme
import (
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"github.com/containous/traefik/log"
2018-03-26 12:12:03 +00:00
acme "github.com/xenolf/lego/acmev2"
2018-03-05 19:54:04 +00:00
)
// Account is used to store lets encrypt registration info
type Account struct {
Email string
Registration *acme.RegistrationResource
PrivateKey []byte
KeyType acme.KeyType
2018-03-05 19:54:04 +00:00
}
2018-03-26 12:12:03 +00:00
const (
// RegistrationURLPathV1Regexp is a regexp which match ACME registration URL in the V1 format
2018-04-17 21:20:33 +00:00
RegistrationURLPathV1Regexp = `^.*/acme/reg/\d+$`
2018-03-26 12:12:03 +00:00
)
2018-03-05 19:54:04 +00:00
// NewAccount creates an account
func NewAccount(email string, keyTypeValue string) (*Account, error) {
keyType := GetKeyType(keyTypeValue)
2018-03-05 19:54:04 +00:00
// Create a user. New accounts need an email and private key to start
privateKey, err := rsa.GenerateKey(rand.Reader, 4096)
if err != nil {
return nil, err
}
return &Account{
Email: email,
PrivateKey: x509.MarshalPKCS1PrivateKey(privateKey),
KeyType: keyType,
2018-03-05 19:54:04 +00:00
}, nil
}
// GetEmail returns email
func (a *Account) GetEmail() string {
return a.Email
}
// GetRegistration returns lets encrypt registration resource
func (a *Account) GetRegistration() *acme.RegistrationResource {
return a.Registration
}
// GetPrivateKey returns private key
func (a *Account) GetPrivateKey() crypto.PrivateKey {
if privateKey, err := x509.ParsePKCS1PrivateKey(a.PrivateKey); err == nil {
return privateKey
}
log.Errorf("Cannot unmarshal private key %+v", a.PrivateKey)
return nil
}
// GetKeyType used to determine which algo to used
func GetKeyType(value string) acme.KeyType {
switch value {
case "EC256":
return acme.EC256
case "EC384":
return acme.EC384
case "RSA2048":
return acme.RSA2048
case "RSA4096":
return acme.RSA4096
case "RSA8192":
return acme.RSA8192
default:
log.Warnf("Unable to determine key type value %s. Use %s as default value", value, acme.RSA4096)
return acme.RSA4096
}
}