common.go 5.3 KB

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