options.go 5.7 KB

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