4
0

bootstrap.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. package ca
  2. import (
  3. "context"
  4. "crypto"
  5. "crypto/tls"
  6. "net"
  7. "net/http"
  8. "strings"
  9. "github.com/pkg/errors"
  10. "github.com/smallstep/certificates/api"
  11. "go.step.sm/crypto/jose"
  12. )
  13. type tokenClaims struct {
  14. SHA string `json:"sha"`
  15. jose.Claims
  16. }
  17. // Bootstrap is a helper function that initializes a client with the
  18. // configuration in the bootstrap token.
  19. func Bootstrap(token string) (*Client, error) {
  20. tok, err := jose.ParseSigned(token)
  21. if err != nil {
  22. return nil, errors.Wrap(err, "error parsing token")
  23. }
  24. var claims tokenClaims
  25. if err := tok.UnsafeClaimsWithoutVerification(&claims); err != nil {
  26. return nil, errors.Wrap(err, "error parsing token")
  27. }
  28. // Validate bootstrap token
  29. switch {
  30. case claims.SHA == "":
  31. return nil, errors.New("invalid bootstrap token: sha claim is not present")
  32. case !strings.HasPrefix(strings.ToLower(claims.Audience[0]), "http"):
  33. return nil, errors.New("invalid bootstrap token: aud claim is not a url")
  34. }
  35. return NewClient(claims.Audience[0], WithRootSHA256(claims.SHA))
  36. }
  37. // BootstrapClient is a helper function that using the given bootstrap token
  38. // return an http.Client configured with a Transport prepared to do TLS
  39. // connections using the client certificate returned by the certificate
  40. // authority. By default the server will kick off a routine that will renew the
  41. // certificate after 2/3rd of the certificate's lifetime has expired.
  42. //
  43. // Usage:
  44. // // Default example with certificate rotation.
  45. // client, err := ca.BootstrapClient(ctx.Background(), token)
  46. //
  47. // // Example canceling automatic certificate rotation.
  48. // ctx, cancel := context.WithCancel(context.Background())
  49. // defer cancel()
  50. // client, err := ca.BootstrapClient(ctx, token)
  51. // if err != nil {
  52. // return err
  53. // }
  54. // resp, err := client.Get("https://internal.smallstep.com")
  55. func BootstrapClient(ctx context.Context, token string, options ...TLSOption) (*http.Client, error) {
  56. b, err := createBootstrap(token)
  57. if err != nil {
  58. return nil, err
  59. }
  60. // Make sure the tlsConfig has all supported roots on RootCAs.
  61. //
  62. // The roots request is only supported if identity certificates are not
  63. // required. In all cases the current root is also added after applying all
  64. // options too.
  65. if !b.RequireClientAuth {
  66. options = append(options, AddRootsToRootCAs())
  67. }
  68. transport, err := b.Client.Transport(ctx, b.SignResponse, b.PrivateKey, options...)
  69. if err != nil {
  70. return nil, err
  71. }
  72. return &http.Client{
  73. Transport: transport,
  74. }, nil
  75. }
  76. // BootstrapServer is a helper function that using the given token returns the
  77. // given http.Server configured with a TLS certificate signed by the Certificate
  78. // Authority. By default the server will kick off a routine that will renew the
  79. // certificate after 2/3rd of the certificate's lifetime has expired.
  80. //
  81. // Without any extra option the server will be configured for mTLS, it will
  82. // require and verify clients certificates, but options can be used to drop this
  83. // requirement, the most common will be only verify the certs if given with
  84. // ca.VerifyClientCertIfGiven(), or add extra CAs with
  85. // ca.AddClientCA(*x509.Certificate).
  86. //
  87. // Usage:
  88. // // Default example with certificate rotation.
  89. // srv, err := ca.BootstrapServer(context.Background(), token, &http.Server{
  90. // Addr: ":443",
  91. // Handler: handler,
  92. // })
  93. //
  94. // // Example canceling automatic certificate rotation.
  95. // ctx, cancel := context.WithCancel(context.Background())
  96. // defer cancel()
  97. // srv, err := ca.BootstrapServer(ctx, token, &http.Server{
  98. // Addr: ":443",
  99. // Handler: handler,
  100. // })
  101. // if err != nil {
  102. // return err
  103. // }
  104. // srv.ListenAndServeTLS("", "")
  105. func BootstrapServer(ctx context.Context, token string, base *http.Server, options ...TLSOption) (*http.Server, error) {
  106. if base.TLSConfig != nil {
  107. return nil, errors.New("server TLSConfig is already set")
  108. }
  109. b, err := createBootstrap(token)
  110. if err != nil {
  111. return nil, err
  112. }
  113. // Make sure the tlsConfig has all supported roots on RootCAs.
  114. //
  115. // The roots request is only supported if identity certificates are not
  116. // required. In all cases the current root is also added after applying all
  117. // options too.
  118. if !b.RequireClientAuth {
  119. options = append(options, AddRootsToCAs())
  120. }
  121. tlsConfig, err := b.Client.GetServerTLSConfig(ctx, b.SignResponse, b.PrivateKey, options...)
  122. if err != nil {
  123. return nil, err
  124. }
  125. base.TLSConfig = tlsConfig
  126. return base, nil
  127. }
  128. // BootstrapListener is a helper function that using the given token returns a
  129. // TLS listener which accepts connections from an inner listener and wraps each
  130. // connection with Server.
  131. //
  132. // Without any extra option the server will be configured for mTLS, it will
  133. // require and verify clients certificates, but options can be used to drop this
  134. // requirement, the most common will be only verify the certs if given with
  135. // ca.VerifyClientCertIfGiven(), or add extra CAs with
  136. // ca.AddClientCA(*x509.Certificate).
  137. //
  138. // Usage:
  139. // inner, err := net.Listen("tcp", ":443")
  140. // if err != nil {
  141. // return nil
  142. // }
  143. // ctx, cancel := context.WithCancel(context.Background())
  144. // defer cancel()
  145. // lis, err := ca.BootstrapListener(ctx, token, inner)
  146. // if err != nil {
  147. // return err
  148. // }
  149. // srv := grpc.NewServer()
  150. // ... // register services
  151. // srv.Serve(lis)
  152. func BootstrapListener(ctx context.Context, token string, inner net.Listener, options ...TLSOption) (net.Listener, error) {
  153. b, err := createBootstrap(token)
  154. if err != nil {
  155. return nil, err
  156. }
  157. // Make sure the tlsConfig has all supported roots on RootCAs.
  158. //
  159. // The roots request is only supported if identity certificates are not
  160. // required. In all cases the current root is also added after applying all
  161. // options too.
  162. if !b.RequireClientAuth {
  163. options = append(options, AddRootsToCAs())
  164. }
  165. tlsConfig, err := b.Client.GetServerTLSConfig(ctx, b.SignResponse, b.PrivateKey, options...)
  166. if err != nil {
  167. return nil, err
  168. }
  169. return tls.NewListener(inner, tlsConfig), nil
  170. }
  171. type bootstrap struct {
  172. Client *Client
  173. RequireClientAuth bool
  174. SignResponse *api.SignResponse
  175. PrivateKey crypto.PrivateKey
  176. }
  177. func createBootstrap(token string) (*bootstrap, error) {
  178. client, err := Bootstrap(token)
  179. if err != nil {
  180. return nil, err
  181. }
  182. version, err := client.Version()
  183. if err != nil {
  184. return nil, err
  185. }
  186. req, pk, err := CreateSignRequest(token)
  187. if err != nil {
  188. return nil, err
  189. }
  190. sign, err := client.Sign(req)
  191. if err != nil {
  192. return nil, err
  193. }
  194. return &bootstrap{
  195. Client: client,
  196. RequireClientAuth: version.RequireClientAuthentication,
  197. SignResponse: sign,
  198. PrivateKey: pk,
  199. }, nil
  200. }