Skip to content

Commit fcb4cc2

Browse files
authored
Merge pull request #301 from SumoLogic/dgould-SUMO-172016
2 parents 35a4b47 + 05db6f7 commit fcb4cc2

File tree

2 files changed

+35
-19
lines changed

2 files changed

+35
-19
lines changed

sumologic/provider.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,16 @@ func Provider() terraform.ResourceProvider {
110110
}
111111
}
112112

113-
func resolveRedirectURL(accessId string, accessKey string) (string, error) {
113+
func resolveRedirectURL(accessId string, accessKey string, authJwt string) (string, error) {
114114
req, err := http.NewRequest(http.MethodHead, "https://api.sumologic.com/api/v1/collectors", nil)
115115
if err != nil {
116116
return "", err
117117
}
118-
req.SetBasicAuth(accessId, accessKey)
118+
if authJwt == "" {
119+
req.SetBasicAuth(accessId, accessKey)
120+
} else {
121+
req.Header.Add("Authorization", "Bearer "+authJwt)
122+
}
119123
client := &http.Client{CheckRedirect: func(req *http.Request, via []*http.Request) error {
120124
return http.ErrUseLastResponse
121125
}}
@@ -135,22 +139,27 @@ func resolveRedirectURL(accessId string, accessKey string) (string, error) {
135139
func providerConfigure(d *schema.ResourceData) (interface{}, error) {
136140
accessId := d.Get("access_id").(string)
137141
accessKey := d.Get("access_key").(string)
142+
authJwt := os.Getenv("SUMOLOGIC_AUTHJWT")
138143
environment := d.Get("environment").(string)
139144
baseUrl := d.Get("base_url").(string)
140145
isInAdminMode := d.Get("admin_mode").(bool)
141146

142147
msg := ""
143-
if accessId == "" {
144-
msg = "sumologic provider: access_id should be set;"
145-
}
146-
147-
if accessKey == "" {
148-
msg = fmt.Sprintf("%s access_key should be set; ", msg)
148+
if authJwt == "" {
149+
if accessId == "" || accessKey == "" {
150+
msg = "sumologic provider: "
151+
}
152+
if accessId == "" {
153+
msg = fmt.Sprintf("%s access_id should be set;", msg)
154+
}
155+
if accessKey == "" {
156+
msg = fmt.Sprintf("%s access_key should be set; ", msg)
157+
}
149158
}
150159

151160
if environment == "" && baseUrl == "" {
152161
log.Printf("Attempting to resolve redirection URL from access key/id")
153-
url, err := resolveRedirectURL(accessId, accessKey)
162+
url, err := resolveRedirectURL(accessId, accessKey, authJwt)
154163
if err != nil {
155164
log.Printf("[WARN] Unable to resolve redirection URL, %s", err)
156165
environment = "us2"
@@ -170,6 +179,7 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
170179
return NewClient(
171180
accessId,
172181
accessKey,
182+
authJwt,
173183
environment,
174184
baseUrl,
175185
isInAdminMode,

sumologic/sumologic_client.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type HttpClient interface {
1919
type Client struct {
2020
AccessID string
2121
AccessKey string
22+
AuthJwt string
2223
Environment string
2324
BaseURL *url.URL
2425
IsInAdminMode bool
@@ -42,14 +43,18 @@ var endpoints = map[string]string{
4243

4344
var rateLimiter = time.NewTicker(time.Minute / 240)
4445

45-
func createNewRequest(method, url string, body io.Reader, accessID string, accessKey string) (*http.Request, error) {
46+
func createNewRequest(method, url string, body io.Reader, accessID string, accessKey string, authJwt string) (*http.Request, error) {
4647
req, err := http.NewRequest(method, url, body)
4748
if err != nil {
4849
return nil, err
4950
}
5051
req.Header.Add("Content-Type", "application/json")
5152
req.Header.Add("User-Agent", "SumoLogicTerraformProvider/"+ProviderVersion)
52-
req.SetBasicAuth(accessID, accessKey)
53+
if authJwt == "" {
54+
req.SetBasicAuth(accessID, accessKey)
55+
} else {
56+
req.Header.Add("Authorization", "Bearer "+authJwt)
57+
}
5358
return req, nil
5459
}
5560

@@ -66,7 +71,7 @@ func (s *Client) PostWithCookies(urlPath string, payload interface{}) ([]byte, [
6671
return nil, nil, err
6772
}
6873

69-
req, err := createNewRequest(http.MethodPost, sumoURL.String(), bytes.NewBuffer(body), s.AccessID, s.AccessKey)
74+
req, err := createNewRequest(http.MethodPost, sumoURL.String(), bytes.NewBuffer(body), s.AccessID, s.AccessKey, s.AuthJwt)
7075
if err != nil {
7176
return nil, nil, err
7277
}
@@ -100,7 +105,7 @@ func (s *Client) GetWithCookies(urlPath string, cookies []*http.Cookie) ([]byte,
100105

101106
sumoURL := s.BaseURL.ResolveReference(relativeURL)
102107

103-
req, err := createNewRequest(http.MethodGet, sumoURL.String(), nil, s.AccessID, s.AccessKey)
108+
req, err := createNewRequest(http.MethodGet, sumoURL.String(), nil, s.AccessID, s.AccessKey, s.AuthJwt)
104109
if err != nil {
105110
return nil, "", err
106111
}
@@ -135,7 +140,7 @@ func (s *Client) Post(urlPath string, payload interface{}) ([]byte, error) {
135140
sumoURL := s.BaseURL.ResolveReference(relativeURL)
136141

137142
body, _ := json.Marshal(payload)
138-
req, err := createNewRequest(http.MethodPost, sumoURL.String(), bytes.NewBuffer(body), s.AccessID, s.AccessKey)
143+
req, err := createNewRequest(http.MethodPost, sumoURL.String(), bytes.NewBuffer(body), s.AccessID, s.AccessKey, s.AuthJwt)
139144
if err != nil {
140145
return nil, err
141146
}
@@ -166,7 +171,7 @@ func (s *Client) Post(urlPath string, payload interface{}) ([]byte, error) {
166171
func (s *Client) PostRawPayload(urlPath string, payload string) ([]byte, error) {
167172
relativeURL, _ := url.Parse(urlPath)
168173
sumoURL := s.BaseURL.ResolveReference(relativeURL)
169-
req, err := createNewRequest(http.MethodPost, sumoURL.String(), bytes.NewBuffer([]byte(payload)), s.AccessID, s.AccessKey)
174+
req, err := createNewRequest(http.MethodPost, sumoURL.String(), bytes.NewBuffer([]byte(payload)), s.AccessID, s.AccessKey, s.AuthJwt)
170175
if err != nil {
171176
return nil, err
172177
}
@@ -194,7 +199,7 @@ func (s *Client) Put(urlPath string, payload interface{}) ([]byte, error) {
194199
_, etag, _ := s.Get(sumoURL.String())
195200

196201
body, _ := json.Marshal(payload)
197-
req, err := createNewRequest(http.MethodPut, sumoURL.String(), bytes.NewBuffer(body), s.AccessID, s.AccessKey)
202+
req, err := createNewRequest(http.MethodPut, sumoURL.String(), bytes.NewBuffer(body), s.AccessID, s.AccessKey, s.AuthJwt)
198203
if err != nil {
199204
return nil, err
200205
}
@@ -227,7 +232,7 @@ func (s *Client) Get(urlPath string) ([]byte, string, error) {
227232
relativeURL, _ := url.Parse(urlPath)
228233
sumoURL := s.BaseURL.ResolveReference(relativeURL)
229234

230-
req, err := createNewRequest(http.MethodGet, sumoURL.String(), nil, s.AccessID, s.AccessKey)
235+
req, err := createNewRequest(http.MethodGet, sumoURL.String(), nil, s.AccessID, s.AccessKey, s.AuthJwt)
231236
if err != nil {
232237
return nil, "", err
233238
}
@@ -261,7 +266,7 @@ func (s *Client) Delete(urlPath string) ([]byte, error) {
261266
relativeURL, _ := url.Parse(urlPath)
262267
sumoURL := s.BaseURL.ResolveReference(relativeURL)
263268

264-
req, err := createNewRequest(http.MethodDelete, sumoURL.String(), nil, s.AccessID, s.AccessKey)
269+
req, err := createNewRequest(http.MethodDelete, sumoURL.String(), nil, s.AccessID, s.AccessKey, s.AuthJwt)
265270
if err != nil {
266271
return nil, err
267272
}
@@ -289,10 +294,11 @@ func (s *Client) Delete(urlPath string) ([]byte, error) {
289294
return d, nil
290295
}
291296

292-
func NewClient(accessID, accessKey, environment, base_url string, admin bool) (*Client, error) {
297+
func NewClient(accessID, accessKey, authJwt, environment, base_url string, admin bool) (*Client, error) {
293298
client := Client{
294299
AccessID: accessID,
295300
AccessKey: accessKey,
301+
AuthJwt: authJwt,
296302
httpClient: http.DefaultClient,
297303
Environment: environment,
298304
IsInAdminMode: admin,

0 commit comments

Comments
 (0)