Skip to content

Commit 3fa080b

Browse files
committed
close response bodies everywhere
1 parent 5069fa0 commit 3fa080b

File tree

10 files changed

+122
-41
lines changed

10 files changed

+122
-41
lines changed

cmd/prometheus-agent/main.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ import (
1111
"path"
1212
"time"
1313

14-
"github.com/nais/device/pkg/logger"
1514
"github.com/prometheus/client_golang/prometheus"
1615
"github.com/prometheus/client_golang/prometheus/promhttp"
1716
log "github.com/sirupsen/logrus"
1817
flag "github.com/spf13/pflag"
18+
19+
"github.com/nais/device/pkg/ioconvenience"
20+
"github.com/nais/device/pkg/logger"
1921
)
2022

2123
const GatewayFetchInterval = 5 * time.Minute
@@ -163,11 +165,7 @@ func getGateways(config Config) ([]Gateway, error) {
163165
return nil, fmt.Errorf("getting gateways from apiserver: %w", err)
164166
}
165167

166-
defer func() {
167-
if err := resp.Body.Close(); err != nil {
168-
log.Errorf("closing get response body, %v", err)
169-
}
170-
}()
168+
defer ioconvenience.CloseReader(resp.Body)
171169

172170
var gateways []Gateway
173171
err = json.NewDecoder(resp.Body).Decode(&gateways)

pkg/apiserver/enroller/device.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@ import (
55
"context"
66
"encoding/json"
77
"fmt"
8-
"github.com/nais/device/pkg/pb"
98
"time"
109

11-
"github.com/nais/device/pkg/bootstrap"
10+
"github.com/nais/device/pkg/ioconvenience"
11+
"github.com/nais/device/pkg/pb"
12+
1213
log "github.com/sirupsen/logrus"
14+
15+
"github.com/nais/device/pkg/bootstrap"
1316
)
1417

