options.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package apiv1
  2. import (
  3. "crypto"
  4. "crypto/x509"
  5. "github.com/pkg/errors"
  6. "github.com/smallstep/certificates/kms"
  7. )
  8. // Options represents the configuration options used to select and configure the
  9. // CertificateAuthorityService (CAS) to use.
  10. type Options struct {
  11. // The type of the CAS to use.
  12. Type string `json:"type"`
  13. // Path to the credentials file used in CloudCAS
  14. CredentialsFile string `json:"credentialsFile"`
  15. // CertificateAuthority reference. In CloudCAS the format is
  16. // `projects/*/locations/*/certificateAuthorities/*`.
  17. CertificateAuthority string `json:"certificateAuthority"`
  18. // Certificate and signer are the issuer certificate,along with any other bundled certificates to be returned in the chain for consumers, and signer used in SoftCAS.
  19. // They are configured in ca.json crt and key properties.
  20. CertificateChain []*x509.Certificate
  21. Signer crypto.Signer `json:"-"`
  22. // IsCreator is set to true when we're creating a certificate authority. Is
  23. // used to skip some validations when initializing a CertificateAuthority.
  24. IsCreator bool `json:"-"`
  25. // KeyManager is the KMS used to generate keys in SoftCAS.
  26. KeyManager kms.KeyManager `json:"-"`
  27. // Project and Location are parameters used in CloudCAS to create a new
  28. // certificate authority.
  29. Project string `json:"-"`
  30. Location string `json:"-"`
  31. }
  32. // Validate checks the fields in Options.
  33. func (o *Options) Validate() error {
  34. var typ Type
  35. if o == nil {
  36. typ = Type(SoftCAS)
  37. } else {
  38. typ = Type(o.Type)
  39. }
  40. // Check that the type can be loaded.
  41. if _, ok := LoadCertificateAuthorityServiceNewFunc(typ); !ok {
  42. return errors.Errorf("unsupported cas type %s", typ)
  43. }
  44. return nil
  45. }
  46. // Is returns if the options have the given type.
  47. func (o *Options) Is(t Type) bool {
  48. if o == nil {
  49. return t.String() == SoftCAS
  50. }
  51. return Type(o.Type).String() == t.String()
  52. }