options.go 4.1 KB

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