1518
func (e *Enroller) WatchDeviceEnrollments(ctx context.Context) {
@@ -87,6 +90,8 @@ func (e *Enroller) fetchDeviceInfos(bootstrapURL string) ([]bootstrap.DeviceInfo
8790
return nil, fmt.Errorf("getting device infos: %w", err)
8891
}
8992

93+
defer ioconvenience.CloseReader(r.Body)
94+
9095
var deviceInfos []bootstrap.DeviceInfo
9196
err = json.NewDecoder(r.Body).Decode(&deviceInfos)
9297
if err != nil {

pkg/apiserver/enroller/gateway.go

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"time"
99

1010
"github.com/nais/device/pkg/bootstrap"
11+
"github.com/nais/device/pkg/ioconvenience"
12+
1113
log "github.com/sirupsen/logrus"
1214
)
1315

@@ -82,6 +84,8 @@ func (e *Enroller) fetchGatewayInfos() ([]bootstrap.GatewayInfo, error) {
8284
return nil, fmt.Errorf("getting gateway infos: %w", err)
8385
}
8486

87+
defer ioconvenience.CloseReader(r.Body)
88+
8589
var gatewayInfos []bootstrap.GatewayInfo
8690
err = json.NewDecoder(r.Body).Decode(&gatewayInfos)
8791
if err != nil {

pkg/apiserver/jita/privilegeduser.go

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"encoding/json"
55
"fmt"
66
"net/http"
7+
8+
"github.com/nais/device/pkg/ioconvenience"
79
)
810

911
type PrivilegedUser struct {
@@ -16,9 +18,13 @@ func (j *Jita) GetPrivilegedUsersForGateway(gateway string) ([]PrivilegedUser, e
1618
if err != nil {
1719
return nil, fmt.Errorf("getting privileged users: %w", err)
1820
}
21+
22+
defer ioconvenience.CloseReader(resp.Body)
23+
1924
if resp.StatusCode != http.StatusOK {
2025
return nil, fmt.Errorf("not ok when calling jita: %v", resp.StatusCode)
2126
}
27+
2228
var users []PrivilegedUser
2329
if err := json.NewDecoder(resp.Body).Decode(&users); err != nil {
2430
return nil, fmt.Errorf("decoding privileged users: %w", err)

pkg/device-agent/bootstrapper/bootstrapper.go

+35-12
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ import (
88
"net/http"
99
"time"
1010

11-
"github.com/nais/device/pkg/bootstrap"
1211
log "github.com/sirupsen/logrus"
12+
13+
"github.com/nais/device/pkg/bootstrap"
14+
"github.com/nais/device/pkg/ioconvenience"
1315
)
1416

1517
func BootstrapDevice(deviceInfo *bootstrap.DeviceInfo, bootstrapAPI string, client *http.Client) (*bootstrap.Config, error) {
@@ -35,11 +37,12 @@ func postDeviceInfo(url string, deviceInfo *bootstrap.DeviceInfo, client *http.C
3537
}
3638

3739
resp, err := client.Post(url, "application/json", bytes.NewReader(dib))
38-
3940
if err != nil {
4041
return fmt.Errorf("posting device info to bootstrap API (%v): %w", url, err)
4142
}
4243

44+
defer ioconvenience.CloseReader(resp.Body)
45+
4346
if resp.StatusCode != http.StatusCreated {
4447
body, err := ioutil.ReadAll(resp.Body)
4548
if err != nil {
@@ -53,20 +56,40 @@ func postDeviceInfo(url string, deviceInfo *bootstrap.DeviceInfo, client *http.C
5356
}
5457

5558
func getBootstrapConfig(url string, client *http.Client) (*bootstrap.Config, error) {
59+
get := func() (*bootstrap.Config, error) {
60+
resp, err := client.Get(url)
61+
if err != nil {
62+
return nil, err
63+
}
64+
65+
defer ioconvenience.CloseReader(resp.Body)
66+
67+
if resp.StatusCode != 200 {
68+
return nil, fmt.Errorf("got statuscode %d from bootstrap api", resp.StatusCode)
69+
}
70+
71+
bootstrapConfig := &bootstrap.Config{}
72+
err = json.NewDecoder(resp.Body).Decode(bootstrapConfig)
73+
if err != nil {
74+
return nil, err
75+
}
76+
77+
return bootstrapConfig, nil
78+
}
79+
5680
attempts := 3
5781

5882
for i := 0; i < attempts; i++ {
59-
resp, err := client.Get(url)
60-
61-
if err == nil && resp.StatusCode == 200 {
62-
var bootstrapConfig bootstrap.Config
63-
if err := json.NewDecoder(resp.Body).Decode(&bootstrapConfig); err == nil {
64-
log.Debugf("Got bootstrap config from bootstrap api: %v", bootstrapConfig)
65-
return &bootstrapConfig, nil
66-
}
83+
bootstrapConfig, err := get()
84+
if err != nil {
85+
log.Warnf("Attempt %d/%d at getting bootstrap config failed: %s", i+1, attempts, err)
86+
time.Sleep(1 * time.Second)
87+
continue
6788
}
68-
time.Sleep(1 * time.Second)
69-
continue
89+
90+
log.Debugf("Got bootstrap config from bootstrap api: %v", bootstrapConfig)
91+
return bootstrapConfig, nil
7092
}
93+
7194
return nil, fmt.Errorf("unable to get boostrap config in %v attempts from %v", attempts, url)
7295
}

pkg/device-agent/eventloop.go

+3-6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525

2626
clientcert "github.com/nais/device/pkg/client-cert"
2727
"github.com/nais/device/pkg/device-agent/config"
28+
"github.com/nais/device/pkg/ioconvenience"
2829

2930
log "github.com/sirupsen/logrus"
3031

@@ -424,12 +425,8 @@ func newVersionAvailable(ctx context.Context) (bool, error) {
424425
return false, fmt.Errorf("retrieve current release version: %s", err)
425426
}
426427

427-
defer func() {
428-
err := resp.Body.Close()
429-
if err != nil {
430-
log.Errorf("close request body: %v", err)
431-
}
432-
}()
428+
defer ioconvenience.CloseReader(resp.Body)
429+
433430
res := &response{}
434431
decoder := json.NewDecoder(resp.Body)
435432
err = decoder.Decode(res)

pkg/gateway-agent/apiserver.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ package gateway_agent
33
import (
44
"encoding/json"
55
"fmt"
6-
"github.com/nais/device/pkg/apiserver/api"
76
"io/ioutil"
87
"net/http"
8+
9+
"github.com/nais/device/pkg/apiserver/api"
10+
"github.com/nais/device/pkg/ioconvenience"
911
)
1012

1113
func GetGatewayConfig(config Config, client http.Client) (*api.GatewayConfig, error) {
@@ -20,7 +22,7 @@ func GetGatewayConfig(config Config, client http.Client) (*api.GatewayConfig, er
2022
return nil, fmt.Errorf("getting peer config from apiserver: %w", err)
2123
}
2224

23-
defer resp.Body.Close()
25+
defer ioconvenience.CloseReader(resp.Body)
2426

2527
b, err := ioutil.ReadAll(resp.Body)
2628
if err != nil {

pkg/gateway-agent/bootstrapper.go

+34-10
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ import (
88
"net/http"
99
"time"
1010

11+
log "github.com/sirupsen/logrus"
12+
1113
"github.com/nais/device/pkg/bootstrap"
1214
"github.com/nais/device/pkg/device-agent/wireguard"
13-
log "github.com/sirupsen/logrus"
15+
"github.com/nais/device/pkg/ioconvenience"
1416
)
1517

1618
type Bootstrapper struct {
@@ -74,6 +76,8 @@ func postGatewayInfo(url string, gatewayInfo *bootstrap.GatewayInfo, client *htt
7476
return fmt.Errorf("posting info to bootstrap API (%v): %w", url, err)
7577
}
7678

79+
defer ioconvenience.CloseReader(resp.Body)
80+
7781
if resp.StatusCode != http.StatusCreated {
7882
body, err := ioutil.ReadAll(resp.Body)
7983
if err != nil {
@@ -89,19 +93,39 @@ func postGatewayInfo(url string, gatewayInfo *bootstrap.GatewayInfo, client *htt
8993
func getBootstrapConfig(url string, client *http.Client) (*bootstrap.Config, error) {
9094
attempts := 3
9195

92-
for i := 0; i < attempts; i++ {
96+
get := func() (*bootstrap.Config, error) {
9397
resp, err := client.Get(url)
98+
if err != nil {
99+
return nil, err
100+
}
101+
defer ioconvenience.CloseReader(resp.Body)
102+
103+
if resp.StatusCode != 200 {
104+
return nil, fmt.Errorf("got statuscode %d from bootstrap-api", resp.StatusCode)
105+
}
94106

95-
if err == nil && resp.StatusCode == 200 {
96-
var bootstrapConfig bootstrap.Config
97-
if err := json.NewDecoder(resp.Body).Decode(&bootstrapConfig); err == nil {
98-
log.Debugf("Got bootstrap config from bootstrap api: %v", bootstrapConfig)
99-
return &bootstrapConfig, nil
100-
}
107+
bootstrapConfig := &bootstrap.Config{}
108+
err = json.NewDecoder(resp.Body).Decode(bootstrapConfig)
109+
110+
if err != nil {
111+
return nil, err
101112
}
102-
time.Sleep(1 * time.Second)
103-
continue
113+
114+
return bootstrapConfig, nil
115+
}
116+
117+
for i := 0; i < attempts; i++ {
118+
bootstrapConfig, err := get()
119+
if err != nil {
120+
log.Warnf("Attempt %d/%d at getting bootstrap config failed: %s", i+1, attempts, err)
121+
time.Sleep(1 * time.Second)
122+
continue
123+
}
124+
125+
log.Debugf("Got bootstrap config from bootstrap api: %v", bootstrapConfig)
126+
return bootstrapConfig, nil
104127
}
128+
105129
return nil, fmt.Errorf("unable to get boostrap config in %v attempts from %v", attempts, url)
106130
}
107131

pkg/ioconvenience/io.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package ioconvenience
2+
3+
import (
4+
"io"
5+
6+
log "github.com/sirupsen/logrus"
7+
)
8+
9+
func CloseReader(r io.ReadCloser) {
10+
err := r.Close()
11+
if err != nil {
12+
log.Warnf("Could not close reader: %s", err)
13+
}
14+
}

pkg/kolide-client/client.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
"time"
1414

1515
log "github.com/sirupsen/logrus"
16+
17+
"github.com/nais/device/pkg/ioconvenience"
1618
)
1719

1820
type KolideClient struct {
@@ -89,11 +91,14 @@ func (kc *KolideClient) get(ctx context.Context, path string) (*http.Response, e
8991
sleep := GetRetryAfter(resp.Header)
9092
log.Debugf("[attempt %d/%d] StatusTooManyRequests: sleeping %v", attempt, MaxHttpRetries, sleep)
9193
respectTheirAuthority(sleep)
94+
ioconvenience.CloseReader(resp.Body)
9295
case statusCode >= 500:
9396
sleep := time.Duration(attempt+1) * time.Second
9497
log.Debugf("[attempt %d/%d] KolideServerError: sleeping %v", attempt, MaxHttpRetries, sleep)
9598
respectTheirAuthority(sleep)
99+
ioconvenience.CloseReader(resp.Body)
96100
default:
101+
ioconvenience.CloseReader(resp.Body)
97102
return nil, fmt.Errorf("unexpected stauts code: %d, response: %v", statusCode, resp)
98103
}
99104
}
@@ -111,15 +116,14 @@ func (kc *KolideClient) GetApiPathf(path string, args ...interface{}) string {
111116

112117
func (kc *KolideClient) GetDevice(ctx context.Context, deviceId uint64) (*Device, error) {
113118
response, err := kc.get(ctx, kc.GetApiPathf("devices/%d", deviceId))
114-
115119
if err != nil {
116120
return nil, fmt.Errorf("getting client: %w", err)
117121
}
118122

119-
var device Device
123+
defer ioconvenience.CloseReader(response.Body)
120124

125+
var device Device
121126
err = json.NewDecoder(response.Body).Decode(&device)
122-
123127
if err != nil {
124128
return nil, fmt.Errorf("decoding device: %w", err)
125129
}
@@ -141,6 +145,8 @@ func (kc *KolideClient) GetCheck(ctx context.Context, checkId int) (*Check, erro
141145
return nil, fmt.Errorf("getting check: %w", err)
142146
}
143147

148+
defer ioconvenience.CloseReader(response.Body)
149+
144150
var check Check
145151
err = json.NewDecoder(response.Body).Decode(&check)
146152
if err != nil {
@@ -171,8 +177,10 @@ func (kc *KolideClient) GetPaginated(ctx context.Context, path string) ([]json.R
171177

172178
responseBytes, err := ioutil.ReadAll(response.Body)
173179
if err != nil {
180+
ioconvenience.CloseReader(response.Body)
174181
return nil, fmt.Errorf("reading response body bytes: %w", err)
175182
}
183+
ioconvenience.CloseReader(response.Body)
176184

177185
var paginatedResponse PaginatedResponse
178186
err = json.Unmarshal(responseBytes, &paginatedResponse)

0 commit comments

Comments
 (0)