Skip to content

Commit 4487529

Browse files
committed
improved the code
1 parent 33ee5b1 commit 4487529

File tree

1 file changed

+30
-12
lines changed
  • sca/bom/buildinfo/technologies/docker

1 file changed

+30
-12
lines changed

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

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/jfrog/jfrog-cli-core/v2/common/project"
1111
"github.com/jfrog/jfrog-cli-core/v2/utils/config"
1212
"github.com/jfrog/jfrog-cli-security/sca/bom/buildinfo/technologies"
13+
"github.com/jfrog/jfrog-cli-security/utils/artifactory"
1314
"github.com/jfrog/jfrog-client-go/utils/log"
1415
xrayUtils "github.com/jfrog/jfrog-client-go/xray/services/utils"
1516
)
@@ -135,24 +136,34 @@ func getArchDigestUsingDocker(fullImageName string) (string, error) {
135136
}
136137
return "", fmt.Errorf("docker pull failed: %s", strings.TrimSpace(string(pullOutput)))
137138
}
138-
// IF IMAGE EXISTS LOCALLY
139-
inspectCmd := exec.Command("docker", "inspect", fullImageName, "--format", "{{.Os}} {{.Architecture}}")
139+
// IF Image exists locally, we need to get the digest of the image for the specific OS/architecture
140+
localOS, localArch, err := getLocalPlatform(fullImageName)
141+
if err != nil {
142+
return "", nil
143+
}
144+
145+
return findDigestForPlatform(fullImageName, localOS, localArch)
146+
}
147+
148+
// Retrieves the OS and architecture of a locally pulled Docker image.
149+
func getLocalPlatform(imageName string) (os, arch string, err error) {
150+
inspectCmd := exec.Command("docker", "inspect", imageName, "--format", "{{.Os}} {{.Architecture}}")
140151
inspectOutput, inspectErr := inspectCmd.CombinedOutput()
141152
if inspectErr != nil {
142153
log.Error(fmt.Sprintf("docker inspect failed: %v", inspectErr))
143-
return "", nil
154+
return "", "", inspectErr
144155
}
145156
parts := strings.Fields(strings.TrimSpace(string(inspectOutput)))
146157
if len(parts) != 2 {
147-
return "", nil
158+
return "", "", fmt.Errorf("unexpected inspect output format")
148159
}
149-
localOS := parts[0]
150-
localArch := parts[1]
151-
152-
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.
160+
log.Debug(fmt.Sprintf("Local platform: %s/%s", parts[0], parts[1]))
161+
return parts[0], parts[1], nil
162+
}
154163

155-
buildxCmd := exec.Command("docker", "buildx", "imagetools", "inspect", fullImageName, "--raw")
164+
// Retrieves the digest for a specific OS/architecture from the image manifest.
165+
func findDigestForPlatform(imageName, targetOS, targetArch string) (string, error) {
166+
buildxCmd := exec.Command("docker", "buildx", "imagetools", "inspect", imageName, "--raw")
156167
buildxOutput, buildxErr := buildxCmd.CombinedOutput()
157168
if buildxErr != nil {
158169
return "", fmt.Errorf("docker buildx imagetools inspect failed: %s", strings.TrimSpace(string(buildxOutput)))
@@ -165,13 +176,13 @@ func getArchDigestUsingDocker(fullImageName string) (string, error) {
165176
}
166177

167178
for _, m := range manifest.Manifests {
168-
if m.Platform.OS == localOS && m.Platform.Architecture == localArch {
179+
if m.Platform.OS == targetOS && m.Platform.Architecture == targetArch {
169180
log.Debug(fmt.Sprintf("Found arch-specific digest: %s", m.Digest))
170181
return m.Digest, nil
171182
}
172183
}
173184

174-
log.Debug(fmt.Sprintf("No matching manifest found for %s/%s", localOS, localArch))
185+
log.Debug(fmt.Sprintf("No matching manifest found for %s/%s", targetOS, targetArch))
175186
return "", nil
176187
}
177188

@@ -192,6 +203,13 @@ func GetDockerRepositoryConfig(imageName string) (*project.RepositoryConfig, err
192203
if err != nil {
193204
return nil, err
194205
}
206+
exists, err := artifactory.IsRepoExists(imageInfo.Repo, serverDetails)
207+
if err != nil {
208+
return nil, fmt.Errorf("Failed to check if repository '%s' exists on Artifactory '%s': %w", imageInfo.Repo, serverDetails.Url, err)
209+
}
210+
if !exists {
211+
return nil, fmt.Errorf("repository '%s' was not found on Artifactory (%s). Ensure the repository exists.", imageInfo.Repo, serverDetails.Url)
212+
}
195213

196214
repoConfig := &project.RepositoryConfig{}
197215
repoConfig.SetServerDetails(serverDetails).SetTargetRepo(imageInfo.Repo)

0 commit comments

Comments
 (0)