common.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package acme
  2. import (
  3. "context"
  4. "crypto/x509"
  5. "time"
  6. "github.com/smallstep/certificates/authority/provisioner"
  7. )
  8. // CertificateAuthority is the interface implemented by a CA authority.
  9. type CertificateAuthority interface {
  10. Sign(cr *x509.CertificateRequest, opts provisioner.SignOptions, signOpts ...provisioner.SignOption) ([]*x509.Certificate, error)
  11. LoadProvisionerByName(string) (provisioner.Interface, error)
  12. }
  13. // Clock that returns time in UTC rounded to seconds.
  14. type Clock struct{}
  15. // Now returns the UTC time rounded to seconds.
  16. func (c *Clock) Now() time.Time {
  17. return time.Now().UTC().Truncate(time.Second)
  18. }
  19. var clock Clock
  20. // Provisioner is an interface that implements a subset of the provisioner.Interface --
  21. // only those methods required by the ACME api/authority.
  22. type Provisioner interface {
  23. AuthorizeSign(ctx context.Context, token string) ([]provisioner.SignOption, error)
  24. GetID() string
  25. GetName() string
  26. DefaultTLSCertDuration() time.Duration
  27. GetOptions() *provisioner.Options
  28. }
  29. // MockProvisioner for testing
  30. type MockProvisioner struct {
  31. Mret1 interface{}
  32. Merr error
  33. MgetID func() string
  34. MgetName func() string
  35. MauthorizeSign func(ctx context.Context, ott string) ([]provisioner.SignOption, error)
  36. MdefaultTLSCertDuration func() time.Duration
  37. MgetOptions func() *provisioner.Options
  38. }
  39. // GetName mock
  40. func (m *MockProvisioner) GetName() string {
  41. if m.MgetName != nil {
  42. return m.MgetName()
  43. }
  44. return m.Mret1.(string)
  45. }
  46. // AuthorizeSign mock
  47. func (m *MockProvisioner) AuthorizeSign(ctx context.Context, ott string) ([]provisioner.SignOption, error) {
  48. if m.MauthorizeSign != nil {
  49. return m.MauthorizeSign(ctx, ott)
  50. }
  51. return m.Mret1.([]provisioner.SignOption), m.Merr
  52. }
  53. // DefaultTLSCertDuration mock
  54. func (m *MockProvisioner) DefaultTLSCertDuration() time.Duration {
  55. if m.MdefaultTLSCertDuration != nil {
  56. return m.MdefaultTLSCertDuration()
  57. }
  58. return m.Mret1.(time.Duration)
  59. }
  60. // GetOptions mock
  61. func (m *MockProvisioner) GetOptions() *provisioner.Options {
  62. if m.MgetOptions != nil {
  63. return m.MgetOptions()
  64. }
  65. return m.Mret1.(*provisioner.Options)
  66. }
  67. // GetID mock
  68. func (m *MockProvisioner) GetID() string {
  69. if m.MgetID != nil {
  70. return m.MgetID()
  71. }
  72. return m.Mret1.(string)
  73. }