Skip to content

Commit f796eba

Browse files
committed
Updated pkg to use new schema
Signed-off-by: Eddie Knight <knight@linux.com>
1 parent 4244933 commit f796eba

File tree

5 files changed

+58
-11
lines changed

5 files changed

+58
-11
lines changed

cmd/pkg/baseline/generator.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"html/template"
99
"os"
1010
"path/filepath"
11-
"strings"
1211

1312
"github.com/ossf/security-baseline/pkg/types"
1413
)
@@ -43,18 +42,12 @@ func (g *Generator) ExportMarkdown(b *types.Baseline, path string) error {
4342
// Create and parse the template
4443
tmpl, err := template.New("baseline").Funcs(template.FuncMap{
4544
// Template function to remove newlines and collapse text
46-
"collapseNewlines": func(s string) string {
47-
return strings.ReplaceAll(s, "\n", " ")
48-
},
45+
"collapseNewlines": collapseNewlines,
4946
"addLinks": func(s string) string {
5047
return addLinksTemplateFunction(b.Lexicon, s)
5148
},
52-
"asLink": func(s string) string {
53-
return asLinkTemplateFunction(s)
54-
},
55-
"subtract": func(a, b int) int {
56-
return a - b
57-
},
49+
"asLink": asLinkTemplateFunction,
50+
"maxLevel": maxLevel,
5851
}).Parse(string(templateContent))
5952
if err != nil {
6053
return fmt.Errorf("error parsing template: %w", err)

cmd/pkg/baseline/loader.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
)
1414

1515
const LexiconFilename = "lexicon.yaml"
16+
const FrameworksFilename = "frameworks.yaml"
1617

1718
// Loader is an object that reads the baseline data
1819
type Loader struct {
@@ -34,7 +35,13 @@ func (l *Loader) Load() (*types.Baseline, error) {
3435
if err != nil {
3536
return nil, fmt.Errorf("error reading lexicon: %w", err)
3637
}
38+
frameworks, err := l.loadFramework()
39+
if err != nil {
40+
return nil, fmt.Errorf("error reading frameworks: %w", err)
41+
}
42+
3743
b.Lexicon = lexicon
44+
b.Frameworks = frameworks
3845

3946
for _, catCode := range types.Categories {
4047
cat, err := l.loadCategory(catCode)
@@ -66,6 +73,24 @@ func (l *Loader) loadLexicon() ([]types.LexiconEntry, error) {
6673
return lexicon, nil
6774
}
6875

76+
// loadLexicon
77+
func (l *Loader) loadFramework() ([]types.FrameworkEntry, error) {
78+
file, err := os.Open(filepath.Join(l.DataPath, FrameworksFilename))
79+
if err != nil {
80+
return nil, fmt.Errorf("error opening file: %w", err)
81+
}
82+
defer file.Close()
83+
84+
var frameworks types.Frameworks
85+
86+
decoder := yaml.NewDecoder(file)
87+
decoder.KnownFields(true)
88+
if err := decoder.Decode(&frameworks); err != nil {
89+
return nil, fmt.Errorf("error decoding YAML: %v", err)
90+
}
91+
return frameworks.Frameworks, nil
92+
}
93+
6994
// loadCategory loads a category definition from its YAML source
7095
func (l *Loader) loadCategory(catCode string) (*types.Category, error) {
7196
file, err := os.Open(filepath.Join(l.DataPath, fmt.Sprintf("OSPS-%s.yaml", catCode)))

cmd/pkg/baseline/template-functions.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,16 @@ package baseline
66
import (
77
"fmt"
88
"regexp"
9+
"strconv"
910
"strings"
1011

1112
"github.com/ossf/security-baseline/pkg/types"
1213
)
1314

15+
func collapseNewlines(s string) string {
16+
return strings.ReplaceAll(s, "\n", " ")
17+
}
18+
1419
func containsSynonym(list []string, entryTerm, term string) bool {
1520
if strings.EqualFold(entryTerm, term) {
1621
return true
@@ -74,3 +79,23 @@ func addLinksTemplateFunction(lexicon []types.LexiconEntry, text string) string
7479
func asLinkTemplateFunction(text string) string {
7580
return "#" + strings.ToLower(strings.ReplaceAll(text, " ", "-"))
7681
}
82+
83+
// loop through maturityLevels
84+
// to see if any are higher than the targetMaturity
85+
func maxLevel(maturityLevels []string, targetMaturity int) bool {
86+
var out bool
87+
for _, maturity := range maturityLevels {
88+
maturityInt, err := strconv.Atoi(maturity[len(maturity)-1:])
89+
if err != nil {
90+
fmt.Println(err)
91+
return false
92+
}
93+
if maturityInt == targetMaturity {
94+
out = true
95+
}
96+
if maturityInt < targetMaturity {
97+
return false
98+
}
99+
}
100+
return out
101+
}

cmd/pkg/baseline/validator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func (v *Validator) Check(b *types.Baseline) error {
3030
if entry.ID == "" {
3131
errs = append(errs, fmt.Errorf("missing ID for 'control' %s", entry.ID))
3232
}
33-
if entry.ControlText == "" {
33+
if entry.Title == "" {
3434
errs = append(errs, fmt.Errorf("missing 'control' text for %s", entry.ID))
3535
}
3636
// For after all fields are populated:

cmd/pkg/types/types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,7 @@ type FrameworkEntry struct {
6262
Version string `yaml:"version"`
6363
URL string `yaml:"url"`
6464
}
65+
66+
type Frameworks struct {
67+
Frameworks []FrameworkEntry `yaml:"mapping-references"`
68+
}

0 commit comments

Comments
 (0)