-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.py
168 lines (117 loc) · 6.05 KB
/
generate.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
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
#%%#######################################################################
#%% Work with controls from: https://github.com/usnistgov/csv-synthetic-controls/
import os, subprocess
import pandas as pd
import tools as t
import pandas as pd
#%%#######################################################################
error_condition = None
errors = list()
#%% Paths os.getcwd()
dir_output = os.path.join(os.getcwd(),'output')
dir_template = os.path.join(os.getcwd(),'templates')
dir_template_support = os.path.join(os.getcwd(),'templates_support')
dir_content = os.path.join(os.getcwd(),'content')
filename_output = 'example'
filename_input = 'NIST_800-53_Rev5_Simulated.csv'
filename_crm = 'template.cdef.crm.yaml'
filename_validation_log = 'validation.log'
filepath_csv = os.path.join(dir_content, filename_input)
filepath_template_crm = os.path.join(dir_template_support, filename_crm)
filepath_validation_log = os.path.join(dir_output, filename_validation_log)
#%% Static Settings
sep = '*'*100
lb = "\n\n"
make_xml = False
validate_oscal_cli = True
oscal_cli = 'Validate/oscal-cli-1.0.2'
crm = None
#%% Setup
df_content = pd.read_csv(filepath_csv)
grouped_controls_df = df_content.groupby('control_id')
t.clean_output(dir_output)
#%% Load all templates
templates = ['template.ssp.csp.yaml','template.ssp.msp.yaml','template.ssp.app.yaml']
# for root, dirs, files in os.walk(dir_template):
# for file in files:
# if file.endswith(".yaml"):
# templates.append(file)
#%%#######################################################################
org_metadata = {}
org_metadata['csp'] = {
'version': '0.0.1',
'ssp_title': 'Cloud Service Provider System Security Plan',
'ssp_system_name': 'Demonstration System representing a Cloud Service Provider',
'ssp_reason': 'This SSP demonstrates prototype modeling for sharing of responsibility.'
}
org_metadata['msp'] = {
'version': '0.0.1',
'ssp_title': 'Managed Service Provider System Security Plan',
'ssp_system_name': 'Demonstration System representing a Managed Service Provider',
'ssp_reason': 'This SSP demonstrates prototype modeling for sharing of responsibility.'
}
org_metadata['app'] = {
'version': '0.0.1',
'ssp_title': 'Application Owner System Security Plan',
'ssp_system_name': 'Demonstration System representing an Application Owner',
'ssp_reason': 'This SSP demonstrates prototype modeling for sharing of responsibility.'
}
#%%#######################################################################
#%% Build SSPs
for ssp_template in templates:
current_org = ssp_template.split('.')[2:-1][0]
filepath_template = os.path.join(dir_template,ssp_template)
filepath_yaml = os.path.join(dir_output, f"{filename_output}.{'.'.join(ssp_template.split('.')[1:-1])}.yaml")
filepath_json = os.path.join(dir_output, f"{filename_output}.{'.'.join(ssp_template.split('.')[1:-1])}.json")
filepath_crm_yaml = os.path.join(dir_output, f"{filename_output}.{'.'.join(ssp_template.split('.')[1:-1])}.crm.yaml")
filepath_crm_json = os.path.join(dir_output, f"{filename_output}.{'.'.join(ssp_template.split('.')[1:-1])}.crm.json")
print(f"Generating [{current_org}]: {filepath_template}")
print(f"YAML: {filepath_yaml}")
print(f"JSON: {filepath_json}\n\n")
metadata = org_metadata[current_org]
# Build Content
print("Building SSP")
if current_org == 'csp':
crm = None
ssp = t.build_ssp(filepath_template, metadata, grouped_controls_df, crm)
# Export YAML file
print(f"YAML: {filepath_yaml}")
t.save_yaml(ssp, filepath_yaml)
# Export JSON file
print(f"JSON: {filepath_json}")
t.save_json(ssp, filepath_json)
if validate_oscal_cli:
with open(filepath_validation_log, "a") as outfile:
subprocess.run([oscal_cli, 'ssp', 'validate', filepath_yaml], stdout=outfile, stderr=outfile)
with open(filepath_validation_log, "a") as outfile:
subprocess.run([oscal_cli, 'ssp', 'validate', filepath_json], stdout=outfile, stderr=outfile)
if make_xml:
with open(filepath_validation_log, "a") as outfile:
subprocess.run([oscal_cli,'ssp','convert','--to=xml',filepath_json,filepath_json+'.xml'], stdout=outfile, stderr=outfile)
if current_org != 'app':
crm = t.build_crm(filepath_template_crm, ssp)
# Export YAML file
print(f"CRM YAML: {filepath_crm_yaml}")
t.save_yaml(crm, filepath_crm_yaml)
# Export JSON file
print(f"CRM JSON: {filepath_crm_json}")
t.save_json(crm, filepath_crm_json)
if validate_oscal_cli:
with open(filepath_validation_log, "a") as outfile:
subprocess.run([oscal_cli, 'component-definition', 'validate', filepath_crm_yaml], stdout=outfile, stderr=outfile)
with open(filepath_validation_log, "a") as outfile:
subprocess.run([oscal_cli, 'component-definition', 'validate', filepath_crm_json], stdout=outfile, stderr=outfile)
if make_xml:
with open(filepath_validation_log, "a") as outfile:
subprocess.run([oscal_cli,'ssp','convert','--to=xml',filepath_crm_json,filepath_crm_json+'.xml'], stdout=outfile, stderr=outfile)
print("\n\n")
#%%#######################################################################
t.record_collection.records = t.record_list
record_content = t.record_collection.dict()['records']
#%%
df = pd.DataFrame(record_content)
for index, row in df.iterrows():
if row['control'] == 'sc-5':
print(f"{row['control']} : {row['uuid']} : {row['a_uuid']}")
df.to_csv('test.csv')
# %%