Skip to content

Commit a6bb5f0

Browse files
committed
remove inline function to verify helm charts in builder
Signed-off-by: Sanskar Jaiswal <[email protected]>
1 parent 51a5e77 commit a6bb5f0

File tree

3 files changed

+25
-56
lines changed

3 files changed

+25
-56
lines changed

internal/helm/chart/builder_local.go

+14-32
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
"github.com/Masterminds/semver/v3"
2727
securejoin "github.com/cyphar/filepath-securejoin"
2828
"helm.sh/helm/v3/pkg/chart/loader"
29-
"helm.sh/helm/v3/pkg/provenance"
3029
"sigs.k8s.io/yaml"
3130

3231
"github.com/fluxcd/pkg/runtime/transform"
@@ -107,21 +106,6 @@ func (b *localChartBuilder) Build(ctx context.Context, ref Reference, p string,
107106
requiresPackaging := isChartDir || opts.VersionMetadata != "" || len(opts.GetValuesFiles()) != 0
108107

109108
var provFilePath string
110-
verifyProvFile := func(chart, provFile string) (*provenance.Verification, error) {
111-
if opts.Keyring != nil {
112-
if _, err := os.Stat(provFile); err != nil {
113-
err = fmt.Errorf("could not load provenance file %s: %w", provFile, err)
114-
return nil, &BuildError{Reason: ErrProvenanceVerification, Err: err}
115-
}
116-
ver, err := verifyChartWithProvFile(bytes.NewReader(opts.Keyring), chart, provFile)
117-
if err != nil {
118-
err = fmt.Errorf("failed to verify helm chart using provenance file: %w", err)
119-
return nil, &BuildError{Reason: ErrProvenanceVerification, Err: err}
120-
}
121-
return ver, nil
122-
}
123-
return nil, nil
124-
}
125109

126110
// If all the following is true, we do not need to package the chart:
127111
// - Chart name from cached chart matches resolved name
@@ -135,16 +119,14 @@ func (b *localChartBuilder) Build(ctx context.Context, ref Reference, p string,
135119
if result.Name == curMeta.Name && result.Version == curMeta.Version {
136120
// We can only verify a cached chart with provenance file if we didn't
137121
// package the chart ourselves, and instead stored it as is.
138-
if !requiresPackaging {
122+
if !requiresPackaging && opts.Keyring != nil {
139123
provFilePath = provenanceFilePath(opts.CachedChart)
140-
ver, err := verifyProvFile(opts.CachedChart, provFilePath)
124+
ver, err := verifyChartWithProvFile(bytes.NewReader(opts.Keyring), opts.CachedChart, provFilePath)
141125
if err != nil {
142-
return nil, err
143-
}
144-
if ver != nil {
145-
result.VerificationSignature = buildVerificationSig(ver)
146-
result.ProvFilePath = provFilePath
126+
return nil, &BuildError{Reason: ErrProvenanceVerification, Err: err}
147127
}
128+
result.VerificationSignature = buildVerificationSig(ver)
129+
result.ProvFilePath = provFilePath
148130
}
149131
result.Path = opts.CachedChart
150132
result.ValuesFiles = opts.GetValuesFiles()
@@ -158,18 +140,18 @@ func (b *localChartBuilder) Build(ctx context.Context, ref Reference, p string,
158140
// If the chart at the path is already packaged and no custom values files
159141
// options are set, we can copy the chart without making modifications
160142
if !requiresPackaging {
161-
provFilePath = provenanceFilePath(p)
162143
if err = copyFileToPath(localRef.Path, p); err != nil {
163144
return result, &BuildError{Reason: ErrChartPull, Err: err}
164145
}
165-
if err = copyFileToPath(provenanceFilePath(localRef.Path), provFilePath); err != nil {
166-
return result, &BuildError{Reason: ErrChartPull, Err: err}
167-
}
168-
ver, err := verifyProvFile(localRef.Path, provFilePath)
169-
if err != nil {
170-
return nil, err
171-
}
172-
if ver != nil {
146+
if opts.Keyring != nil {
147+
provFilePath = provenanceFilePath(p)
148+
if err = copyFileToPath(provenanceFilePath(localRef.Path), provFilePath); err != nil {
149+
return result, &BuildError{Reason: ErrChartPull, Err: err}
150+
}
151+
ver, err := verifyChartWithProvFile(bytes.NewReader(opts.Keyring), localRef.Path, provFilePath)
152+
if err != nil {
153+
return nil, err
154+
}
173155
result.ProvFilePath = provFilePath
174156
result.VerificationSignature = buildVerificationSig(ver)
175157
}

internal/helm/chart/builder_remote.go

+8-24
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
helmchart "helm.sh/helm/v3/pkg/chart"
2828
"helm.sh/helm/v3/pkg/chart/loader"
2929
"helm.sh/helm/v3/pkg/chartutil"
30-
"helm.sh/helm/v3/pkg/provenance"
3130
"sigs.k8s.io/yaml"
3231

3332
"github.com/fluxcd/pkg/runtime/transform"
@@ -107,18 +106,6 @@ func (b *remoteChartBuilder) Build(_ context.Context, ref Reference, p string, o
107106

108107
requiresPackaging := len(opts.GetValuesFiles()) != 0 || opts.VersionMetadata != ""
109108

110-
verifyProvFile := func(chart, provFile string) (*provenance.Verification, error) {
111-
if opts.Keyring != nil {
112-
ver, err := verifyChartWithProvFile(bytes.NewReader(opts.Keyring), chart, provFile)
113-
if err != nil {
114-
err = fmt.Errorf("failed to verify helm chart using provenance file %s: %w", provFile, err)
115-
return nil, &BuildError{Reason: ErrProvenanceVerification, Err: err}
116-
}
117-
return ver, nil
118-
}
119-
return nil, nil
120-
}
121-
122109
var provFilePath string
123110

124111
// If all the following is true, we do not need to download and/or build the chart:
@@ -133,16 +120,14 @@ func (b *remoteChartBuilder) Build(_ context.Context, ref Reference, p string, o
133120
if result.Name == curMeta.Name && result.Version == curMeta.Version {
134121
// We can only verify a cached chart with provenance file if we didn't
135122
// package the chart ourselves, and instead stored it as is.
136-
if !requiresPackaging {
123+
if !requiresPackaging && opts.Keyring != nil {
137124
provFilePath = provenanceFilePath(opts.CachedChart)
138-
ver, err := verifyProvFile(opts.CachedChart, provFilePath)
125+
ver, err := verifyChartWithProvFile(bytes.NewReader(opts.Keyring), opts.CachedChart, provFilePath)
139126
if err != nil {
140127
return nil, err
141128
}
142-
if ver != nil {
143-
result.ProvFilePath = provFilePath
144-
result.VerificationSignature = buildVerificationSig(ver)
145-
}
129+
result.ProvFilePath = provFilePath
130+
result.VerificationSignature = buildVerificationSig(ver)
146131
}
147132
result.Path = opts.CachedChart
148133
result.ValuesFiles = opts.GetValuesFiles()
@@ -178,14 +163,13 @@ func (b *remoteChartBuilder) Build(_ context.Context, ref Reference, p string, o
178163
if err != nil {
179164
return nil, err
180165
}
181-
ver, err := verifyProvFile(chart.Name(), provFilePath)
166+
ver, err := verifyChartWithProvFile(bytes.NewReader(opts.Keyring), chart.Name(), provFilePath)
167+
182168
if err != nil {
183169
return nil, err
184170
}
185-
if ver != nil {
186-
result.ProvFilePath = provFilePath
187-
result.VerificationSignature = buildVerificationSig(ver)
188-
}
171+
result.ProvFilePath = provFilePath
172+
result.VerificationSignature = buildVerificationSig(ver)
189173
}
190174

191175
// Use literal chart copy from remote if no custom values files options are

internal/helm/chart/verify.go

+3
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ func verifyChartWithProvFile(keyring io.Reader, chartPath, provFilePath string)
5555

5656
sig := &provenance.Signatory{KeyRing: ring}
5757
verification, err := sig.Verify(chartPath, provFilePath)
58+
if err != nil {
59+
err = fmt.Errorf("failed to verify helm chart using provenance file: %w", err)
60+
}
5861
return verification, err
5962
}
6063

0 commit comments

Comments
 (0)