options.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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(cfg *config.Config) Option {
  22. return func(a *Authority) error {
  23. a.config = cfg
  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. // WithPassword set the password to decrypt the intermediate key as well as the
  36. // ssh host and user keys if they are not overridden by other options.
  37. func WithPassword(password []byte) Option {
  38. return func(a *Authority) (err error) {
  39. a.password = password
  40. return
  41. }
  42. }
  43. // WithSSHHostPassword set the password to decrypt the key used to sign SSH host
  44. // certificates.
  45. func WithSSHHostPassword(password []byte) Option {
  46. return func(a *Authority) (err error) {
  47. a.sshHostPassword = password
  48. return
  49. }
  50. }
  51. // WithSSHUserPassword set the password to decrypt the key used to sign SSH user
  52. // certificates.
  53. func WithSSHUserPassword(password []byte) Option {
  54. return func(a *Authority) (err error) {
  55. a.sshUserPassword = password
  56. return
  57. }
  58. }
  59. // WithIssuerPassword set the password to decrypt the certificate issuer private
  60. // key used in RA mode.
  61. func WithIssuerPassword(password []byte) Option {
  62. return func(a *Authority) (err error) {
  63. a.issuerPassword = password
  64. return
  65. }
  66. }
  67. // WithDatabase sets an already initialized authority database to a new
  68. // authority. This option is intended to be use on graceful reloads.
  69. func WithDatabase(d db.AuthDB) Option {
  70. return func(a *Authority) error {
  71. a.db = d
  72. return nil
  73. }
  74. }
  75. // WithGetIdentityFunc sets a custom function to retrieve the identity from
  76. // an external resource.
  77. func WithGetIdentityFunc(fn func(ctx context.Context, p provisioner.Interface, email string) (*provisioner.Identity, error)) Option {
  78. return func(a *Authority) error {
  79. a.getIdentityFunc = fn
  80. return nil
  81. }
  82. }
  83. // WithSSHBastionFunc sets a custom function to get the bastion for a
  84. // given user-host pair.
  85. func WithSSHBastionFunc(fn func(ctx context.Context, user, host string) (*config.Bastion, error)) Option {
  86. return func(a *Authority) error {
  87. a.sshBastionFunc = fn
  88. return nil
  89. }
  90. }
  91. // WithSSHGetHosts sets a custom function to get the bastion for a
  92. // given user-host pair.
  93. func WithSSHGetHosts(fn func(ctx context.Context, cert *x509.Certificate) ([]config.Host, error)) Option {
  94. return func(a *Authority) error {
  95. a.sshGetHostsFunc = fn
  96. return nil
  97. }
  98. }
  99. // WithSSHCheckHost sets a custom function to check whether a given host is
  100. // step ssh enabled. The token is used to validate the request, while the roots
  101. // are used to validate the token.
  102. func WithSSHCheckHost(fn func(ctx context.Context, principal string, tok string, roots []*x509.Certificate) (bool, error)) Option {
  103. return func(a *Authority) error {
  104. a.sshCheckHostFunc = fn
  105. return nil
  106. }
  107. }
  108. // WithKeyManager defines the key manager used to get and create keys, and sign
  109. // certificates.
  110. func WithKeyManager(k kms.KeyManager) Option {
  111. return func(a *Authority) error {
  112. a.keyManager = k
  113. return nil
  114. }
  115. }
  116. // WithX509Signer defines the signer used to sign X509 certificates.
  117. func WithX509Signer(crt *x509.Certificate, s crypto.Signer) Option {
  118. return func(a *Authority) error {
  119. srv, err := cas.New(context.Background(), casapi.Options{
  120. Type: casapi.SoftCAS,
  121. Signer: s,
  122. CertificateChain: []*x509.Certificate{crt},
  123. })
  124. if err != nil {
  125. return err
  126. }
  127. a.x509CAService = srv
  128. return nil
  129. }
  130. }
  131. // WithSSHUserSigner defines the signer used to sign SSH user certificates.
  132. func WithSSHUserSigner(s crypto.Signer) Option {
  133. return func(a *Authority) error {
  134. signer, err := ssh.NewSignerFromSigner(s)
  135. if err != nil {
  136. return errors.Wrap(err, "error creating ssh user signer")
  137. }
  138. a.sshCAUserCertSignKey = signer
  139. // Append public key to list of user certs
  140. pub := signer.PublicKey()
  141. a.sshCAUserCerts = append(a.sshCAUserCerts, pub)
  142. a.sshCAUserFederatedCerts = append(a.sshCAUserFederatedCerts, pub)
  143. return nil
  144. }
  145. }
  146. // WithSSHHostSigner defines the signer used to sign SSH host certificates.
  147. func WithSSHHostSigner(s crypto.Signer) Option {
  148. return func(a *Authority) error {
  149. signer, err := ssh.NewSignerFromSigner(s)
  150. if err != nil {
  151. return errors.Wrap(err, "error creating ssh host signer")
  152. }
  153. a.sshCAHostCertSignKey = signer
  154. // Append public key to list of host certs
  155. pub := signer.PublicKey()
  156. a.sshCAHostCerts = append(a.sshCAHostCerts, pub)
  157. a.sshCAHostFederatedCerts = append(a.sshCAHostFederatedCerts, pub)
  158. return nil
  159. }
  160. }
  161. // WithX509RootCerts is an option that allows to define the list of root
  162. // certificates to use. This option will replace any root certificate defined
  163. // before.
  164. func WithX509RootCerts(rootCerts ...*x509.Certificate) Option {
  165. return func(a *Authority) error {
  166. a.rootX509Certs = rootCerts
  167. return nil
  168. }
  169. }
  170. // WithX509FederatedCerts is an option that allows to define the list of
  171. // federated certificates. This option will replace any federated certificate
  172. // defined before.
  173. func WithX509FederatedCerts(certs ...*x509.Certificate) Option {
  174. return func(a *Authority) error {
  175. a.federatedX509Certs = certs
  176. return nil
  177. }
  178. }
  179. // WithX509RootBundle is an option that allows to define the list of root
  180. // certificates. This option will replace any root certificate defined before.
  181. func WithX509RootBundle(pemCerts []byte) Option {
  182. return func(a *Authority) error {
  183. certs, err := readCertificateBundle(pemCerts)
  184. if err != nil {
  185. return err
  186. }
  187. a.rootX509Certs = certs
  188. return nil
  189. }
  190. }
  191. // WithX509FederatedBundle is an option that allows to define the list of
  192. // federated certificates. This option will replace any federated certificate
  193. // defined before.
  194. func WithX509FederatedBundle(pemCerts []byte) Option {
  195. return func(a *Authority) error {
  196. certs, err := readCertificateBundle(pemCerts)
  197. if err != nil {
  198. return err
  199. }
  200. a.federatedX509Certs = certs
  201. return nil
  202. }
  203. }
  204. // WithAdminDB is an option to set the database backing the admin APIs.
  205. func WithAdminDB(d admin.DB) Option {
  206. return func(a *Authority) error {
  207. a.adminDB = d
  208. return nil
  209. }
  210. }
  211. // WithLinkedCAToken is an option to set the authentication token used to enable
  212. // linked ca.
  213. func WithLinkedCAToken(token string) Option {
  214. return func(a *Authority) error {
  215. a.linkedCAToken = token
  216. return nil
  217. }
  218. }
  219. func readCertificateBundle(pemCerts []byte) ([]*x509.Certificate, error) {
  220. var block *pem.Block
  221. var certs []*x509.Certificate
  222. for len(pemCerts) > 0 {
  223. block, pemCerts = pem.Decode(pemCerts)
  224. if block == nil {
  225. break
  226. }
  227. if block.Type != "CERTIFICATE" || len(block.Headers) != 0 {
  228. continue
  229. }
  230. cert, err := x509.ParseCertificate(block.Bytes)
  231. if err != nil {
  232. return nil, err
  233. }
  234. certs = append(certs, cert)
  235. }
  236. return certs, nil
  237. }