acme.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package provisioner
  2. import (
  3. "context"
  4. "crypto/x509"
  5. "time"
  6. "github.com/pkg/errors"
  7. )
  8. // ACME is the acme provisioner type, an entity that can authorize the ACME
  9. // provisioning flow.
  10. type ACME struct {
  11. *base
  12. ID string `json:"-"`
  13. Type string `json:"type"`
  14. Name string `json:"name"`
  15. ForceCN bool `json:"forceCN,omitempty"`
  16. // RequireEAB makes the provisioner require ACME EAB to be provided
  17. // by clients when creating a new Account. If set to true, the provided
  18. // EAB will be verified. If set to false and an EAB is provided, it is
  19. // not verified. Defaults to false.
  20. RequireEAB bool `json:"requireEAB,omitempty"`
  21. Claims *Claims `json:"claims,omitempty"`
  22. Options *Options `json:"options,omitempty"`
  23. ctl *Controller
  24. }
  25. // GetID returns the provisioner unique identifier.
  26. func (p ACME) GetID() string {
  27. if p.ID != "" {
  28. return p.ID
  29. }
  30. return p.GetIDForToken()
  31. }
  32. // GetIDForToken returns an identifier that will be used to load the provisioner
  33. // from a token.
  34. func (p *ACME) GetIDForToken() string {
  35. return "acme/" + p.Name
  36. }
  37. // GetTokenID returns the identifier of the token.
  38. func (p *ACME) GetTokenID(ott string) (string, error) {
  39. return "", errors.New("acme provisioner does not implement GetTokenID")
  40. }
  41. // GetName returns the name of the provisioner.
  42. func (p *ACME) GetName() string {
  43. return p.Name
  44. }
  45. // GetType returns the type of provisioner.
  46. func (p *ACME) GetType() Type {
  47. return TypeACME
  48. }
  49. // GetEncryptedKey returns the base provisioner encrypted key if it's defined.
  50. func (p *ACME) GetEncryptedKey() (string, string, bool) {
  51. return "", "", false
  52. }
  53. // GetOptions returns the configured provisioner options.
  54. func (p *ACME) GetOptions() *Options {
  55. return p.Options
  56. }
  57. // DefaultTLSCertDuration returns the default TLS cert duration enforced by
  58. // the provisioner.
  59. func (p *ACME) DefaultTLSCertDuration() time.Duration {
  60. return p.ctl.Claimer.DefaultTLSCertDuration()
  61. }
  62. // Init initializes and validates the fields of a JWK type.
  63. func (p *ACME) Init(config Config) (err error) {
  64. switch {
  65. case p.Type == "":
  66. return errors.New("provisioner type cannot be empty")
  67. case p.Name == "":
  68. return errors.New("provisioner name cannot be empty")
  69. }
  70. p.ctl, err = NewController(p, p.Claims, config)
  71. return
  72. }
  73. // AuthorizeSign does not do any validation, because all validation is handled
  74. // in the ACME protocol. This method returns a list of modifiers / constraints
  75. // on the resulting certificate.
  76. func (p *ACME) AuthorizeSign(ctx context.Context, token string) ([]SignOption, error) {
  77. return []SignOption{
  78. // modifiers / withOptions
  79. newProvisionerExtensionOption(TypeACME, p.Name, ""),
  80. newForceCNOption(p.ForceCN),
  81. profileDefaultDuration(p.ctl.Claimer.DefaultTLSCertDuration()),
  82. // validators
  83. defaultPublicKeyValidator{},
  84. newValidityValidator(p.ctl.Claimer.MinTLSCertDuration(), p.ctl.Claimer.MaxTLSCertDuration()),
  85. }, nil
  86. }
  87. // AuthorizeRevoke is called just before the certificate is to be revoked by
  88. // the CA. It can be used to authorize revocation of a certificate. It
  89. // currently is a no-op.
  90. // TODO(hs): add configuration option that toggles revocation? Or change function signature to make it more useful?
  91. // Or move certain logic out of the Revoke API to here? Would likely involve some more stuff in the ctx.
  92. func (p *ACME) AuthorizeRevoke(ctx context.Context, token string) error {
  93. return nil
  94. }
  95. // AuthorizeRenew returns an error if the renewal is disabled.
  96. // NOTE: This method does not actually validate the certificate or check it's
  97. // revocation status. Just confirms that the provisioner that created the
  98. // certificate was configured to allow renewals.
  99. func (p *ACME) AuthorizeRenew(ctx context.Context, cert *x509.Certificate) error {
  100. return p.ctl.AuthorizeRenew(ctx, cert)
  101. }