Skip to content
This repository was archived by the owner on Jul 23, 2023. It is now read-only.

Commit 7632c00

Browse files
authored
Merge pull request #23 from armosec/improve-getters
get registry and repository from full name if missing
2 parents 36cb3d3 + e8f61a2 commit 7632c00

4 files changed

+233
-1
lines changed

containerscan/commonContainerScanSummaryResultMethods.go

+31-1
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,40 @@ func (summary *CommonContainerScanSummaryResult) GetStatus() string {
5151
}
5252

5353
func (summary *CommonContainerScanSummaryResult) GetRegistry() string {
54-
return summary.Registry
54+
if summary.Registry != "" {
55+
return summary.Registry
56+
}
57+
if summary.ImageID != "" {
58+
return getRegistryFromImageID(summary.ImageID)
59+
}
60+
61+
if summary.ImageTag != "" {
62+
return getRegistryFromImageID(summary.ImageTag)
63+
}
64+
return ""
65+
}
66+
67+
func (summary *CommonContainerScanSummaryResult) GetRepository() string {
68+
if summary.ImageID != "" {
69+
return getRepositoryFromImageID(summary.ImageID)
70+
}
71+
72+
if summary.ImageTag != "" {
73+
return getRepositoryFromImageID(summary.ImageTag)
74+
}
75+
return ""
5576
}
5677

5778
func (summary *CommonContainerScanSummaryResult) GetImageTageSuffix() string {
79+
if summary.ImageTagSuffix != "" {
80+
return summary.ImageTagSuffix
81+
}
82+
if summary.ImageID != "" {
83+
return getImageTagFromImageID(summary.ImageID)
84+
}
85+
if summary.ImageTag != "" {
86+
return getImageTagFromImageID(summary.ImageTag)
87+
}
5888
return summary.ImageTagSuffix
5989
}
6090

