4
0

bootstrap.go 5.7 KB

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