Skip to content

Commit ae2db6b

Browse files
qmuntalbgavrilMS
authored andcommitted
always close IMDS response body
1 parent 37c70e2 commit ae2db6b

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

apps/internal/oauth/ops/authority/authority.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -543,17 +543,19 @@ func detectRegion(ctx context.Context) string {
543543
client := http.Client{
544544
Timeout: time.Duration(2 * time.Second),
545545
}
546-
req, _ := http.NewRequest("GET", imdsEndpoint, nil)
546+
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, imdsEndpoint, nil)
547547
req.Header.Set("Metadata", "true")
548548
resp, err := client.Do(req)
549+
if err == nil {
550+
defer resp.Body.Close()
551+
}
549552
// If the request times out or there is an error, it is retried once
550-
if err != nil || resp.StatusCode != 200 {
553+
if err != nil || resp.StatusCode != http.StatusOK {
551554
resp, err = client.Do(req)
552-
if err != nil || resp.StatusCode != 200 {
555+
if err != nil || resp.StatusCode != http.StatusOK {
553556
return ""
554557
}
555558
}
556-
defer resp.Body.Close()
557559
response, err := io.ReadAll(resp.Body)
558560
if err != nil {
559561
return ""

0 commit comments

Comments
 (0)