provisioner.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package authority
  2. import (
  3. "time"
  4. "github.com/pkg/errors"
  5. "github.com/smallstep/cli/crypto/x509util"
  6. jose "gopkg.in/square/go-jose.v2"
  7. )
  8. // ProvisionerClaims so that individual provisioners can override global claims.
  9. type ProvisionerClaims struct {
  10. globalClaims *ProvisionerClaims
  11. MinTLSDur *Duration `json:"minTLSCertDuration,omitempty"`
  12. MaxTLSDur *Duration `json:"maxTLSCertDuration,omitempty"`
  13. DefaultTLSDur *Duration `json:"defaultTLSCertDuration,omitempty"`
  14. DisableRenewal *bool `json:"disableRenewal,omitempty"`
  15. }
  16. // Init initializes and validates the individual provisioner claims.
  17. func (pc *ProvisionerClaims) Init(global *ProvisionerClaims) (*ProvisionerClaims, error) {
  18. if pc == nil {
  19. pc = &ProvisionerClaims{}
  20. }
  21. pc.globalClaims = global
  22. err := pc.Validate()
  23. return pc, err
  24. }
  25. // DefaultTLSCertDuration returns the default TLS cert duration for the
  26. // provisioner. If the default is not set within the provisioner, then the global
  27. // default from the authority configuration will be used.
  28. func (pc *ProvisionerClaims) DefaultTLSCertDuration() time.Duration {
  29. if pc.DefaultTLSDur == nil || pc.DefaultTLSDur.Duration == 0 {
  30. return pc.globalClaims.DefaultTLSCertDuration()
  31. }
  32. return pc.DefaultTLSDur.Duration
  33. }
  34. // MinTLSCertDuration returns the minimum TLS cert duration for the provisioner.
  35. // If the minimum is not set within the provisioner, then the global
  36. // minimum from the authority configuration will be used.
  37. func (pc *ProvisionerClaims) MinTLSCertDuration() time.Duration {
  38. if pc.MinTLSDur == nil || pc.MinTLSDur.Duration == 0 {
  39. return pc.globalClaims.MinTLSCertDuration()
  40. }
  41. return pc.MinTLSDur.Duration
  42. }
  43. // MaxTLSCertDuration returns the maximum TLS cert duration for the provisioner.
  44. // If the maximum is not set within the provisioner, then the global
  45. // maximum from the authority configuration will be used.
  46. func (pc *ProvisionerClaims) MaxTLSCertDuration() time.Duration {
  47. if pc.MaxTLSDur == nil || pc.MaxTLSDur.Duration == 0 {
  48. return pc.globalClaims.MaxTLSCertDuration()
  49. }
  50. return pc.MaxTLSDur.Duration
  51. }
  52. // IsDisableRenewal returns if the renewal flow is disabled for the
  53. // provisioner. If the property is not set within the provisioner, then the
  54. // global value from the authority configuration will be used.
  55. func (pc *ProvisionerClaims) IsDisableRenewal() bool {
  56. if pc.DisableRenewal == nil {
  57. return pc.globalClaims.IsDisableRenewal()
  58. }
  59. return *pc.DisableRenewal
  60. }
  61. // Validate validates and modifies the Claims with default values.
  62. func (pc *ProvisionerClaims) Validate() error {
  63. var (
  64. min = pc.MinTLSCertDuration()
  65. max = pc.MaxTLSCertDuration()
  66. def = pc.DefaultTLSCertDuration()
  67. )
  68. switch {
  69. case min == 0:
  70. return errors.Errorf("claims: MinTLSCertDuration cannot be empty")
  71. case max == 0:
  72. return errors.Errorf("claims: MaxTLSCertDuration cannot be empty")
  73. case def == 0:
  74. return errors.Errorf("claims: DefaultTLSCertDuration cannot be empty")
  75. case max < min:
  76. return errors.Errorf("claims: MaxCertDuration cannot be less "+
  77. "than MinCertDuration: MaxCertDuration - %v, MinCertDuration - %v", max, min)
  78. case def < min:
  79. return errors.Errorf("claims: DefaultCertDuration cannot be less than MinCertDuration: DefaultCertDuration - %v, MinCertDuration - %v", def, min)
  80. case max < def:
  81. return errors.Errorf("claims: MaxCertDuration cannot be less than DefaultCertDuration: MaxCertDuration - %v, DefaultCertDuration - %v", max, def)
  82. default:
  83. return nil
  84. }
  85. }
  86. // Provisioner - authorized entity that can sign tokens necessary for signature requests.
  87. type Provisioner struct {
  88. Name string `json:"name,omitempty"`
  89. Type string `json:"type,omitempty"`
  90. Key *jose.JSONWebKey `json:"key,omitempty"`
  91. EncryptedKey string `json:"encryptedKey,omitempty"`
  92. Claims *ProvisionerClaims `json:"claims,omitempty"`
  93. }
  94. // Init initializes and validates a the fields of Provisioner type.
  95. func (p *Provisioner) Init(global *ProvisionerClaims) error {
  96. switch {
  97. case p.Name == "":
  98. return errors.New("provisioner name cannot be empty")
  99. case p.Type == "":
  100. return errors.New("provisioner type cannot be empty")
  101. case p.Key == nil:
  102. return errors.New("provisioner key cannot be empty")
  103. }
  104. var err error
  105. p.Claims, err = p.Claims.Init(global)
  106. return err
  107. }
  108. // getTLSApps returns a list of modifiers and validators that will be applied to
  109. // the certificate.
  110. func (p *Provisioner) getTLSApps(so SignOptions) ([]x509util.WithOption, []certClaim, error) {
  111. c := p.Claims
  112. return []x509util.WithOption{
  113. x509util.WithNotBeforeAfterDuration(so.NotBefore,
  114. so.NotAfter, c.DefaultTLSCertDuration()),
  115. withProvisionerOID(p.Name, p.Key.KeyID),
  116. }, []certClaim{
  117. &certTemporalClaim{
  118. min: c.MinTLSCertDuration(),
  119. max: c.MaxTLSCertDuration(),
  120. },
  121. }, nil
  122. }
  123. // ID returns the provisioner identifier. The name and credential id should
  124. // uniquely identify any provisioner.
  125. func (p *Provisioner) ID() string {
  126. return p.Name + ":" + p.Key.KeyID
  127. }