containerscan/image_id_parsers.go

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package containerscan
2+
3+
import (
4+
"strings"
5+
)
6+
7+
func getRegistryFromImageID(imageID string) string {
8+
var registryAndRepo string
9+
atParts := strings.Split(imageID, "@")
10+
11+
if len(atParts) > 1 {
12+
registryAndRepo = atParts[0]
13+
} else {
14+
colonParts := strings.Split(imageID, ":")
15+
if len(colonParts) == 0 {
16+
return ""
17+
}
18+
registryAndRepo = colonParts[0]
19+
}
20+
21+
registryAndRepoParts := strings.SplitN(registryAndRepo, "/", 2)
22+
23+
if len(registryAndRepoParts) < 2 {
24+
return ""
25+
}
26+
if strings.Contains(registryAndRepoParts[0], ".") {
27+
return registryAndRepoParts[0]
28+
}
29+
30+
return ""
31+
}
32+
33+
func getRepositoryFromImageID(imageID string) string {
34+
var registryAndRepo string
35+
atParts := strings.Split(imageID, "@")
36+
37+
if len(atParts) > 1 {
38+
registryAndRepo = atParts[0]
39+
} else {
40+
colonParts := strings.Split(imageID, ":")
41+
if len(colonParts) == 0 {
42+
return ""
43+
}
44+
registryAndRepo = colonParts[0]
45+
}
46+
if !strings.Contains(registryAndRepo, ".") {
47+
return registryAndRepo
48+
}
49+
50+
registryAndRepoParts := strings.SplitN(registryAndRepo, "/", 2)
51+
52+
if len(registryAndRepoParts) < 2 {
53+
return ""
54+
}
55+
56+
return registryAndRepoParts[1]
57+
}
58+
59+
func getImageTagFromImageID(imageID string) string {
60+
parts := strings.Split(imageID, ":")
61+
isHash := strings.Contains(imageID, "@")
62+
if isHash {
63+
parts = strings.Split(imageID, "@")
64+
}
65+
66+
// The tag is expected to be after the last colon
67+
if len(parts) > 1 {
68+
return parts[len(parts)-1]
69+
}
70+
71+
return "" // return an empty string if imageID doesn't contain a tag
72+
}
+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package containerscan
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestGetRegistryFromImageID(t *testing.T) {
8+
tests := []struct {
9+
name string
10+
imageID string
11+
expected string
12+
}{
13+
{
14+
name: "Valid image ID with hash",
15+
imageID: "docker.io/library/alpine@sha256:2345",
16+
expected: "docker.io",
17+
},
18+
{
19+
name: "Valid image ID with tag",
20+
imageID: "quay.io/kubescape/gateway:v0.1.13",
21+
expected: "quay.io",
22+
},
23+
{
24+
name: "No registry in image ID",
25+
imageID: "library/alpine@sha256:2345",
26+
expected: "",
27+
},
28+
{
29+
name: "Empty image ID",
30+
imageID: "",
31+
expected: "",
32+
},
33+
{
34+
name: "Image ID without hash",
35+
imageID: "my.registry.io/library/alpine",
36+
expected: "my.registry.io",
37+
},
38+
}
39+
40+
for _, tt := range tests {
41+
t.Run(tt.name, func(t *testing.T) {
42+
result := getRegistryFromImageID(tt.imageID)
43+
if result != tt.expected {
44+
t.Errorf("got %q, want %q", result, tt.expected)
45+
}
46+
})
47+
}
48+
}
49+
50+
func TestGetRepositoryFromImageID(t *testing.T) {
51+
tests := []struct {
52+
name string
53+
imageID string
54+
expected string
55+
}{
56+
{
57+
name: "Valid image ID with hash",
58+
imageID: "docker.io/library/alpine@sha256:2345",
59+
expected: "library/alpine",
60+
},
61+
{
62+
name: "Valid image ID with tag",
63+
imageID: "quay.io/kubescape/gateway:v0.1.13",
64+
expected: "kubescape/gateway",
65+
},
66+
{
67+
name: "No registry in image ID",
68+
imageID: "library/alpine@sha256:2345",
69+
expected: "library/alpine",
70+
},
71+
{
72+
name: "Empty image ID",
73+
imageID: "",
74+
expected: "",
75+
},
76+
{
77+
name: "Image ID without hash",
78+
imageID: "my.registry.io/library/alpine",
79+
expected: "library/alpine",
80+
},
81+
}
82+
83+
for _, tt := range tests {
84+
t.Run(tt.name, func(t *testing.T) {
85+
result := getRepositoryFromImageID(tt.imageID)
86+
if result != tt.expected {
87+
t.Errorf("got %q, want %q", result, tt.expected)
88+
}
89+
})
90+
}
91+
}
92+
93+
func TestGetImageTagFromImageID(t *testing.T) {
94+
tests := []struct {
95+
name string
96+
imageID string
97+
expected string
98+
}{
99+
{
100+
name: "Valid image ID with hash",
101+
imageID: "docker.io/library/alpine@sha256:2345",
102+
expected: "sha256:2345",
103+
},
104+
{
105+
name: "Valid image ID with tag",
106+
imageID: "quay.io/kubescape/gateway:v0.1.13",
107+
expected: "v0.1.13",
108+
},
109+
{
110+
name: "No tag in image ID",
111+
imageID: "docker.io/library/alpine",
112+
expected: "",
113+
},
114+
{
115+
name: "Empty image ID",
116+
imageID: "",
117+
expected: "",
118+
},
119+
}
120+
121+
for _, tt := range tests {
122+
t.Run(tt.name, func(t *testing.T) {
123+
result := getImageTagFromImageID(tt.imageID)
124+
if result != tt.expected {
125+
t.Errorf("got %q, want %q", result, tt.expected)
126+
}
127+
})
128+
}
129+
}

containerscan/interfaces.go

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ type ContainerScanSummaryResult interface {
3939
GetContainerName() string
4040
GetStatus() string
4141
GetRegistry() string
42+
GetRepository() string
4243
GetImageTageSuffix() string
4344
GetVersion() string
4445
GetCustomerGUID() string

0 commit comments

Comments
 (0)