4
0

options.go 5.1 KB

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