provisioner.go 5.3 KB

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