acme.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package provisioner
  2. import (
  3. "context"
  4. "crypto/x509"
  5. "time"
  6. "github.com/pkg/errors"
  7. "github.com/smallstep/certificates/errs"
  8. )
  9. // ACME is the acme provisioner type, an entity that can authorize the ACME
  10. // provisioning flow.
  11. type ACME struct {
  12. *base
  13. ID string `json:"-"`
  14. Type string `json:"type"`
  15. Name string `json:"name"`
  16. ForceCN bool `json:"forceCN,omitempty"`
  17. Claims *Claims `json:"claims,omitempty"`
  18. Options *Options `json:"options,omitempty"`
  19. claimer *Claimer
  20. }
  21. // GetID returns the provisioner unique identifier.
  22. func (p ACME) GetID() string {
  23. if p.ID != "" {
  24. return p.ID
  25. }
  26. return p.GetIDForToken()
  27. }
  28. // GetIDForToken returns an identifier that will be used to load the provisioner
  29. // from a token.
  30. func (p *ACME) GetIDForToken() string {
  31. return "acme/" + p.Name
  32. }
  33. // GetTokenID returns the identifier of the token.
  34. func (p *ACME) GetTokenID(ott string) (string, error) {
  35. return "", errors.New("acme provisioner does not implement GetTokenID")
  36. }
  37. // GetName returns the name of the provisioner.
  38. func (p *ACME) GetName() string {
  39. return p.Name
  40. }
  41. // GetType returns the type of provisioner.
  42. func (p *ACME) GetType() Type {
  43. return TypeACME
  44. }
  45. // GetEncryptedKey returns the base provisioner encrypted key if it's defined.
  46. func (p *ACME) GetEncryptedKey() (string, string, bool) {
  47. return "", "", false
  48. }
  49. // GetOptions returns the configured provisioner options.
  50. func (p *ACME) GetOptions() *Options {
  51. return p.Options
  52. }
  53. // DefaultTLSCertDuration returns the default TLS cert duration enforced by
  54. // the provisioner.
  55. func (p *ACME) DefaultTLSCertDuration() time.Duration {
  56. return p.claimer.DefaultTLSCertDuration()
  57. }
  58. // Init initializes and validates the fields of a JWK type.
  59. func (p *ACME) Init(config Config) (err error) {
  60. switch {
  61. case p.Type == "":
  62. return errors.New("provisioner type cannot be empty")
  63. case p.Name == "":
  64. return errors.New("provisioner name cannot be empty")
  65. }
  66. // Update claims with global ones
  67. if p.claimer, err = NewClaimer(p.Claims, config.Claims); err != nil {
  68. return err
  69. }
  70. return err
  71. }
  72. // AuthorizeSign does not do any validation, because all validation is handled
  73. // in the ACME protocol. This method returns a list of modifiers / constraints
  74. // on the resulting certificate.
  75. func (p *ACME) AuthorizeSign(ctx context.Context, token string) ([]SignOption, error) {
  76. return []SignOption{
  77. // modifiers / withOptions
  78. newProvisionerExtensionOption(TypeACME, p.Name, ""),
  79. newForceCNOption(p.ForceCN),
  80. profileDefaultDuration(p.claimer.DefaultTLSCertDuration()),
  81. // validators
  82. defaultPublicKeyValidator{},
  83. newValidityValidator(p.claimer.MinTLSCertDuration(), p.claimer.MaxTLSCertDuration()),
  84. }, nil
  85. }
  86. // AuthorizeRenew returns an error if the renewal is disabled.
  87. // NOTE: This method does not actually validate the certificate or check it's
  88. // revocation status. Just confirms that the provisioner that created the
  89. // certificate was configured to allow renewals.
  90. func (p *ACME) AuthorizeRenew(ctx context.Context, cert *x509.Certificate) error {
  91. if p.claimer.IsDisableRenewal() {
  92. return errs.Unauthorized("acme.AuthorizeRenew; renew is disabled for acme provisioner '%s'", p.GetName())
  93. }
  94. return nil
  95. }