bootstrap.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 returns an http.Server configured
  35. // with the given address and handler, and prepared to use TLS connections.
  36. // 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. // BootstrapClient is a helper function that using the given bootstrap token
  81. // return an http.Client configured with a Transport prepared to do TLS
  82. // connections using the client certificate returned by the certificate
  83. // authority. By default the server will kick off a routine that will renew the
  84. // certificate after 2/3rd of the certificate's lifetime has expired.
  85. //
  86. // Usage:
  87. // // Default example with certificate rotation.
  88. // client, err := ca.BootstrapClient(ctx.Background(), token)
  89. //
  90. // // Example canceling automatic certificate rotation.
  91. // ctx, cancel := context.WithCancel(context.Background())
  92. // defer cancel()
  93. // client, err := ca.BootstrapClient(ctx, token)
  94. // if err != nil {
  95. // return err
  96. // }
  97. // resp, err := client.Get("https://internal.smallstep.com")
  98. func BootstrapClient(ctx context.Context, token string) (*http.Client, error) {
  99. client, err := Bootstrap(token)
  100. if err != nil {
  101. return nil, err
  102. }
  103. req, pk, err := CreateSignRequest(token)
  104. if err != nil {
  105. return nil, err
  106. }
  107. sign, err := client.Sign(req)
  108. if err != nil {
  109. return nil, err
  110. }
  111. transport, err := client.Transport(ctx, sign, pk)
  112. if err != nil {
  113. return nil, err
  114. }
  115. return &http.Client{
  116. Transport: transport,
  117. }, nil
  118. }