Skip to content

Commit

Permalink
add hidden option to fetch only metadata with Get()
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Bertschy <[email protected]>
  • Loading branch information
matthyx committed Nov 22, 2024
1 parent 8b43fa4 commit d7aed14
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 14 deletions.
13 changes: 13 additions & 0 deletions pkg/registry/file/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,19 @@ func (s *StorageImpl) Get(ctx context.Context, key string, opts storage.GetOptio
// get is a helper function for Get to allow calls without locks from other methods that already have them
func (s *StorageImpl) get(ctx context.Context, key string, opts storage.GetOptions, objPtr runtime.Object) error {
p := filepath.Join(s.root, key)
if opts.ResourceVersion == "metadata" {
// get metadata from SQLite
conn, err := s.pool.Take(context.Background())
if err != nil {
return fmt.Errorf("take connection: %w", err)
}
defer s.pool.Put(conn)
metadata, err := ReadMetadata(conn, key)
if err != nil {
return fmt.Errorf("read metadata: %w", err)
}
return json.Unmarshal(metadata, objPtr)
}
payloadFile, err := s.appFs.OpenFile(makePayloadPath(p), syscall.O_DIRECT|os.O_RDONLY, 0)
if err != nil {
if errors.Is(err, afero.ErrFileNotFound) {
Expand Down
65 changes: 52 additions & 13 deletions pkg/registry/file/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/gob"
"encoding/json"
"fmt"
"testing"

Expand Down Expand Up @@ -264,24 +265,37 @@ func isNotFoundError(_ assert.TestingT, err error, _ ...any) bool {
func TestStorageImpl_Get(t *testing.T) {
var emptyObj bytes.Buffer
_ = gob.NewEncoder(&emptyObj).Encode(v1beta1.SBOMSyft{})
var realMeta bytes.Buffer
_ = json.NewEncoder(&realMeta).Encode(v1beta1.SBOMSyft{
ObjectMeta: v1.ObjectMeta{
Name: "toto",
},
})
var realObj bytes.Buffer
_ = gob.NewEncoder(&realObj).Encode(v1beta1.SBOMSyft{
ObjectMeta: v1.ObjectMeta{
Name: "toto",
},
Spec: v1beta1.SBOMSyftSpec{
Metadata: v1beta1.SPDXMeta{
Tool: v1beta1.ToolMeta{
Name: "syft"},
},
},
})
type args struct {
key string
opts storage.GetOptions
objPtr runtime.Object
}
tests := []struct {
name string
args args
content string
create bool
wantErr assert.ErrorAssertionFunc
want runtime.Object
name string
args args
content []byte
contentMeta []byte
create bool
wantErr assert.ErrorAssertionFunc
want runtime.Object
}{
{
name: "not found",
Expand All @@ -305,7 +319,7 @@ func TestStorageImpl_Get(t *testing.T) {
key: "/spdx.softwarecomposition.kubescape.io/sbomsyfts/kubescape/toto",
objPtr: &v1beta1.SBOMSyft{},
},
content: emptyObj.String(),
content: emptyObj.Bytes(),
create: true,
wantErr: assert.NoError,
want: &v1beta1.SBOMSyft{},
Expand All @@ -316,9 +330,31 @@ func TestStorageImpl_Get(t *testing.T) {
key: "/spdx.softwarecomposition.kubescape.io/sbomsyfts/kubescape/toto",
objPtr: &v1beta1.SBOMSyft{},
},
content: realObj.String(),
content: realObj.Bytes(),
create: true,
wantErr: assert.NoError,
want: &v1beta1.SBOMSyft{
ObjectMeta: v1.ObjectMeta{
Name: "toto",
},
Spec: v1beta1.SBOMSyftSpec{
Metadata: v1beta1.SPDXMeta{
Tool: v1beta1.ToolMeta{
Name: "syft"},
},
},
},
},
{
name: "real object - metadata only",
args: args{
key: "/spdx.softwarecomposition.kubescape.io/sbomsyfts/kubescape/toto",
objPtr: &v1beta1.SBOMSyft{},
opts: storage.GetOptions{ResourceVersion: "metadata"},
},
contentMeta: realMeta.Bytes(),
create: true,
wantErr: assert.NoError,
want: &v1beta1.SBOMSyft{
ObjectMeta: v1.ObjectMeta{
Name: "toto",
Expand All @@ -331,24 +367,27 @@ func TestStorageImpl_Get(t *testing.T) {
key: "/spdx.softwarecomposition.kubescape.io/sbomsyfts/kubescape/toto",
objPtr: &v1beta1.SBOMSyft{},
},
content: string(realObj.Bytes()[10]),
content: realObj.Bytes()[10:10],
create: true,
wantErr: isNotFoundError,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
fs := afero.NewMemMapFs()
if tt.create {
path := getStoredPayloadFilepath(DefaultStorageRoot, tt.args.key)
_ = afero.WriteFile(fs, path, []byte(tt.content), 0644)
}
pool := NewTestPool(t.TempDir())
require.NotNil(t, pool)
defer func(pool *sqlitemigration.Pool) {
_ = pool.Close()
}(pool)
s := NewStorageImpl(fs, DefaultStorageRoot, pool, nil)
if tt.create {
conn, err := pool.Take(context.Background())
require.NoError(t, err)
require.NoError(t, WriteJSON(conn, tt.args.key, tt.contentMeta))
require.NoError(t, afero.WriteFile(fs, getStoredPayloadFilepath(DefaultStorageRoot, tt.args.key), tt.content, 0644))
pool.Put(conn)
}
if err := s.Get(context.TODO(), tt.args.key, tt.args.opts, tt.args.objPtr); !tt.wantErr(t, err) {
t.Errorf("Get() error = %v, wantErr %v", err, tt.wantErr(t, err))
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/registry/file/vulnerabilitysummarystorage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,9 +443,9 @@ func TestVulnSummaryStorageImpl_Get(t *testing.T) {
}(pool)
sch := scheme.Scheme
require.NoError(t, softwarecomposition.AddToScheme(sch))
realStorage := NewStorageImpl(afero.NewMemMapFs(), "/", pool, sch)

for _, tt := range tests {
realStorage := NewStorageImpl(afero.NewMemMapFs(), "/", pool, sch)
t.Run(tt.name, func(t *testing.T) {
if tt.createObj {
for i := range tt.args.keyCreatedObj {
Expand Down

0 comments on commit d7aed14

Please sign in to comment.