client.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package identity
  2. import (
  3. "crypto/tls"
  4. "crypto/x509"
  5. "encoding/json"
  6. "fmt"
  7. "io/ioutil"
  8. "net/http"
  9. "net/url"
  10. "github.com/pkg/errors"
  11. )
  12. // Client wraps http.Client with a transport using the step root and identity.
  13. type Client struct {
  14. CaURL *url.URL
  15. *http.Client
  16. }
  17. // ResolveReference resolves the given reference from the CaURL.
  18. func (c *Client) ResolveReference(ref *url.URL) *url.URL {
  19. return c.CaURL.ResolveReference(ref)
  20. }
  21. // LoadStepClient configures an http.Client with the root in
  22. // $STEPPATH/config/defaults.json and the identity defined in
  23. // $STEPPATH/config/identity.json
  24. func LoadClient() (*Client, error) {
  25. b, err := ioutil.ReadFile(DefaultsFile)
  26. if err != nil {
  27. return nil, errors.Wrapf(err, "error reading %s", DefaultsFile)
  28. }
  29. var defaults defaultsConfig
  30. if err := json.Unmarshal(b, &defaults); err != nil {
  31. return nil, errors.Wrapf(err, "error unmarshaling %s", DefaultsFile)
  32. }
  33. if err := defaults.Validate(); err != nil {
  34. return nil, errors.Wrapf(err, "error validating %s", DefaultsFile)
  35. }
  36. caURL, err := url.Parse(defaults.CaURL)
  37. if err != nil {
  38. return nil, errors.Wrapf(err, "error validating %s", DefaultsFile)
  39. }
  40. if caURL.Scheme == "" {
  41. caURL.Scheme = "https"
  42. }
  43. identity, err := LoadDefaultIdentity()
  44. if err != nil {
  45. return nil, err
  46. }
  47. if err := identity.Validate(); err != nil {
  48. return nil, errors.Wrapf(err, "error validating %s", IdentityFile)
  49. }
  50. if kind := identity.Kind(); kind != MutualTLS {
  51. return nil, errors.Errorf("unsupported identity %s: only mTLS is currently supported", kind)
  52. }
  53. // Prepare transport with information in defaults.json and identity.json
  54. tr := http.DefaultTransport.(*http.Transport).Clone()
  55. tr.TLSClientConfig = &tls.Config{}
  56. // RootCAs
  57. b, err = ioutil.ReadFile(defaults.Root)
  58. if err != nil {
  59. return nil, errors.Wrapf(err, "error loading %s", defaults.Root)
  60. }
  61. pool := x509.NewCertPool()
  62. if pool.AppendCertsFromPEM(b) {
  63. tr.TLSClientConfig.RootCAs = pool
  64. }
  65. // Certificate
  66. crt, err := tls.LoadX509KeyPair(identity.Certificate, identity.Key)
  67. if err != nil {
  68. return nil, fmt.Errorf("error loading certificate: %v", err)
  69. }
  70. tr.TLSClientConfig.Certificates = []tls.Certificate{crt}
  71. return &Client{
  72. CaURL: caURL,
  73. Client: &http.Client{
  74. Transport: tr,
  75. },
  76. }, nil
  77. }
  78. type defaultsConfig struct {
  79. CaURL string `json:"ca-url"`
  80. Root string `json:"root"`
  81. }
  82. func (c *defaultsConfig) Validate() error {
  83. switch {
  84. case c.CaURL == "":
  85. return fmt.Errorf("missing or invalid `ca-url` property")
  86. case c.Root == "":
  87. return fmt.Errorf("missing or invalid `root` property")
  88. default:
  89. return nil
  90. }
  91. }