issuer.go 3.0 KB

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