/
summary_table.go
263 lines (232 loc) · 9.33 KB
/
summary_table.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package control
import (
"github.com/jbowtie/gokogiri/xml"
"github.com/opencontrol/fedramp-templater/common/origin"
"github.com/opencontrol/fedramp-templater/common/source"
"github.com/opencontrol/fedramp-templater/common/status"
"github.com/opencontrol/fedramp-templater/opencontrols"
"github.com/opencontrol/fedramp-templater/reporter"
"gopkg.in/fatih/set.v0"
"fmt"
)
const (
responsibleRoleField = "Responsible Role"
controlOriginationField = "Control Origination"
implementationStatusField = "Implementation Status"
)
// SummaryTable represents the node in the Word docx XML tree that corresponds to the summary information for a security control.
type SummaryTable struct {
table
originTable *controlOrigination
statusTable *implementationStatus
}
// NewSummaryTable creates a SummaryTable instance.
func NewSummaryTable(root xml.Node) (SummaryTable, error) {
tbl := table{Root: root}
originTable, err := newControlOrigination(&tbl)
if err != nil {
return SummaryTable{}, err
}
statusTable, err := newImplementationStatus(&tbl)
if err != nil {
return SummaryTable{}, err
}
return SummaryTable{tbl, originTable, statusTable}, nil
}
func (st *SummaryTable) controlName() (name string, err error) {
return st.table.controlName()
}
// ControlName - name of the control
func (st *SummaryTable) ControlName() (name string, err error) {
return st.table.controlName()
}
func (st *SummaryTable) fillResponsibleRole(openControlData opencontrols.Data, control string) (err error) {
roleCell, err := findResponsibleRole(st)
if err != nil {
return
}
roles := openControlData.GetResponsibleRoles(control)
roleCell.setValue(roles)
return
}
func (st *SummaryTable) fillControlOrigination(openControlData opencontrols.Data, control string) (err error) {
controlOrigins := openControlData.GetControlOrigins(control)
checkedOriginsSet := controlOrigins.GetCheckedOrigins()
checkedOrigins := origin.ConvertSetToKeys(checkedOriginsSet)
for _, checkedOrigin := range checkedOrigins {
if checkedOrigin == origin.NoOrigin {
continue
}
st.originTable.origins[checkedOrigin].SetCheckMarkTo(true)
}
return
}
func (st *SummaryTable) fillImplementationStatus(openControlData opencontrols.Data, control string) (err error) {
implementationStatuses := openControlData.GetImplementationStatuses(control)
checkedStatusesSet := implementationStatuses.GetCheckedStatuses()
checkedStatuses := status.ConvertSetToKeys(checkedStatusesSet)
for _, checkedStatus := range checkedStatuses {
if checkedStatus == status.NoStatus {
continue
}
st.statusTable.statuses[checkedStatus].SetCheckMarkTo(true)
}
return
}
// Fill inserts the OpenControl justifications into the table. Note this modifies the `table`.
func (st *SummaryTable) Fill(openControlData opencontrols.Data) (err error) {
control, err := st.controlName()
if err != nil {
return
}
err = st.fillResponsibleRole(openControlData, control)
if err != nil {
return
}
fmt.Println("Filling controls for ", control)
err = st.fillControlOrigination(openControlData, control)
if err != nil {
return
}
err = st.fillImplementationStatus(openControlData, control)
if err != nil {
return
}
return
}
// diffControlOrigination computes the diff of the control origination.
func (st *SummaryTable) diffControlOrigination(control string,
openControlData opencontrols.Data) ([]reporter.Reporter, error) {
// find the control origins currently checked in the section in the doc.
docControlOrigins := st.originTable.getCheckedOrigins()
// find the control origins noted in the yaml.
yamlControlOriginationData := openControlData.GetControlOrigins(control)
// find the control origins currently checked in the section in the YAML.
yamlControlOrigins := yamlControlOriginationData.GetCheckedOrigins()
// find the difference of the two sets.
controlOriginMap := origin.GetSourceMappings()
reports := []reporter.Reporter{}
// find only the origins in the document.
onlyInDocOrigins := set.Difference(docControlOrigins, yamlControlOrigins)
// create the diff report for the origins only in the document.
onlyInDocOriginReports := st.createControlOriginsDiffReport(onlyInDocOrigins, controlOriginMap, control, source.SSP)
reports = append(reports, onlyInDocOriginReports...)
// find only the origins in the yaml.
onlyInYAMLOrigins := set.Difference(yamlControlOrigins, docControlOrigins)
// create the diff report for the origins only in the yaml.
onlyInYAMLOriginReports := st.createControlOriginsDiffReport(onlyInYAMLOrigins, controlOriginMap, control, source.YAML)
reports = append(reports, onlyInYAMLOriginReports...)
return reports, nil
}
// diffImplementationStatus computes the diff of the implementation status.
func (st *SummaryTable) diffImplementationStatus(control string,
openControlData opencontrols.Data) ([]reporter.Reporter, error) {
// find the implementation statues currently checked in the section in the doc.
docImplementationStatuses := st.statusTable.getCheckedStatuses()
// find the implementation statuses noted in the yaml.
yamlImplementationStatusData := openControlData.GetImplementationStatuses(control)
// find the implementation statuses currently checked in the section in the YAML.
yamlImplementationStatuses := yamlImplementationStatusData.GetCheckedStatuses()
// find the difference of the two sets.
implementationStatusMap := status.GetSourceMappings()
reports := []reporter.Reporter{}
// find only the statuses in the document.
onlyInDocStatuses := set.Difference(docImplementationStatuses, yamlImplementationStatuses)
// create the diff report for the statuses only in the document.
onlyInDocStatusReports := st.createImplementationStatusesDiffReport(onlyInDocStatuses, implementationStatusMap, control, source.SSP)
reports = append(reports, onlyInDocStatusReports...)
// find only the origins in the yaml.
onlyInYAMLStatuses := set.Difference(yamlImplementationStatuses, docImplementationStatuses)
// create the diff report for the origins only in the yaml.
onlyInYAMLStatusReports := st.createImplementationStatusesDiffReport(onlyInYAMLStatuses, implementationStatusMap, control, source.YAML)
reports = append(reports, onlyInYAMLStatusReports...)
return reports, nil
}
func (*SummaryTable) createControlOriginsDiffReport(diff set.Interface,
controlOriginSrcMap map[origin.Key]origin.SrcMapping, control string, src source.Source) []reporter.Reporter {
reports := []reporter.Reporter{}
secondField := field{text: ""}
originKeys := origin.ConvertSetToKeys(diff)
for _, originKey := range originKeys {
var firstField field
switch src {
case source.SSP:
firstField.text = controlOriginSrcMap[originKey][source.SSP]
firstField.source = source.SSP
secondField.source = source.YAML
case source.YAML:
firstField.text = controlOriginSrcMap[originKey][source.YAML]
firstField.source = source.YAML
secondField.source = source.SSP
}
// Get the doc mapping and put it in the doc.
reports = append(reports, NewDiff(control, controlOriginationField, firstField, secondField))
}
return reports
}
func (*SummaryTable) createImplementationStatusesDiffReport(diff set.Interface,
implementationStatusSrcMap map[status.Key]status.SrcMapping, control string, src source.Source) []reporter.Reporter {
reports := []reporter.Reporter{}
secondField := field{text: ""}
statusKeys := status.ConvertSetToKeys(diff)
for _, statusKey := range statusKeys {
var firstField field
switch src {
case source.SSP:
firstField.text = implementationStatusSrcMap[statusKey][source.SSP]
firstField.source = source.SSP
secondField.source = source.YAML
case source.YAML:
firstField.text = implementationStatusSrcMap[statusKey][source.YAML]
firstField.source = source.YAML
secondField.source = source.SSP
}
// Get the doc mapping and put it in the doc.
reports = append(reports, NewDiff(control, implementationStatusField, firstField, secondField))
}
return reports
}
// diffResponsibleRole computes the diff of the responsible role cell.
func (st *SummaryTable) diffResponsibleRole(control string, openControlData opencontrols.Data) ([]reporter.Reporter, error) {
roleCell, err := findResponsibleRole(st)
if err != nil {
return []reporter.Reporter{}, err
}
yamlField := field{source: source.YAML}
yamlField.text = openControlData.GetResponsibleRoles(control)
sspField := field{source: source.SSP}
sspField.text = roleCell.getValue()
if roleCell.isDefaultValue(sspField.text) || yamlField.text == sspField.text {
return []reporter.Reporter{}, nil
}
return []reporter.Reporter{
NewDiff(control, responsibleRoleField, sspField, yamlField),
}, nil
}
// Diff returns the list of diffs in the control table.
func (st *SummaryTable) Diff(openControlData opencontrols.Data) ([]reporter.Reporter, error) {
reports := []reporter.Reporter{}
control, err := st.controlName()
if err != nil {
return reports, err
}
// Diff the responsible roles
diffReports, err := st.diffResponsibleRole(control, openControlData)
if err != nil {
return reports, err
}
reports = append(reports, diffReports...)
// Diff the control origination
diffReports, err = st.diffControlOrigination(control, openControlData)
if err != nil {
return reports, err
}
reports = append(reports, diffReports...)
// Diff the implememtation status
diffReports, err = st.diffImplementationStatus(control, openControlData)
if err != nil {
return reports, err
}
reports = append(reports, diffReports...)
return reports, nil
}