provisioner.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "time"
  6. "github.com/pkg/errors"
  7. "github.com/smallstep/cli/crypto/pki"
  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 (
  14. tokenLifetime = 5 * time.Minute
  15. )
  16. type Provisioner interface {
  17. Name() string
  18. Kid() string
  19. Token(subject string) (string, error)
  20. }
  21. type provisioner struct {
  22. name string
  23. kid string
  24. caUrl string
  25. caRoot string
  26. jwk *jose.JSONWebKey
  27. tokenLifetime time.Duration
  28. }
  29. // Name returns the provisioner's name.
  30. func (p *provisioner) Name() string {
  31. return p.name
  32. }
  33. // Kid returns the provisioners key ID.
  34. func (p *provisioner) Kid() string {
  35. return p.kid
  36. }
  37. // Token generates a bootstrap token for a subject.
  38. func (p *provisioner) Token(subject string) (string, error) {
  39. // A random jwt id will be used to identify duplicated tokens
  40. jwtID, err := randutil.Hex(64) // 256 bits
  41. if err != nil {
  42. return "", err
  43. }
  44. notBefore := time.Now()
  45. notAfter := notBefore.Add(tokenLifetime)
  46. signUrl := fmt.Sprintf("%v/1.0/sign", p.caUrl)
  47. tokOptions := []token.Options{
  48. token.WithJWTID(jwtID),
  49. token.WithKid(p.kid),
  50. token.WithIssuer(p.name),
  51. token.WithAudience(signUrl),
  52. token.WithValidity(notBefore, notAfter),
  53. token.WithRootCA(p.caRoot),
  54. }
  55. tok, err := provision.New(subject, tokOptions...)
  56. if err != nil {
  57. return "", err
  58. }
  59. return tok.SignedString(p.jwk.Algorithm, p.jwk.Key)
  60. }
  61. func decryptProvisionerJWK(encryptedKey, passFile string) (*jose.JSONWebKey, error) {
  62. decrypted, err := jose.Decrypt("", []byte(encryptedKey), jose.WithPasswordFile(passFile))
  63. if err != nil {
  64. return nil, err
  65. }
  66. jwk := new(jose.JSONWebKey)
  67. if err := json.Unmarshal(decrypted, jwk); err != nil {
  68. return nil, errors.Wrap(err, "error unmarshalling provisioning key")
  69. }
  70. return jwk, nil
  71. }
  72. // loadProvisionerJWKByKid retrieves a provisioner key from the CA by key ID and
  73. // decrypts it using the specified password file.
  74. func loadProvisionerJWKByKid(kid, caUrl, caRoot, passFile string) (*jose.JSONWebKey, error) {
  75. encrypted, err := pki.GetProvisionerKey(caUrl, caRoot, kid)
  76. if err != nil {
  77. return nil, err
  78. }
  79. return decryptProvisionerJWK(encrypted, passFile)
  80. }
  81. // loadProvisionerJWKByName retrieves the list of provisioners and encrypted key then
  82. // returns the key of the first provisioner with a matching name that can be successfully
  83. // decrypted with the specified password file.
  84. func loadProvisionerJWKByName(name, caUrl, caRoot, passFile string) (key *jose.JSONWebKey, err error) {
  85. provisioners, err := pki.GetProvisioners(caUrl, caRoot)
  86. if err != nil {
  87. err = errors.Wrap(err, "error getting the provisioners")
  88. return
  89. }
  90. for _, provisioner := range provisioners {
  91. if provisioner.Name == name {
  92. key, err = decryptProvisionerJWK(provisioner.EncryptedKey, passFile)
  93. if err == nil {
  94. return
  95. }
  96. }
  97. }
  98. return nil, errors.New(fmt.Sprintf("provisioner '%s' not found (or your password is wrong)", name))
  99. }
  100. // NewProvisioner loads and decrypts key material from the CA for the named
  101. // provisioner. The key identified by `kid` will be used if specified. If `kid`
  102. // is the empty string we'll use the first key for the named provisioner that
  103. // decrypts using `passFile`.
  104. func NewProvisioner(name, kid, caUrl, caRoot, passFile string) (Provisioner, error) {
  105. var jwk *jose.JSONWebKey
  106. var err error
  107. if kid != "" {
  108. jwk, err = loadProvisionerJWKByKid(kid, caUrl, caRoot, passFile)
  109. } else {
  110. jwk, err = loadProvisionerJWKByName(name, caUrl, caRoot, passFile)
  111. }
  112. if err != nil {
  113. return nil, err
  114. }
  115. return &provisioner{
  116. name: name,
  117. kid: jwk.KeyID,
  118. caUrl: caUrl,
  119. caRoot: caRoot,
  120. jwk: jwk,
  121. tokenLifetime: tokenLifetime,
  122. }, nil
  123. }