acme.go 2.9 KB

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