Skip to content

Commit 74f8dd0

Browse files
authored
fix: resolve linting issues and modernize code (#109)
* fix: resolve linting issues and modernize code - Replace deprecated io/ioutil with io package - Fix ineffectual assignments in configmaps.go and configmaps_test.go - Optimize loop condition in WithRetry function - Update Makefile with better target organization and dependencies All tests passing with 92.5% coverage. Signed-off-by: Todd Ekenstam <tekenstam@gmail.com> * chore: add lint workflow Signed-off-by: Todd Ekenstam <tekenstam@gmail.com> * fix: update lint enforcement Signed-off-by: Todd Ekenstam <tekenstam@gmail.com> * fix: update license check Signed-off-by: Todd Ekenstam <tekenstam@gmail.com> * fix: improve security check Signed-off-by: Todd Ekenstam <tekenstam@gmail.com> * chore: upload gosec artifact Signed-off-by: Todd Ekenstam <tekenstam@gmail.com> --------- Signed-off-by: Todd Ekenstam <tekenstam@gmail.com>
1 parent f4e7f92 commit 74f8dd0

7 files changed

Lines changed: 171 additions & 10 deletions

File tree

.github/workflows/lint.yml

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
name: Lint and Scan
2+
3+
on:
4+
# Only run on PRs targeting master
5+
pull_request:
6+
branches: [ master ]
7+
types: [opened, synchronize, reopened]
8+
# For direct pushes to master only
9+
push:
10+
branches: [ master ]
11+
paths-ignore:
12+
- '**.md'
13+
- 'docs/**'
14+
- '.github/**'
15+
- '!.github/workflows/lint.yml'
16+
17+
# Prevent duplicate workflow runs
18+
concurrency:
19+
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
20+
cancel-in-progress: true
21+
22+
jobs:
23+
golangci:
24+
name: Go Linting
25+
runs-on: ubuntu-latest
26+
# Allow job to succeed even with lint issues for now
27+
continue-on-error: true
28+
steps:
29+
- name: Check out code
30+
uses: actions/checkout@v4
31+
32+
- name: Set up Go
33+
uses: actions/setup-go@v5
34+
with:
35+
go-version: 1.24.x
36+
cache: true
37+
38+
# Simple linting first using standard go tools
39+
- name: Run go fmt
40+
run: |
41+
go fmt ./...
42+
43+
- name: Run go vet
44+
run: |
45+
go vet ./...
46+
47+
- name: Run golangci-lint
48+
id: lint
49+
uses: golangci/golangci-lint-action@v7
50+
with:
51+
version: latest
52+
53+
gosec-issues:
54+
name: Security Scan Issues
55+
runs-on: ubuntu-latest
56+
steps:
57+
- name: Check out code
58+
uses: actions/checkout@v4
59+
60+
# Fail only on high severity issues
61+
- name: Run gosec security scan
62+
uses: securego/gosec@master
63+
with:
64+
args: -exclude-generated -fmt=json -out=results.json ./...
65+
66+
- name: Check for high severity issues
67+
run: |
68+
if [ ! -f results.json ]; then
69+
echo "Error: gosec scan results not found"
70+
exit 1
71+
fi
72+
73+
# Check if any high severity issues exist (level 3)
74+
HIGH_ISSUES=$(cat results.json | grep -c '"severity":"HIGH"' || true)
75+
if [ "$HIGH_ISSUES" -gt 0 ]; then
76+
echo "Found $HIGH_ISSUES high severity security issues!"
77+
cat results.json | grep -A 5 -B 5 '"severity":"HIGH"'
78+
exit 1
79+
else
80+
echo "No high severity security issues found."
81+
fi
82+
83+
- name: Upload security scan results
84+
if: always() # Run even if previous steps failed
85+
uses: actions/upload-artifact@v4
86+
with:
87+
name: gosec-results
88+
path: results.json
89+
retention-days: 7
90+
if-no-files-found: warn
91+
92+
license-check:
93+
name: License Compliance
94+
runs-on: ubuntu-latest
95+
steps:
96+
- name: Check out code
97+
uses: actions/checkout@v4
98+
99+
- name: Set up Go
100+
uses: actions/setup-go@v5
101+
with:
102+
go-version: 1.24.x
103+
104+
- name: Check License Headers
105+
run: |
106+
# Only check Go files that aren't in vendor or generated
107+
echo "Checking for Apache License headers in Go files..."
108+
# Store files missing license in a variable
109+
MISSING_LICENSE=$(find . -name "*.go" -type f -not -path "*/vendor/*" -not -path "*/mocks/*" | xargs grep -L "Licensed under the Apache License" || true)
110+
111+
# If any files are missing license headers, report and exit with error
112+
if [ -n "$MISSING_LICENSE" ]; then
113+
echo "ERROR: The following files are missing Apache License headers:"
114+
echo "$MISSING_LICENSE"
115+
echo "License check failed. Please add the appropriate license headers."
116+
exit 1
117+
else
118+
echo "License check passed. All files have proper license headers."
119+
fi

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
bin/*
22
coverage.txt
33
coverage.html
4+
results.json
45
dist/
56
.windsurfrules

Makefile

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,45 @@ LDFLAGS=-ldflags "-X ${LDFLAG_LOCATION}.buildDate=${BUILD} -X ${LDFLAG_LOCATION}
1010
GIT_TAG=$(shell git rev-parse --short HEAD)
1111
IMAGE ?= aws-auth:latest
1212

13+
all: lint test build
14+
1315
build:
1416
CGO_ENABLED=0 go build ${LDFLAGS} -o bin/aws-auth github.com/keikoproj/aws-auth
1517
chmod +x bin/aws-auth
1618

17-
test:
19+
test: fmt vet
1820
go test -v ./... -coverprofile coverage.txt
1921
go tool cover -html=coverage.txt -o coverage.html
2022

23+
# Run go fmt against code
24+
fmt:
25+
go fmt ./...
26+
27+
# Run go vet against code
28+
vet:
29+
go vet ./...
30+
2131
docker-build:
2232
docker build -t $(IMAGE) .
2333

2434
docker-push:
25-
docker push ${IMAGE}
35+
docker push ${IMAGE}
36+
37+
LOCALBIN = $(shell pwd)/bin
38+
$(LOCALBIN):
39+
mkdir -p $(LOCALBIN)
40+
41+
GOLANGCI_LINT_VERSION := v2.1.1
42+
GOLANGCI_LINT = $(shell pwd)/bin/golangci-lint
43+
.PHONY: golangci-lint
44+
$(GOLANGCI_LINT): $(LOCALBIN)
45+
GOBIN=$(LOCALBIN) go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@$(GOLANGCI_LINT_VERSION)
46+
47+
.PHONY: lint
48+
lint: $(GOLANGCI_LINT)
49+
@echo "Running golangci-lint"
50+
$(GOLANGCI_LINT) run ./...
51+
52+
.PHONY: clean
53+
clean:
54+
@rm -rf ./bin

example/example.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/*
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
*/
15+
116
package main
217

318
import (

pkg/mapper/configmaps.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func UpdateAuthMap(k kubernetes.Interface, authData AwsAuthData, cm *v1.ConfigMa
9191
"mapUsers": string(mapUsers),
9292
}
9393

94-
cm, err = k.CoreV1().ConfigMaps(AwsAuthNamespace).Update(context.Background(), cm, metav1.UpdateOptions{})
94+
_, err = k.CoreV1().ConfigMaps(AwsAuthNamespace).Update(context.Background(), cm, metav1.UpdateOptions{})
9595
if err != nil {
9696
return err
9797
}

pkg/mapper/configmaps_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func TestConfigMaps_Update(t *testing.T) {
8888
err = UpdateAuthMap(client, auth, cm)
8989
g.Expect(err).NotTo(gomega.HaveOccurred())
9090

91-
auth, cm, err = ReadAuthMap(client)
91+
auth, _, err = ReadAuthMap(client)
9292
g.Expect(err).NotTo(gomega.HaveOccurred())
9393

9494
fmt.Println(auth.MapRoles[0])

pkg/mapper/types.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ package mapper
1717

1818
import (
1919
"fmt"
20-
"io/ioutil"
20+
"io"
2121
"log"
2222
"strings"
2323
"time"
@@ -42,7 +42,7 @@ func New(client kubernetes.Interface, isCommandline bool) *AuthMapper {
4242
mapper.KubernetesClient = client
4343

4444
if !isCommandline {
45-
log.SetOutput(ioutil.Discard)
45+
log.SetOutput(io.Discard)
4646
}
4747
return mapper
4848
}
@@ -246,10 +246,7 @@ func WithRetry(fn RetriableFunction, args *MapperArguments) (interface{}, error)
246246
}
247247
)
248248

249-
for {
250-
if counter >= args.MaxRetryCount {
251-
break
252-
}
249+
for counter < args.MaxRetryCount {
253250

254251
if out, err = fn(); err != nil {
255252
d := bkoff.Duration()

0 commit comments

Comments
 (0)