bootstrap.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package ca
  2. import (
  3. "context"
  4. "net/http"
  5. "strings"
  6. "github.com/pkg/errors"
  7. "github.com/smallstep/cli/jose"
  8. "gopkg.in/square/go-jose.v2/jwt"
  9. )
  10. type tokenClaims struct {
  11. SHA string `json:"sha"`
  12. jose.Claims
  13. }
  14. // Bootstrap is a helper function that initializes a client with the
  15. // configuration in the bootstrap token.
  16. func Bootstrap(token string) (*Client, error) {
  17. tok, err := jwt.ParseSigned(token)
  18. if err != nil {
  19. return nil, errors.Wrap(err, "error parsing token")
  20. }
  21. var claims tokenClaims
  22. if err := tok.UnsafeClaimsWithoutVerification(&claims); err != nil {
  23. return nil, errors.Wrap(err, "error parsing token")
  24. }
  25. // Validate bootstrap token
  26. switch {
  27. case len(claims.SHA) == 0:
  28. return nil, errors.New("invalid bootstrap token: sha claim is not present")
  29. case !strings.HasPrefix(strings.ToLower(claims.Audience[0]), "http"):
  30. return nil, errors.New("invalid bootstrap token: aud claim is not a url")
  31. }
  32. return NewClient(claims.Audience[0], WithRootSHA256(claims.SHA))
  33. }
  34. // BootstrapServer is a helper function that using the given token returns the
  35. // given http.Server configured with a TLS certificate signed by the Certificate
  36. // Authority. By default the server will kick off a routine that will renew the
  37. // certificate after 2/3rd of the certificate's lifetime has expired.
  38. //
  39. // Usage:
  40. // // Default example with certificate rotation.
  41. // srv, err := ca.BootstrapServer(context.Background(), token, &http.Server{
  42. // Addr: ":443",
  43. // Handler: handler,
  44. // })
  45. //
  46. // // Example canceling automatic certificate rotation.
  47. // ctx, cancel := context.WithCancel(context.Background())
  48. // defer cancel()
  49. // srv, err := ca.BootstrapServer(ctx, token, &http.Server{
  50. // Addr: ":443",
  51. // Handler: handler,
  52. // })
  53. // if err != nil {
  54. // return err
  55. // }
  56. // srv.ListenAndServeTLS("", "")
  57. func BootstrapServer(ctx context.Context, token string, base *http.Server) (*http.Server, error) {
  58. if base.TLSConfig != nil {
  59. return nil, errors.New("server TLSConfig is already set")
  60. }
  61. client, err := Bootstrap(token)
  62. if err != nil {
  63. return nil, err
  64. }
  65. req, pk, err := CreateSignRequest(token)
  66. if err != nil {
  67. return nil, err
  68. }
  69. sign, err := client.Sign(req)
  70. if err != nil {
  71. return nil, err
  72. }
  73. tlsConfig, err := client.GetServerTLSConfig(ctx, sign, pk)
  74. if err != nil {
  75. return nil, err
  76. }
  77. base.TLSConfig = tlsConfig
  78. return base, nil
  79. }
  80. // BootstrapServerWithMTLS is a helper function that using the given token
  81. // returns the given http.Server configured with a TLS certificate signed by the
  82. // Certificate Authority, this server will always require and verify a client
  83. // certificate. By default the server will kick off a routine that will renew
  84. // the certificate after 2/3rd of the certificate's lifetime has expired.
  85. //
  86. // Usage:
  87. // // Default example with certificate rotation.
  88. // srv, err := ca.BootstrapServerWithMTLS(context.Background(), token, &http.Server{
  89. // Addr: ":443",
  90. // Handler: handler,
  91. // })
  92. //
  93. // // Example canceling automatic certificate rotation.
  94. // ctx, cancel := context.WithCancel(context.Background())
  95. // defer cancel()
  96. // srv, err := ca.BootstrapServerWithMTLS(ctx, token, &http.Server{
  97. // Addr: ":443",
  98. // Handler: handler,
  99. // })
  100. // if err != nil {
  101. // return err
  102. // }
  103. // srv.ListenAndServeTLS("", "")
  104. func BootstrapServerWithMTLS(ctx context.Context, token string, base *http.Server) (*http.Server, error) {
  105. if base.TLSConfig != nil {
  106. return nil, errors.New("server TLSConfig is already set")
  107. }
  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. tlsConfig, err := client.GetServerMutualTLSConfig(ctx, sign, pk)
  121. if err != nil {
  122. return nil, err
  123. }
  124. base.TLSConfig = tlsConfig
  125. return base, nil
  126. }
  127. // BootstrapClient is a helper function that using the given bootstrap token
  128. // return an http.Client configured with a Transport prepared to do TLS
  129. // connections using the client certificate returned by the certificate
  130. // authority. By default the server will kick off a routine that will renew the
  131. // certificate after 2/3rd of the certificate's lifetime has expired.
  132. //
  133. // Usage:
  134. // // Default example with certificate rotation.
  135. // client, err := ca.BootstrapClient(ctx.Background(), token)
  136. //
  137. // // Example canceling automatic certificate rotation.
  138. // ctx, cancel := context.WithCancel(context.Background())
  139. // defer cancel()
  140. // client, err := ca.BootstrapClient(ctx, token)
  141. // if err != nil {
  142. // return err
  143. // }
  144. // resp, err := client.Get("https://internal.smallstep.com")
  145. func BootstrapClient(ctx context.Context, token string) (*http.Client, error) {
  146. client, err := Bootstrap(token)
  147. if err != nil {
  148. return nil, err
  149. }
  150. req, pk, err := CreateSignRequest(token)
  151. if err != nil {
  152. return nil, err
  153. }
  154. sign, err := client.Sign(req)
  155. if err != nil {
  156. return nil, err
  157. }
  158. transport, err := client.Transport(ctx, sign, pk)
  159. if err != nil {
  160. return nil, err
  161. }
  162. return &http.Client{
  163. Transport: transport,
  164. }, nil
  165. }