Skip to content

Commit 32bc73c

Browse files
committed
improved the code
1 parent ae9c166 commit 32bc73c

File tree

8 files changed

+67
-44
lines changed

8 files changed

+67
-44
lines changed

cli/scancommands.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -648,8 +648,8 @@ func getCurationCommand(c *components.Context) (*curation.CurationAuditCommand,
648648
SetInsecureTls(c.GetBoolFlagValue(flags.InsecureTls)).
649649
SetNpmScope(c.GetStringFlagValue(flags.DepType)).
650650
SetPipRequirementsFile(c.GetStringFlagValue(flags.RequirementsFile)).
651-
SetDockerImageName(c.GetStringFlagValue(flags.DockerImageName)).
652651
SetSolutionFilePath(c.GetStringFlagValue(flags.SolutionPath))
652+
curationAuditCommand.SetDockerImageName(c.GetStringFlagValue(flags.DockerImageName))
653653
return curationAuditCommand, nil
654654
}
655655

commands/audit/auditbasicparams.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ type AuditParamsInterface interface {
4949
AllowPartialResults() bool
5050
GetXrayVersion() string
5151
GetConfigProfile() *xscservices.ConfigProfile
52-
DockerImageName() string
53-
SetDockerImageName(dockerImageName string) *AuditBasicParams
5452
SolutionFilePath() string
5553
SetSolutionFilePath(solutionFilePath string) *AuditBasicParams
5654
}
@@ -83,7 +81,6 @@ type AuditBasicParams struct {
8381
xrayVersion string
8482
xscVersion string
8583
configProfile *xscservices.ConfigProfile
86-
dockerImageName string
8784
solutionFilePath string
8885
}
8986

@@ -337,15 +334,6 @@ func (abp *AuditBasicParams) GetConfigProfile() *xscservices.ConfigProfile {
337334
return abp.configProfile
338335
}
339336

340-
func (abp *AuditBasicParams) DockerImageName() string {
341-
return abp.dockerImageName
342-
}
343-
344-
func (abp *AuditBasicParams) SetDockerImageName(dockerImageName string) *AuditBasicParams {
345-
abp.dockerImageName = dockerImageName
346-
return abp
347-
}
348-
349337
func (abp *AuditBasicParams) SolutionFilePath() string {
350338
return abp.solutionFilePath
351339
}

commands/audit/auditparams.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,6 @@ func (params *AuditParams) ToBuildInfoBomGenParams() (bomParams technologies.Bui
232232
PipRequirementsFile: params.PipRequirementsFile(),
233233
// Pnpm params
234234
MaxTreeDepth: params.MaxTreeDepth(),
235-
// Docker params
236-
DockerImageName: params.DockerImageName(),
237235
}
238236
return
239237
}

commands/curation/curationaudit.go

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ type CurationAuditCommand struct {
219219
workingDirs []string
220220
OriginPath string
221221
parallelRequests int
222+
dockerImageName string
222223
audit.AuditParamsInterface
223224
}
224225

@@ -255,6 +256,15 @@ func (ca *CurationAuditCommand) SetParallelRequests(threads int) *CurationAuditC
255256
return ca
256257
}
257258

