options.go 6.2 KB

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