4
0

common.go 6.5 KB

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