client.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. // LoadClient 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. GetClientCertificate: identity.GetClientCertificateFunc(),
  57. }
  58. // RootCAs
  59. b, err = ioutil.ReadFile(defaults.Root)
  60. if err != nil {
  61. return nil, errors.Wrapf(err, "error loading %s", defaults.Root)
  62. }
  63. pool := x509.NewCertPool()
  64. if pool.AppendCertsFromPEM(b) {
  65. tr.TLSClientConfig.RootCAs = pool
  66. }
  67. return &Client{
  68. CaURL: caURL,
  69. Client: &http.Client{
  70. Transport: tr,
  71. },
  72. }, nil
  73. }
  74. type defaultsConfig struct {
  75. CaURL string `json:"ca-url"`
  76. Root string `json:"root"`
  77. }
  78. func (c *defaultsConfig) Validate() error {
  79. switch {
  80. case c.CaURL == "":
  81. return fmt.Errorf("missing or invalid `ca-url` property")
  82. case c.Root == "":
  83. return fmt.Errorf("missing or invalid `root` property")
  84. default:
  85. return nil
  86. }
  87. }