issuer.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package stepcas
  2. import (
  3. "net/url"
  4. "strings"
  5. "time"
  6. "github.com/google/uuid"
  7. "github.com/pkg/errors"
  8. "github.com/smallstep/certificates/ca"
  9. "github.com/smallstep/certificates/cas/apiv1"
  10. )
  11. // raAuthorityNS is a custom namespace used to generate endpoint ids based on
  12. // the authority id.
  13. var raAuthorityNS = uuid.MustParse("d6f14c1f-2f92-47bf-a04f-7b2c11382edd")
  14. // newServerEndpointID returns a uuid v5 using raAuthorityNS as the namespace.
  15. // The return uuid will be used as the server endpoint id, it will be unique per
  16. // authority.
  17. func newServerEndpointID(data string) uuid.UUID {
  18. return uuid.NewSHA1(raAuthorityNS, []byte(data))
  19. }
  20. type raInfo struct {
  21. AuthorityID string `json:"authorityId,omitempty"`
  22. EndpointID string `json:"endpointId,omitempty"`
  23. ProvisionerID string `json:"provisionerId,omitempty"`
  24. ProvisionerType string `json:"provisionerType,omitempty"`
  25. ProvisionerName string `json:"provisionerName,omitempty"`
  26. }
  27. type stepIssuer interface {
  28. SignToken(subject string, sans []string, info *raInfo) (string, error)
  29. RevokeToken(subject string) (string, error)
  30. Lifetime(d time.Duration) time.Duration
  31. }
  32. // newStepIssuer returns the configured step issuer.
  33. func newStepIssuer(caURL *url.URL, client *ca.Client, iss *apiv1.CertificateIssuer) (stepIssuer, error) {
  34. if err := validateCertificateIssuer(iss); err != nil {
  35. return nil, err
  36. }
  37. switch strings.ToLower(iss.Type) {
  38. case "x5c":
  39. return newX5CIssuer(caURL, iss)
  40. case "jwk":
  41. return newJWKIssuer(caURL, client, iss)
  42. default:
  43. return nil, errors.Errorf("stepCAS `certificateIssuer.type` %s is not supported", iss.Type)
  44. }
  45. }
  46. // validateCertificateIssuer validates the configuration of the certificate
  47. // issuer.
  48. func validateCertificateIssuer(iss *apiv1.CertificateIssuer) error {
  49. switch {
  50. case iss == nil:
  51. return errors.New("stepCAS 'certificateIssuer' cannot be nil")
  52. case iss.Type == "":
  53. return errors.New("stepCAS `certificateIssuer.type` cannot be empty")
  54. }
  55. switch strings.ToLower(iss.Type) {
  56. case "x5c":
  57. return validateX5CIssuer(iss)
  58. case "jwk":
  59. return validateJWKIssuer(iss)
  60. default:
  61. return errors.Errorf("stepCAS `certificateIssuer.type` %s is not supported", iss.Type)
  62. }
  63. }
  64. // validateX5CIssuer validates the configuration of x5c issuer.
  65. func validateX5CIssuer(iss *apiv1.CertificateIssuer) error {
  66. switch {
  67. case iss.Certificate == "":
  68. return errors.New("stepCAS `certificateIssuer.crt` cannot be empty")
  69. case iss.Key == "":
  70. return errors.New("stepCAS `certificateIssuer.key` cannot be empty")
  71. case iss.Provisioner == "":
  72. return errors.New("stepCAS `certificateIssuer.provisioner` cannot be empty")
  73. default:
  74. return nil
  75. }
  76. }
  77. // validateJWKIssuer validates the configuration of jwk issuer. If the key is
  78. // not given, then it will download it from the CA. If the password is not set
  79. // it will be prompted.
  80. func validateJWKIssuer(iss *apiv1.CertificateIssuer) error {
  81. switch {
  82. case iss.Provisioner == "":
  83. return errors.New("stepCAS `certificateIssuer.provisioner` cannot be empty")
  84. default:
  85. return nil
  86. }
  87. }