common.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package acme
  2. import (
  3. "context"
  4. "crypto/x509"
  5. "time"
  6. "github.com/smallstep/certificates/authority"
  7. "github.com/smallstep/certificates/authority/provisioner"
  8. )
  9. // Clock that returns time in UTC rounded to seconds.
  10. type Clock struct{}
  11. // Now returns the UTC time rounded to seconds.
  12. func (c *Clock) Now() time.Time {
  13. return time.Now().UTC().Truncate(time.Second)
  14. }
  15. var clock Clock
  16. // CertificateAuthority is the interface implemented by a CA authority.
  17. type CertificateAuthority interface {
  18. SignWithContext(ctx context.Context, cr *x509.CertificateRequest, opts provisioner.SignOptions, signOpts ...provisioner.SignOption) ([]*x509.Certificate, error)
  19. AreSANsAllowed(ctx context.Context, sans []string) error
  20. IsRevoked(sn string) (bool, error)
  21. Revoke(context.Context, *authority.RevokeOptions) error
  22. LoadProvisionerByName(string) (provisioner.Interface, error)
  23. GetBackdate() *time.Duration
  24. }
  25. // NewContext adds the given acme components to the context.
  26. func NewContext(ctx context.Context, db DB, client Client, linker Linker, fn PrerequisitesChecker) context.Context {
  27. ctx = NewDatabaseContext(ctx, db)
  28. ctx = NewClientContext(ctx, client)
  29. ctx = NewLinkerContext(ctx, linker)
  30. // Prerequisite checker is optional.
  31. if fn != nil {
  32. ctx = NewPrerequisitesCheckerContext(ctx, fn)
  33. }
  34. return ctx
  35. }
  36. // PrerequisitesChecker is a function that checks if all prerequisites for
  37. // serving ACME are met by the CA configuration.
  38. type PrerequisitesChecker func(ctx context.Context) (bool, error)
  39. // DefaultPrerequisitesChecker is the default PrerequisiteChecker and returns
  40. // always true.
  41. func DefaultPrerequisitesChecker(context.Context) (bool, error) {
  42. return true, nil
  43. }
  44. type prerequisitesKey struct{}
  45. // NewPrerequisitesCheckerContext adds the given PrerequisitesChecker to the
  46. // context.
  47. func NewPrerequisitesCheckerContext(ctx context.Context, fn PrerequisitesChecker) context.Context {
  48. return context.WithValue(ctx, prerequisitesKey{}, fn)
  49. }
  50. // PrerequisitesCheckerFromContext returns the PrerequisitesChecker in the
  51. // context.
  52. func PrerequisitesCheckerFromContext(ctx context.Context) (PrerequisitesChecker, bool) {
  53. fn, ok := ctx.Value(prerequisitesKey{}).(PrerequisitesChecker)
  54. return fn, ok && fn != nil
  55. }
  56. // Provisioner is an interface that implements a subset of the provisioner.Interface --
  57. // only those methods required by the ACME api/authority.
  58. type Provisioner interface {
  59. AuthorizeOrderIdentifier(ctx context.Context, identifier provisioner.ACMEIdentifier) error
  60. AuthorizeSign(ctx context.Context, token string) ([]provisioner.SignOption, error)
  61. AuthorizeRevoke(ctx context.Context, token string) error
  62. IsChallengeEnabled(ctx context.Context, challenge provisioner.ACMEChallenge) bool
  63. IsAttestationFormatEnabled(ctx context.Context, format provisioner.ACMEAttestationFormat) bool
  64. GetAttestationRoots() (*x509.CertPool, bool)
  65. GetID() string
  66. GetName() string
  67. DefaultTLSCertDuration() time.Duration
  68. GetOptions() *provisioner.Options
  69. }
  70. type provisionerKey struct{}
  71. // NewProvisionerContext adds the given provisioner to the context.
  72. func NewProvisionerContext(ctx context.Context, v Provisioner) context.Context {
  73. return context.WithValue(ctx, provisionerKey{}, v)
  74. }
  75. // ProvisionerFromContext returns the current provisioner from the given context.
  76. func ProvisionerFromContext(ctx context.Context) (v Provisioner, ok bool) {
  77. v, ok = ctx.Value(provisionerKey{}).(Provisioner)
  78. return
  79. }
  80. // MustProvisionerFromContext returns the current provisioner from the given context.
  81. // It will panic if it's not in the context.
  82. func MustProvisionerFromContext(ctx context.Context) Provisioner {
  83. var (
  84. v Provisioner
  85. ok bool
  86. )
  87. if v, ok = ProvisionerFromContext(ctx); !ok {
  88. panic("acme provisioner is not the context")
  89. }
  90. return v
  91. }
  92. // MockProvisioner for testing
  93. type MockProvisioner struct {
  94. Mret1 any
  95. Merr error
  96. MgetID func() string
  97. MgetName func() string
  98. MauthorizeOrderIdentifier func(ctx context.Context, identifier provisioner.ACMEIdentifier) error
  99. MauthorizeSign func(ctx context.Context, ott string) ([]provisioner.SignOption, error)
  100. MauthorizeRevoke func(ctx context.Context, token string) error
  101. MisChallengeEnabled func(ctx context.Context, challenge provisioner.ACMEChallenge) bool
  102. MisAttFormatEnabled func(ctx context.Context, format provisioner.ACMEAttestationFormat) bool
  103. MgetAttestationRoots func() (*x509.CertPool, bool)
  104. MdefaultTLSCertDuration func() time.Duration
  105. MgetOptions func() *provisioner.Options
  106. }
  107. // GetName mock
  108. func (m *MockProvisioner) GetName() string {
  109. if m.MgetName != nil {
  110. return m.MgetName()
  111. }
  112. return m.Mret1.(string)
  113. }
  114. // AuthorizeOrderIdentifier mock
  115. func (m *MockProvisioner) AuthorizeOrderIdentifier(ctx context.Context, identifier provisioner.ACMEIdentifier) error {
  116. if m.MauthorizeOrderIdentifier != nil {
  117. return m.MauthorizeOrderIdentifier(ctx, identifier)
  118. }
  119. return m.Merr
  120. }
  121. // AuthorizeSign mock
  122. func (m *MockProvisioner) AuthorizeSign(ctx context.Context, ott string) ([]provisioner.SignOption, error) {
  123. if m.MauthorizeSign != nil {
  124. return m.MauthorizeSign(ctx, ott)
  125. }
  126. return m.Mret1.([]provisioner.SignOption), m.Merr
  127. }
  128. // AuthorizeRevoke mock
  129. func (m *MockProvisioner) AuthorizeRevoke(ctx context.Context, token string) error {
  130. if m.MauthorizeRevoke != nil {
  131. return m.MauthorizeRevoke(ctx, token)
  132. }
  133. return m.Merr
  134. }
  135. // IsChallengeEnabled mock
  136. func (m *MockProvisioner) IsChallengeEnabled(ctx context.Context, challenge provisioner.ACMEChallenge) bool {
  137. if m.MisChallengeEnabled != nil {
  138. return m.MisChallengeEnabled(ctx, challenge)
  139. }
  140. return m.Merr == nil
  141. }
  142. // IsAttestationFormatEnabled mock
  143. func (m *MockProvisioner) IsAttestationFormatEnabled(ctx context.Context, format provisioner.ACMEAttestationFormat) bool {
  144. if m.MisAttFormatEnabled != nil {
  145. return m.MisAttFormatEnabled(ctx, format)
  146. }
  147. return m.Merr == nil
  148. }
  149. func (m *MockProvisioner) GetAttestationRoots() (*x509.CertPool, bool) {
  150. if m.MgetAttestationRoots != nil {
  151. return m.MgetAttestationRoots()
  152. }
  153. return m.Mret1.(*x509.CertPool), m.Mret1 != nil
  154. }
  155. // DefaultTLSCertDuration mock
  156. func (m *MockProvisioner) DefaultTLSCertDuration() time.Duration {
  157. if m.MdefaultTLSCertDuration != nil {
  158. return m.MdefaultTLSCertDuration()
  159. }
  160. return m.Mret1.(time.Duration)
  161. }
  162. // GetOptions mock
  163. func (m *MockProvisioner) GetOptions() *provisioner.Options {
  164. if m.MgetOptions != nil {
  165. return m.MgetOptions()
  166. }
  167. return m.Mret1.(*provisioner.Options)
  168. }
  169. // GetID mock
  170. func (m *MockProvisioner) GetID() string {
  171. if m.MgetID != nil {
  172. return m.MgetID()
  173. }
  174. return m.Mret1.(string)
  175. }