Skip to content

Commit 95fa852

Browse files
committed
Merge pull request #52 from hashicorp/phinze/go-rootcerts
Use go-rootcerts to configure TLS
2 parents 0008886 + def1343 commit 95fa852

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

v1/client.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package atlas
22

33
import (
44
"bytes"
5+
"crypto/tls"
56
"encoding/json"
67
"fmt"
78
"io"
@@ -14,6 +15,7 @@ import (
1415
"strings"
1516

1617
"github.com/hashicorp/go-cleanhttp"
18+
"github.com/hashicorp/go-rootcerts"
1719
)
1820

1921
const (
@@ -24,6 +26,14 @@ const (
2426
// default Atlas address.
2527
atlasEndpointEnvVar = "ATLAS_ADDRESS"
2628

29+
// atlasCAFileEnvVar is the environment variable that causes the client to
30+
// load trusted certs from a file
31+
atlasCAFileEnvVar = "ATLAS_CAFILE"
32+
33+
// atlasCAPathEnvVar is the environment variable that causes the client to
34+
// load trusted certs from a directory
35+
atlasCAPathEnvVar = "ATLAS_CAPATH"
36+
2737
// atlasTokenHeader is the header key used for authenticating with Atlas
2838
atlasTokenHeader = "X-Atlas-Token"
2939
)
@@ -112,6 +122,17 @@ func NewClient(urlString string) (*Client, error) {
112122
// init() sets defaults on the client.
113123
func (c *Client) init() error {
114124
c.HTTPClient = cleanhttp.DefaultClient()
125+
tlsConfig := &tls.Config{}
126+
err := rootcerts.ConfigureTLS(tlsConfig, &rootcerts.Config{
127+
CAFile: os.Getenv(atlasCAFileEnvVar),
128+
CAPath: os.Getenv(atlasCAPathEnvVar),
129+
})
130+
if err != nil {
131+
return err
132+
}
133+
t := cleanhttp.DefaultTransport()
134+
t.TLSClientConfig = tlsConfig
135+
c.HTTPClient.Transport = t
115136
return nil
116137
}
117138

0 commit comments

Comments
 (0)