common.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. Sign(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(ctx 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. AuthorizeChallenge(ctx context.Context, challenge string) error
  62. GetID() string
  63. GetName() string
  64. DefaultTLSCertDuration() time.Duration
  65. GetOptions() *provisioner.Options
  66. }
  67. type provisionerKey struct{}
  68. // NewProvisionerContext adds the given provisioner to the context.
  69. func NewProvisionerContext(ctx context.Context, v Provisioner) context.Context {
  70. return context.WithValue(ctx, provisionerKey{}, v)
  71. }
  72. // ProvisionerFromContext returns the current provisioner from the given context.
  73. func ProvisionerFromContext(ctx context.Context) (v Provisioner, ok bool) {
  74. v, ok = ctx.Value(provisionerKey{}).(Provisioner)
  75. return
  76. }
  77. // MustLinkerFromContext returns the current provisioner from the given context.
  78. // It will panic if it's not in the context.
  79. func MustProvisionerFromContext(ctx context.Context) Provisioner {
  80. if v, ok := ProvisionerFromContext(ctx); !ok {
  81. panic("acme provisioner is not the context")
  82. } else {
  83. return v
  84. }
  85. }
  86. // MockProvisioner for testing
  87. type MockProvisioner struct {
  88. Mret1 interface{}
  89. Merr error
  90. MgetID func() string
  91. MgetName func() string
  92. MauthorizeOrderIdentifier func(ctx context.Context, identifier provisioner.ACMEIdentifier) error
  93. MauthorizeSign func(ctx context.Context, ott string) ([]provisioner.SignOption, error)
  94. MauthorizeRevoke func(ctx context.Context, token string) error
  95. MauthorizeChallenge func(Ctx context.Context, challenge string) error
  96. MdefaultTLSCertDuration func() time.Duration
  97. MgetOptions func() *provisioner.Options
  98. }
  99. // GetName mock
  100. func (m *MockProvisioner) GetName() string {
  101. if m.MgetName != nil {
  102. return m.MgetName()
  103. }
  104. return m.Mret1.(string)
  105. }
  106. // AuthorizeOrderIdentifiers mock
  107. func (m *MockProvisioner) AuthorizeOrderIdentifier(ctx context.Context, identifier provisioner.ACMEIdentifier) error {
  108. if m.MauthorizeOrderIdentifier != nil {
  109. return m.MauthorizeOrderIdentifier(ctx, identifier)
  110. }
  111. return m.Merr
  112. }
  113. // AuthorizeSign mock
  114. func (m *MockProvisioner) AuthorizeSign(ctx context.Context, ott string) ([]provisioner.SignOption, error) {
  115. if m.MauthorizeSign != nil {
  116. return m.MauthorizeSign(ctx, ott)
  117. }
  118. return m.Mret1.([]provisioner.SignOption), m.Merr
  119. }
  120. // AuthorizeRevoke mock
  121. func (m *MockProvisioner) AuthorizeRevoke(ctx context.Context, token string) error {
  122. if m.MauthorizeRevoke != nil {
  123. return m.MauthorizeRevoke(ctx, token)
  124. }
  125. return m.Merr
  126. }
  127. // AuthorizeChallenge mock
  128. func (m *MockProvisioner) AuthorizeChallenge(ctx context.Context, challenge string) error {
  129. if m.MauthorizeChallenge != nil {
  130. return m.MauthorizeChallenge(ctx, challenge)
  131. }
  132. return m.Merr
  133. }
  134. // DefaultTLSCertDuration mock
  135. func (m *MockProvisioner) DefaultTLSCertDuration() time.Duration {
  136. if m.MdefaultTLSCertDuration != nil {
  137. return m.MdefaultTLSCertDuration()
  138. }
  139. return m.Mret1.(time.Duration)
  140. }
  141. // GetOptions mock
  142. func (m *MockProvisioner) GetOptions() *provisioner.Options {
  143. if m.MgetOptions != nil {
  144. return m.MgetOptions()
  145. }
  146. return m.Mret1.(*provisioner.Options)
  147. }
  148. // GetID mock
  149. func (m *MockProvisioner) GetID() string {
  150. if m.MgetID != nil {
  151. return m.MgetID()
  152. }
  153. return m.Mret1.(string)
  154. }