common.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. // CertificateAuthority is the interface implemented by a CA authority.
  10. type CertificateAuthority interface {
  11. Sign(cr *x509.CertificateRequest, opts provisioner.SignOptions, signOpts ...provisioner.SignOption) ([]*x509.Certificate, error)
  12. AreSANsAllowed(ctx context.Context, sans []string) error
  13. IsRevoked(sn string) (bool, error)
  14. Revoke(context.Context, *authority.RevokeOptions) error
  15. LoadProvisionerByName(string) (provisioner.Interface, error)
  16. }
  17. // Clock that returns time in UTC rounded to seconds.
  18. type Clock struct{}
  19. // Now returns the UTC time rounded to seconds.
  20. func (c *Clock) Now() time.Time {
  21. return time.Now().UTC().Truncate(time.Second)
  22. }
  23. var clock Clock
  24. // Provisioner is an interface that implements a subset of the provisioner.Interface --
  25. // only those methods required by the ACME api/authority.
  26. type Provisioner interface {
  27. AuthorizeOrderIdentifier(ctx context.Context, identifier provisioner.ACMEIdentifier) error
  28. AuthorizeSign(ctx context.Context, token string) ([]provisioner.SignOption, error)
  29. AuthorizeRevoke(ctx context.Context, token string) error
  30. GetID() string
  31. GetName() string
  32. DefaultTLSCertDuration() time.Duration
  33. GetOptions() *provisioner.Options
  34. }
  35. // MockProvisioner for testing
  36. type MockProvisioner struct {
  37. Mret1 interface{}
  38. Merr error
  39. MgetID func() string
  40. MgetName func() string
  41. MauthorizeOrderIdentifier func(ctx context.Context, identifier provisioner.ACMEIdentifier) error
  42. MauthorizeSign func(ctx context.Context, ott string) ([]provisioner.SignOption, error)
  43. MauthorizeRevoke func(ctx context.Context, token string) error
  44. MdefaultTLSCertDuration func() time.Duration
  45. MgetOptions func() *provisioner.Options
  46. }
  47. // GetName mock
  48. func (m *MockProvisioner) GetName() string {
  49. if m.MgetName != nil {
  50. return m.MgetName()
  51. }
  52. return m.Mret1.(string)
  53. }
  54. // AuthorizeOrderIdentifiers mock
  55. func (m *MockProvisioner) AuthorizeOrderIdentifier(ctx context.Context, identifier provisioner.ACMEIdentifier) error {
  56. if m.MauthorizeOrderIdentifier != nil {
  57. return m.MauthorizeOrderIdentifier(ctx, identifier)
  58. }
  59. return m.Merr
  60. }
  61. // AuthorizeSign mock
  62. func (m *MockProvisioner) AuthorizeSign(ctx context.Context, ott string) ([]provisioner.SignOption, error) {
  63. if m.MauthorizeSign != nil {
  64. return m.MauthorizeSign(ctx, ott)
  65. }
  66. return m.Mret1.([]provisioner.SignOption), m.Merr
  67. }
  68. // AuthorizeRevoke mock
  69. func (m *MockProvisioner) AuthorizeRevoke(ctx context.Context, token string) error {
  70. if m.MauthorizeRevoke != nil {
  71. return m.MauthorizeRevoke(ctx, token)
  72. }
  73. return m.Merr
  74. }
  75. // DefaultTLSCertDuration mock
  76. func (m *MockProvisioner) DefaultTLSCertDuration() time.Duration {
  77. if m.MdefaultTLSCertDuration != nil {
  78. return m.MdefaultTLSCertDuration()
  79. }
  80. return m.Mret1.(time.Duration)
  81. }
  82. // GetOptions mock
  83. func (m *MockProvisioner) GetOptions() *provisioner.Options {
  84. if m.MgetOptions != nil {
  85. return m.MgetOptions()
  86. }
  87. return m.Mret1.(*provisioner.Options)
  88. }
  89. // GetID mock
  90. func (m *MockProvisioner) GetID() string {
  91. if m.MgetID != nil {
  92. return m.MgetID()
  93. }
  94. return m.Mret1.(string)
  95. }