services.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package apiv1
  2. import (
  3. "crypto"
  4. "crypto/x509"
  5. "net/http"
  6. "strings"
  7. )
  8. // CertificateAuthorityService is the interface implemented to support external
  9. // certificate authorities.
  10. type CertificateAuthorityService interface {
  11. CreateCertificate(req *CreateCertificateRequest) (*CreateCertificateResponse, error)
  12. RenewCertificate(req *RenewCertificateRequest) (*RenewCertificateResponse, error)
  13. RevokeCertificate(req *RevokeCertificateRequest) (*RevokeCertificateResponse, error)
  14. }
  15. // CertificateAuthorityCRLGenerator is an optional interface implemented by CertificateAuthorityService
  16. // that has a method to create a CRL
  17. type CertificateAuthorityCRLGenerator interface {
  18. CreateCRL(req *CreateCRLRequest) (*CreateCRLResponse, error)
  19. }
  20. // CertificateAuthorityGetter is an interface implemented by a
  21. // CertificateAuthorityService that has a method to get the root certificate.
  22. type CertificateAuthorityGetter interface {
  23. GetCertificateAuthority(req *GetCertificateAuthorityRequest) (*GetCertificateAuthorityResponse, error)
  24. }
  25. // CertificateAuthorityCreator is an interface implemented by a
  26. // CertificateAuthorityService that has a method to create a new certificate
  27. // authority.
  28. type CertificateAuthorityCreator interface {
  29. CreateCertificateAuthority(req *CreateCertificateAuthorityRequest) (*CreateCertificateAuthorityResponse, error)
  30. }
  31. // CertificateAuthoritySigner is an optional interface implemented by a
  32. // CertificateAuthorityService that has a method that returns a [crypto.Signer]
  33. // using the same key used to issue certificates.
  34. type CertificateAuthoritySigner interface {
  35. GetSigner() (crypto.Signer, error)
  36. }
  37. // SignatureAlgorithmGetter is an optional implementation in a crypto.Signer
  38. // that returns the SignatureAlgorithm to use.
  39. type SignatureAlgorithmGetter interface {
  40. SignatureAlgorithm() x509.SignatureAlgorithm
  41. }
  42. // Type represents the CAS type used.
  43. type Type string
  44. const (
  45. // DefaultCAS is a CertificateAuthorityService using software.
  46. DefaultCAS = ""
  47. // SoftCAS is a CertificateAuthorityService using software.
  48. SoftCAS = "softcas"
  49. // CloudCAS is a CertificateAuthorityService using Google Cloud CAS.
  50. CloudCAS = "cloudcas"
  51. // StepCAS is a CertificateAuthorityService using another step-ca instance.
  52. StepCAS = "stepcas"
  53. // VaultCAS is a CertificateAuthorityService using Hasicorp Vault PKI.
  54. VaultCAS = "vaultcas"
  55. // ExternalCAS is a CertificateAuthorityService using an external injected CA implementation
  56. ExternalCAS = "externalcas"
  57. )
  58. // String returns a string from the type. It will always return the lower case
  59. // version of the Type, as we need a standard type to compare and use as the
  60. // registry key.
  61. func (t Type) String() string {
  62. if t == "" {
  63. return SoftCAS
  64. }
  65. return strings.ToLower(string(t))
  66. }
  67. // TypeOf returns the type of the given CertificateAuthorityService.
  68. func TypeOf(c CertificateAuthorityService) Type {
  69. if ct, ok := c.(interface{ Type() Type }); ok {
  70. return ct.Type()
  71. }
  72. return ExternalCAS
  73. }
  74. // NotImplementedError is the type of error returned if an operation is not implemented.
  75. type NotImplementedError struct {
  76. Message string
  77. }
  78. // Error implements the error interface.
  79. func (e NotImplementedError) Error() string {
  80. if e.Message != "" {
  81. return e.Message
  82. }
  83. return "not implemented"
  84. }
  85. // StatusCode implements the StatusCoder interface and returns the HTTP 501
  86. // error.
  87. func (e NotImplementedError) StatusCode() int {
  88. return http.StatusNotImplemented
  89. }
  90. // ValidationError is the type of error returned if request is not properly
  91. // validated.
  92. type ValidationError struct {
  93. Message string
  94. }
  95. // NotImplementedError implements the error interface.
  96. func (e ValidationError) Error() string {
  97. if e.Message != "" {
  98. return e.Message
  99. }
  100. return "bad request"
  101. }
  102. // StatusCode implements the StatusCoder interface and returns the HTTP 400
  103. // error.
  104. func (e ValidationError) StatusCode() int {
  105. return http.StatusBadRequest
  106. }