bootstrap.go 6.5 KB

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