Skip to content

Commit 95f568d

Browse files
refactor: simplify code across multiple files
- docker.go: simplify imageTag to direct return, remove redundant else after return in getImageIdFromDockerDaemonJsonMessages - ecr.go: use slices.Contains in IsFindingIgnored and AreSeverityLevelsValid, simplify GetECRClient to direct return, extract newUnsupportedImageFinding helper to reduce duplication - helpers.go: use map[string]struct{} instead of map[string]bool in dedupList for idiomatic set semantics, simplify variable names - junit.go: simplify time.Duration(1) * time.Second to time.Second Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent a773662 commit 95f568d

File tree

4 files changed

+28
-52
lines changed

4 files changed

+28
-52
lines changed

docker.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,7 @@ func imagePush(client *dockerClient.Client, authConfig dockerRegistry.AuthConfig
6363
}
6464

6565
func imageTag(client *dockerClient.Client, imageId string, newImageId string) error {
66-
67-
err := client.ImageTag(context.Background(), imageId, newImageId)
68-
if err != nil {
69-
return err
70-
}
71-
72-
return nil
66+
return client.ImageTag(context.Background(), imageId, newImageId)
7367
}
7468

7569
func getImageIdFromDockerDaemonJsonMessages(message bytes.Buffer) (ImageId, error) {
@@ -90,10 +84,9 @@ func getImageIdFromDockerDaemonJsonMessages(message bytes.Buffer) (ImageId, erro
9084
var r dockerTypes.PushResult
9185
if err := json.Unmarshal(*jsonMessage.Aux, &r); err != nil {
9286
return result, err
93-
} else {
94-
result.tag = r.Tag
95-
result.digest = r.Digest
9687
}
88+
result.tag = r.Tag
89+
result.digest = r.Digest
9790
}
9891
}
9992
return result, nil

ecr.go

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"errors"
2525
"fmt"
2626
"os"
27+
"slices"
2728
"strings"
2829
"time"
2930

@@ -37,7 +38,6 @@ import (
3738
)
3839

3940
func GetFindingSeverityLevelsAsList() []string {
40-
// TODO: is there a better way?
4141
return []string{
4242
string(types.FindingSeverityCritical),
4343
string(types.FindingSeverityHigh),
@@ -83,29 +83,19 @@ func GetIgnoredFindings(findings []types.ImageScanFinding, severityLevelsToIgnor
8383
}
8484

8585
func IsFindingIgnored(finding types.ImageScanFinding, severityLevelsToIgnore []string, cveToIgnore []string) (bool, string) {
86-
for _, severityLevel := range severityLevelsToIgnore {
87-
if string(finding.Severity) == severityLevel {
88-
return true, "Ignored severyity level"
89-
}
86+
if slices.Contains(severityLevelsToIgnore, string(finding.Severity)) {
87+
return true, "Ignored severyity level"
9088
}
91-
for _, cve := range cveToIgnore {
92-
if finding.Name != nil && string(*finding.Name) == cve {
93-
return true, "Ignored individual CVE"
94-
}
89+
if finding.Name != nil && slices.Contains(cveToIgnore, *finding.Name) {
90+
return true, "Ignored individual CVE"
9591
}
9692
return false, ""
9793
}
9894

99-
// TODO: is there a better way?
10095
func AreSeverityLevelsValid(levels string) (bool, error) {
96+
validLevels := GetFindingSeverityLevelsAsList()
10197
for _, level := range strings.Fields(levels) {
102-
isValid := false
103-
for _, validLevel := range GetFindingSeverityLevelsAsList() {
104-
if level == validLevel {
105-
isValid = true
106-
}
107-
}
108-
if !isValid {
98+
if !slices.Contains(validLevels, level) {
10999
return false, fmt.Errorf("%s is not a valid finding severity level. Valid levels are: %s", level, GetFindingSeverityLevelsAsString())
110100
}
111101
}
@@ -117,10 +107,7 @@ func GetECRClient() (*ecr.Client, error) {
117107
if err != nil {
118108
return nil, err
119109
}
120-
121-
client := ecr.NewFromConfig(cfg)
122-
123-
return client, nil
110+
return ecr.NewFromConfig(cfg), nil
124111
}
125112

126113
func getAuthorizationToken(client *ecr.Client) ([]types.AuthorizationData, error) {
@@ -173,6 +160,14 @@ func GetECRRepo(registryName string) (reference.Named, error) {
173160
return reg, nil
174161
}
175162

163+
func newUnsupportedImageFinding(description string) []types.ImageScanFinding {
164+
return []types.ImageScanFinding{{
165+
Name: aws.String("ECR_ERROR_UNSUPPORTED_IMAGE"),
166+
Description: aws.String(description),
167+
Severity: types.FindingSeverityInformational,
168+
}}
169+
}
170+
176171
func GetImageScanResults(client *ecr.Client, imageId ImageId, ecrRepoName string, timeout time.Duration) ([]types.ImageScanFinding, error) {
177172
input := ecr.DescribeImageScanFindingsInput{
178173
ImageId: &types.ImageIdentifier{
@@ -210,11 +205,7 @@ func GetImageScanResults(client *ecr.Client, imageId ImageId, ecrRepoName string
210205
continue
211206
}
212207
// Exhausted retries - treat as unsupported image
213-
findings = []types.ImageScanFinding{{
214-
Name: aws.String("ECR_ERROR_UNSUPPORTED_IMAGE"),
215-
Description: aws.String("Image scan does not exist - image is not supported for scanning"),
216-
Severity: types.FindingSeverityInformational}}
217-
return findings, nil
208+
return newUnsupportedImageFinding("Image scan does not exist - image is not supported for scanning"), nil
218209
}
219210
// For non-ScanNotFound errors, fall through to legacy error handling
220211
break
@@ -232,11 +223,7 @@ func GetImageScanResults(client *ecr.Client, imageId ImageId, ecrRepoName string
232223
if failedOutput.ImageScanStatus.Status == types.ScanStatusFailed &&
233224
failedOutput.ImageScanStatus.Description != nil &&
234225
strings.Contains(*failedOutput.ImageScanStatus.Description, "UnsupportedImageError") {
235-
findings = []types.ImageScanFinding{{
236-
Name: aws.String("ECR_ERROR_UNSUPPORTED_IMAGE"),
237-
Description: failedOutput.ImageScanStatus.Description,
238-
Severity: types.FindingSeverityInformational}}
239-
return findings, nil
226+
return newUnsupportedImageFinding(*failedOutput.ImageScanStatus.Description), nil
240227
}
241228

242229
return nil, waiterErr

helpers.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,13 @@ limitations under the License.
1818
package main
1919

2020
func dedupList(inList []string) []string {
21-
// Use look up table to avoid iterating over the list
22-
// again and again
23-
lookUpTable := make(map[string]bool)
24-
outList := []string{}
21+
seen := make(map[string]struct{})
22+
result := []string{}
2523
for _, item := range inList {
26-
// if the item is in the look up table then we should
27-
// have it in the list
28-
if _, value := lookUpTable[item]; !value {
29-
lookUpTable[item] = true
30-
outList = append(outList, item)
24+
if _, exists := seen[item]; !exists {
25+
seen[item] = struct{}{}
26+
result = append(result, item)
3127
}
3228
}
33-
return outList
29+
return result
3430
}

junit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func WriteJunitReport(findings []types.ImageScanFinding, output io.Writer) error
5050
}
5151
test := parser.Test{
5252
Name: severity,
53-
Duration: time.Duration(1) * time.Second,
53+
Duration: time.Second,
5454
Result: result,
5555
Output: output,
5656
}

0 commit comments

Comments
 (0)