certificates/autocert/controller/provisioner.go

198 lines
5.3 KiB
Go
Raw Normal View History

2019-01-18 00:07:27 +00:00
package main
import (
"encoding/json"
"fmt"
2019-02-12 04:27:41 +00:00
"path/filepath"
2019-01-18 00:07:27 +00:00
"time"
"github.com/pkg/errors"
2019-03-08 00:15:21 +00:00
provisioners "github.com/smallstep/certificates/authority/provisioner"
2019-02-12 04:27:41 +00:00
"github.com/smallstep/certificates/ca"
"github.com/smallstep/cli/config"
2019-01-18 00:07:27 +00:00
"github.com/smallstep/cli/crypto/randutil"
"github.com/smallstep/cli/jose"
"github.com/smallstep/cli/token"
"github.com/smallstep/cli/token/provision"
)
const (
tokenLifetime = 5 * time.Minute
)
2019-02-12 04:27:41 +00:00
// Provisioner is an authorized entity that can sign tokens necessary for
// signature requests.
2019-01-18 00:07:27 +00:00
type Provisioner interface {
Name() string
Kid() string
Token(subject string) (string, error)
}
type provisioner struct {
name string
kid string
2019-02-12 04:27:41 +00:00
caURL string
2019-01-18 00:07:27 +00:00
caRoot string
jwk *jose.JSONWebKey
tokenLifetime time.Duration
}
// Name returns the provisioner's name.
func (p *provisioner) Name() string {
return p.name
}
// Kid returns the provisioners key ID.
func (p *provisioner) Kid() string {
return p.kid
}
// Token generates a bootstrap token for a subject.
func (p *provisioner) Token(subject string) (string, error) {
// A random jwt id will be used to identify duplicated tokens
jwtID, err := randutil.Hex(64) // 256 bits
if err != nil {
return "", err
}
notBefore := time.Now()
notAfter := notBefore.Add(tokenLifetime)
2019-02-12 04:27:41 +00:00
signURL := fmt.Sprintf("%v/1.0/sign", p.caURL)
2019-01-18 00:07:27 +00:00
tokOptions := []token.Options{
token.WithJWTID(jwtID),
token.WithKid(p.kid),
token.WithIssuer(p.name),
2019-02-12 04:27:41 +00:00
token.WithAudience(signURL),
2019-01-18 00:07:27 +00:00
token.WithValidity(notBefore, notAfter),
token.WithRootCA(p.caRoot),
}
tok, err := provision.New(subject, tokOptions...)
if err != nil {
return "", err
}
return tok.SignedString(p.jwk.Algorithm, p.jwk.Key)
}
func decryptProvisionerJWK(encryptedKey, passFile string) (*jose.JSONWebKey, error) {
decrypted, err := jose.Decrypt("", []byte(encryptedKey), jose.WithPasswordFile(passFile))
if err != nil {
return nil, err
}
jwk := new(jose.JSONWebKey)
if err := json.Unmarshal(decrypted, jwk); err != nil {
return nil, errors.Wrap(err, "error unmarshalling provisioning key")
}
return jwk, nil
}
// loadProvisionerJWKByKid retrieves a provisioner key from the CA by key ID and
// decrypts it using the specified password file.
2019-02-12 04:27:41 +00:00
func loadProvisionerJWKByKid(kid, caURL, caRoot, passFile string) (*jose.JSONWebKey, error) {
encrypted, err := getProvisionerKey(caURL, caRoot, kid)
2019-01-18 00:07:27 +00:00
if err != nil {
return nil, err
}
2019-02-12 04:27:41 +00:00
2019-01-18 00:07:27 +00:00
return decryptProvisionerJWK(encrypted, passFile)
}
// loadProvisionerJWKByName retrieves the list of provisioners and encrypted key then
// returns the key of the first provisioner with a matching name that can be successfully
// decrypted with the specified password file.
2019-02-12 04:27:41 +00:00
func loadProvisionerJWKByName(name, caURL, caRoot, passFile string) (key *jose.JSONWebKey, err error) {
provisioners, err := getProvisioners(caURL, caRoot)
2019-01-18 00:07:27 +00:00
if err != nil {
err = errors.Wrap(err, "error getting the provisioners")
return
}
for _, provisioner := range provisioners {
2019-03-08 00:15:21 +00:00
if provisioner.GetName() == name {
if _, encryptedKey, ok := provisioner.GetEncryptedKey(); ok {
key, err = decryptProvisionerJWK(encryptedKey, passFile)
if err == nil {
return
}
2019-01-18 00:07:27 +00:00
}
}
}
2019-02-12 04:27:41 +00:00
return nil, errors.Errorf("provisioner '%s' not found (or your password is wrong)", name)
2019-01-18 00:07:27 +00:00
}
// NewProvisioner loads and decrypts key material from the CA for the named
// provisioner. The key identified by `kid` will be used if specified. If `kid`
// is the empty string we'll use the first key for the named provisioner that
// decrypts using `passFile`.
2019-02-12 04:27:41 +00:00
func NewProvisioner(name, kid, caURL, caRoot, passFile string) (Provisioner, error) {
2019-01-18 00:07:27 +00:00
var jwk *jose.JSONWebKey
var err error
if kid != "" {
2019-02-12 04:27:41 +00:00
jwk, err = loadProvisionerJWKByKid(kid, caURL, caRoot, passFile)
2019-01-18 00:07:27 +00:00
} else {
2019-02-12 04:27:41 +00:00
jwk, err = loadProvisionerJWKByName(name, caURL, caRoot, passFile)
2019-01-18 00:07:27 +00:00
}
if err != nil {
return nil, err
}
return &provisioner{
name: name,
kid: jwk.KeyID,
2019-02-12 04:27:41 +00:00
caURL: caURL,
2019-01-18 00:07:27 +00:00
caRoot: caRoot,
jwk: jwk,
tokenLifetime: tokenLifetime,
}, nil
2019-02-12 04:27:41 +00:00
}
// getRootCAPath returns the path where the root CA is stored based on the
// STEPPATH environment variable.
func getRootCAPath() string {
return filepath.Join(config.StepPath(), "certs", "root_ca.crt")
}
// getProvisioners returns the map of provisioners on the given CA.
2019-03-08 00:15:21 +00:00
func getProvisioners(caURL, rootFile string) (provisioners.List, error) {
2019-02-12 04:27:41 +00:00
if len(rootFile) == 0 {
rootFile = getRootCAPath()
}
client, err := ca.NewClient(caURL, ca.WithRootFile(rootFile))
if err != nil {
return nil, err
}
cursor := ""
2019-03-08 00:15:21 +00:00
var provisioners provisioners.List
2019-02-12 04:27:41 +00:00
for {
resp, err := client.Provisioners(ca.WithProvisionerCursor(cursor), ca.WithProvisionerLimit(100))
if err != nil {
return nil, err
}
provisioners = append(provisioners, resp.Provisioners...)
if resp.NextCursor == "" {
return provisioners, nil
}
cursor = resp.NextCursor
}
}
// getProvisionerKey returns the encrypted provisioner key with the for the
// given kid.
func getProvisionerKey(caURL, rootFile, kid string) (string, error) {
if len(rootFile) == 0 {
rootFile = getRootCAPath()
}
client, err := ca.NewClient(caURL, ca.WithRootFile(rootFile))
if err != nil {
return "", err
}
resp, err := client.ProvisionerKey(kid)
if err != nil {
return "", err
}
return resp.Key, nil
}