Skip to content

Commit 8aaf4e1

Browse files
authored
Add insecure flag to zarf package create (zarf-dev#500)
1 parent 6fb8cb4 commit 8aaf4e1

File tree

10 files changed

+45
-27
lines changed

10 files changed

+45
-27
lines changed

src/cmd/package.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ func init() {
9292

9393
packageCreateCmd.Flags().BoolVar(&config.DeployOptions.Confirm, "confirm", false, "Confirm package creation without prompting")
9494
packageCreateCmd.Flags().StringVar(&zarfImageCache, "zarf-cache", config.ZarfDefaultImageCachePath, "Specify the location of the Zarf image cache")
95-
packageCreateCmd.Flags().BoolVar(&config.SkipSBOM, "skip-sbom", false, "Skip generating SBOM for this package")
95+
packageCreateCmd.Flags().BoolVar(&config.CreateOptions.SkipSBOM, "skip-sbom", false, "Skip generating SBOM for this package")
96+
packageCreateCmd.Flags().BoolVar(&config.CreateOptions.Insecure, "insecure", false, "Allow insecure registry connections when pulling OCI images")
9697

9798
packageDeployCmd.Flags().BoolVar(&config.DeployOptions.Confirm, "confirm", false, "Confirm package deployment without prompting")
9899
packageDeployCmd.Flags().StringVar(&config.DeployOptions.Components, "components", "", "Comma-separated list of components to install. Adding this flag will skip the init prompts for which components to install")

src/cmd/tools.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"github.com/defenseunicorns/zarf/src/internal/utils"
1616
k9s "github.com/derailed/k9s/cmd"
1717
craneCmd "github.com/google/go-containerregistry/cmd/crane/cmd"
18-
"github.com/google/go-containerregistry/pkg/crane"
1918
"github.com/mholt/archiver/v3"
2019
"github.com/spf13/cobra"
2120
"github.com/spf13/cobra/doc"
@@ -151,7 +150,7 @@ func init() {
151150
archiverCmd.AddCommand(archiverCompressCmd)
152151
archiverCmd.AddCommand(archiverDecompressCmd)
153152

154-
cranePlatformOptions := []crane.Option{config.GetCraneOptions()}
153+
cranePlatformOptions := config.GetCraneOptions()
155154
registryCmd.AddCommand(craneCmd.NewCmdAuthLogin())
156155
registryCmd.AddCommand(craneCmd.NewCmdPull(&cranePlatformOptions))
157156
registryCmd.AddCommand(craneCmd.NewCmdPush(&cranePlatformOptions))

src/config/config.go

+29-18
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package config
33
import (
44
"fmt"
55
"os"
6+
"path/filepath"
67
"runtime"
78
"strings"
89
"time"
@@ -46,20 +47,19 @@ var (
4647
// CLIVersion track the version of the CLI
4748
CLIVersion = "unset"
4849

50+
// CreeateOptions tracks the user-defined options used to create the package
51+
CreateOptions types.ZarfCreateOptions
52+
4953
// DeployOptions tracks user-defined values for the active deployment
5054
DeployOptions types.ZarfDeployOptions
5155

5256
CliArch string
5357

5458
ZarfSeedPort string
5559

56-
// Do not process SBOM data
57-
SkipSBOM bool
58-
5960
// Private vars
60-
zarfImageCachePath = ZarfDefaultImageCachePath
61-
active types.ZarfPackage
62-
state types.ZarfState
61+
active types.ZarfPackage
62+
state types.ZarfState
6363
)
6464

6565
func IsZarfInitConfig() bool {
@@ -84,11 +84,23 @@ func GetArch() string {
8484
return runtime.GOARCH
8585
}
8686

87-
func GetCraneOptions() crane.Option {
88-
return crane.WithPlatform(&v1.Platform{
89-
OS: "linux",
90-
Architecture: GetArch(),
91-
})
87+
func GetCraneOptions() []crane.Option {
88+
var options []crane.Option
89+
90+
// Handle insecure registry option
91+
if CreateOptions.Insecure {
92+
options = append(options, crane.Insecure)
93+
}
94+
95+
// Add the image platform info
96+
options = append(options,
97+
crane.WithPlatform(&v1.Platform{
98+
OS: "linux",
99+
Architecture: GetArch(),
100+
}),
101+
)
102+
103+
return options
92104
}
93105

94106
func GetCraneAuthOption(username string, secret string) crane.Option {
@@ -200,16 +212,15 @@ func BuildConfig(path string) error {
200212
}
201213

202214
func SetImageCachePath(cachePath string) {
203-
zarfImageCachePath = cachePath
215+
CreateOptions.ImageCachePath = cachePath
204216
}
205217

206218
func GetImageCachePath() string {
207219
homePath, _ := os.UserHomeDir()
208-
if zarfImageCachePath == ZarfDefaultImageCachePath {
209-
return fmt.Sprintf("%s/%s", homePath, zarfImageCachePath)
210-
}
211-
if string(zarfImageCachePath[0]) == "~" {
212-
return fmt.Sprintf("%s/%s", homePath, zarfImageCachePath[len("~/"):])
220+
221+
if CreateOptions.ImageCachePath == "" {
222+
return filepath.Join(homePath, ZarfDefaultImageCachePath)
213223
}
214-
return zarfImageCachePath
224+
225+
return strings.Replace(CreateOptions.ImageCachePath, "~", homePath, 1)
215226
}

src/internal/images/copy.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
)
88

99
func Copy(src string, dest string) {
10-
if err := crane.Copy(src, dest, config.GetCraneOptions()); err != nil {
10+
if err := crane.Copy(src, dest, config.GetCraneOptions()...); err != nil {
1111
message.Fatal(err, "Unable to copy the image")
1212
}
1313
}

src/internal/images/pull.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func PullAll(buildImageList []string, imageTarballPath string) map[name.Tag]v1.I
4242

4343
for idx, src := range buildImageList {
4444
spinner.Updatef("Fetching image metadata (%d of %d): %s", idx+1, imageCount, src)
45-
img, err := crane.Pull(src, config.GetCraneOptions())
45+
img, err := crane.Pull(src, config.GetCraneOptions()...)
4646
if err != nil {
4747
spinner.Fatalf(err, "Unable to pull the image %s", src)
4848
}

src/internal/images/push.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func PushToZarfRegistry(imageTarballPath string, buildImageList []string) {
2424

2525
for _, src := range buildImageList {
2626
spinner.Updatef("Updating image %s", src)
27-
img, err := crane.LoadTag(imageTarballPath, src, config.GetCraneOptions())
27+
img, err := crane.LoadTag(imageTarballPath, src, config.GetCraneOptions()...)
2828
if err != nil {
2929
spinner.Errorf(err, "Unable to load the image from the update package")
3030
return

src/internal/packager/injector.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ func hasSeedImages(spinner *message.Spinner) bool {
221221
// After delay, try running
222222
default:
223223
// Check for the existence of the image in the injection pod registry, on error continue
224-
if _, err := crane.Manifest(ref, config.GetCraneOptions()); err != nil {
224+
if _, err := crane.Manifest(ref, config.GetCraneOptions()...); err != nil {
225225
message.Debugf("Could not get image ref %s: %v", ref, err)
226226
} else {
227227
// If not error, return true, there image is present

src/internal/packager/prepare.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ func FindImages(repoHelmChartPath string) {
170170
if sortedImages := k8s.SortImages(maybeImages, matchedImages); len(sortedImages) > 0 {
171171
var realImages []string
172172
for _, image := range sortedImages {
173-
if descriptor, err := crane.Head(image, config.GetCraneOptions()); err != nil {
173+
if descriptor, err := crane.Head(image, config.GetCraneOptions()...); err != nil {
174174
// Test if this is a real image, if not just quiet log to debug, this is normal
175175
message.Debugf("Suspected image does not appear to be valid: %w", err)
176176
} else {

src/internal/sbom/catalog.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ var tranformRegex = regexp.MustCompile(`(?m)[^a-zA-Z0-9\.\-]`)
2626

2727
func CatalogImages(tagToImage map[name.Tag]v1.Image, sbomDir, tarPath string) {
2828
// Ignore SBOM creation if there the flag is set
29-
if config.SkipSBOM {
29+
if config.CreateOptions.SkipSBOM {
3030
message.Debug("Skipping SBOM processing per --skip-sbom flag")
3131
return
3232
}

src/types/types.go

+7
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,13 @@ type ZarfDeployOptions struct {
165165
NodePort string
166166
}
167167

168+
// ZarfCreeateOptions tracks the user-defined options used to create the package
169+
type ZarfCreateOptions struct {
170+
SkipSBOM bool
171+
ImageCachePath string
172+
Insecure bool
173+
}
174+
168175
// ZarfImport structure for including imported zarf components
169176
type ZarfComponentImport struct {
170177
ComponentName string `yaml:"name,omitempty"`

0 commit comments

Comments
 (0)