Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: close response Body when the returned error is nil (#146) #152

Merged
merged 1 commit into from
Jun 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions internal/pkg/vault/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,15 +370,14 @@ func (c *Client) getAllKeys(subPath string) (map[string]string, error) {
if err != nil {
return nil, err
}
defer func() {
_ = resp.Body.Close()
}()

if resp.StatusCode < 200 || resp.StatusCode > 299 {
return nil, pkg.NewErrSecretStore(fmt.Sprintf("Received a '%d' response from the secret store", resp.StatusCode))
}

defer func() {
_ = resp.Body.Close()
}()

var result map[string]interface{}
err = json.NewDecoder(resp.Body).Decode(&result)
if err != nil {
Expand Down Expand Up @@ -440,9 +439,7 @@ func (c *Client) store(subPath string, secrets map[string]string) error {
return err
}
defer func() {
if resp.Body != nil {
_ = resp.Body.Close()
}
_ = resp.Body.Close()
}()

if resp.StatusCode < 200 || resp.StatusCode > 299 {
Expand Down
5 changes: 5 additions & 0 deletions internal/pkg/vault/secrets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,7 @@ func (emc *ErrorMockCaller) Do(_ *http.Request) (*http.Response, error) {
}

return &http.Response{
Body: ioutil.NopCloser(bytes.NewBufferString("")),
StatusCode: emc.StatusCode,
}, nil
}
Expand All @@ -819,6 +820,7 @@ func (caller *InMemoryMockCaller) Do(req *http.Request) (*http.Response, error)
if caller.nErrorsReturned != caller.NErrorsBeforeSuccess {
caller.nErrorsReturned++
return &http.Response{
Body: ioutil.NopCloser(bytes.NewBufferString("")),
StatusCode: 404,
}, nil
}
Expand All @@ -831,6 +833,7 @@ func (caller *InMemoryMockCaller) Do(req *http.Request) (*http.Response, error)
case http.MethodGet:
if req.URL.Path != testPath {
return &http.Response{
Body: ioutil.NopCloser(bytes.NewBufferString("")),
StatusCode: 404,
}, nil
}
Expand All @@ -843,13 +846,15 @@ func (caller *InMemoryMockCaller) Do(req *http.Request) (*http.Response, error)
case http.MethodPost:
if req.URL.Path != testPath {
return &http.Response{
Body: ioutil.NopCloser(bytes.NewBufferString("")),
StatusCode: 404,
}, nil
}
var result map[string]string
_ = json.NewDecoder(req.Body).Decode(&result)
caller.Result = result
return &http.Response{
Body: ioutil.NopCloser(bytes.NewBufferString("")),
StatusCode: 200,
}, nil
default:
Expand Down