bootstrap.go 4.2 KB

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