-
Notifications
You must be signed in to change notification settings - Fork 0
/
cat.py
137 lines (119 loc) · 3.86 KB
/
cat.py
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
from pathlib import Path
import click
from typing import List
from oscal.oscal import Metadata, BackMatter, Resource, oscalize_control_id, control_to_sort_order, Role, Property, Link, OSCAL_VERSION
from oscal.catalog import Group, Catalog
from ruamel.yaml import YAML
class CatMaker:
def __init__(self, path):
yaml = YAML(typ="safe")
with open(path, "r+") as f:
self.catalog_yaml = yaml.load(f)
def add_back_matter(self):
self.backmatter = BackMatter(
resources=[]
)
self.create_resources()
return self.backmatter
def create_resources(self):
self.resource_lookup:dict = {}
for _, c in self.catalog_yaml.items():
references = c.get("references") if c.get("references") else []
for r in references:
if not hasattr(self.resource_lookup, r):
rs = Resource(
title=r
)
self.resource_lookup[r] = rs
self.backmatter.resources.append(rs)
def add_groups(self):
groups:dict = {}
for i, c in self.catalog_yaml.items():
fid = i[:2]
if not hasattr(groups, fid):
groups[fid] = Group(
id=fid,
group_class="CMS ARS 3.1",
title=c.get("control_family"),
params=[],
props=[],
links=[],
parts=[],
groups=[],
controls=[],
)
return groups
def add_roles(self, roles_list:List):
roles:List = [Role]
for r in roles_list:
roles.append(Role(
id=r.get("id"),
title=r.get("title"),
))
return roles
def controls(self):
controls:dict = {}
for i, c in self.catalog_yaml.items():
controls[i] = self.add_control(c)
return controls
def add_control(self, control:dict):
"""
"""
references = control.get("references") if control.get("references") else []
related = control.get("related") if control.get("related") else []
props = self.add_control_props(control.get("control_number"))
links = self.get_links(ref=references, rel=related)
return {"props": props, "links": links}
def get_links(self, ref:List, rel:List):
links:List = [Link]
for rf in ref:
if rf in self.resource_lookup:
uuid = self.resource_lookup[rf].uuid
links.append(Link(
href=f"#{uuid}",
rel="reference",
))
for rl in rel:
l = rl.lower()
links.append(Link(
href=f"#{l}",
rel="related",
))
return links
def add_control_props(self, control_id):
props:List = [Property]
props.append(Property(
name="label",
value=control_id
))
props.append(Property(
name="sort-id",
value=control_to_sort_order(control_id)
))
return props
@click.command()
@click.argument(
"source",
type=click.Path(exists=True, dir_okay=False, file_okay=True, resolve_path=True),
)
def main(source):
"""
:param str source: path to YAML file
"""
file_path = Path(source)
cat = CatMaker(file_path)
roles = cat.add_roles([
{"id": "maintainer", "title": "Document creator"},
{"id": "system-owner-poc-management", "title": "Contact"},
]),
md = Metadata(
title="CMS ARS 3.1",
version=OSCAL_VERSION,
# roles=roles,
)
bm = cat.add_back_matter()
groups = cat.add_groups()
controls = cat.controls()
print(controls)
if __name__ == "__main__":
main()