Skip to content

Commit

Permalink
Fix broken JSON in test snapshots (#827)
Browse files Browse the repository at this point in the history
* replace IMAGE_SIGNATURE_JSON with the actual keys (keyid and signature)
* replace ATTESTATION_SIGNATURE_JSON with the keys (keyid and signature)
* pretty prints the json in validate_image.snaps 
* Todo: Need to fix the exceptions in snaps.go MatchSnapshot function.
  • Loading branch information
joejstuart authored Jul 19, 2023
1 parent e2a3714 commit 86c3cf3
Show file tree
Hide file tree
Showing 4 changed files with 2,028 additions and 274 deletions.
16 changes: 8 additions & 8 deletions acceptance/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,26 +230,26 @@ func setupKeys(ctx context.Context, vars map[string]string, environment []string
}

func setupSigs(ctx context.Context, vars map[string]string, environment []string) ([]string, map[string]string, error) {
type valFunc func(context.Context) (map[string]string, error)
type valFunc func(context.Context, string) (map[string]string, error)

setVar := func(name string, v valFunc) error {
val, err := v(ctx)
vals, err := v(ctx, name)
if err != nil {
return err
}

for n, v := range val {
vars[fmt.Sprintf("%s_%s", name, n)] = v
for n, v := range vals {
vars[n] = v
}

return nil
}

for n, v := range map[string]valFunc{
"ATTESTATION_SIGNATURES_JSON": image.JSONAttestationSignaturesFrom,
"ATTESTATION_SIGNATURES_XML": image.XMLAttestationSignaturesFrom,
"IMAGE_SIGNATURES_JSON": image.JSONImageSignaturesFrom,
"IMAGE_SIGNATURES_XML": image.XMLImageSignaturesFrom,
"ATTESTATION_SIGNATURE": image.JSONAttestationSignaturesFrom,
"ATTESTATION_SIGNATURES_XML": image.XMLAttestationSignaturesFrom,
"IMAGE_SIGNATURE": image.JSONImageSignaturesFrom,
"IMAGE_SIGNATURES_XML": image.XMLImageSignaturesFrom,
} {
if err := setVar(n, v); err != nil {
return environment, vars, err
Expand Down
30 changes: 16 additions & 14 deletions acceptance/image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ func unmarshallSignatures(rawCosignSignature []byte) (*cosign.Signatures, error)

// JSONAttestationSignaturesFrom returns the list of attestation signatures found in the context in
// JSON format. If not found, and empty JSON array is returned.
func JSONAttestationSignaturesFrom(ctx context.Context) (map[string]string, error) {
func JSONAttestationSignaturesFrom(ctx context.Context, prefix string) (map[string]string, error) {
if !testenv.HasState[imageState](ctx) {
return nil, nil
}
Expand All @@ -701,17 +701,18 @@ func JSONAttestationSignaturesFrom(ctx context.Context) (map[string]string, erro

signatures := map[string]string{}
for name, signature := range state.AttestationSignatures {
json, err := json.Marshal(signature)
if err != nil {
return nil, err
if signature.KeyID != "" {
signatures[fmt.Sprintf("%s_KEY_ID_%s", prefix, name)] = signature.KeyID
}
if signature.Signature != "" {
signatures[fmt.Sprintf("%s_%s", prefix, name)] = signature.Signature
}
signatures[name] = string(json)
}

return signatures, nil
}

func XMLAttestationSignaturesFrom(ctx context.Context) (map[string]string, error) {
func XMLAttestationSignaturesFrom(ctx context.Context, prefix string) (map[string]string, error) {
if !testenv.HasState[imageState](ctx) {
return nil, nil
}
Expand Down Expand Up @@ -744,7 +745,7 @@ func XMLAttestationSignaturesFrom(ctx context.Context) (map[string]string, error
if err != nil {
return nil, err
}
ret[name] = string(xml)
ret[fmt.Sprintf("%s_%s", prefix, name)] = string(xml)
}

return ret, nil
Expand All @@ -765,7 +766,7 @@ func RawAttestationSignaturesFrom(ctx context.Context) map[string]string {
return ret
}

func JSONImageSignaturesFrom(ctx context.Context) (map[string]string, error) {
func JSONImageSignaturesFrom(ctx context.Context, prefix string) (map[string]string, error) {
if !testenv.HasState[imageState](ctx) {
return nil, nil
}
Expand All @@ -774,17 +775,18 @@ func JSONImageSignaturesFrom(ctx context.Context) (map[string]string, error) {

ret := map[string]string{}
for name, signature := range state.ImageSignatures {
json, err := json.Marshal(signature)
if err != nil {
return nil, err
if signature.KeyID != "" {
ret[fmt.Sprintf("%s_KEY_ID_%s", prefix, name)] = signature.KeyID
}
if signature.Signature != "" {
ret[fmt.Sprintf("%s_%s", prefix, name)] = signature.Signature
}
ret[name] = string(json)
}

return ret, nil
}

func XMLImageSignaturesFrom(ctx context.Context) (map[string]string, error) {
func XMLImageSignaturesFrom(ctx context.Context, prefix string) (map[string]string, error) {
if !testenv.HasState[imageState](ctx) {
return nil, nil
}
Expand Down Expand Up @@ -817,7 +819,7 @@ func XMLImageSignaturesFrom(ctx context.Context) (map[string]string, error) {
if err != nil {
return nil, err
}
ret[name] = string(xml)
ret[fmt.Sprintf("%s_%s", prefix, name)] = string(xml)
}

return ret, nil
Expand Down
10 changes: 9 additions & 1 deletion acceptance/snaps/snaps.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,15 @@ func MatchSnapshot(ctx context.Context, qualifier, text string, vars map[string]
errs := capture(ctx, qualifier)

for k, v := range vars {
text = strings.ReplaceAll(text, v, "${"+k+"}")
// Todo: remove ASAP. Just a hack until a solution is figured out
if k == "IMAGE_SIGNATURES_XML_acceptance/image" {
continue
}
if k == "known_PUBLIC_KEY_JSON" || k == "unknown_PUBLIC_KEY_JSON" {
text = strings.ReplaceAll(text, v, "\"${"+k+"}\"")
} else {
text = strings.ReplaceAll(text, v, "${"+k+"}")
}
}

// replace any remaining timestamps
Expand Down
Loading

0 comments on commit 86c3cf3

Please sign in to comment.