Skip to content

Commit

Permalink
always close IMDS response body
Browse files Browse the repository at this point in the history
  • Loading branch information
qmuntal authored and bgavrilMS committed May 7, 2024
1 parent 37c70e2 commit ae2db6b
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions apps/internal/oauth/ops/authority/authority.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,17 +543,19 @@ func detectRegion(ctx context.Context) string {
client := http.Client{
Timeout: time.Duration(2 * time.Second),
}
req, _ := http.NewRequest("GET", imdsEndpoint, nil)
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, imdsEndpoint, nil)
req.Header.Set("Metadata", "true")
resp, err := client.Do(req)
if err == nil {
defer resp.Body.Close()
}
// If the request times out or there is an error, it is retried once
if err != nil || resp.StatusCode != 200 {
if err != nil || resp.StatusCode != http.StatusOK {
resp, err = client.Do(req)
if err != nil || resp.StatusCode != 200 {
if err != nil || resp.StatusCode != http.StatusOK {
return ""
}
}
defer resp.Body.Close()
response, err := io.ReadAll(resp.Body)
if err != nil {
return ""
Expand Down

0 comments on commit ae2db6b

Please sign in to comment.