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 len(claims.SHA) == 0:
  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. // BootstrapServer is a helper function that using the given token returns the
  36. // given http.Server configured with a TLS certificate signed by the Certificate
  37. // Authority. By default the server will kick off a routine that will renew the
  38. // certificate after 2/3rd of the certificate's lifetime has expired.
  39. //
  40. // Without any extra option the server will be configured for mTLS, it will
  41. // require and verify clients certificates, but options can be used to drop this
  42. // requirement, the most common will be only verify the certs if given with
  43. // ca.VerifyClientCertIfGiven(), or add extra CAs with
  44. // ca.AddClientCA(*x509.Certificate).
  45. //
  46. // Usage:
  47. // // Default example with certificate rotation.
  48. // srv, err := ca.BootstrapServer(context.Background(), token, &http.Server{
  49. // Addr: ":443",
  50. // Handler: handler,
  51. // })
  52. //
  53. // // Example canceling automatic certificate rotation.
  54. // ctx, cancel := context.WithCancel(context.Background())
  55. // defer cancel()
  56. // srv, err := ca.BootstrapServer(ctx, token, &http.Server{
  57. // Addr: ":443",
  58. // Handler: handler,
  59. // })
  60. // if err != nil {
  61. // return err
  62. // }
  63. // srv.ListenAndServeTLS("", "")
  64. func BootstrapServer(ctx context.Context, token string, base *http.Server, options ...TLSOption) (*http.Server, error) {
  65. if base.TLSConfig != nil {
  66. return nil, errors.New("server TLSConfig is already set")
  67. }
  68. client, err := Bootstrap(token)
  69. if err != nil {
  70. return nil, err
  71. }
  72. req, pk, err := CreateSignRequest(token)
  73. if err != nil {
  74. return nil, err
  75. }
  76. sign, err := client.Sign(req)
  77. if err != nil {
  78. return nil, err
  79. }
  80. // Make sure the tlsConfig have all supported roots on ClientCAs and RootCAs
  81. options = append(options, AddRootsToCAs())
  82. tlsConfig, err := client.GetServerTLSConfig(ctx, sign, pk, options...)
  83. if err != nil {
  84. return nil, err
  85. }
  86. base.TLSConfig = tlsConfig
  87. return base, nil
  88. }
  89. // BootstrapClient is a helper function that using the given bootstrap token
  90. // return an http.Client configured with a Transport prepared to do TLS
  91. // connections using the client certificate returned by the certificate
  92. // authority. By default the server will kick off a routine that will renew the
  93. // certificate after 2/3rd of the certificate's lifetime has expired.
  94. //
  95. // Usage:
  96. // // Default example with certificate rotation.
  97. // client, err := ca.BootstrapClient(ctx.Background(), token)
  98. //
  99. // // Example canceling automatic certificate rotation.
  100. // ctx, cancel := context.WithCancel(context.Background())
  101. // defer cancel()
  102. // client, err := ca.BootstrapClient(ctx, token)
  103. // if err != nil {
  104. // return err
  105. // }
  106. // resp, err := client.Get("https://internal.smallstep.com")
  107. func BootstrapClient(ctx context.Context, token string, options ...TLSOption) (*http.Client, error) {
  108. client, err := Bootstrap(token)
  109. if err != nil {
  110. return nil, err
  111. }
  112. req, pk, err := CreateSignRequest(token)
  113. if err != nil {
  114. return nil, err
  115. }
  116. sign, err := client.Sign(req)
  117. if err != nil {
  118. return nil, err
  119. }
  120. // Make sure the tlsConfig have all supported roots on RootCAs
  121. options = append(options, AddRootsToRootCAs())
  122. transport, err := client.Transport(ctx, sign, pk, options...)
  123. if err != nil {
  124. return nil, err
  125. }
  126. return &http.Client{
  127. Transport: transport,
  128. }, 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. }