Skip to content

Commit

Permalink
[KEYCLOAK-11183] Fix linting errors reported by GolangCI on Gatekeeper
Browse files Browse the repository at this point in the history
 - Replace gometalinter by golangci-lint in the Makefile
 - Fix linting errors reported by golangci-lint
  • Loading branch information
Bruno Oliveira da Silva committed Aug 27, 2019
1 parent d874534 commit 4023a75
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 10 deletions.
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ vet:
fi

lint:
@echo "--> Running golint"
@which golint 2>/dev/null ; if [ $$? -eq 1 ]; then \
go get -u github.com/golang/lint/golint; \
@echo "--> Running golangci-lint"
@which golangci-lint 2>/dev/null ; if [ $$? -eq 1 ]; then \
go get -u github.com/golangci/golangci-lint/cmd/golangci-lint; \
fi
@golint .

Expand All @@ -114,7 +114,7 @@ gofmt:

verify:
@echo "--> Verifying the code"
gometalinter --disable=errcheck --disable=gocyclo --disable=gas --disable=aligncheck --errors
golangci-lint run

format:
@echo "--> Running go fmt"
Expand Down
17 changes: 13 additions & 4 deletions cookies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,16 @@ func TestCookieDomainHostHeader(t *testing.T) {
cookie = c
}
}
defer resp.Body.Close()

assert.NotNil(t, cookie)
assert.Equal(t, cookie.Domain, "127.0.0.1")
}

func TestCookieBasePath(t *testing.T) {
const baseURI = "/base-uri"
cfg := newFakeKeycloakConfig()
cfg.BaseURI = "/base-uri"
cfg.BaseURI = baseURI

_, _, svc := newTestProxyService(cfg)

Expand All @@ -52,12 +55,14 @@ func TestCookieBasePath(t *testing.T) {

var cookie *http.Cookie
for _, c := range resp.Cookies() {
if c.Name == "kc-access" {
if c.Name == accessCookie {
cookie = c
}
}
defer resp.Body.Close()

assert.NotNil(t, cookie)
assert.Equal(t, "/base-uri", cookie.Path)
assert.Equal(t, baseURI, cookie.Path)
}

func TestCookieWithoutBasePath(t *testing.T) {
Expand All @@ -71,10 +76,12 @@ func TestCookieWithoutBasePath(t *testing.T) {

var cookie *http.Cookie
for _, c := range resp.Cookies() {
if c.Name == "kc-access" {
if c.Name == accessCookie {
cookie = c
}
}
defer resp.Body.Close()

assert.NotNil(t, cookie)
assert.Equal(t, "/", cookie.Path)
}
Expand All @@ -92,6 +99,8 @@ func TestCookieDomain(t *testing.T) {
cookie = c
}
}
defer resp.Body.Close()

assert.NotNil(t, cookie)
assert.Equal(t, cookie.Domain, "domain.com")
}
Expand Down
3 changes: 3 additions & 0 deletions e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@ func checkListenOrBail(endpoint string) bool {
waitTime = 100 * time.Millisecond
)
checkListen := http.Client{}
//nolint:bodyclose
_, err := checkListen.Get(endpoint)
limit := 0
for err != nil && limit < maxWaitCycles {
time.Sleep(waitTime)
//nolint:bodyclose
_, err = checkListen.Get(endpoint)
limit++
}
Expand Down Expand Up @@ -161,4 +163,5 @@ func TestCorsWithUpstream(t *testing.T) {
// check the returned upstream response after proxying contains CORS headers
assert.Equal(t, []string{"*"}, resp.Header["Access-Control-Allow-Origin"])
}
defer resp.Body.Close()
}
1 change: 1 addition & 0 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ func (r *oauthProxy) logoutHandler(w http.ResponseWriter, req *http.Request) {
zap.Int("status", response.StatusCode),
zap.String("response", fmt.Sprintf("%s", content)))
}
defer response.Body.Close()
}
// step: should we redirect the user
if redirectURL != "" {
Expand Down
2 changes: 1 addition & 1 deletion middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func (r *oauthProxy) authenticationMiddleware(resource *Resource) func(http.Hand
next.ServeHTTP(w, req.WithContext(r.redirectToAuthorization(w, req)))
return
}
} else {
} else { //nolint:gocritic
if err := verifyToken(r.client, user.token); err != nil {
// step: if the error post verification is anything other than a token
// expired error we immediately throw an access forbidden - as there is
Expand Down
1 change: 1 addition & 0 deletions middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ func (f *fakeProxy) performUserLogin(uri string) error {
}
}
}
defer resp.Body.Close()

return nil
}
Expand Down
1 change: 1 addition & 0 deletions oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ func getUserinfo(client *oauth2.Client, endpoint string, token string) (jose.Cla
if err := json.Unmarshal(content, &claims); err != nil {
return nil, err
}
defer resp.Body.Close()

return claims, nil
}
Expand Down
3 changes: 2 additions & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ func (r *oauthProxy) createForwardingProxy() error {
if err := r.createUpstreamProxy(nil); err != nil {
return err
}
//nolint:bodyclose
forwardingHandler := r.forwardProxyHandler()

// set the http handler
Expand Down Expand Up @@ -453,7 +454,7 @@ func (r *oauthProxy) createHTTPListener(config listenerConfig) (net.Listener, er
if listener, err = net.Listen("unix", socket); err != nil {
return nil, err
}
} else {
} else { //nolint:gocritic
if listener, err = net.Listen("tcp", config.listen); err != nil {
return nil, err
}
Expand Down

0 comments on commit 4023a75

Please sign in to comment.