Skip to content

Commit a4d4085

Browse files
geofffranksameowlia
authored andcommitted
Catch or explicitly ignore unhandled errors
1 parent 5b920eb commit a4d4085

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+127
-18
lines changed

src/code.cloudfoundry.org/bosh-dns-adapter/config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type Config struct {
2727
}
2828

2929
func init() {
30+
// #nosec G104 - don't handle error here - only thing that would be returned is if we had an empty name passed in, and we're in an init block with limitid handling capability
3031
validator.SetValidationFunc("cidr", func(v interface{}, param string) error {
3132
cidr, ok := v.(string)
3233
if !ok {

src/code.cloudfoundry.org/bosh-dns-adapter/main.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ func main() {
9393
}
9494

9595
go func() {
96-
http.Serve(l, metricsWrap("GetIPs", http.HandlerFunc(getIPsHandler.ServeHTTP)))
96+
err = http.Serve(l, metricsWrap("GetIPs", http.HandlerFunc(getIPsHandler.ServeHTTP)))
97+
logger.Info("http-server-returned", lager.Data{"error": err})
9798
}()
9899

99100
uptimeSource := metrics.NewUptimeSource()
@@ -122,6 +123,9 @@ func main() {
122123
logger.Info("server-started")
123124
sig := <-signalChannel
124125
monitor.Signal(sig)
125-
l.Close()
126+
err = l.Close()
127+
if err != nil {
128+
logger.Error("erro-closing-server", err)
129+
}
126130
logger.Info("server-stopped")
127131
}

src/code.cloudfoundry.org/bosh-dns-adapter/sdcclient/service_discovery_client.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ func (s *ServiceDiscoveryClient) IPs(infrastructureName string) ([]string, error
8686
}
8787

8888
defer func(httpResp *http.Response) {
89+
// #nosec G104 - ignore errors in the defer block, this response was bad anyway and we just want to best-effort clean it up
8990
io.Copy(io.Discard, httpResp.Body)
91+
// #nosec G104 - ignore errors in the defer block, this response was bad anyway and we just want to best-effort clean it up
9092
httpResp.Body.Close()
9193
}(httpResp)
9294

@@ -102,6 +104,7 @@ func (s *ServiceDiscoveryClient) IPs(infrastructureName string) ([]string, error
102104
}
103105

104106
bytes, err := io.ReadAll(httpResp.Body)
107+
// #nosec G104 - ignore closing body errors. we either have all the data and can keep going
105108
httpResp.Body.Close()
106109
if err != nil {
107110
return []string{}, err

src/code.cloudfoundry.org/cf-pusher/cmd/cf-pusher/main.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,13 @@ func main() {
231231
if err := apiConnector.Connect(); err != nil {
232232
log.Fatalf("connecting to api: %s", err)
233233
}
234-
adapter.TargetOrg(scaleGroup.Org)
235-
adapter.TargetSpace(scaleGroup.Space)
234+
if err := adapter.TargetOrg(scaleGroup.Org); err != nil {
235+
log.Fatalf("targeting org %s: %s", scaleGroup.Org, err)
236+
}
237+
238+
if err := adapter.TargetSpace(scaleGroup.Space); err != nil {
239+
log.Fatalf("targeting space %s: %s", scaleGroup.Space, err)
240+
}
236241

237242
// declare what apps we expect
238243
expectedApps := map[string]int{

src/code.cloudfoundry.org/policy-server/cmd/policy-server-asg-syncer/main.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,10 @@ func main() {
158158

159159
err = <-monitor.Wait()
160160
if connectionPool != nil {
161-
connectionPool.Close()
161+
closeErr := connectionPool.Close()
162+
if closeErr != nil {
163+
logger.Error("error-closing-connection-pool", err)
164+
}
162165
}
163166
if err != nil {
164167
logger.Error("exited-with-failure", err)

src/code.cloudfoundry.org/policy-server/cmd/policy-server-internal/main.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,10 @@ func main() {
202202

203203
err = <-monitor.Wait()
204204
if connectionPool != nil {
205-
connectionPool.Close()
205+
closeErr := connectionPool.Close()
206+
if closeErr != nil {
207+
logger.Error("error-closing-connection-pool", err)
208+
}
206209
}
207210
if err != nil {
208211
logger.Error("exited-with-failure", err)

src/code.cloudfoundry.org/policy-server/cmd/policy-server/main.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,10 @@ func main() {
324324

325325
err = <-monitor.Wait()
326326
if connectionPool != nil {
327-
connectionPool.Close()
327+
closeErr := connectionPool.Close()
328+
if closeErr != nil {
329+
logger.Error("error-closing-connection-pool", err)
330+
}
328331
}
329332
if err != nil {
330333
logger.Error("exited-with-failure", err)

src/code.cloudfoundry.org/policy-server/handlers/asgs_index.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ func (h *AsgsIndex) ServeHTTP(w http.ResponseWriter, req *http.Request) {
5252
}
5353

5454
w.WriteHeader(http.StatusOK)
55+
// #nosec G104 - ignore errors writing http responses to avoid spamming logs during a DoS
5556
w.Write(bytes)
5657
}
5758

src/code.cloudfoundry.org/policy-server/handlers/policies_cleanup.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,6 @@ func (h *PoliciesCleanup) ServeHTTP(w http.ResponseWriter, req *http.Request) {
5959
}
6060

6161
w.WriteHeader(http.StatusOK)
62+
// #nosec G104 - ignore errors writing http responses to avoid spamming logs during a DoS
6263
w.Write(bytes)
6364
}

src/code.cloudfoundry.org/policy-server/handlers/policies_create.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,5 +96,6 @@ func (h *PoliciesCreate) ServeHTTP(w http.ResponseWriter, req *http.Request) {
9696

9797
logger.Info("created-policies", lager.Data{"policies": policies, "userName": tokenData.UserName})
9898
w.WriteHeader(http.StatusOK)
99+
// #nosec G104 - ignore error writing http response to avoid spamming logs on a DoS
99100
w.Write([]byte("{}"))
100101
}

src/code.cloudfoundry.org/policy-server/handlers/policies_delete.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,6 @@ func (h *PoliciesDelete) ServeHTTP(w http.ResponseWriter, req *http.Request) {
6363

6464
logger.Info("deleted-policies", lager.Data{"policies": policies, "userName": tokenData.UserName})
6565
w.WriteHeader(http.StatusOK)
66+
// #nosec G104 - ignore errors writing http responses to avoid spamming logs during a DoS
6667
w.Write([]byte(`{}`))
6768
}

src/code.cloudfoundry.org/policy-server/handlers/policies_index.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ func (h *PoliciesIndex) ServeHTTP(w http.ResponseWriter, req *http.Request) {
8080
}
8181

8282
w.WriteHeader(http.StatusOK)
83+
// #nosec G104 - ignore errors writing http responses to avoid spamming logs during a DoS
8384
w.Write(bytes)
8485
}
8586

src/code.cloudfoundry.org/policy-server/handlers/policies_index_internal.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ func (h *PoliciesIndexInternal) ServeHTTP(w http.ResponseWriter, req *http.Reque
5454
}
5555

5656
w.WriteHeader(http.StatusOK)
57+
// #nosec G104 - ignore errors writing http responses to avoid spamming logs during a DoS
5758
w.Write(bytes)
5859
}
5960

src/code.cloudfoundry.org/policy-server/handlers/policies_last_updated_internal.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,6 @@ func (h *PoliciesLastUpdatedInternal) ServeHTTP(w http.ResponseWriter, req *http
3434
}
3535

3636
w.WriteHeader(http.StatusOK)
37+
// #nosec G104 - ignore errors writing http responses to avoid spamming logs during a DoS
3738
w.Write([]byte(strconv.Itoa(lastUpdated)))
3839
}

src/code.cloudfoundry.org/policy-server/handlers/tags_create.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,6 @@ func (h *TagsCreate) ServeHTTP(w http.ResponseWriter, req *http.Request) {
5555
return
5656
}
5757
w.WriteHeader(http.StatusOK)
58+
// #nosec G104 - ignore errors writing http responses to avoid spamming logs during a DoS
5859
w.Write(tagJSON)
5960
}

src/code.cloudfoundry.org/policy-server/handlers/tags_index.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,6 @@ func (h *TagsIndex) ServeHTTP(w http.ResponseWriter, req *http.Request) {
4141
}
4242

4343
w.WriteHeader(http.StatusOK)
44+
// #nosec G104 - ignore errors writing http responses to avoid spamming logs during a DoS
4445
w.Write(responseBytes)
4546
}

src/code.cloudfoundry.org/policy-server/handlers/uptime_handler.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ func (h *UptimeHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
1414
w.WriteHeader(http.StatusOK)
1515
currentTime := time.Now()
1616
uptime := currentTime.Sub(h.StartTime)
17+
// #nosec G104 - ignore errors writing http responses to avoid spamming logs during a DoS
1718
w.Write([]byte(fmt.Sprintf("Network policy server, up for %v\n", uptime)))
1819
}

src/code.cloudfoundry.org/policy-server/handlers/whoami_handler.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,6 @@ func (h *WhoAmIHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
3232
}
3333

3434
w.WriteHeader(http.StatusOK)
35+
// #nosec G104 - ignore errors writing http responses to avoid spamming logs during a DoS
3536
w.Write(responseJSON)
3637
}

src/code.cloudfoundry.org/policy-server/integration/helpers/configurable_mock_cc_server.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,24 +81,28 @@ func (c *ConfigurableMockCCServer) ServeHTTP(w http.ResponseWriter, r *http.Requ
8181

8282
if r.URL.Path == "/v3/apps" {
8383
w.WriteHeader(http.StatusOK)
84+
// #nosec G104 - ignore errors writing http responses to avoid spamming logs during a DoS
8485
w.Write([]byte(buildCCGuidsResponse(c.apps)))
8586
return
8687
}
8788

8889
if r.URL.Path == "/v3/spaces" {
8990
w.WriteHeader(http.StatusOK)
91+
// #nosec G104 - ignore errors writing http responses to avoid spamming logs during a DoS
9092
w.Write([]byte(buildCCGuidsResponse(c.spaces)))
9193
return
9294
}
9395

9496
if r.URL.Path == "/v3/security_groups" {
9597
w.WriteHeader(http.StatusOK)
98+
// #nosec G104 - ignore errors writing http responses to avoid spamming logs during a DoS
9699
w.Write([]byte(buildCCSecurityGroupsResponse(c.securityGroups)))
97100
return
98101
}
99102

100103
if r.URL.Path == "/internal/v4/asg_latest_update" {
101104
w.WriteHeader(http.StatusOK)
105+
// #nosec G104 - ignore errors writing http responses to avoid spamming logs during a DoS
102106
w.Write([]byte(buildCCASGLatestUpdateResponse(c.asgLatestUpdate)))
103107
}
104108

src/code.cloudfoundry.org/policy-server/integration/helpers/helpers.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,61 +35,72 @@ var MockCCServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWrite
3535
if r.URL.Path == "/v3/apps" {
3636
if strings.Contains(r.URL.RawQuery, "app-guid-not-in-my-spaces") {
3737
w.WriteHeader(http.StatusOK)
38+
// #nosec G104 - ignore errors writing http responses to avoid spamming logs during a DoS
3839
w.Write([]byte(fixtures.AppsV3TwoSpaces))
3940
return
4041
}
4142
if strings.Contains(r.URL.RawQuery, "live-app-1-guid") {
4243
w.WriteHeader(http.StatusOK)
44+
// #nosec G104 - ignore errors writing http responses to avoid spamming logs during a DoS
4345
w.Write([]byte(fixtures.AppsV3LiveApp1GUID))
4446
return
4547
}
4648
if strings.Contains(r.URL.RawQuery, "live-app-2-guid") {
4749
w.WriteHeader(http.StatusOK)
50+
// #nosec G104 - ignore errors writing http responses to avoid spamming logs during a DoS
4851
w.Write([]byte(fixtures.AppsV3LiveApp2GUID))
4952
return
5053
}
5154
if strings.Contains(r.URL.RawQuery, "live-app-3-guid") {
5255
w.WriteHeader(http.StatusOK)
56+
// #nosec G104 - ignore errors writing http responses to avoid spamming logs during a DoS
5357
w.Write([]byte(fixtures.AppsV3LiveApp3GUID))
5458
return
5559
}
5660
w.WriteHeader(http.StatusOK)
61+
// #nosec G104 - ignore errors writing http responses to avoid spamming logs during a DoS
5762
w.Write([]byte(fixtures.AppsV3OneSpace))
5863
return
5964
}
6065

6166
if r.URL.Path == "/v3/spaces" {
6267
w.WriteHeader(http.StatusOK)
68+
// #nosec G104 - ignore errors writing http responses to avoid spamming logs during a DoS
6369
w.Write([]byte(fixtures.SpaceV3LiveSpaces))
6470
return
6571
}
6672

6773
if r.URL.Path == "/v2/spaces/space-1-guid" {
6874
w.WriteHeader(http.StatusOK)
75+
// #nosec G104 - ignore errors writing http responses to avoid spamming logs during a DoS
6976
w.Write([]byte(fixtures.Space1))
7077
return
7178
}
7279
if r.URL.Path == "/v2/spaces/space-2-guid" {
7380
w.WriteHeader(http.StatusOK)
81+
// #nosec G104 - ignore errors writing http responses to avoid spamming logs during a DoS
7482
w.Write([]byte(fixtures.Space2))
7583
return
7684
}
7785

7886
if r.URL.Path == "/v2/spaces" {
7987
if strings.Contains(r.URL.RawQuery, "space-1") {
8088
w.WriteHeader(http.StatusOK)
89+
// #nosec G104 - ignore errors writing http responses to avoid spamming logs during a DoS
8190
w.Write([]byte(fixtures.SubjectSpace))
8291
return
8392
}
8493
if strings.Contains(r.URL.RawQuery, "space-2") {
8594
w.WriteHeader(http.StatusOK)
95+
// #nosec G104 - ignore errors writing http responses to avoid spamming logs during a DoS
8696
w.Write([]byte(fixtures.SubjectSpaceEmpty))
8797
return
8898
}
8999
}
90100

91101
if r.URL.Path == "/v2/users/some-user-or-client-id/spaces" {
92102
w.WriteHeader(http.StatusOK)
103+
// #nosec G104 - ignore errors writing http responses to avoid spamming logs during a DoS
93104
w.Write([]byte(fixtures.SubjectSpaces))
94105
return
95106
}
@@ -109,18 +120,23 @@ var MockUAAServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWrit
109120
switch token {
110121
case "valid-token":
111122
w.WriteHeader(http.StatusOK)
123+
// #nosec G104 - ignore errors writing http responses to avoid spamming logs during a DoS
112124
w.Write([]byte(`{"scope":["network.admin"], "user_name":"some-user", "sub": "some-user-or-client-id"}`))
113125
case "valid-client-token":
114126
w.WriteHeader(http.StatusOK)
127+
// #nosec G104 - ignore errors writing http responses to avoid spamming logs during a DoS
115128
w.Write([]byte(`{"scope":["network.admin"], "sub": "some-client-id"}`))
116129
case "space-dev-with-network-write-token":
117130
w.WriteHeader(http.StatusOK)
131+
// #nosec G104 - ignore errors writing http responses to avoid spamming logs during a DoS
118132
w.Write([]byte(`{"scope":["network.write"], "user_name":"some-user", "sub": "some-user-or-client-id"}`))
119133
case "space-dev-token":
120134
w.WriteHeader(http.StatusOK)
135+
// #nosec G104 - ignore errors writing http responses to avoid spamming logs during a DoS
121136
w.Write([]byte(`{"scope":[], "user_name":"some-user", "sub": "some-user-or-client-id"}`))
122137
default:
123138
w.WriteHeader(http.StatusBadRequest)
139+
// #nosec G104 - ignore errors writing http responses to avoid spamming logs during a DoS
124140
w.Write([]byte(`{"error_description":"banana"}`))
125141
}
126142
} else {
@@ -142,6 +158,7 @@ var MockUAAServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWrit
142158
`
143159
if r.Header["Authorization"][0] == "Basic dGVzdDp0ZXN0" {
144160
w.WriteHeader(http.StatusOK)
161+
// #nosec G104 - ignore errors writing http responses to avoid spamming logs during a DoS
145162
w.Write([]byte(token))
146163
} else {
147164
w.WriteHeader(http.StatusUnauthorized)
@@ -263,7 +280,10 @@ func VerifyTCPConnection(address string) error {
263280
if err != nil {
264281
return err
265282
}
266-
conn.Close()
283+
err = conn.Close()
284+
if err != nil {
285+
return err
286+
}
267287
return nil
268288
}
269289

src/code.cloudfoundry.org/policy-server/store/migrations_store.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ func (m *MigrationsStore) HasV2MigrationOccurred() (bool, error) {
5555
rows, err := m.DBConn.Query(query)
5656
defer func() {
5757
if rows != nil {
58+
// #nosec G104 - don't override the return value in the defer block because our close failed
5859
rows.Close()
5960
}
6061
}()
@@ -103,8 +104,9 @@ func (m *MigrationsStore) tableExists(tableName string) bool {
103104
if err != nil {
104105
return false
105106
}
106-
rows.Close()
107-
return true
107+
err = rows.Close()
108+
// if Query() fails, we return false (including if there are failures not related to the table existing). do the same here
109+
return err == nil
108110
}
109111

110112
func (m *MigrationsStore) migrationIDExists(ids ...string) (bool, error) {

src/code.cloudfoundry.org/service-discovery-controller/mbus/subscriber.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,10 @@ func (s *Subscriber) RunOnce() error {
131131

132132
s.table.ResumePruning()
133133

134-
s.sendStartMessage()
134+
err = s.sendStartMessage()
135+
if err != nil {
136+
s.logger.Error("error sending start message after reconnecting to nats server", err)
137+
}
135138
})),
136139
nats.DisconnectHandler(nats.ConnHandler(func(conn *nats.Conn) {
137140
s.logger.Info(

src/code.cloudfoundry.org/service-discovery-controller/routes/server.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,17 @@ func (s *Server) Run(signals <-chan os.Signal, ready chan<- struct{}) error {
125125
for {
126126
select {
127127
case err := <-exited:
128-
httpServer.Close()
128+
closeErr := httpServer.Close()
129+
if closeErr != nil {
130+
s.logger.Error("Error closing SDC http server: %s", err)
131+
}
129132
s.logger.Info(fmt.Sprintf("SDC http server exiting with: %v", err))
130133
return err
131134
case signal := <-signals:
132-
httpServer.Close()
135+
err := httpServer.Close()
136+
if err != nil {
137+
s.logger.Error("Error closing SDC http server: %s", err)
138+
}
133139
s.logger.Info(fmt.Sprintf("SDC http server exiting with signal: %v", signal))
134140
return nil
135141
}

src/code.cloudfoundry.org/test-helpers/conflicting_server.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ import (
1111
func LaunchConflictingServer(port int) *http.Server {
1212
address := fmt.Sprintf("127.0.0.1:%d", port)
1313
conflictingServer := &http.Server{Addr: address, ReadHeaderTimeout: 5 * time.Second}
14-
go func() { conflictingServer.ListenAndServe() }()
14+
go func() {
15+
err := conflictingServer.ListenAndServe()
16+
if err != nil {
17+
fmt.Printf("conflictingServer closed with error: %s\n", err)
18+
}
19+
}()
1520
client := &http.Client{}
1621
Eventually(func() bool {
1722
resp, err := client.Get(fmt.Sprintf("http://%s", address))

0 commit comments

Comments
 (0)