options.go 3.9 KB

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