/
mapper.go
208 lines (197 loc) · 5.68 KB
/
mapper.go
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package generator
import (
"context"
"fmt"
"os"
"strings"
"time"
"github.com/gocomply/oscalkit/impl"
"github.com/gocomply/oscalkit/types/oscal"
"github.com/gocomply/oscalkit/types/oscal/catalog"
"github.com/gocomply/oscalkit/types/oscal/profile"
"github.com/sirupsen/logrus"
)
// CreateCatalogsFromProfile maps profile controls to multiple catalogs
func CreateCatalogsFromProfile(profileArg *profile.Profile) ([]*catalog.Catalog, error) {
t := time.Now()
done := 0
errChan := make(chan error)
catalogChan := make(chan *catalog.Catalog)
var outputCatalogs []*catalog.Catalog
logrus.Info("fetching alterations...")
alterations, err := GetAlters(profileArg)
if err != nil {
return nil, err
}
logrus.Info("fetching alterations from import chain complete")
logrus.Debug("processing alteration and parameters... \nmapping to controls...")
// Get first import of the profile (which is a catalog)
for _, profileImport := range profileArg.Imports {
err := profileImport.ValidateHref()
if err != nil {
return nil, err
}
go func(profileImport profile.Import) {
catalogHelper := impl.NISTCatalog{}
c := make(chan *catalog.Catalog)
e := make(chan error)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// ForEach Import's Href, Fetch the Catalog JSON file
getCatalogForImport(ctx, profileImport, c, e, profileImport.Href)
select {
case importedCatalog := <-c:
// Prepare a new catalog object to merge into the final List of OutputCatalogs
if profileArg.Modify != nil {
nc := impl.NISTCatalog{}
importedCatalog = ProcessAlterations(alterations, importedCatalog)
importedCatalog = ProcessSetParam(profileArg.Modify.ParameterSettings, importedCatalog, &nc)
}
newCatalog, err := GetMappedCatalogControlsFromImport(importedCatalog, profileImport, &catalogHelper)
if err != nil {
errChan <- err
return
}
catalogChan <- &newCatalog
case err := <-e:
errChan <- err
return
}
}(profileImport)
}
for {
select {
case err := <-errChan:
return nil, err
case newCatalog := <-catalogChan:
done++
if newCatalog != nil {
outputCatalogs = append(outputCatalogs, newCatalog)
}
if done == len(profileArg.Imports) {
logrus.Infof("successfully mapped controls in %f seconds", time.Since(t).Seconds())
return outputCatalogs, nil
}
}
}
}
func getSubControl(call profile.Call, ctrls []catalog.Control, helper impl.Catalog) (catalog.Control, error) {
for _, ctrl := range ctrls {
if ctrl.Id == helper.GetControl(call.ControlId) {
for _, subctrl := range ctrl.Controls {
if subctrl.Id == call.ControlId {
return subctrl, nil
}
}
}
}
return catalog.Control{}, fmt.Errorf("could not find subcontrol %s in catalog", call.ControlId)
}
// GetMappedCatalogControlsFromImport gets mapped controls in catalog per profile import
func GetMappedCatalogControlsFromImport(importedCatalog *catalog.Catalog, profileImport profile.Import, catalogHelper impl.Catalog) (catalog.Catalog, error) {
newCatalog := catalog.Catalog{
Metadata: importedCatalog.Metadata,
Groups: []catalog.Group{},
}
for _, group := range importedCatalog.Groups {
newGroup := catalog.Group{
Title: group.Title,
Controls: []catalog.Control{},
}
for _, ctrl := range group.Controls {
for _, call := range profileImport.Include.IdSelectors {
if call.ControlId == "" {
if strings.ToLower(ctrl.Id) == strings.ToLower(catalogHelper.GetControl(call.ControlId)) {
ctrlExistsInGroup := false
sc, err := getSubControl(call, group.Controls, &impl.NISTCatalog{})
if err != nil {
return catalog.Catalog{}, err
}
for i, mappedCtrl := range newGroup.Controls {
if mappedCtrl.Id == strings.ToLower(catalogHelper.GetControl(call.ControlId)) {
ctrlExistsInGroup = true
newGroup.Controls[i].Controls = append(newGroup.Controls[i].Controls, sc)
}
}
if !ctrlExistsInGroup {
newGroup.Controls = append(newGroup.Controls,
catalog.Control{
Id: ctrl.Id,
Class: ctrl.Class,
Title: ctrl.Title,
Parameters: ctrl.Parameters,
Parts: ctrl.Parts,
Controls: []catalog.Control{sc},
})
}
}
}
if strings.ToLower(call.ControlId) == strings.ToLower(ctrl.Id) {
ctrlExists := false
for _, x := range newGroup.Controls {
if x.Id == ctrl.Id {
ctrlExists = true
continue
}
}
if !ctrlExists {
newGroup.Controls = append(newGroup.Controls,
catalog.Control{
Id: ctrl.Id,
Class: ctrl.Class,
Title: ctrl.Title,
Controls: []catalog.Control{},
Parameters: ctrl.Parameters,
Parts: ctrl.Parts,
},
)
}
}
}
}
if len(newGroup.Controls) > 0 {
newCatalog.Groups = append(newCatalog.Groups, newGroup)
}
}
return newCatalog, nil
}
func getCatalogForImport(ctx context.Context, i profile.Import, c chan *catalog.Catalog, e chan error, basePath string) {
go func(i profile.Import) {
err := i.ValidateHref()
if err != nil {
e <- fmt.Errorf("href cannot be nil")
return
}
path, err := GetFilePath(i.Href)
if err != nil {
e <- err
return
}
f, err := os.Open(path)
if err != nil {
e <- err
return
}
defer f.Close()
o, err := oscal.New(f)
if err != nil {
e <- err
return
}
if o.Catalog != nil {
c <- o.Catalog
return
}
newP, err := SetBasePath(o.Profile, basePath)
if err != nil {
e <- err
return
}
o.Profile = newP
for _, p := range o.Profile.Imports {
go func(p profile.Import) {
getCatalogForImport(ctx, p, c, e, basePath)
}(p)
}
}(i)
}