From 1a01779e5bc314e4c3b10e35d80a4fb903fe8f28 Mon Sep 17 00:00:00 2001 From: Justin Chadwell Date: Tue, 28 Mar 2023 11:40:43 +0100 Subject: [PATCH] buildflags: merge attest flags if disabled is set This ensures that `--sbom=` and `--attest type=sbom` can be appropriately merged for build, and `--sbom=` and `target.attest=["type=sbom"]` can be appropriately merged for bake. Signed-off-by: Justin Chadwell --- util/buildflags/attests.go | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/util/buildflags/attests.go b/util/buildflags/attests.go index 38370dfa..aef872c9 100644 --- a/util/buildflags/attests.go +++ b/util/buildflags/attests.go @@ -30,9 +30,16 @@ func ParseAttests(in []string) (map[string]*string, error) { k := "attest:" + attestType if _, ok := out[k]; ok { - return nil, errors.Errorf("duplicate attestation field %s", attestType) + if disabled == nil { + return nil, errors.Errorf("duplicate attestation field %s", attestType) + } + if *disabled { + out[k] = nil + } + continue } - if disabled { + + if disabled != nil && *disabled { out[k] = nil } else { out[k] = &in @@ -41,23 +48,23 @@ func ParseAttests(in []string) (map[string]*string, error) { return out, nil } -func parseAttest(in string) (string, bool, error) { +func parseAttest(in string) (string, *bool, error) { if in == "" { - return "", false, nil + return "", nil, nil } csvReader := csv.NewReader(strings.NewReader(in)) fields, err := csvReader.Read() if err != nil { - return "", false, err + return "", nil, err } attestType := "" - disabled := true + var disabled *bool for _, field := range fields { key, value, ok := strings.Cut(field, "=") if !ok { - return "", false, errors.Errorf("invalid value %s", field) + return "", nil, errors.Errorf("invalid value %s", field) } key = strings.TrimSpace(strings.ToLower(key)) @@ -65,14 +72,15 @@ func parseAttest(in string) (string, bool, error) { case "type": attestType = value case "disabled": - disabled, err = strconv.ParseBool(value) + b, err := strconv.ParseBool(value) if err != nil { - return "", false, err + return "", nil, err } + disabled = &b } } if attestType == "" { - return "", false, errors.Errorf("attestation type not specified") + return "", nil, errors.Errorf("attestation type not specified") } return attestType, disabled, nil