provisioner.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "path/filepath"
  6. "time"
  7. "github.com/pkg/errors"
  8. "github.com/smallstep/certificates/authority"
  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.Name == name {
  97. key, err = decryptProvisionerJWK(provisioner.EncryptedKey, passFile)
  98. if err == nil {
  99. return
  100. }
  101. }
  102. }
  103. return nil, errors.Errorf("provisioner '%s' not found (or your password is wrong)", name)
  104. }
  105. // NewProvisioner loads and decrypts key material from the CA for the named
  106. // provisioner. The key identified by `kid` will be used if specified. If `kid`
  107. // is the empty string we'll use the first key for the named provisioner that
  108. // decrypts using `passFile`.
  109. func NewProvisioner(name, kid, caURL, caRoot, passFile string) (Provisioner, error) {
  110. var jwk *jose.JSONWebKey
  111. var err error
  112. if kid != "" {
  113. jwk, err = loadProvisionerJWKByKid(kid, caURL, caRoot, passFile)
  114. } else {
  115. jwk, err = loadProvisionerJWKByName(name, caURL, caRoot, passFile)
  116. }
  117. if err != nil {
  118. return nil, err
  119. }
  120. return &provisioner{
  121. name: name,
  122. kid: jwk.KeyID,
  123. caURL: caURL,
  124. caRoot: caRoot,
  125. jwk: jwk,
  126. tokenLifetime: tokenLifetime,
  127. }, nil
  128. }
  129. // getRootCAPath returns the path where the root CA is stored based on the
  130. // STEPPATH environment variable.
  131. func getRootCAPath() string {
  132. return filepath.Join(config.StepPath(), "certs", "root_ca.crt")
  133. }
  134. // getProvisioners returns the map of provisioners on the given CA.
  135. func getProvisioners(caURL, rootFile string) ([]*authority.Provisioner, error) {
  136. if len(rootFile) == 0 {
  137. rootFile = getRootCAPath()
  138. }
  139. client, err := ca.NewClient(caURL, ca.WithRootFile(rootFile))
  140. if err != nil {
  141. return nil, err
  142. }
  143. cursor := ""
  144. provisioners := []*authority.Provisioner{}
  145. for {
  146. resp, err := client.Provisioners(ca.WithProvisionerCursor(cursor), ca.WithProvisionerLimit(100))
  147. if err != nil {
  148. return nil, err
  149. }
  150. provisioners = append(provisioners, resp.Provisioners...)
  151. if resp.NextCursor == "" {
  152. return provisioners, nil
  153. }
  154. cursor = resp.NextCursor
  155. }
  156. }
  157. // getProvisionerKey returns the encrypted provisioner key with the for the
  158. // given kid.
  159. func getProvisionerKey(caURL, rootFile, kid string) (string, error) {
  160. if len(rootFile) == 0 {
  161. rootFile = getRootCAPath()
  162. }
  163. client, err := ca.NewClient(caURL, ca.WithRootFile(rootFile))
  164. if err != nil {
  165. return "", err
  166. }
  167. resp, err := client.ProvisionerKey(kid)
  168. if err != nil {
  169. return "", err
  170. }
  171. return resp.Key, nil
  172. }