options.go 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. // CertificateAuthority reference:
  14. // In StepCAS the value is the CA url, e.g. "https://ca.smallstep.com:9000".
  15. // In CloudCAS the format is "projects/*/locations/*/certificateAuthorities/*".
  16. CertificateAuthority string `json:"certificateAuthority,omitempty"`
  17. // CertificateAuthorityFingerprint is the root fingerprint used to
  18. // authenticate the connection to the CA when using StepCAS.
  19. CertificateAuthorityFingerprint string `json:"certificateAuthorityFingerprint,omitempty"`
  20. // CertificateIssuer contains the configuration used in StepCAS.
  21. CertificateIssuer *CertificateIssuer `json:"certificateIssuer,omitempty"`
  22. // Path to the credentials file used in CloudCAS. If not defined the default
  23. // authentication mechanism provided by Google SDK will be used. See
  24. // https://cloud.google.com/docs/authentication.
  25. CredentialsFile string `json:"credentialsFile,omitempty"`
  26. // Certificate and signer are the issuer certificate, along with any other
  27. // bundled certificates to be returned in the chain for consumers, and
  28. // signer used in SoftCAS. They are configured in ca.json crt and key
  29. // properties.
  30. CertificateChain []*x509.Certificate `json:"-"`
  31. Signer crypto.Signer `json:"-"`
  32. // IsCreator is set to true when we're creating a certificate authority. It
  33. // is used to skip some validations when initializing a
  34. // CertificateAuthority. This option is used on SoftCAS and CloudCAS.
  35. IsCreator bool `json:"-"`
  36. // IsCAGetter is set to true when we're just using the
  37. // CertificateAuthorityGetter interface to retrieve the root certificate. It
  38. // is used to skip some validations when initializing a
  39. // CertificateAuthority. This option is used on StepCAS.
  40. IsCAGetter bool `json:"-"`
  41. // KeyManager is the KMS used to generate keys in SoftCAS.
  42. KeyManager kms.KeyManager `json:"-"`
  43. // Project, Location, CaPool and GCSBucket are parameters used in CloudCAS
  44. // to create a new certificate authority. If a CaPool does not exist it will
  45. // be created. GCSBucket is optional, if not provided GCloud will create a
  46. // managed bucket.
  47. Project string `json:"-"`
  48. Location string `json:"-"`
  49. CaPool string `json:"-"`
  50. CaPoolTier string `json:"-"`
  51. GCSBucket string `json:"-"`
  52. }
  53. // CertificateIssuer contains the properties used to use the StepCAS certificate
  54. // authority service.
  55. type CertificateIssuer struct {
  56. Type string `json:"type"`
  57. Provisioner string `json:"provisioner,omitempty"`
  58. Certificate string `json:"crt,omitempty"`
  59. Key string `json:"key,omitempty"`
  60. Password string `json:"password,omitempty"`
  61. }
  62. // Validate checks the fields in Options.
  63. func (o *Options) Validate() error {
  64. var typ Type
  65. if o == nil {
  66. typ = Type(SoftCAS)
  67. } else {
  68. typ = Type(o.Type)
  69. }
  70. // Check that the type can be loaded.
  71. if _, ok := LoadCertificateAuthorityServiceNewFunc(typ); !ok {
  72. return errors.Errorf("unsupported cas type %s", typ)
  73. }
  74. return nil
  75. }
  76. // Is returns if the options have the given type.
  77. func (o *Options) Is(t Type) bool {
  78. if o == nil {
  79. return t.String() == SoftCAS
  80. }
  81. return Type(o.Type).String() == t.String()
  82. }