common.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. AuthorizeSign(ctx context.Context, token string) ([]provisioner.SignOption, error)
  27. AuthorizeRevoke(ctx context.Context, token string) error
  28. GetID() string
  29. GetName() string
  30. DefaultTLSCertDuration() time.Duration
  31. GetOptions() *provisioner.Options
  32. }
  33. // MockProvisioner for testing
  34. type MockProvisioner struct {
  35. Mret1 interface{}
  36. Merr error
  37. MgetID func() string
  38. MgetName func() string
  39. MauthorizeSign func(ctx context.Context, ott string) ([]provisioner.SignOption, error)
  40. MauthorizeRevoke func(ctx context.Context, token string) error
  41. MdefaultTLSCertDuration func() time.Duration
  42. MgetOptions func() *provisioner.Options
  43. }
  44. // GetName mock
  45. func (m *MockProvisioner) GetName() string {
  46. if m.MgetName != nil {
  47. return m.MgetName()
  48. }
  49. return m.Mret1.(string)
  50. }
  51. // AuthorizeSign mock
  52. func (m *MockProvisioner) AuthorizeSign(ctx context.Context, ott string) ([]provisioner.SignOption, error) {
  53. if m.MauthorizeSign != nil {
  54. return m.MauthorizeSign(ctx, ott)
  55. }
  56. return m.Mret1.([]provisioner.SignOption), m.Merr
  57. }
  58. // AuthorizeRevoke mock
  59. func (m *MockProvisioner) AuthorizeRevoke(ctx context.Context, token string) error {
  60. if m.MauthorizeRevoke != nil {
  61. return m.MauthorizeRevoke(ctx, token)
  62. }
  63. return m.Merr
  64. }
  65. // DefaultTLSCertDuration mock
  66. func (m *MockProvisioner) DefaultTLSCertDuration() time.Duration {
  67. if m.MdefaultTLSCertDuration != nil {
  68. return m.MdefaultTLSCertDuration()
  69. }
  70. return m.Mret1.(time.Duration)
  71. }
  72. // GetOptions mock
  73. func (m *MockProvisioner) GetOptions() *provisioner.Options {
  74. if m.MgetOptions != nil {
  75. return m.MgetOptions()
  76. }
  77. return m.Mret1.(*provisioner.Options)
  78. }
  79. // GetID mock
  80. func (m *MockProvisioner) GetID() string {
  81. if m.MgetID != nil {
  82. return m.MgetID()
  83. }
  84. return m.Mret1.(string)
  85. }