@@ -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
3940func 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
8585func 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?
10095func 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
126113func 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+
176171func 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
0 commit comments