services.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. )
  49. // String returns a string from the type. It will always return the lower case
  50. // version of the Type, as we need a standard type to compare and use as the
  51. // registry key.
  52. func (t Type) String() string {
  53. if t == "" {
  54. return SoftCAS
  55. }
  56. return strings.ToLower(string(t))
  57. }
  58. // NotImplementedError is the type of error returned if an operation is not implemented.
  59. type NotImplementedError struct {
  60. Message string
  61. }
  62. // Error implements the error interface.
  63. func (e NotImplementedError) Error() string {
  64. if e.Message != "" {
  65. return e.Message
  66. }
  67. return "not implemented"
  68. }
  69. // StatusCode implements the StatusCoder interface and returns the HTTP 501
  70. // error.
  71. func (e NotImplementedError) StatusCode() int {
  72. return http.StatusNotImplemented
  73. }
  74. // ValidationError is the type of error returned if request is not properly
  75. // validated.
  76. type ValidationError struct {
  77. Message string
  78. }
  79. // NotImplementedError implements the error interface.
  80. func (e ValidationError) Error() string {
  81. if e.Message != "" {
  82. return e.Message
  83. }
  84. return "bad request"
  85. }
  86. // StatusCode implements the StatusCoder interface and returns the HTTP 400
  87. // error.
  88. func (e ValidationError) StatusCode() int {
  89. return http.StatusBadRequest
  90. }