4
0

bootstrap.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 ott")
  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. The
  36. // certificate will automatically rotate if necessary.
  37. //
  38. // Usage:
  39. // srv, err := ca.BootstrapServer(":443", token, handler)
  40. // if err != nil {
  41. // return err
  42. // }
  43. // srv.ListenAndServeTLS("", "")
  44. func BootstrapServer(addr, token string, handler http.Handler) (*http.Server, error) {
  45. client, err := Bootstrap(token)
  46. if err != nil {
  47. return nil, err
  48. }
  49. req, pk, err := CreateSignRequest(token)
  50. if err != nil {
  51. return nil, err
  52. }
  53. sign, err := client.Sign(req)
  54. if err != nil {
  55. return nil, err
  56. }
  57. tlsConfig, err := client.GetServerTLSConfig(context.Background(), sign, pk)
  58. if err != nil {
  59. return nil, err
  60. }
  61. return &http.Server{
  62. Addr: addr,
  63. Handler: handler,
  64. TLSConfig: tlsConfig,
  65. }, nil
  66. }
  67. // BootstrapClient is a helper function that using the given bootstrap token
  68. // return an http.Client configured with a Transport prepared to do TLS
  69. // connections using the client certificate returned by the certificate
  70. // authority. The certificate will automatically rotate if necessary.
  71. //
  72. // Usage:
  73. // client, err := ca.BootstrapClient(token)
  74. // if err != nil {
  75. // return err
  76. // }
  77. // resp, err := client.Get("https://internal.smallstep.com")
  78. func BootstrapClient(token string) (*http.Client, error) {
  79. client, err := Bootstrap(token)
  80. if err != nil {
  81. return nil, err
  82. }
  83. req, pk, err := CreateSignRequest(token)
  84. if err != nil {
  85. return nil, err
  86. }
  87. sign, err := client.Sign(req)
  88. if err != nil {
  89. return nil, err
  90. }
  91. transport, err := client.Transport(context.Background(), sign, pk)
  92. if err != nil {
  93. return nil, err
  94. }
  95. return &http.Client{
  96. Transport: transport,
  97. }, nil
  98. }