4
0

bootstrap.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. req, pk, err := CreateSignRequest(token)
  59. if err != nil {
  60. return nil, err
  61. }
  62. sign, err := client.Sign(req)
  63. if err != nil {
  64. return nil, err
  65. }
  66. // Make sure the tlsConfig have all supported roots on RootCAs
  67. options = append(options, AddRootsToRootCAs())
  68. transport, err := client.Transport(ctx, sign, pk, 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. client, err := Bootstrap(token)
  110. if err != nil {
  111. return nil, err
  112. }
  113. req, pk, err := CreateSignRequest(token)
  114. if err != nil {
  115. return nil, err
  116. }
  117. sign, err := client.Sign(req)
  118. if err != nil {
  119. return nil, err
  120. }
  121. // Make sure the tlsConfig have all supported roots on ClientCAs and RootCAs
  122. options = append(options, AddRootsToCAs())
  123. tlsConfig, err := client.GetServerTLSConfig(ctx, sign, pk, options...)
  124. if err != nil {
  125. return nil, err
  126. }
  127. base.TLSConfig = tlsConfig
  128. return base, nil
  129. }
  130. // BootstrapListener is a helper function that using the given token returns a
  131. // TLS listener which accepts connections from an inner listener and wraps each
  132. // connection with Server.
  133. //
  134. // Without any extra option the server will be configured for mTLS, it will
  135. // require and verify clients certificates, but options can be used to drop this
  136. // requirement, the most common will be only verify the certs if given with
  137. // ca.VerifyClientCertIfGiven(), or add extra CAs with
  138. // ca.AddClientCA(*x509.Certificate).
  139. //
  140. // Usage:
  141. // inner, err := net.Listen("tcp", ":443")
  142. // if err != nil {
  143. // return nil
  144. // }
  145. // ctx, cancel := context.WithCancel(context.Background())
  146. // defer cancel()
  147. // lis, err := ca.BootstrapListener(ctx, token, inner)
  148. // if err != nil {
  149. // return err
  150. // }
  151. // srv := grpc.NewServer()
  152. // ... // register services
  153. // srv.Serve(lis)
  154. func BootstrapListener(ctx context.Context, token string, inner net.Listener, options ...TLSOption) (net.Listener, error) {
  155. client, err := Bootstrap(token)
  156. if err != nil {
  157. return nil, err
  158. }
  159. req, pk, err := CreateSignRequest(token)
  160. if err != nil {
  161. return nil, err
  162. }
  163. sign, err := client.Sign(req)
  164. if err != nil {
  165. return nil, err
  166. }
  167. // Make sure the tlsConfig have all supported roots on ClientCAs and RootCAs
  168. options = append(options, AddRootsToCAs())
  169. tlsConfig, err := client.GetServerTLSConfig(ctx, sign, pk, options...)
  170. if err != nil {
  171. return nil, err
  172. }
  173. return tls.NewListener(inner, tlsConfig), nil
  174. }