options.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. // CertificateChain contains the issuer certificate, along with any other
  27. // bundled certificates to be returned in the chain to consumers. It is used
  28. // used in SoftCAS and it is configured in the crt property of the ca.json.
  29. CertificateChain []*x509.Certificate `json:"-"`
  30. // Signer is the private key or a KMS signer for the issuer certificate. It
  31. // is used in SoftCAS and it is configured in the key property of the
  32. // ca.json.
  33. Signer crypto.Signer `json:"-"`
  34. // CertificateSigner combines CertificateChain and Signer in a callback that
  35. // returns the chain of certificate and signer used to sign X.509
  36. // certificates in SoftCAS.
  37. CertificateSigner func() ([]*x509.Certificate, crypto.Signer, error) `json:"-"`
  38. // IsCreator is set to true when we're creating a certificate authority. It
  39. // is used to skip some validations when initializing a
  40. // CertificateAuthority. This option is used on SoftCAS and CloudCAS.
  41. IsCreator bool `json:"-"`
  42. // IsCAGetter is set to true when we're just using the
  43. // CertificateAuthorityGetter interface to retrieve the root certificate. It
  44. // is used to skip some validations when initializing a
  45. // CertificateAuthority. This option is used on StepCAS.
  46. IsCAGetter bool `json:"-"`
  47. // KeyManager is the KMS used to generate keys in SoftCAS.
  48. KeyManager kms.KeyManager `json:"-"`
  49. // Project, Location, CaPool and GCSBucket are parameters used in CloudCAS
  50. // to create a new certificate authority. If a CaPool does not exist it will
  51. // be created. GCSBucket is optional, if not provided GCloud will create a
  52. // managed bucket.
  53. Project string `json:"-"`
  54. Location string `json:"-"`
  55. CaPool string `json:"-"`
  56. CaPoolTier string `json:"-"`
  57. GCSBucket string `json:"-"`
  58. }
  59. // CertificateIssuer contains the properties used to use the StepCAS certificate
  60. // authority service.
  61. type CertificateIssuer struct {
  62. Type string `json:"type"`
  63. Provisioner string `json:"provisioner,omitempty"`
  64. Certificate string `json:"crt,omitempty"`
  65. Key string `json:"key,omitempty"`
  66. Password string `json:"password,omitempty"`
  67. }
  68. // Validate checks the fields in Options.
  69. func (o *Options) Validate() error {
  70. var typ Type
  71. if o == nil {
  72. typ = Type(SoftCAS)
  73. } else {
  74. typ = Type(o.Type)
  75. }
  76. // Check that the type can be loaded.
  77. if _, ok := LoadCertificateAuthorityServiceNewFunc(typ); !ok {
  78. return errors.Errorf("unsupported cas type %s", typ)
  79. }
  80. return nil
  81. }
  82. // Is returns if the options have the given type.
  83. func (o *Options) Is(t Type) bool {
  84. if o == nil {
  85. return t.String() == SoftCAS
  86. }
  87. return Type(o.Type).String() == t.String()
  88. }