options.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. package authority
  2. import (
  3. "context"
  4. "crypto"
  5. "crypto/x509"
  6. "encoding/pem"
  7. "github.com/pkg/errors"
  8. "github.com/smallstep/certificates/authority/provisioner"
  9. "github.com/smallstep/certificates/cas"
  10. casapi "github.com/smallstep/certificates/cas/apiv1"
  11. "github.com/smallstep/certificates/db"
  12. "github.com/smallstep/certificates/kms"
  13. "golang.org/x/crypto/ssh"
  14. )
  15. // Option sets options to the Authority.
  16. type Option func(*Authority) error
  17. // WithConfig replaces the current config with the given one. No validation is
  18. // performed in the given value.
  19. func WithConfig(config *Config) Option {
  20. return func(a *Authority) error {
  21. a.config = config
  22. return nil
  23. }
  24. }
  25. // WithConfigFile reads the given filename as a configuration file and replaces
  26. // the current one. No validation is performed in the given configuration.
  27. func WithConfigFile(filename string) Option {
  28. return func(a *Authority) (err error) {
  29. a.config, err = LoadConfiguration(filename)
  30. return
  31. }
  32. }
  33. // WithDatabase sets an already initialized authority database to a new
  34. // authority. This option is intended to be use on graceful reloads.
  35. func WithDatabase(db db.AuthDB) Option {
  36. return func(a *Authority) error {
  37. a.db = db
  38. return nil
  39. }
  40. }
  41. // WithGetIdentityFunc sets a custom function to retrieve the identity from
  42. // an external resource.
  43. func WithGetIdentityFunc(fn func(ctx context.Context, p provisioner.Interface, email string) (*provisioner.Identity, error)) Option {
  44. return func(a *Authority) error {
  45. a.getIdentityFunc = fn
  46. return nil
  47. }
  48. }
  49. // WithSSHBastionFunc sets a custom function to get the bastion for a
  50. // given user-host pair.
  51. func WithSSHBastionFunc(fn func(ctx context.Context, user, host string) (*Bastion, error)) Option {
  52. return func(a *Authority) error {
  53. a.sshBastionFunc = fn
  54. return nil
  55. }
  56. }
  57. // WithSSHGetHosts sets a custom function to get the bastion for a
  58. // given user-host pair.
  59. func WithSSHGetHosts(fn func(ctx context.Context, cert *x509.Certificate) ([]Host, error)) Option {
  60. return func(a *Authority) error {
  61. a.sshGetHostsFunc = fn
  62. return nil
  63. }
  64. }
  65. // WithSSHCheckHost sets a custom function to check whether a given host is
  66. // step ssh enabled. The token is used to validate the request, while the roots
  67. // are used to validate the token.
  68. func WithSSHCheckHost(fn func(ctx context.Context, principal string, tok string, roots []*x509.Certificate) (bool, error)) Option {
  69. return func(a *Authority) error {
  70. a.sshCheckHostFunc = fn
  71. return nil
  72. }
  73. }
  74. // WithKeyManager defines the key manager used to get and create keys, and sign
  75. // certificates.
  76. func WithKeyManager(k kms.KeyManager) Option {
  77. return func(a *Authority) error {
  78. a.keyManager = k
  79. return nil
  80. }
  81. }
  82. // WithX509Signer defines the signer used to sign X509 certificates.
  83. func WithX509Signer(crt *x509.Certificate, s crypto.Signer) Option {
  84. return func(a *Authority) error {
  85. srv, err := cas.New(context.Background(), casapi.Options{
  86. Type: casapi.SoftCAS,
  87. Signer: s,
  88. CertificateChain: []*x509.Certificate{crt},
  89. })
  90. if err != nil {
  91. return err
  92. }
  93. a.x509CAService = srv
  94. return nil
  95. }
  96. }
  97. // WithSSHUserSigner defines the signer used to sign SSH user certificates.
  98. func WithSSHUserSigner(s crypto.Signer) Option {
  99. return func(a *Authority) error {
  100. signer, err := ssh.NewSignerFromSigner(s)
  101. if err != nil {
  102. return errors.Wrap(err, "error creating ssh user signer")
  103. }
  104. a.sshCAUserCertSignKey = signer
  105. // Append public key to list of user certs
  106. pub := signer.PublicKey()
  107. a.sshCAUserCerts = append(a.sshCAUserCerts, pub)
  108. a.sshCAUserFederatedCerts = append(a.sshCAUserFederatedCerts, pub)
  109. return nil
  110. }
  111. }
  112. // WithSSHHostSigner defines the signer used to sign SSH host certificates.
  113. func WithSSHHostSigner(s crypto.Signer) Option {
  114. return func(a *Authority) error {
  115. signer, err := ssh.NewSignerFromSigner(s)
  116. if err != nil {
  117. return errors.Wrap(err, "error creating ssh host signer")
  118. }
  119. a.sshCAHostCertSignKey = signer
  120. // Append public key to list of host certs
  121. pub := signer.PublicKey()
  122. a.sshCAHostCerts = append(a.sshCAHostCerts, pub)
  123. a.sshCAHostFederatedCerts = append(a.sshCAHostFederatedCerts, pub)
  124. return nil
  125. }
  126. }
  127. // WithX509RootCerts is an option that allows to define the list of root
  128. // certificates to use. This option will replace any root certificate defined
  129. // before.
  130. func WithX509RootCerts(rootCerts ...*x509.Certificate) Option {
  131. return func(a *Authority) error {
  132. a.rootX509Certs = rootCerts
  133. return nil
  134. }
  135. }
  136. // WithX509FederatedCerts is an option that allows to define the list of
  137. // federated certificates. This option will replace any federated certificate
  138. // defined before.
  139. func WithX509FederatedCerts(certs ...*x509.Certificate) Option {
  140. return func(a *Authority) error {
  141. a.federatedX509Certs = certs
  142. return nil
  143. }
  144. }
  145. // WithX509RootBundle is an option that allows to define the list of root
  146. // certificates. This option will replace any root certificate defined before.
  147. func WithX509RootBundle(pemCerts []byte) Option {
  148. return func(a *Authority) error {
  149. certs, err := readCertificateBundle(pemCerts)
  150. if err != nil {
  151. return err
  152. }
  153. a.rootX509Certs = certs
  154. return nil
  155. }
  156. }
  157. // WithX509FederatedBundle is an option that allows to define the list of
  158. // federated certificates. This option will replace any federated certificate
  159. // defined before.
  160. func WithX509FederatedBundle(pemCerts []byte) Option {
  161. return func(a *Authority) error {
  162. certs, err := readCertificateBundle(pemCerts)
  163. if err != nil {
  164. return err
  165. }
  166. a.federatedX509Certs = certs
  167. return nil
  168. }
  169. }
  170. func readCertificateBundle(pemCerts []byte) ([]*x509.Certificate, error) {
  171. var block *pem.Block
  172. var certs []*x509.Certificate
  173. for len(pemCerts) > 0 {
  174. block, pemCerts = pem.Decode(pemCerts)
  175. if block == nil {
  176. break
  177. }
  178. if block.Type != "CERTIFICATE" || len(block.Headers) != 0 {
  179. continue
  180. }
  181. cert, err := x509.ParseCertificate(block.Bytes)
  182. if err != nil {
  183. return nil, err
  184. }
  185. certs = append(certs, cert)
  186. }
  187. return certs, nil
  188. }