-
Notifications
You must be signed in to change notification settings - Fork 5
/
oscal.go
263 lines (239 loc) · 7.58 KB
/
oscal.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 oscal
import (
"bytes"
"encoding/json"
"encoding/xml"
"errors"
"fmt"
"io"
"io/ioutil"
"github.com/gocomply/oscalkit/pkg/oscal/constants"
sap "github.com/gocomply/oscalkit/types/oscal/assessment_plan"
sar "github.com/gocomply/oscalkit/types/oscal/assessment_results"
"github.com/gocomply/oscalkit/types/oscal/catalog"
"github.com/gocomply/oscalkit/types/oscal/component_definition"
poam "github.com/gocomply/oscalkit/types/oscal/plan_of_action_and_milestones"
"github.com/gocomply/oscalkit/types/oscal/profile"
ssp "github.com/gocomply/oscalkit/types/oscal/system_security_plan"
yaml "gopkg.in/yaml.v2"
)
const (
catalogRootElement = "catalog"
profileRootElement = "profile"
sspRootElement = "system-security-plan"
componentElement = "component-definition"
poamRootElement = "plan-of-action-and-milestones"
sapRootElement = "assessment-plan"
sarRootElement = "assessment-results"
)
// OSCAL contains specific OSCAL components
type OSCAL struct {
XMLName xml.Name `json:"-" yaml:"-"`
Catalog *catalog.Catalog `json:"catalog,omitempty" yaml:"catalog,omitempty"`
// Declarations *Declarations `json:"declarations,omitempty" yaml:"declarations,omitempty"`
Profile *profile.Profile `json:"profile,omitempty" yaml:"profile,omitempty"`
*ssp.SystemSecurityPlan `xml:"system-security-plan" json:"system-security-plan"`
*poam.PlanOfActionAndMilestones `xml:"plan-of-action-and-milestones"`
*sap.AssessmentPlan `xml:"assessment-plan"`
*sar.AssessmentResults `xml:"assessment-results"`
Component *component_definition.ComponentDefinition
documentType constants.DocumentType
}
func (o *OSCAL) DocumentType() constants.DocumentType {
if o.Catalog != nil {
return constants.CatalogDocument
} else if o.Profile != nil {
return constants.ProfileDocument
} else if o.SystemSecurityPlan != nil {
return constants.SSPDocument
} else if o.Component != nil {
return constants.ComponentDocument
} else if o.PlanOfActionAndMilestones != nil {
return constants.POAMDocument
} else if o.AssessmentPlan != nil {
return constants.AssessmentPlanDocument
} else if o.AssessmentResults != nil {
return constants.AssessmentResultsDocument
} else {
return constants.UnknownDocument
}
}
// MarshalXML marshals either a catalog or a profile
func (o *OSCAL) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
if o.Catalog != nil {
o.XMLName = o.Catalog.XMLName
if err := e.Encode(o.Catalog); err != nil {
return err
}
} else if o.Profile != nil {
o.XMLName = o.Profile.XMLName
if err := e.Encode(o.Profile); err != nil {
return err
}
} else if o.SystemSecurityPlan != nil {
o.XMLName = o.SystemSecurityPlan.XMLName
if err := e.Encode(o.SystemSecurityPlan); err != nil {
return err
}
}
return nil
}
// New returns a concrete OSCAL type from a reader
func New(r io.Reader) (*OSCAL, error) {
oscalBytes, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
d := xml.NewDecoder(bytes.NewReader(oscalBytes))
for {
token, err := d.Token()
if err != nil || token == nil {
break
}
switch startElement := token.(type) {
case xml.StartElement:
switch startElement.Name.Local {
case catalogRootElement:
var catalog catalog.Catalog
if err := d.DecodeElement(&catalog, &startElement); err != nil {
return nil, err
}
return &OSCAL{Catalog: &catalog}, nil
case profileRootElement:
var profile profile.Profile
if err := d.DecodeElement(&profile, &startElement); err != nil {
return nil, err
}
return &OSCAL{Profile: &profile}, nil
case sspRootElement:
var ssp ssp.SystemSecurityPlan
if err := d.DecodeElement(&ssp, &startElement); err != nil {
return nil, err
}
return &OSCAL{SystemSecurityPlan: &ssp}, nil
case componentElement:
var component component_definition.ComponentDefinition
if err := d.DecodeElement(&component, &startElement); err != nil {
return nil, err
}
return &OSCAL{Component: &component}, nil
case poamRootElement:
var poam poam.PlanOfActionAndMilestones
if err := d.DecodeElement(&poam, &startElement); err != nil {
return nil, err
}
return &OSCAL{PlanOfActionAndMilestones: &poam}, nil
case sapRootElement:
var sap sap.AssessmentPlan
if err := d.DecodeElement(&sap, &startElement); err != nil {
return nil, err
}
return &OSCAL{AssessmentPlan: &sap}, nil
case sarRootElement:
var sar sar.AssessmentResults
if err := d.DecodeElement(&sar, &startElement); err != nil {
return nil, err
}
return &OSCAL{AssessmentResults: &sar}, nil
}
}
}
var oscalT map[string]json.RawMessage
if err := json.Unmarshal(oscalBytes, &oscalT); err == nil {
for k, v := range oscalT {
switch k {
case catalogRootElement:
var catalog catalog.Catalog
if err := json.Unmarshal(v, &catalog); err != nil {
return nil, err
}
return &OSCAL{Catalog: &catalog}, nil
case profileRootElement:
var profile profile.Profile
if err := json.Unmarshal(v, &profile); err != nil {
return nil, err
}
return &OSCAL{Profile: &profile}, nil
case componentElement:
var component component_definition.ComponentDefinition
if err := json.Unmarshal(v, &component); err != nil {
return nil, err
}
return &OSCAL{Component: &component}, nil
case sspRootElement:
var ssp ssp.SystemSecurityPlan
if err := json.Unmarshal(v, &ssp); err != nil {
return nil, err
}
return &OSCAL{SystemSecurityPlan: &ssp}, nil
case poamRootElement:
var poam poam.PlanOfActionAndMilestones
if err := json.Unmarshal(v, &poam); err != nil {
return nil, err
}
return &OSCAL{PlanOfActionAndMilestones: &poam}, nil
case sapRootElement:
var sap sap.AssessmentPlan
if err := json.Unmarshal(v, &sap); err != nil {
return nil, err
}
return &OSCAL{AssessmentPlan: &sap}, nil
case sarRootElement:
var sar sar.AssessmentResults
if err := json.Unmarshal(v, &sar); err != nil {
return nil, err
}
return &OSCAL{AssessmentResults: &sar}, nil
}
}
}
return nil, errors.New("Malformed OSCAL. Must be XML or JSON")
}
// XML writes the OSCAL object as XML to the given writer
func (o *OSCAL) XML(w io.Writer, prettify bool) error {
return o.encode(encodeOptions{"xml", prettify, w})
}
// JSON writes the OSCAL object as JSON to the given writer
func (o *OSCAL) JSON(w io.Writer, prettify bool) error {
return o.encode(encodeOptions{"json", prettify, w})
}
// YAML writes the OSCAL object as YAML to the given writer
func (o *OSCAL) YAML(w io.Writer) error {
return o.encode(encodeOptions{format: "yaml", writer: w})
}
// Write marshals OSCAL object as xml, json, or yaml
func (o *OSCAL) Write(w io.Writer, format constants.DocumentFormat, prettify bool) error {
switch format {
case constants.XmlFormat:
return o.XML(w, prettify)
case constants.JsonFormat:
return o.JSON(w, prettify)
case constants.YamlFormat:
return o.YAML(w)
}
return fmt.Errorf("Unrecognized document format requested: %s", format.String())
}
type encodeOptions struct {
format string
prettify bool
writer io.Writer
}
func (o *OSCAL) encode(options encodeOptions) error {
switch options.format {
case "xml":
e := xml.NewEncoder(options.writer)
if options.prettify {
e.Indent("", " ")
}
return e.Encode(o)
case "json":
e := json.NewEncoder(options.writer)
if options.prettify {
e.SetIndent("", " ")
}
return e.Encode(o)
case "yaml":
return yaml.NewEncoder(options.writer).Encode(o)
}
return errors.New("Incorrect format specified")
}