Skip to content

Commit f361001

Browse files
committed
Remove hardcoded refs from ociartifact code
Fixes: https://issues.redhat.com/browse/RUN-3578 Signed-off-by: Nicola Sella <[email protected]>
1 parent 4a2e52f commit f361001

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed

pkg/machine/ocipull/ociartifact.go

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ import (
1010
"runtime"
1111
"strings"
1212

13+
"github.com/containers/podman/v5/pkg/api/handlers/utils"
1314
"github.com/containers/podman/v5/pkg/machine/compression"
1415
"github.com/containers/podman/v5/pkg/machine/define"
15-
"github.com/containers/podman/v5/utils"
16+
podmanutils "github.com/containers/podman/v5/utils"
1617
"github.com/opencontainers/go-digest"
1718
specV1 "github.com/opencontainers/image-spec/specs-go/v1"
1819
"github.com/sirupsen/logrus"
20+
"go.podman.io/common/pkg/config"
1921
"go.podman.io/image/v5/docker"
2022
"go.podman.io/image/v5/docker/reference"
2123
"go.podman.io/image/v5/image"
@@ -24,9 +26,6 @@ import (
2426
)
2527

2628
const (
27-
artifactRegistry = "quay.io"
28-
artifactRepo = "podman"
29-
artifactImageName = "machine-os"
3029
artifactOriginalName = specV1.AnnotationTitle
3130
machineOS = "linux"
3231
)
@@ -52,6 +51,24 @@ type DiskArtifactOpts struct {
5251
os string
5352
}
5453

54+
// getMachineImageConfig retrieves the machine image configuration from c/common
55+
func getMachineImageConfig() (string, error) {
56+
cfg, err := config.Default()
57+
if err != nil {
58+
return "", fmt.Errorf("failed to load configuration: %w", err)
59+
}
60+
61+
return cfg.Machine.Image, nil
62+
}
63+
64+
func validateMachineImageConfig(imageRef string) (string, error) {
65+
if err := utils.IsRegistryReference(imageRef); err != nil {
66+
return "", fmt.Errorf("invalid machine image reference: %w", err)
67+
}
68+
69+
return imageRef, nil
70+
}
71+
5572
/*
5673
5774
This interface is for automatically pulling a disk artifact(qcow2, raw, vhdx file) from a pre-determined
@@ -92,8 +109,17 @@ func NewOCIArtifactPull(ctx context.Context, dirs *define.MachineDirs, endpoint
92109

93110
cache := false
94111
if endpoint == "" {
95-
imageName := artifactImageName
96-
endpoint = fmt.Sprintf("docker://%s/%s/%s:%s", artifactRegistry, artifactRepo, imageName, artifactVersion.majorMinor())
112+
// Get machine image from configuration
113+
machineImage, err := getMachineImageConfig()
114+
if err != nil {
115+
return nil, fmt.Errorf("failed to get machine image configuration: %w", err)
116+
}
117+
118+
if _, err := validateMachineImageConfig(machineImage); err != nil {
119+
return nil, fmt.Errorf("machine image ref could be misconfigured in containers.conf: %w", err)
120+
}
121+
122+
endpoint = fmt.Sprintf("docker://%s:%s", machineImage, artifactVersion.majorMinor())
97123
cache = true
98124
}
99125

@@ -191,7 +217,7 @@ func (o *OCIArtifactDisk) cleanCache(cachedImagePath string) func() {
191217
for _, file := range files {
192218
path := filepath.Join(o.dirs.ImageCacheDir.GetPath(), file.Name())
193219
logrus.Debugf("cleaning cached file: %s", path)
194-
err := utils.GuardedRemoveAll(path)
220+
err := podmanutils.GuardedRemoveAll(path)
195221
if err != nil && !errors.Is(err, os.ErrNotExist) {
196222
logrus.Warn("failed to clean machine image cache: ", err)
197223
}
@@ -290,7 +316,7 @@ func (o *OCIArtifactDisk) unpack(diskArtifactHash digest.Digest) error {
290316
}
291317

292318
// Clean up the oci dir which is no longer needed
293-
return utils.GuardedRemoveAll(blobDir.GetPath())
319+
return podmanutils.GuardedRemoveAll(blobDir.GetPath())
294320
}
295321

296322
func (o *OCIArtifactDisk) decompress() error {

0 commit comments

Comments
 (0)