Skip to content

Commit

Permalink
Change platform selection logic (#189)
Browse files Browse the repository at this point in the history
This reverts commit d7551b7.

Defaulting to platform for all providers resulted in syft having unnecessary
when pulling an image that had a particular digest, if that digest didn't match
the architecture of the host running the pull. Revert commit
d7551b7, which introduced that error, but then
add platform defaulting logic back for the OCI Registry Provider, since
defaulting there has been specifically requested. Add integ tests to cover the
new behavior. Also, update integ tests to use the manifest ID for assertsion,
since the RepoDigests array can be empty.

Signed-off-by: Will Murphy <will.murphy@anchore.com>
  • Loading branch information
willmurphyscode authored Jun 9, 2023
1 parent e14bc44 commit 5b5049b
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 241 deletions.
51 changes: 25 additions & 26 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,13 @@ func GetImageFromSource(ctx context.Context, imgStr string, source image.Source,
func selectImageProvider(imgStr string, source image.Source, cfg config) (image.Provider, error) {
var provider image.Provider
tempDirGenerator := rootTempDirGenerator.NewGenerator()

if err := setPlatform(source, &cfg, runtime.GOARCH); err != nil {
return nil, err
}
platformSelectionUnsupported := fmt.Errorf("specified platform=%q however image source=%q does not support selecting platform", cfg.Platform.String(), source.String())

switch source {
case image.DockerTarballSource:
if cfg.Platform != nil {
return nil, platformSelectionUnsupported
}
// note: the imgStr is the path on disk to the tar file
provider = docker.NewProviderFromTarball(imgStr, tempDirGenerator)
case image.DockerDaemonSource:
Expand All @@ -130,43 +130,42 @@ func selectImageProvider(imgStr string, source image.Source, cfg config) (image.
return nil, err
}
case image.OciDirectorySource:
if cfg.Platform != nil {
return nil, platformSelectionUnsupported
}
provider = oci.NewProviderFromPath(imgStr, tempDirGenerator)
case image.OciTarballSource:
if cfg.Platform != nil {
return nil, platformSelectionUnsupported
}
provider = oci.NewProviderFromTarball(imgStr, tempDirGenerator)
case image.OciRegistrySource:
defaultPlatformIfNil(&cfg)
provider = oci.NewProviderFromRegistry(imgStr, tempDirGenerator, cfg.Registry, cfg.Platform)
case image.SingularitySource:
if cfg.Platform != nil {
return nil, platformSelectionUnsupported
}
provider = sif.NewProviderFromPath(imgStr, tempDirGenerator)
default:
return nil, fmt.Errorf("unable to determine image source")
}
return provider, nil
}

func setPlatform(source image.Source, cfg *config, defaultArch string) error {
// we should override the platform based on the host architecture if the user did not specify a platform
// see https://github.com/anchore/stereoscope/issues/149 for more details
defaultPlatform, err := image.NewPlatform(defaultArch)
if err != nil {
log.WithFields("error", err).Warnf("unable to set default platform to %q", runtime.GOARCH)
defaultPlatform = nil
}

switch source {
case image.DockerTarballSource, image.OciDirectorySource, image.OciTarballSource, image.SingularitySource:
if cfg.Platform != nil {
return fmt.Errorf("specified platform=%q however image source=%q does not support selecting platform", cfg.Platform.String(), source.String())
}

case image.DockerDaemonSource, image.PodmanDaemonSource, image.OciRegistrySource:
if cfg.Platform == nil {
cfg.Platform = defaultPlatform
// defaultPlatformIfNil sets the platform to use the host's architecture
// if no platform was specified. The OCI registry provider uses "linux/amd64"
// as a hard-coded default platform, which has surprised customers
// running stereoscope on non-amd64 hosts. If platform is already
// set on the config, or the code can't generate a matching platform,
// do nothing.
func defaultPlatformIfNil(cfg *config) {
if cfg.Platform == nil {
p, err := image.NewPlatform(fmt.Sprintf("linux/%s", runtime.GOARCH))
if err == nil {
cfg.Platform = p
}

default:
return fmt.Errorf("unable to determine image source to select platform")
}
return nil
}

// GetImage parses the user provided image string and provides an image object;
Expand Down
193 changes: 0 additions & 193 deletions client_test.go

This file was deleted.

Loading

0 comments on commit 5b5049b

Please sign in to comment.