attestations: tidy up AddAttestation function

Signed-off-by: Justin Chadwell <me@jedevc.com>
This commit is contained in:
Justin Chadwell
2022-08-31 08:54:40 +01:00
parent 2254cfedac
commit 70e5300a76
2 changed files with 9 additions and 15 deletions

View File

@@ -6489,24 +6489,22 @@ func testExportAttestations(t *testing.T, sb integration.Sandbox) {
return nil, err
}
res.AddAttestation(pk, &result.InTotoAttestation{
PredicateRefKey: "foo",
PredicatePath: "/attestation.json",
PredicateType: "https://example.com/attestations/v1.0",
PredicatePath: "/attestation.json",
PredicateType: "https://example.com/attestations/v1.0",
Subjects: []result.InTotoSubject{
&result.InTotoSubjectSelf{},
},
}, map[string]gateway.Reference{"foo": refAttest})
}, refAttest)
res.AddAttestation(pk, &result.InTotoAttestation{
PredicateRefKey: "bar",
PredicatePath: "/attestation2.json",
PredicateType: "https://example.com/attestations2/v1.0",
PredicatePath: "/attestation2.json",
PredicateType: "https://example.com/attestations2/v1.0",
Subjects: []result.InTotoSubject{
&result.InTotoSubjectRaw{
Name: "/attestation.json",
Digest: []digest.Digest{successDigest},
},
},
}, map[string]gateway.Reference{"bar": refAttest})
}, refAttest)
}
dt, err := json.Marshal(expPlatforms)

View File

@@ -39,23 +39,19 @@ func (r *Result[T]) AddRef(k string, ref T) {
r.mu.Unlock()
}
func (r *Result[T]) AddAttestation(k string, v Attestation, refs map[string]T) {
func (r *Result[T]) AddAttestation(k string, v Attestation, ref T) {
r.mu.Lock()
if r.Attestations == nil {
r.Attestations = map[string][]Attestation{}
}
// if the attestation refs aren't already internal, then insert them
switch v := v.(type) {
case *InTotoAttestation:
if !strings.HasPrefix(v.PredicateRefKey, attestationRefPrefix) {
internalKey := attestationRefPrefix + identity.NewID()
r.Refs[internalKey] = refs[v.PredicateRefKey]
v.PredicateRefKey = internalKey
v.PredicateRefKey = "attestation:" + identity.NewID()
r.Refs[v.PredicateRefKey] = ref
}
}
r.Attestations[k] = append(r.Attestations[k], v)
r.mu.Unlock()
}