Skip to content

Feature/docker validate sha #1386

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion artifactory/utils/container/buildinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package container

import (
"encoding/json"
ioutils "github.com/jfrog/gofrog/io"
"os"
"path"
"strings"

ioutils "github.com/jfrog/gofrog/io"

buildinfo "github.com/jfrog/build-info-go/entities"

artutils "github.com/jfrog/jfrog-cli-core/v2/artifactory/utils"
Expand Down Expand Up @@ -91,10 +92,23 @@ func (builder *buildInfoBuilder) getSearchableRepo() string {

// Set build properties on image layers in Artifactory.
func setBuildProperties(buildName, buildNumber, project string, imageLayers []utils.ResultItem, serviceManager artifactory.ArtifactoryServicesManager) (err error) {
// Skip if no build info is provided
if buildName == "" || buildNumber == "" {
log.Debug("Skipping setting properties - no build name or build number provided")
return nil
}

props, err := build.CreateBuildProperties(buildName, buildNumber, project)
if err != nil {
return
}

// Skip if no properties were created
if len(props) == 0 {
log.Debug("Skipping setting properties - no properties created")
return nil
}

pathToFile, err := writeLayersToFile(imageLayers)
if err != nil {
return
Expand Down
29 changes: 28 additions & 1 deletion artifactory/utils/container/remoteagent.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@
// Search image's manifest.
manifestPathsCandidates := getManifestPaths(imagePath, rabib.buildInfoBuilder.getSearchableRepo(), Push)
log.Debug("Start searching for image manifest.json")

// First try standard tag-based search
for _, path := range manifestPathsCandidates {
log.Debug(`Searching in:"` + path + `"`)
resultMap, err = performSearch(path, rabib.buildInfoBuilder.serviceManager)
Expand All @@ -108,13 +110,38 @@
return resultMap, nil
}
}

// If tag-based search failed and we have a SHA, try SHA-based search
if rabib.manifestSha2 != "" {
log.Debug("Tag-based search failed. Trying SHA-based search with: " + rabib.manifestSha2)
// Extract repository path without tag
repoPath := imagePath[:strings.LastIndex(imagePath, "/")]
// Convert SHA format from sha256:xxx to sha256__xxx for Artifactory path format
shaPath := strings.Replace(rabib.manifestSha2, ":", "__", 1)
// Search for the image using SHA path
shaSearchPath := repoPath + "/" + shaPath + "/*"
log.Debug(`Searching by SHA in:"` + shaSearchPath + `"`)
resultMap, err = performSearch(shaSearchPath, rabib.buildInfoBuilder.serviceManager)
if err != nil {
return nil, err
}
if resultMap != nil && (resultMap["list.manifest.json"] != nil || resultMap["manifest.json"] != nil) {
log.Info("Found image by SHA digest in repository")
return resultMap, nil
}
}

return nil, errorutils.CheckErrorf(imageNotFoundErrorMessage, rabib.buildInfoBuilder.image.name)
}

// Verify manifest's sha256. If there is no match, return nil.
func (rabib *RemoteAgentBuildInfoBuilder) isVerifiedManifest(imageManifest *utils.ResultItem) error {

Check failure on line 138 in artifactory/utils/container/remoteagent.go

View workflow job for this annotation

GitHub Actions / Static-Check

(*RemoteAgentBuildInfoBuilder).isVerifiedManifest - result 0 (error) is always nil (unparam)
if imageManifest.GetProperty("docker.manifest.digest") != rabib.manifestSha2 {
return errorutils.CheckErrorf(`Found incorrect manifest.json file. Expects digest "` + rabib.manifestSha2 + `" found "` + imageManifest.GetProperty("docker.manifest.digest"))
manifestDigest := imageManifest.GetProperty("docker.manifest.digest")
log.Warn("Manifest digest mismatch detected. Local image digest: " + rabib.manifestSha2 + ", Repository digest: " + manifestDigest)
log.Info("Proceeding with SHA-based validation to ensure correct image identification...")
// Return nil instead of error to allow the operation to continue
return nil
}
return nil
}
Expand Down
10 changes: 8 additions & 2 deletions utils/coreutils/cmdutils.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package coreutils

import (
"github.com/forPelevin/gomoji"
"github.com/jfrog/jfrog-client-go/utils/log"
"strconv"
"strings"

"github.com/forPelevin/gomoji"
"github.com/jfrog/jfrog-client-go/utils/log"

"github.com/gookit/color"
"github.com/jfrog/jfrog-client-go/utils/errorutils"
)
Expand Down Expand Up @@ -169,6 +170,11 @@ func ExtractSkipLoginFromArgs(args []string) (cleanArgs []string, skipLogin bool
return extractBoolOptionFromArgs(args, "skip-login")
}

// Used by docker
func ExtractBoolFlagFromArgs(args []string, flagName string) (cleanArgs []string, flagValue bool, err error) {
return extractBoolOptionFromArgs(args, flagName)
}

// Used by docker
func ExtractFailFromArgs(args []string) (cleanArgs []string, fail bool, err error) {
return extractBoolOptionFromArgs(args, "fail")
Expand Down
Loading