Skip to content

Commit 128acdb

Browse files
authored
Merge pull request #3027 from LaurentGoderre/fix-attest-extra-args
Fix attest extra arguments
2 parents 411d3f8 + b06bddf commit 128acdb

File tree

4 files changed

+56
-9
lines changed

4 files changed

+56
-9
lines changed

bake/hcl_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ func TestHCLAttrsCapsuleType(t *testing.T) {
608608
target "app" {
609609
attest = [
610610
{ type = "provenance", mode = "max" },
611-
"type=sbom,disabled=true",
611+
"type=sbom,disabled=true,generator=foo,\"ENV1=bar,baz\",ENV2=hello",
612612
]
613613
614614
cache-from = [
@@ -641,7 +641,7 @@ func TestHCLAttrsCapsuleType(t *testing.T) {
641641
require.NoError(t, err)
642642

643643
require.Equal(t, 1, len(c.Targets))
644-
require.Equal(t, []string{"type=provenance,mode=max", "type=sbom,disabled=true"}, stringify(c.Targets[0].Attest))
644+
require.Equal(t, []string{"type=provenance,mode=max", "type=sbom,disabled=true,\"ENV1=bar,baz\",ENV2=hello,generator=foo"}, stringify(c.Targets[0].Attest))
645645
require.Equal(t, []string{"type=local,dest=../out", "type=oci,dest=../out.tar"}, stringify(c.Targets[0].Outputs))
646646
require.Equal(t, []string{"type=local,src=path/to/cache", "user/app:cache"}, stringify(c.Targets[0].CacheFrom))
647647
require.Equal(t, []string{"type=local,dest=path/to/cache"}, stringify(c.Targets[0].CacheTo))

util/buildflags/attests.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,8 @@ func (a *Attest) UnmarshalText(text []byte) error {
148148
if !ok {
149149
return errors.Errorf("invalid value %s", field)
150150
}
151-
key = strings.TrimSpace(strings.ToLower(key))
152151

153-
switch key {
152+
switch strings.TrimSpace(strings.ToLower(key)) {
154153
case "type":
155154
a.Type = value
156155
case "disabled":

util/buildflags/attests_test.go

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,21 @@ func TestAttests(t *testing.T) {
1313
attests := Attests{
1414
{Type: "provenance", Attrs: map[string]string{"mode": "max"}},
1515
{Type: "sbom", Disabled: true},
16+
{Type: "sbom", Attrs: map[string]string{
17+
"generator": "scanner",
18+
"ENV1": `"foo,bar"`,
19+
"Env2": "hello",
20+
}},
1621
}
1722

18-
expected := `[{"type":"provenance","mode":"max"},{"type":"sbom","disabled":true}]`
23+
expected := `[{"type":"provenance","mode":"max"},{"type":"sbom","disabled":true},{"ENV1":"\"foo,bar\"","Env2":"hello","generator":"scanner","type":"sbom"}]`
1924
actual, err := json.Marshal(attests)
2025
require.NoError(t, err)
2126
require.JSONEq(t, expected, string(actual))
2227
})
2328

2429
t.Run("UnmarshalJSON", func(t *testing.T) {
25-
in := `[{"type":"provenance","mode":"max"},{"type":"sbom","disabled":true}]`
30+
in := `[{"type":"provenance","mode":"max"},{"type":"sbom","disabled":true},{"ENV1":"\"foo,bar\"","Env2":"hello","generator":"scanner","type":"sbom"}]`
2631

2732
var actual Attests
2833
err := json.Unmarshal([]byte(in), &actual)
@@ -31,6 +36,11 @@ func TestAttests(t *testing.T) {
3136
expected := Attests{
3237
{Type: "provenance", Attrs: map[string]string{"mode": "max"}},
3338
{Type: "sbom", Disabled: true, Attrs: map[string]string{}},
39+
{Type: "sbom", Disabled: false, Attrs: map[string]string{
40+
"generator": "scanner",
41+
"ENV1": `"foo,bar"`,
42+
"Env2": "hello",
43+
}},
3444
}
3545
require.Equal(t, expected, actual)
3646
})
@@ -41,7 +51,14 @@ func TestAttests(t *testing.T) {
4151
"type": cty.StringVal("provenance"),
4252
"mode": cty.StringVal("max"),
4353
}),
54+
cty.ObjectVal(map[string]cty.Value{
55+
"type": cty.StringVal("sbom"),
56+
"generator": cty.StringVal("scan"),
57+
"ENV1": cty.StringVal(`foo,bar`),
58+
"Env2": cty.StringVal(`hello`),
59+
}),
4460
cty.StringVal("type=sbom,disabled=true"),
61+
cty.StringVal(`type=sbom,generator=scan,"FOO=bar,baz",Hello=World`),
4562
})
4663

4764
var actual Attests
@@ -50,7 +67,17 @@ func TestAttests(t *testing.T) {
5067

5168
expected := Attests{
5269
{Type: "provenance", Attrs: map[string]string{"mode": "max"}},
70+
{Type: "sbom", Attrs: map[string]string{
71+
"generator": "scan",
72+
"ENV1": "foo,bar",
73+
"Env2": "hello",
74+
}},
5375
{Type: "sbom", Disabled: true, Attrs: map[string]string{}},
76+
{Type: "sbom", Attrs: map[string]string{
77+
"generator": "scan",
78+
"FOO": "bar,baz",
79+
"Hello": "World",
80+
}},
5481
}
5582
require.Equal(t, expected, actual)
5683
})
@@ -59,6 +86,11 @@ func TestAttests(t *testing.T) {
5986
attests := Attests{
6087
{Type: "provenance", Attrs: map[string]string{"mode": "max"}},
6188
{Type: "sbom", Disabled: true},
89+
{Type: "sbom", Attrs: map[string]string{
90+
"generator": "scan",
91+
"ENV1": `"foo,bar"`,
92+
"Env2": "hello",
93+
}},
6294
}
6395

6496
actual := attests.ToCtyValue()
@@ -71,6 +103,12 @@ func TestAttests(t *testing.T) {
71103
"type": cty.StringVal("sbom"),
72104
"disabled": cty.StringVal("true"),
73105
}),
106+
cty.MapVal(map[string]cty.Value{
107+
"type": cty.StringVal("sbom"),
108+
"generator": cty.StringVal("scan"),
109+
"ENV1": cty.StringVal(`"foo,bar"`),
110+
"Env2": cty.StringVal("hello"),
111+
}),
74112
})
75113

76114
result := actual.Equals(expected)

util/buildflags/export.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package buildflags
22

33
import (
4+
"encoding/csv"
45
"encoding/json"
56
"maps"
67
"regexp"
@@ -259,9 +260,18 @@ func (w *csvBuilder) Write(key, value string) {
259260
if w.sb.Len() > 0 {
260261
w.sb.WriteByte(',')
261262
}
262-
w.sb.WriteString(key)
263-
w.sb.WriteByte('=')
264-
w.sb.WriteString(value)
263+
264+
pair := key + "=" + value
265+
if strings.ContainsRune(pair, ',') || strings.ContainsRune(pair, '"') {
266+
var attr strings.Builder
267+
writer := csv.NewWriter(&attr)
268+
writer.Write([]string{pair})
269+
writer.Flush()
270+
// Strips the extra newline added by the csv writer
271+
pair = strings.TrimSpace(attr.String())
272+
}
273+
274+
w.sb.WriteString(pair)
265275
}
266276

267277
func (w *csvBuilder) WriteAttributes(attrs map[string]string) {

0 commit comments

Comments
 (0)