Skip to content

Commit 16bcf8f

Browse files
authored
Merge branch 'main' into added-hauler-copy-cmd
2 parents 8c8a113 + b75f9a2 commit 16bcf8f

12 files changed

Lines changed: 527 additions & 206 deletions

File tree

cmd/hauler/cli/store.go

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package cli
33
import (
44
"fmt"
55
"os"
6-
"strconv"
76

87
"github.com/spf13/cobra"
98
"helm.sh/helm/v4/pkg/action"
@@ -78,11 +77,18 @@ func addStoreSync(rso *flags.StoreRootOpts, ro *flags.CliRootOpts) *cobra.Comman
7877
if o.CaFile == "" {
7978
o.CaFile = os.Getenv(consts.CaFile)
8079
}
81-
if o.InsecureSkipTLSVerify == nil {
82-
if v := os.Getenv(consts.InsecureSkipTLSVerify); v != "" {
83-
b, _ := strconv.ParseBool(v)
84-
o.InsecureSkipTLSVerify = &b
85-
}
80+
81+
// record which precedence-carrying flags the user explicitly set, so
82+
// the resolvers can let an explicit CLI value win over per-item/annotation
83+
o.TlogChanged = cmd.Flags().Changed("use-tlog-verify")
84+
o.ExcludeExtrasChanged = cmd.Flags().Changed("exclude-extras")
85+
o.InsecureChanged = cmd.Flags().Changed("insecure-skip-tls-verify")
86+
o.StoreChanged = cmd.Flags().Changed("store")
87+
o.RetriesChanged = cmd.Flags().Changed("retries")
88+
89+
// env var only applies when the flag wasn't set, so an explicit --insecure-skip-tls-verify=false still wins
90+
if !o.InsecureChanged && os.Getenv(consts.InsecureSkipTLSVerify) == "true" {
91+
o.InsecureSkipTLSVerify = true
8692
}
8793

8894
// --dry-run requires --products
@@ -124,12 +130,6 @@ func addStoreSync(rso *flags.StoreRootOpts, ro *flags.CliRootOpts) *cobra.Comman
124130
}
125131
rso.BlobConcurrency = bc
126132

127-
// resolve *bool: nil unless the user explicitly passed the flag
128-
if cmd.Flags().Changed("insecure-skip-tls-verify") {
129-
v, _ := cmd.Flags().GetBool("insecure-skip-tls-verify")
130-
o.InsecureSkipTLSVerify = &v
131-
}
132-
133133
return nil
134134
},
135135
RunE: func(cmd *cobra.Command, args []string) error {
@@ -411,11 +411,9 @@ func addStoreAddImage(rso *flags.StoreRootOpts, ro *flags.CliRootOpts) *cobra.Co
411411
if o.CaFile == "" {
412412
o.CaFile = os.Getenv(consts.CaFile)
413413
}
414-
if o.InsecureSkipTLSVerify == nil {
415-
if v := os.Getenv(consts.InsecureSkipTLSVerify); v != "" {
416-
b, _ := strconv.ParseBool(v)
417-
o.InsecureSkipTLSVerify = &b
418-
}
414+
// env var only applies when the flag wasn't set, so an explicit --insecure-skip-tls-verify=false still wins
415+
if !cmd.Flags().Changed("insecure-skip-tls-verify") && os.Getenv(consts.InsecureSkipTLSVerify) == "true" {
416+
o.InsecureSkipTLSVerify = true
419417
}
420418
return nil
421419
},

cmd/hauler/cli/store/add.go

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func AddFileCmd(ctx context.Context, o *flags.AddFileOpts, s *store.Layout, refe
5959
cfg := v1.File{
6060
Path: reference,
6161
CaFile: o.CaFile,
62-
InsecureSkipTLSVerify: &o.InsecureSkipTLSVerify,
62+
InsecureSkipTLSVerify: o.InsecureSkipTLSVerify,
6363
}
6464
if len(o.Name) > 0 {
6565
cfg.Name = o.Name
@@ -83,7 +83,7 @@ func storeFile(ctx context.Context, s *store.Layout, fi v1.File, ro *flags.CliRo
8383

8484
copts := getter.ClientOptions{
8585
NameOverride: fi.Name,
86-
InsecureSkipTLSVerify: derefInsecure(fi.InsecureSkipTLSVerify),
86+
InsecureSkipTLSVerify: fi.InsecureSkipTLSVerify,
8787
CAFile: fi.CaFile,
8888
}
8989

@@ -248,15 +248,15 @@ func AddImageCmd(ctx context.Context, o *flags.AddImageOpts, s *store.Layout, re
248248
func addImageVerifyConfig(o *flags.AddImageOpts) cosign.Config {
249249
switch {
250250
case o.Key != "":
251-
return cosign.Config{Key: o.Key, Tlog: o.Tlog, InsecureSkipTLSVerify: derefInsecure(o.InsecureSkipTLSVerify), CaFile: o.CaFile}
251+
return cosign.Config{Key: o.Key, Tlog: o.Tlog, InsecureSkipTLSVerify: o.InsecureSkipTLSVerify, CaFile: o.CaFile}
252252
case o.CertIdentityRegexp != "" || o.CertIdentity != "":
253253
return cosign.Config{
254254
CertIdentity: o.CertIdentity,
255255
CertIdentityRegexp: o.CertIdentityRegexp,
256256
CertOidcIssuer: o.CertOidcIssuer,
257257
CertOidcIssuerRegexp: o.CertOidcIssuerRegexp,
258258
CertGithubWorkflowRepository: o.CertGithubWorkflowRepository,
259-
InsecureSkipTLSVerify: derefInsecure(o.InsecureSkipTLSVerify),
259+
InsecureSkipTLSVerify: o.InsecureSkipTLSVerify,
260260
CaFile: o.CaFile,
261261
}
262262
default:
@@ -480,7 +480,7 @@ func storeImage(ctx context.Context, s *store.Layout, i v1.Image, platform strin
480480
return err
481481
}
482482

483-
insecureSkipTLSVerify := derefInsecure(i.InsecureSkipTLSVerify)
483+
insecureSkipTLSVerify := i.InsecureSkipTLSVerify
484484
caFile := i.CaFile
485485

486486
log.BaseFromContext(ctx).Debugf("resolving image [%s] (verified=%t, platform=%q, excludeExtras=%t, insecureSkipTLSVerify=%t, caFile=%q, rewrite=%q, digest=%q)", i.Name, verified, platform, excludeExtras, insecureSkipTLSVerify, caFile, rewrite, pinnedDigest)
@@ -619,7 +619,13 @@ func rewriteReference(ctx context.Context, s *store.Layout, oldRef name.Referenc
619619
// index.docker.io. Preserve the original registry when the source is non-docker.
620620
if newRegistry == "index.docker.io" && !strings.HasPrefix(rawRewrite, "docker.io") && !strings.HasPrefix(rawRewrite, "index.docker.io") {
621621
newRegistry = oldRegistry
622-
newRepo = strings.TrimPrefix(newRepo, "library/") //if rewrite has library/ prefix in path it is stripped off unless registry specified in rewrite
622+
rewriteRepo := strings.TrimPrefix(rawRewrite, "/")
623+
if i := strings.LastIndex(rewriteRepo, ":"); i != -1 {
624+
rewriteRepo = rewriteRepo[:i]
625+
}
626+
if !strings.HasPrefix(rewriteRepo, "library/") {
627+
newRepo = strings.TrimPrefix(newRepo, "library/")
628+
}
623629
}
624630
oldTotal := oldRepo + ":" + oldTag
625631
newTotal := newRepo + ":" + newTag
@@ -777,11 +783,11 @@ type chartJob struct {
777783
//
778784
// The three precedence rules are not uniform. registry is CLI > annotation.
779785
// excludeExtras is a one-way switch that any of the three sources can flip on
780-
// and none can flip off. platform is per-chart > CLI > annotation: an explicit
781-
// --platform is run-time intent and outranks manifest metadata. That last rule
782-
// must stay identical to resolveImageJobs's, or a single `hauler store sync`
783-
// run would pull a chart's discovered images for a different platform than the
784-
// manifest's own Images section.
786+
// and none can flip off, since a plain bool has no unset state. platform is
787+
// CLI > per-chart > annotation. That last rule must stay identical to
788+
// resolveImageJobs's, or a single `hauler store sync` run would pull a
789+
// chart's discovered images for a different platform than the manifest's own
790+
// Images section.
785791
//
786792
// Every job allocates its own *action.ChartPathOptions. flags.AddChartOpts
787793
// holds that as a pointer, so copying the struct alone would leave sibling
@@ -795,20 +801,15 @@ func resolveChartJobs(o *flags.SyncOpts, annotations map[string]string, manifest
795801

796802
jobs := make([]chartJob, 0, len(charts))
797803
for _, ch := range charts {
798-
excludeExtras := o.ExcludeExtras
799-
if !o.ExcludeExtras && annotations[consts.ImageAnnotationExcludeExtras] == "true" {
800-
excludeExtras = true
801-
}
802-
if ch.ExcludeExtras {
803-
excludeExtras = ch.ExcludeExtras
804-
}
804+
excludeExtras := resolveBoolFlag(ch.ExcludeExtras, annotations[consts.ImageAnnotationExcludeExtras] == "true", o.ExcludeExtras, o.ExcludeExtrasChanged)
805805

806806
platform := o.Platform
807-
if o.Platform == "" && annotations[consts.ImageAnnotationPlatform] != "" {
808-
platform = annotations[consts.ImageAnnotationPlatform]
809-
}
810-
if ch.Platform != "" {
811-
platform = ch.Platform
807+
if o.Platform == "" {
808+
if ch.Platform != "" {
809+
platform = ch.Platform
810+
} else if annotations[consts.ImageAnnotationPlatform] != "" {
811+
platform = annotations[consts.ImageAnnotationPlatform]
812+
}
812813
}
813814

814815
var valuesFiles []string
@@ -826,16 +827,13 @@ func resolveChartJobs(o *flags.SyncOpts, annotations map[string]string, manifest
826827
if caFile == "" {
827828
if ch.CaFile != "" {
828829
caFile = ch.CaFile
829-
} else if annotations[consts.ImageAnnotationCaFile] == "true" {
830+
} else if annotations[consts.ImageAnnotationCaFile] != "" {
830831
caFile = annotations[consts.ImageAnnotationCaFile]
831832
}
832833
}
833834

834-
insecureSkipTLSVerify := false
835-
if o.CaFile == "" {
836-
insecureSkipTLSVerify = resolveInsecure(ch.InsecureSkipTLSVerify, annotations, o.InsecureSkipTLSVerify)
837-
} else {
838-
}
835+
// a CA file and skipping TLS verification are mutually exclusive: providing one forces verification on
836+
insecureSkipTLSVerify := o.CaFile == "" && resolveBoolFlag(ch.InsecureSkipTLSVerify, annotations[consts.ImageAnnotationInsecureSkipTLSVerify] == "true", o.InsecureSkipTLSVerify, o.InsecureChanged)
839837

840838
jobs = append(jobs, chartJob{
841839
cfg: ch,
@@ -1381,12 +1379,11 @@ func fetchChart(ctx context.Context, s *store.Layout, j chartJob, tempRoot strin
13811379
// there is no separate per-discovered-image TLS knob in a chart
13821380
// manifest, so the registry a chart's images live in is assumed
13831381
// to share the chart repo's trust configuration.
1384-
chartInsecure := j.opts.ChartOpts.InsecureSkipTLSVerify
13851382
imageJobs = append(imageJobs, imageJob{
13861383
img: v1.Image{
13871384
Name: relocated,
13881385
CaFile: j.opts.ChartOpts.CaFile,
1389-
InsecureSkipTLSVerify: &chartInsecure,
1386+
InsecureSkipTLSVerify: j.opts.ChartOpts.InsecureSkipTLSVerify,
13901387
},
13911388
platform: j.opts.Platform,
13921389
excludeExtras: j.opts.ExcludeExtras,
@@ -1461,6 +1458,7 @@ func fetchChart(ctx context.Context, s *store.Layout, j chartJob, tempRoot strin
14611458
// rewrite. A rewrite that omits a tag inherits ref's.
14621459
func rewriteChartReference(ctx context.Context, s *store.Layout, ref name.Reference, rewrite string) error {
14631460
rewrite = strings.TrimPrefix(rewrite, "/")
1461+
rawRewrite := rewrite
14641462
newRef, err := name.ParseReference(rewrite)
14651463
if err != nil {
14661464
// error... don't continue with a bad reference
@@ -1483,6 +1481,13 @@ func rewriteChartReference(ctx context.Context, s *store.Layout, ref name.Refere
14831481
// rename chart name in store
14841482
oldRepo := ref.Context().RepositoryStr()
14851483
newRepo := newRef.Context().RepositoryStr()
1484+
rewriteRepo := rawRewrite
1485+
if i := strings.LastIndex(rewriteRepo, ":"); i != -1 {
1486+
rewriteRepo = rewriteRepo[:i]
1487+
}
1488+
if !strings.HasPrefix(rewriteRepo, "library/") {
1489+
newRepo = strings.TrimPrefix(newRepo, "library/")
1490+
}
14861491
newTag := newRef.Identifier()
14871492
if tag, ok := newRef.(name.Tag); ok {
14881493
newTag = tag.TagStr()

0 commit comments

Comments
 (0)