259+
func (ca *CurationAuditCommand) DockerImageName() string {
260+
return ca.dockerImageName
261+
}
262+
263+
func (ca *CurationAuditCommand) SetDockerImageName(dockerImageName string) *CurationAuditCommand {
264+
ca.dockerImageName = dockerImageName
265+
return ca
266+
}
267+
258268
func (ca *CurationAuditCommand) Run() (err error) {
259269
rootDir, err := os.Getwd()
260270
if err != nil {
@@ -353,6 +363,7 @@ func getPolicyAndConditionId(policy, condition string) string {
353363
func (ca *CurationAuditCommand) doCurateAudit(results map[string]*CurationReport) error {
354364
techs := techutils.DetectedTechnologiesList()
355365
if ca.DockerImageName() != "" {
366+
log.Debug(fmt.Sprintf("Docker image name '%s' was provided, running Docker curation audit.", ca.DockerImageName()))
356367
techs = []string{techutils.Docker.String()}
357368
}
358369
for _, tech := range techs {
@@ -968,7 +979,8 @@ func getUrlNameAndVersionByTech(tech techutils.Technology, node *xrayUtils.Graph
968979
downloadUrls, name, version = getNugetNameScopeAndVersion(node.Id, artiUrl, repo)
969980
return
970981
case techutils.Docker:
971-
return getDockerNameScopeAndVersion(node.Id, artiUrl, repo)
982+
downloadUrls, name, version = getDockerNameAndVersion(node.Id, artiUrl, repo)
983+
return
972984
}
973985
return
974986
}
@@ -1153,20 +1165,27 @@ func buildNpmDownloadUrl(url, repo, name, scope, version string) []string {
11531165
return []string{packageUrl}
11541166
}
11551167

1156-
func getDockerNameScopeAndVersion(id, artiUrl, repo string) (downloadUrls []string, name, scope, version string) {
1168+
func getDockerNameAndVersion(id, artiUrl, repo string) (downloadUrls []string, name, version string) {
11571169
if id == "" {
11581170
return
11591171
}
11601172

11611173
id = strings.TrimPrefix(id, "docker://")
11621174

1163-
if idx := strings.Index(id, ":sha256:"); idx > 0 {
1164-
name = id[:idx]
1165-
version = id[idx+1:]
1166-
} else if idx := strings.LastIndex(id, ":"); idx > 0 {
1167-
name = id[:idx]
1168-
version = id[idx+1:]
1169-
} else {
1175+
sha256Idx := strings.Index(id, ":sha256:")
1176+
tagIdx := strings.LastIndex(id, ":")
1177+
1178+
switch {
1179+
// Example: docker://nginx:sha256:abc123def456
1180+
case sha256Idx > 0:
1181+
name = id[:sha256Idx]
1182+
version = id[sha256Idx+1:]
1183+
// Example: docker://nginx:1.21
1184+
case tagIdx > 0:
1185+
name = id[:tagIdx]
1186+
version = id[tagIdx+1:]
1187+
// Example: docker://nginx (no tag specified, defaults to "latest")
1188+
default:
11701189
name = id
11711190
version = "latest"
11721191
}
@@ -1176,7 +1195,7 @@ func getDockerNameScopeAndVersion(id, artiUrl, repo string) (downloadUrls []stri
11761195
strings.TrimSuffix(artiUrl, "/"), repo, name, version)}
11771196
}
11781197

1179-
return downloadUrls, name, scope, version
1198+
return
11801199
}
11811200

11821201
func GetCurationOutputFormat(formatFlagVal string) (format outFormat.OutputFormat, err error) {

commands/curation/curationaudit_test.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,15 +1187,14 @@ func Test_getGemNameScopeAndVersion(t *testing.T) {
11871187
}
11881188
}
11891189

1190-
func Test_getDockerNameScopeAndVersion(t *testing.T) {
1190+
func Test_getDockerNameAndVersion(t *testing.T) {
11911191
tests := []struct {
11921192
name string
11931193
id string
11941194
artiUrl string
11951195
repo string
11961196
wantDownloadUrls []string
11971197
wantName string
1198-
wantScope string
11991198
wantVersion string
12001199
}{
12011200
{
@@ -1205,7 +1204,6 @@ func Test_getDockerNameScopeAndVersion(t *testing.T) {
12051204
repo: "docker-remote",
12061205
wantDownloadUrls: []string{"http://test.jfrog.io/artifactory/api/docker/docker-remote/v2/nginx/manifests/1.21.0"},
12071206
wantName: "nginx",
1208-
wantScope: "",
12091207
wantVersion: "1.21.0",
12101208
},
12111209
{
@@ -1215,7 +1213,6 @@ func Test_getDockerNameScopeAndVersion(t *testing.T) {
12151213
repo: "docker-remote",
12161214
wantDownloadUrls: []string{"http://test.jfrog.io/artifactory/api/docker/docker-remote/v2/registry.example.com/nginx/manifests/1.21.0"},
12171215
wantName: "registry.example.com/nginx",
1218-
wantScope: "",
12191216
wantVersion: "1.21.0",
12201217
},
12211218
{
@@ -1225,7 +1222,6 @@ func Test_getDockerNameScopeAndVersion(t *testing.T) {
12251222
repo: "docker-remote",
12261223
wantDownloadUrls: []string{"http://test.jfrog.io/artifactory/api/docker/docker-remote/v2/nginx/manifests/sha256:abc123def456"},
12271224
wantName: "nginx",
1228-
wantScope: "",
12291225
wantVersion: "sha256:abc123def456",
12301226
},
12311227
{
@@ -1235,16 +1231,14 @@ func Test_getDockerNameScopeAndVersion(t *testing.T) {
12351231
repo: "docker-remote",
12361232
wantDownloadUrls: []string{"http://test.jfrog.io/artifactory/api/docker/docker-remote/v2/nginx/manifests/latest"},
12371233
wantName: "nginx",
1238-
wantScope: "",
12391234
wantVersion: "latest",
12401235
},
12411236
}
12421237
for _, tt := range tests {
12431238
t.Run(tt.name, func(t *testing.T) {
1244-
gotDownloadUrls, gotName, gotScope, gotVersion := getDockerNameScopeAndVersion(tt.id, tt.artiUrl, tt.repo)
1239+
gotDownloadUrls, gotName, gotVersion := getDockerNameAndVersion(tt.id, tt.artiUrl, tt.repo)
12451240
assert.Equal(t, tt.wantDownloadUrls, gotDownloadUrls, "downloadUrls mismatch")
12461241
assert.Equal(t, tt.wantName, gotName, "name mismatch")
1247-
assert.Equal(t, tt.wantScope, gotScope, "scope mismatch")
12481242
assert.Equal(t, tt.wantVersion, gotVersion, "version mismatch")
12491243
})
12501244
}

curation_test.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"github.com/stretchr/testify/assert"
1515
"github.com/stretchr/testify/require"
1616

17-
"github.com/jfrog/jfrog-cli-security/cli"
1817
"github.com/jfrog/jfrog-cli-security/commands/curation"
1918
securityTests "github.com/jfrog/jfrog-cli-security/tests"
2019
securityTestUtils "github.com/jfrog/jfrog-cli-security/tests/utils"
@@ -114,16 +113,12 @@ func TestDockerCurationAudit(t *testing.T) {
114113
}
115114
cleanUp := integration.UseTestHomeWithDefaultXrayConfig(t)
116115
defer cleanUp()
117-
integration.CreateJfrogHomeConfig(t, "", securityTests.XrDetails, true)
118-
119-
testCli := integration.GetXrayTestCli(cli.GetJfrogCliSecurityApp(), false)
120116

121117
testImage := fmt.Sprintf("%s/%s/%s", *securityTests.ContainerRegistry, "docker-curation", "ganodndentcom/drupal")
122118

123-
output := testCli.WithoutCredentials().RunCliCmdWithOutput(t, "curation-audit",
119+
output := securityTests.PlatformCli.WithoutCredentials().RunCliCmdWithOutput(t, "curation-audit",
124120
"--image="+testImage,
125121
"--format="+string(format.Json))
126-
127122
bracketIndex := strings.Index(output, "[")
128123
require.GreaterOrEqual(t, bracketIndex, 0, "Expected JSON array in output, got: %s", output)
129124

sca/bom/buildinfo/technologies/docker/docker.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,7 @@ func ParseDockerImage(imageName string) (*DockerImageInfo, error) {
5656
}
5757

5858
info.Registry = parts[0]
59-
repo, image := parseRegistryAndExtract(info.Registry, parts[1])
60-
info.Repo = repo
61-
info.Image = image
59+
info.Repo, info.Image = parseRegistryAndExtract(info.Registry, parts[1])
6260

6361
log.Debug(fmt.Sprintf("Parsed Docker image - Registry: %s, Repo: %s, Image: %s, Tag: %s",
6462
info.Registry, info.Repo, info.Image, info.Tag))
@@ -121,6 +119,8 @@ func BuildDependencyTree(params technologies.BuildInfoBomGeneratorParams) ([]*xr
121119

122120
log.Debug(fmt.Sprintf("Docker image reference: %s", imageRef))
123121

122+
// Note: It does NOT extract the actual dependencies/packages inside the Docker image layers.
123+
// The graph contains just the image reference to verify if it's blocked by curation policies.
124124
rootId := imageInfo.Image + ":" + imageInfo.Tag
125125
return []*xrayUtils.GraphNode{{Id: rootId, Nodes: []*xrayUtils.GraphNode{{Id: imageRef}}}},
126126
[]string{imageRef}, nil
@@ -150,6 +150,7 @@ func getArchDigestUsingDocker(fullImageName string) (string, error) {
150150
localArch := parts[1]
151151

152152
log.Debug(fmt.Sprintf("Local platform: %s/%s", localOS, localArch))
153+
// In case the image is found locally, we need to get the digest of the image using the buildx.
153154

154155
buildxCmd := exec.Command("docker", "buildx", "imagetools", "inspect", fullImageName, "--raw")
155156
buildxOutput, buildxErr := buildxCmd.CombinedOutput()

sca/bom/buildinfo/technologies/docker/docker_test.go

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,32 @@ func TestBuildDependencyTree(t *testing.T) {
138138
name string
139139
dockerImageName string
140140
expectError bool
141+
errorContains string
141142
}{
142-
{name: "Empty image name", dockerImageName: "", expectError: true},
143-
{name: "No registry", dockerImageName: "image:tag", expectError: true},
143+
{
144+
name: "Empty image name",
145+
dockerImageName: "",
146+
expectError: true,
147+
errorContains: "docker image name is required",
148+
},
149+
{
150+
name: "No registry - single part image",
151+
dockerImageName: "nginx",
152+
expectError: true,
153+
errorContains: "invalid docker image format",
154+
},
155+
{
156+
name: "No registry - image with tag only",
157+
dockerImageName: "nginx:1.21",
158+
expectError: true,
159+
errorContains: "invalid docker image format",
160+
},
161+
{
162+
name: "Whitespace only",
163+
dockerImageName: " ",
164+
expectError: true,
165+
errorContains: "invalid docker image format",
166+
},
144167
}
145168

146169
for _, tt := range tests {
@@ -149,6 +172,11 @@ func TestBuildDependencyTree(t *testing.T) {
149172
_, _, err := BuildDependencyTree(params)
150173
if tt.expectError {
151174
assert.Error(t, err)
175+
if tt.errorContains != "" {
176+
assert.Contains(t, err.Error(), tt.errorContains)
177+
}
178+
} else {
179+
assert.NoError(t, err)
152180
}
153181
})
154182
}

0 commit comments

Comments
 (0)