Skip to content

Commit 01dbbdd

Browse files
committed
feat(adv): new FSPutter constructor for automatic encode config
Signed-off-by: Dan Luhring <dluhring@chainguard.dev>
1 parent 99931d8 commit 01dbbdd

File tree

8 files changed

+147
-0
lines changed

8 files changed

+147
-0
lines changed

pkg/advisory/fs_putter.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
cgaid "github.com/chainguard-dev/advisory-schema/pkg/advisory"
1212
v2 "github.com/chainguard-dev/advisory-schema/pkg/advisory/v2"
13+
"github.com/chainguard-dev/yam/pkg/util"
1314
"github.com/chainguard-dev/yam/pkg/yam/formatted"
1415
"github.com/wolfi-dev/wolfictl/pkg/configs/rwfs"
1516
)
@@ -58,6 +59,33 @@ func NewFSPutter(fsys rwfs.FS, enc DocumentEncoder) *FSPutter {
5859
}
5960
}
6061

62+
// NewFSPutterWithAutomaticEncoder creates and returns a new FSPutter. It
63+
// determines the encoder configuration by attempting to use the `.yam.yaml`
64+
// file at the root of the given `fsys`. If none is available, a default
65+
// configuration is used for the encoder.
66+
func NewFSPutterWithAutomaticEncoder(fsys rwfs.FS) *FSPutter {
67+
// We'll set defaults to be used if we can't open and use the config file.
68+
encodeOptions := formatted.EncodeOptions{
69+
Indent: 2,
70+
GapExpressions: []string{".", ".advisories"},
71+
}
72+
73+
// Best-effort attempt to read the config file. If any we hit any errors, we're
74+
// totally fine using the above defaults.
75+
76+
cfgFile, err := fsys.Open(util.ConfigFileName)
77+
if err == nil {
78+
defer cfgFile.Close()
79+
80+
encodeOptionsFromFsys, err := formatted.ReadConfigFrom(cfgFile)
81+
if err == nil {
82+
encodeOptions = *encodeOptionsFromFsys
83+
}
84+
}
85+
86+
return NewFSPutter(fsys, NewYamDocumentEncoder(encodeOptions))
87+
}
88+
6189
func (p FSPutter) Upsert(_ context.Context, request Request) (string, error) {
6290
if request.Package == "" {
6391
return "", ErrEmptyPackage

pkg/advisory/fs_putter_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,47 @@ func TestFSPutter_Upsert(t *testing.T) {
159159
})
160160
}
161161
}
162+
163+
func TestNewFSPutterWithAutomaticEncoder(t *testing.T) {
164+
cases := []string{
165+
"with-config-file",
166+
"without-config-file",
167+
}
168+
169+
testTime := v2.Timestamp(time.Date(2025, 3, 11, 2, 40, 18, 0, time.UTC))
170+
171+
for _, tt := range cases {
172+
t.Run(tt, func(t *testing.T) {
173+
ctx := t.Context()
174+
testDir := filepath.Join("testdata", "fs_putter", "automatic-encoder", tt)
175+
176+
fsys, err := testerfs.New(os.DirFS(testDir))
177+
require.NoError(t, err, "creating test filesystem")
178+
179+
p := NewFSPutterWithAutomaticEncoder(fsys) // (we want to test this constructor specifically)
180+
require.NotNil(t, p, "creating FSPutter")
181+
182+
req := Request{
183+
Package: "foo",
184+
AdvisoryID: "CGA-xvg9-g29c-rr68",
185+
Event: v2.Event{
186+
Timestamp: testTime,
187+
Type: v2.EventTypeFixed,
188+
Data: v2.Fixed{
189+
FixedVersion: "1.2.3-r4",
190+
},
191+
},
192+
}
193+
194+
// We're upserting just so that we can see if `NewFSPutterWithAutomaticEncoder`
195+
// set up the encoder correctly (when YAML data is written to the filesystem).
196+
197+
_, err = p.Upsert(ctx, req)
198+
require.NoError(t, err, "upserting advisory data")
199+
200+
if diff := fsys.DiffAll(); diff != "" {
201+
t.Errorf("filesystem in an unexpected state (-want +got):\n%s", diff)
202+
}
203+
})
204+
}
205+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
gap:
2+
- ".advisories[].aliases"
3+
indent: 2
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
gap:
2+
- ".advisories[].aliases"
3+
indent: 2
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
schema-version: 2.0.2
2+
3+
package:
4+
name: foo
5+
6+
advisories:
7+
- id: CGA-xvg9-g29c-rr68
8+
aliases:
9+
- CVE-2016-7075
10+
- GHSA-7w66-j2r2-vm3
11+
events:
12+
- timestamp: 2025-03-10T17:55:22Z
13+
type: false-positive-determination
14+
data:
15+
type: component-vulnerability-mismatch
16+
note: Vulnerability only impacts OpenShift
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
schema-version: 2.0.2
2+
package:
3+
name: foo
4+
advisories:
5+
- id: CGA-xvg9-g29c-rr68
6+
aliases:
7+
- CVE-2016-7075
8+
9+
- GHSA-7w66-j2r2-vm3
10+
events:
11+
- timestamp: 2025-03-10T17:55:22Z
12+
type: false-positive-determination
13+
data:
14+
type: component-vulnerability-mismatch
15+
note: Vulnerability only impacts OpenShift
16+
- timestamp: 2025-03-11T02:40:18Z
17+
type: fixed
18+
data:
19+
fixed-version: 1.2.3-r4
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
schema-version: 2.0.2
2+
package:
3+
name: foo
4+
advisories:
5+
- id: CGA-xvg9-g29c-rr68
6+
aliases:
7+
- CVE-2016-7075
8+
- GHSA-7w66-j2r2-vm3
9+
events:
10+
- timestamp: 2025-03-10T17:55:22Z
11+
type: false-positive-determination
12+
data:
13+
type: component-vulnerability-mismatch
14+
note: Vulnerability only impacts OpenShift
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
schema-version: 2.0.2
2+
3+
package:
4+
name: foo
5+
6+
advisories:
7+
- id: CGA-xvg9-g29c-rr68
8+
aliases:
9+
- CVE-2016-7075
10+
- GHSA-7w66-j2r2-vm3
11+
events:
12+
- timestamp: 2025-03-10T17:55:22Z
13+
type: false-positive-determination
14+
data:
15+
type: component-vulnerability-mismatch
16+
note: Vulnerability only impacts OpenShift
17+
- timestamp: 2025-03-11T02:40:18Z
18+
type: fixed
19+
data:
20+
fixed-version: 1.2.3-r4

0 commit comments

Comments
 (0)