services.go 3.3 KB

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