provisioner.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. package ca
  2. import (
  3. "encoding/json"
  4. "net/url"
  5. "time"
  6. "github.com/pkg/errors"
  7. "github.com/smallstep/certificates/authority/provisioner"
  8. "github.com/smallstep/cli/crypto/randutil"
  9. "github.com/smallstep/cli/jose"
  10. "github.com/smallstep/cli/token"
  11. "github.com/smallstep/cli/token/provision"
  12. )
  13. const tokenLifetime = 5 * time.Minute
  14. // Provisioner is an authorized entity that can sign tokens necessary for
  15. // signature requests.
  16. type Provisioner struct {
  17. *Client
  18. name string
  19. kid string
  20. audience string
  21. fingerprint string
  22. jwk *jose.JSONWebKey
  23. tokenLifetime time.Duration
  24. }
  25. // NewProvisioner loads and decrypts key material from the CA for the named
  26. // provisioner. The key identified by `kid` will be used if specified. If `kid`
  27. // is the empty string we'll use the first key for the named provisioner that
  28. // decrypts using `password`.
  29. func NewProvisioner(name, kid, caURL string, password []byte, opts ...ClientOption) (*Provisioner, error) {
  30. client, err := NewClient(caURL, opts...)
  31. if err != nil {
  32. return nil, err
  33. }
  34. // Get the fingerprint of the current connection
  35. fp, err := client.RootFingerprint()
  36. if err != nil {
  37. return nil, err
  38. }
  39. var jwk *jose.JSONWebKey
  40. switch {
  41. case name == "":
  42. return nil, errors.New("provisioner name cannot be empty")
  43. case kid == "":
  44. jwk, err = loadProvisionerJWKByName(client, name, password)
  45. default:
  46. jwk, err = loadProvisionerJWKByKid(client, kid, password)
  47. }
  48. if err != nil {
  49. return nil, err
  50. }
  51. return &Provisioner{
  52. Client: client,
  53. name: name,
  54. kid: jwk.KeyID,
  55. audience: client.endpoint.ResolveReference(&url.URL{Path: "/1.0/sign"}).String(),
  56. fingerprint: fp,
  57. jwk: jwk,
  58. tokenLifetime: tokenLifetime,
  59. }, nil
  60. }
  61. // Name returns the provisioner's name.
  62. func (p *Provisioner) Name() string {
  63. return p.name
  64. }
  65. // Kid returns the provisioners key ID.
  66. func (p *Provisioner) Kid() string {
  67. return p.kid
  68. }
  69. // SetFingerprint overwrites the default fingerprint used.
  70. func (p *Provisioner) SetFingerprint(sum string) {
  71. p.fingerprint = sum
  72. }
  73. // Token generates a bootstrap token for a subject.
  74. func (p *Provisioner) Token(subject string, sans ...string) (string, error) {
  75. if len(sans) == 0 {
  76. sans = []string{subject}
  77. }
  78. // A random jwt id will be used to identify duplicated tokens
  79. jwtID, err := randutil.Hex(64) // 256 bits
  80. if err != nil {
  81. return "", err
  82. }
  83. notBefore := time.Now()
  84. notAfter := notBefore.Add(tokenLifetime)
  85. tokOptions := []token.Options{
  86. token.WithJWTID(jwtID),
  87. token.WithKid(p.kid),
  88. token.WithIssuer(p.name),
  89. token.WithAudience(p.audience),
  90. token.WithValidity(notBefore, notAfter),
  91. token.WithSANS(sans),
  92. }
  93. if p.fingerprint != "" {
  94. tokOptions = append(tokOptions, token.WithSHA(p.fingerprint))
  95. }
  96. tok, err := provision.New(subject, tokOptions...)
  97. if err != nil {
  98. return "", err
  99. }
  100. return tok.SignedString(p.jwk.Algorithm, p.jwk.Key)
  101. }
  102. func decryptProvisionerJWK(encryptedKey string, password []byte) (*jose.JSONWebKey, error) {
  103. enc, err := jose.ParseEncrypted(encryptedKey)
  104. if err != nil {
  105. return nil, err
  106. }
  107. data, err := enc.Decrypt(password)
  108. if err != nil {
  109. return nil, err
  110. }
  111. jwk := new(jose.JSONWebKey)
  112. if err := json.Unmarshal(data, jwk); err != nil {
  113. return nil, errors.Wrap(err, "error unmarshaling provisioning key")
  114. }
  115. return jwk, nil
  116. }
  117. // loadProvisionerJWKByKid retrieves a provisioner key from the CA by key ID and
  118. // decrypts it using the specified password.
  119. func loadProvisionerJWKByKid(client *Client, kid string, password []byte) (*jose.JSONWebKey, error) {
  120. encrypted, err := getProvisionerKey(client, kid)
  121. if err != nil {
  122. return nil, err
  123. }
  124. return decryptProvisionerJWK(encrypted, password)
  125. }
  126. // loadProvisionerJWKByName retrieves the list of provisioners and encrypted key then
  127. // returns the key of the first provisioner with a matching name that can be successfully
  128. // decrypted with the specified password.
  129. func loadProvisionerJWKByName(client *Client, name string, password []byte) (key *jose.JSONWebKey, err error) {
  130. provisioners, err := getProvisioners(client)
  131. if err != nil {
  132. err = errors.Wrap(err, "error getting the provisioners")
  133. return
  134. }
  135. for _, provisioner := range provisioners {
  136. if provisioner.GetName() == name {
  137. if _, encryptedKey, ok := provisioner.GetEncryptedKey(); ok {
  138. key, err = decryptProvisionerJWK(encryptedKey, password)
  139. if err == nil {
  140. return
  141. }
  142. }
  143. }
  144. }
  145. return nil, errors.Errorf("provisioner '%s' not found (or your password is wrong)", name)
  146. }
  147. // getProvisioners returns the list of provisioners using the configured client.
  148. func getProvisioners(client *Client) (provisioner.List, error) {
  149. var cursor string
  150. var provisioners provisioner.List
  151. for {
  152. resp, err := client.Provisioners(WithProvisionerCursor(cursor), WithProvisionerLimit(100))
  153. if err != nil {
  154. return nil, err
  155. }
  156. provisioners = append(provisioners, resp.Provisioners...)
  157. if resp.NextCursor == "" {
  158. return provisioners, nil
  159. }
  160. cursor = resp.NextCursor
  161. }
  162. }
  163. // getProvisionerKey returns the encrypted provisioner key for the given kid.
  164. func getProvisionerKey(client *Client, kid string) (string, error) {
  165. resp, err := client.ProvisionerKey(kid)
  166. if err != nil {
  167. return "", err
  168. }
  169. return resp.Key, nil
  170. }