issuer.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package stepcas
  2. import (
  3. "net/url"
  4. "strings"
  5. "time"
  6. "github.com/pkg/errors"
  7. "github.com/smallstep/certificates/ca"
  8. "github.com/smallstep/certificates/cas/apiv1"
  9. )
  10. type stepIssuer interface {
  11. SignToken(subject string, sans []string) (string, error)
  12. RevokeToken(subject string) (string, error)
  13. Lifetime(d time.Duration) time.Duration
  14. }
  15. // newStepIssuer returns the configured step issuer.
  16. func newStepIssuer(caURL *url.URL, client *ca.Client, iss *apiv1.CertificateIssuer) (stepIssuer, error) {
  17. if err := validateCertificateIssuer(iss); err != nil {
  18. return nil, err
  19. }
  20. switch strings.ToLower(iss.Type) {
  21. case "x5c":
  22. return newX5CIssuer(caURL, iss)
  23. case "jwk":
  24. return newJWKIssuer(caURL, client, iss)
  25. default:
  26. return nil, errors.Errorf("stepCAS `certificateIssuer.type` %s is not supported", iss.Type)
  27. }
  28. }
  29. // validateCertificateIssuer validates the configuration of the certificate
  30. // issuer.
  31. func validateCertificateIssuer(iss *apiv1.CertificateIssuer) error {
  32. switch {
  33. case iss == nil:
  34. return errors.New("stepCAS 'certificateIssuer' cannot be nil")
  35. case iss.Type == "":
  36. return errors.New("stepCAS `certificateIssuer.type` cannot be empty")
  37. }
  38. switch strings.ToLower(iss.Type) {
  39. case "x5c":
  40. return validateX5CIssuer(iss)
  41. case "jwk":
  42. return validateJWKIssuer(iss)
  43. default:
  44. return errors.Errorf("stepCAS `certificateIssuer.type` %s is not supported", iss.Type)
  45. }
  46. }
  47. // validateX5CIssuer validates the configuration of x5c issuer.
  48. func validateX5CIssuer(iss *apiv1.CertificateIssuer) error {
  49. switch {
  50. case iss.Certificate == "":
  51. return errors.New("stepCAS `certificateIssuer.crt` cannot be empty")
  52. case iss.Key == "":
  53. return errors.New("stepCAS `certificateIssuer.key` cannot be empty")
  54. case iss.Provisioner == "":
  55. return errors.New("stepCAS `certificateIssuer.provisioner` cannot be empty")
  56. default:
  57. return nil
  58. }
  59. }
  60. // validateJWKIssuer validates the configuration of jwk issuer. If the key is
  61. // not given, then it will download it from the CA. If the password is not set
  62. // it will be prompted.
  63. func validateJWKIssuer(iss *apiv1.CertificateIssuer) error {
  64. switch {
  65. case iss.Provisioner == "":
  66. return errors.New("stepCAS `certificateIssuer.provisioner` cannot be empty")
  67. default:
  68. return nil
  69. }
  70. }