-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwrite.go
More file actions
97 lines (86 loc) · 2.24 KB
/
write.go
File metadata and controls
97 lines (86 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main
import (
"os"
"sort"
"github.com/ossf/gemara/layer2"
"gopkg.in/yaml.v3"
)
func writeOutputCatalog(path string) error {
catalog := generateOutputCatalog()
data, err := yaml.Marshal(catalog)
if err != nil {
return err
}
return os.WriteFile(path, data, 0644)
}
func generateOutputCatalog() (outputCatalog layer2.Catalog) {
var sharedControls []string
var sharedThreats []string
var sharedCapabilities []string
for _, item := range selectedCapabilities {
sharedCapabilities = appendIfMissing(sharedCapabilities, item.id)
for _, threat := range item.capability.Threats {
sharedThreats = appendIfMissing(sharedThreats, threat.Data.Id)
for _, control := range threat.Controls {
sharedControls = appendIfMissing(sharedControls, control.Data.Id)
}
}
}
sort.Sort(sort.StringSlice(sharedControls))
sort.Sort(sort.StringSlice(sharedThreats))
sort.Sort(sort.StringSlice(sharedCapabilities))
// Convert string slices to MappingEntry slices
controlEntries := make([]layer2.MappingEntry, len(sharedControls))
for i, controlId := range sharedControls {
controlEntries[i] = layer2.MappingEntry{
ReferenceId: controlId,
Strength: 1, // Default strength
}
}
threatEntries := make([]layer2.MappingEntry, len(sharedThreats))
for i, threatId := range sharedThreats {
threatEntries[i] = layer2.MappingEntry{
ReferenceId: threatId,
Strength: 1, // Default strength
}
}
capabilityEntries := make([]layer2.MappingEntry, len(sharedCapabilities))
for i, capabilityId := range sharedCapabilities {
capabilityEntries[i] = layer2.MappingEntry{
ReferenceId: capabilityId,
Strength: 1, // Default strength
}
}
outputCatalog = layer2.Catalog{
Metadata: layer2.Metadata{
Title: catalogName,
},
ImportedControls: []layer2.Mapping{
{
ReferenceId: "CCC",
Entries: controlEntries,
},
},
ImportedThreats: []layer2.Mapping{
{
ReferenceId: "CCC",
Entries: threatEntries,
},
},
ImportedCapabilities: []layer2.Mapping{
{
ReferenceId: "CCC",
Entries: capabilityEntries,
},
},
}
return outputCatalog
}
func appendIfMissing(slice []string, i string) []string {
for _, ele := range slice {
if ele == i {
return slice
}
}
return append(slice, i)
}