Exclude content digests from Chart.lock metadata hash

HashReq now strips per-dependency digest fields so the top-level lock
digest continues to detect Chart.yaml drift independently of tarball bytes.

Signed-off-by: Simone Tollardo <simone.tollardo@redhat.com>
pull/32243/head
Simone Tollardo 4 weeks ago
parent a565ae40ca
commit 6411a69ebb
No known key found for this signature in database
GPG Key ID: 1A829EE297D64CE7

@ -209,9 +209,13 @@ func (r *Resolver) Resolve(reqs []*chart.Dependency, repoNames map[string]string
// HashReq generates a hash of the dependencies.
//
// This should be used only to compare against another hash generated by this
// function.
// function. Per-dependency content digests are excluded; the top-level lock
// digest tracks Chart.yaml constraints and resolved metadata only.
func HashReq(req, lock []*chart.Dependency) (string, error) {
data, err := json.Marshal([2][]*chart.Dependency{req, lock})
data, err := json.Marshal([2][]*chart.Dependency{
dependenciesForMetadataHash(req),
dependenciesForMetadataHash(lock),
})
if err != nil {
return "", err
}
@ -219,6 +223,23 @@ func HashReq(req, lock []*chart.Dependency) (string, error) {
return "sha256:" + s, err
}
// dependenciesForMetadataHash returns a copy of deps with content digests cleared.
func dependenciesForMetadataHash(deps []*chart.Dependency) []*chart.Dependency {
if len(deps) == 0 {
return nil
}
out := make([]*chart.Dependency, len(deps))
for i, d := range deps {
if d == nil {
continue
}
cp := *d
cp.Digest = ""
out[i] = &cp
}
return out
}
// HashV2Req generates a hash of requirements generated in Helm v2.
//
// This should be used only to compare against another hash generated by the

@ -19,6 +19,9 @@ import (
"runtime"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
chart "helm.sh/helm/v4/pkg/chart/v2"
"helm.sh/helm/v4/pkg/registry"
)
@ -308,3 +311,21 @@ func TestGetLocalPath(t *testing.T) {
})
}
}
func TestHashReqIgnoresContentDigest(t *testing.T) {
req := []*chart.Dependency{
{Name: "alpine", Version: "0.1.0", Repository: "http://localhost:8879/charts"},
}
lockWithout := []*chart.Dependency{
{Name: "alpine", Version: "0.1.0", Repository: "http://localhost:8879/charts"},
}
lockWith := []*chart.Dependency{
{Name: "alpine", Version: "0.1.0", Repository: "http://localhost:8879/charts", Digest: "sha256:abc123"},
}
h1, err := HashReq(req, lockWithout)
require.NoError(t, err)
h2, err := HashReq(req, lockWith)
require.NoError(t, err)
assert.Equal(t, h1, h2)
}

Loading…
Cancel
Save