Skip to content

Commit 12a8129

Browse files
authored
feat: simplify plugin structure via SDK v1.9.0 (#171)
* updated to use the latest SDK version Signed-off-by: Eddie Knight <knight@linux.com> * go mod tody now that sdk v1.8.0 is released Signed-off-by: Eddie Knight <knight@linux.com> * removed now-redundant function Signed-off-by: Eddie Knight <knight@linux.com> * fixed tests Signed-off-by: Eddie Knight <knight@linux.com> * Extract embed.FS catalog reader logic to SDK v1.9.0 Signed-off-by: Eddie Knight <knight@linux.com> * go mod tidy Signed-off-by: Eddie Knight <knight@linux.com> --------- Signed-off-by: Eddie Knight <knight@linux.com>
1 parent f08c779 commit 12a8129

File tree

32 files changed

+340
-1488
lines changed

32 files changed

+340
-1488
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,7 @@ config.yml
1010
output
1111

1212
# go test coverage output
13-
coverage.out
13+
coverage.out
14+
15+
# A safe space for keeping local development notes
16+
TODO.md

data/baseline/reader.go

Lines changed: 3 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@ package baseline
22

33
import (
44
"embed"
5-
"fmt"
6-
"path"
75

8-
"github.com/goccy/go-yaml"
96
"github.com/ossf/gemara/layer2"
7+
"github.com/privateerproj/privateer-sdk/pluginkit"
108
)
119

1210
// We have tight control over the catalog right now while it is local, but
@@ -16,78 +14,6 @@ const dataDir string = "catalog"
1614
//go:embed catalog
1715
var files embed.FS
1816

19-
func GetAssessmentRequirements() (map[string]*layer2.AssessmentRequirement, error) {
20-
requirements := make(map[string]*layer2.AssessmentRequirement)
21-
catalog, err := loadCatalog()
22-
if err != nil {
23-
return nil, err
24-
}
25-
for _, family := range catalog.ControlFamilies {
26-
for _, control := range family.Controls {
27-
for _, requirement := range control.AssessmentRequirements {
28-
requirements[requirement.Id] = &requirement
29-
}
30-
}
31-
}
32-
33-
if len(requirements) == 0 {
34-
return nil, fmt.Errorf("GetAssessmentRequirements: 0 requirements found")
35-
}
36-
37-
return requirements, nil
38-
}
39-
40-
// ReadAllYAMLFiles reads all YAML files in the data directory and returns the complete catalog data
41-
func loadCatalog() (catalog layer2.Catalog, err error) {
42-
dir, err := files.ReadDir(dataDir)
43-
// Check if files are in the right place
44-
if err != nil {
45-
return catalog, fmt.Errorf("data directory does not exist: %s", dataDir)
46-
}
47-
48-
catalog = layer2.Catalog{
49-
ControlFamilies: []layer2.ControlFamily{},
50-
}
51-
52-
// Process each YAML file
53-
for _, file := range dir {
54-
filePath := path.Join(dataDir, file.Name())
55-
controlFamily, err := readYAMLFile(filePath)
56-
if err != nil {
57-
return catalog, fmt.Errorf("failed to read file %s: %w", filePath, err)
58-
}
59-
60-
catalog.ControlFamilies = append(catalog.ControlFamilies, *controlFamily)
61-
}
62-
63-
return catalog, nil
64-
}
65-
66-
// ReadYAMLFile reads a single YAML file and returns the control family data
67-
func readYAMLFile(filePath string) (*layer2.ControlFamily, error) {
68-
data, err := files.ReadFile(filePath)
69-
if err != nil {
70-
return nil, fmt.Errorf("failed to read file: %w", err)
71-
}
72-
73-
var yamlData layer2.Catalog
74-
if err := yaml.Unmarshal(data, &yamlData); err != nil {
75-
return nil, fmt.Errorf("failed to unmarshal YAML: %w", err)
76-
}
77-
78-
if len(yamlData.ControlFamilies) == 0 {
79-
return nil, fmt.Errorf("no control families found in file: %s", filePath)
80-
}
81-
82-
// Assuming one control family per file as per the current structure
83-
familyData := yamlData.ControlFamilies[0]
84-
85-
controlFamily := &layer2.ControlFamily{
86-
Id: familyData.Id, // Use the ID from the YAML data
87-
Title: familyData.Title,
88-
Description: familyData.Description,
89-
Controls: familyData.Controls,
90-
}
91-
92-
return controlFamily, nil
17+
func GetBaselineCatalog() (layer2.Catalog, error) {
18+
return pluginkit.GetPluginCatalog(dataDir, files)
9319
}

data/baseline/reader_test.go

Lines changed: 0 additions & 97 deletions
This file was deleted.

data/baseline/test_data/malformed_catalog.yaml

Lines changed: 0 additions & 1 deletion
This file was deleted.

data/rest-data.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ func (r *RestData) loadSecurityInsights() {
238238
func (r *RestData) getRepoContents() {
239239
_, content, _, err := r.ghClient.Repositories.GetContents(context.Background(), r.owner, r.repo, "", nil)
240240
if err != nil {
241-
r.Config.Logger.Error(fmt.Sprintf("failed to retrieve contents top level contents: %s", err.Error()))
241+
r.Config.Logger.Error(fmt.Sprintf("failed to retrieve top-level repo contents via GitHub API: %s", err.Error()))
242242
return
243243
}
244244
r.contents.Content = content
@@ -247,7 +247,7 @@ func (r *RestData) getRepoContents() {
247247
return
248248
}
249249
r.contents.SubContent = make(map[string]RepoContent)
250-
r.Config.Logger.Trace(fmt.Sprintf("retrieved %d top-level contents", len(r.contents.Content)))
250+
r.Config.Logger.Trace(fmt.Sprintf("found %d top-level objects from GitHub API", len(r.contents.Content)))
251251
}
252252

253253
func (c *RepoContent) GetSubdirContentByPath(r *RestData, path string) (RepoContent, error) {

0 commit comments

Comments
 (0)