2019-04-03 19:37:17 +00:00
|
|
|
package ca
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2019-06-18 02:01:04 +00:00
|
|
|
"net/url"
|
2019-04-03 19:37:17 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/smallstep/certificates/authority/provisioner"
|
|
|
|
"github.com/smallstep/cli/token"
|
|
|
|
"github.com/smallstep/cli/token/provision"
|
2020-08-24 21:44:11 +00:00
|
|
|
"go.step.sm/crypto/jose"
|
2020-08-10 18:26:51 +00:00
|
|
|
"go.step.sm/crypto/randutil"
|
2019-04-03 19:37:17 +00:00
|
|
|
)
|
|
|
|
|
2019-06-18 02:01:04 +00:00
|
|
|
const tokenLifetime = 5 * time.Minute
|
2019-04-03 19:37:17 +00:00
|
|
|
|
|
|
|
// Provisioner is an authorized entity that can sign tokens necessary for
|
|
|
|
// signature requests.
|
|
|
|
type Provisioner struct {
|
2019-06-18 02:01:04 +00:00
|
|
|
*Client
|
2019-04-03 19:37:17 +00:00
|
|
|
name string
|
|
|
|
kid string
|
2019-06-18 02:01:04 +00:00
|
|
|
audience string
|
2019-12-10 21:40:14 +00:00
|
|
|
sshAudience string
|
2019-06-18 02:01:04 +00:00
|
|
|
fingerprint string
|
2019-04-03 19:37:17 +00:00
|
|
|
jwk *jose.JSONWebKey
|
|
|
|
tokenLifetime time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2019-04-12 21:59:55 +00:00
|
|
|
// decrypts using `password`.
|
2019-06-18 02:01:04 +00:00
|
|
|
func NewProvisioner(name, kid, caURL string, password []byte, opts ...ClientOption) (*Provisioner, error) {
|
|
|
|
client, err := NewClient(caURL, opts...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the fingerprint of the current connection
|
|
|
|
fp, err := client.RootFingerprint()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-04-03 19:37:17 +00:00
|
|
|
var jwk *jose.JSONWebKey
|
2019-04-12 21:59:55 +00:00
|
|
|
switch {
|
|
|
|
case name == "":
|
|
|
|
return nil, errors.New("provisioner name cannot be empty")
|
|
|
|
case kid == "":
|
2019-06-18 02:01:04 +00:00
|
|
|
jwk, err = loadProvisionerJWKByName(client, name, password)
|
2019-04-12 21:59:55 +00:00
|
|
|
default:
|
2019-06-18 02:01:04 +00:00
|
|
|
jwk, err = loadProvisionerJWKByKid(client, kid, password)
|
2019-04-03 19:37:17 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &Provisioner{
|
2019-06-18 02:01:04 +00:00
|
|
|
Client: client,
|
2019-04-03 19:37:17 +00:00
|
|
|
name: name,
|
|
|
|
kid: jwk.KeyID,
|
2019-06-18 02:01:04 +00:00
|
|
|
audience: client.endpoint.ResolveReference(&url.URL{Path: "/1.0/sign"}).String(),
|
2019-12-10 21:40:14 +00:00
|
|
|
sshAudience: client.endpoint.ResolveReference(&url.URL{Path: "/1.0/ssh/sign"}).String(),
|
2019-06-18 02:01:04 +00:00
|
|
|
fingerprint: fp,
|
2019-04-03 19:37:17 +00:00
|
|
|
jwk: jwk,
|
|
|
|
tokenLifetime: tokenLifetime,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2019-06-18 02:01:04 +00:00
|
|
|
// SetFingerprint overwrites the default fingerprint used.
|
|
|
|
func (p *Provisioner) SetFingerprint(sum string) {
|
|
|
|
p.fingerprint = sum
|
|
|
|
}
|
|
|
|
|
2019-04-03 19:37:17 +00:00
|
|
|
// Token generates a bootstrap token for a subject.
|
2019-06-18 02:01:04 +00:00
|
|
|
func (p *Provisioner) Token(subject string, sans ...string) (string, error) {
|
|
|
|
if len(sans) == 0 {
|
|
|
|
sans = []string{subject}
|
|
|
|
}
|
|
|
|
|
2019-04-03 19:37:17 +00:00
|
|
|
// 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)
|
|
|
|
tokOptions := []token.Options{
|
|
|
|
token.WithJWTID(jwtID),
|
|
|
|
token.WithKid(p.kid),
|
|
|
|
token.WithIssuer(p.name),
|
2019-06-18 02:01:04 +00:00
|
|
|
token.WithAudience(p.audience),
|
2019-04-03 19:37:17 +00:00
|
|
|
token.WithValidity(notBefore, notAfter),
|
2019-06-18 02:01:04 +00:00
|
|
|
token.WithSANS(sans),
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.fingerprint != "" {
|
|
|
|
tokOptions = append(tokOptions, token.WithSHA(p.fingerprint))
|
2019-04-03 19:37:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tok, err := provision.New(subject, tokOptions...)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tok.SignedString(p.jwk.Algorithm, p.jwk.Key)
|
|
|
|
}
|
|
|
|
|
2020-02-07 19:42:56 +00:00
|
|
|
// SSHToken generates a SSH token.
|
2019-12-10 21:40:14 +00:00
|
|
|
func (p *Provisioner) SSHToken(certType, keyID string, principals []string) (string, error) {
|
|
|
|
jwtID, err := randutil.Hex(64)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
notBefore := time.Now()
|
|
|
|
notAfter := notBefore.Add(tokenLifetime)
|
|
|
|
tokOptions := []token.Options{
|
|
|
|
token.WithJWTID(jwtID),
|
|
|
|
token.WithKid(p.kid),
|
|
|
|
token.WithIssuer(p.name),
|
|
|
|
token.WithAudience(p.sshAudience),
|
|
|
|
token.WithValidity(notBefore, notAfter),
|
2020-07-23 01:24:45 +00:00
|
|
|
token.WithSSH(provisioner.SignSSHOptions{
|
2019-12-10 21:40:14 +00:00
|
|
|
CertType: certType,
|
|
|
|
Principals: principals,
|
|
|
|
KeyID: keyID,
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.fingerprint != "" {
|
|
|
|
tokOptions = append(tokOptions, token.WithSHA(p.fingerprint))
|
|
|
|
}
|
|
|
|
|
|
|
|
tok, err := provision.New(keyID, tokOptions...)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tok.SignedString(p.jwk.Algorithm, p.jwk.Key)
|
|
|
|
}
|
|
|
|
|
2019-04-03 19:37:17 +00:00
|
|
|
func decryptProvisionerJWK(encryptedKey string, password []byte) (*jose.JSONWebKey, error) {
|
|
|
|
enc, err := jose.ParseEncrypted(encryptedKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
data, err := enc.Decrypt(password)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
jwk := new(jose.JSONWebKey)
|
|
|
|
if err := json.Unmarshal(data, jwk); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error unmarshaling provisioning key")
|
|
|
|
}
|
|
|
|
return jwk, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// loadProvisionerJWKByKid retrieves a provisioner key from the CA by key ID and
|
2019-04-12 21:59:55 +00:00
|
|
|
// decrypts it using the specified password.
|
2019-06-18 02:01:04 +00:00
|
|
|
func loadProvisionerJWKByKid(client *Client, kid string, password []byte) (*jose.JSONWebKey, error) {
|
|
|
|
encrypted, err := getProvisionerKey(client, kid)
|
2019-04-03 19:37:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return decryptProvisionerJWK(encrypted, password)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2019-04-12 21:59:55 +00:00
|
|
|
// decrypted with the specified password.
|
2019-06-18 02:01:04 +00:00
|
|
|
func loadProvisionerJWKByName(client *Client, name string, password []byte) (key *jose.JSONWebKey, err error) {
|
|
|
|
provisioners, err := getProvisioners(client)
|
2019-04-03 19:37:17 +00:00
|
|
|
if err != nil {
|
|
|
|
err = errors.Wrap(err, "error getting the provisioners")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, provisioner := range provisioners {
|
|
|
|
if provisioner.GetName() == name {
|
|
|
|
if _, encryptedKey, ok := provisioner.GetEncryptedKey(); ok {
|
|
|
|
key, err = decryptProvisionerJWK(encryptedKey, password)
|
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, errors.Errorf("provisioner '%s' not found (or your password is wrong)", name)
|
|
|
|
}
|
|
|
|
|
2019-06-18 02:01:04 +00:00
|
|
|
// getProvisioners returns the list of provisioners using the configured client.
|
|
|
|
func getProvisioners(client *Client) (provisioner.List, error) {
|
|
|
|
var cursor string
|
2019-04-03 19:37:17 +00:00
|
|
|
var provisioners provisioner.List
|
|
|
|
for {
|
|
|
|
resp, err := client.Provisioners(WithProvisionerCursor(cursor), WithProvisionerLimit(100))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
provisioners = append(provisioners, resp.Provisioners...)
|
|
|
|
if resp.NextCursor == "" {
|
|
|
|
return provisioners, nil
|
|
|
|
}
|
|
|
|
cursor = resp.NextCursor
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-12 21:59:55 +00:00
|
|
|
// getProvisionerKey returns the encrypted provisioner key for the given kid.
|
2019-06-18 02:01:04 +00:00
|
|
|
func getProvisionerKey(client *Client, kid string) (string, error) {
|
2019-04-03 19:37:17 +00:00
|
|
|
resp, err := client.ProvisionerKey(kid)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return resp.Key, nil
|
|
|
|
}
|