common.go 3.2 KB

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