forked from oscal-compass/compliance-to-policy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compliance_to_policy.py
64 lines (52 loc) · 1.94 KB
/
compliance_to_policy.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
import argparse
import os
import pathlib
import sys
import tempfile
from c2p.framework.c2p import C2P
from c2p.framework.models.c2p_config import C2PConfig, ComplianceOscal
sys.path.append(os.path.join(os.path.dirname(__file__), '../..'))
from plugins_public.plugins.kyverno import PluginConfigKyverno, PluginKyverno
TEST_DATA_DIR = 'plugins_public/tests/data/kyverno'
parser = argparse.ArgumentParser()
parser.add_argument(
'-c',
'--component_definition',
type=str,
default=f'{TEST_DATA_DIR}/component-definition.json',
help=f'Path to component-definition.json (default: {TEST_DATA_DIR}/component-definition.json',
required=False,
)
parser.add_argument(
'-o', '--out', type=str, help='Path to output directory (default: system temporary directory)', required=False
)
args = parser.parse_args()
tmpdirname = args.out if args.out != None else tempfile.mkdtemp()
# Setup c2p_config
c2p_config = C2PConfig()
c2p_config.compliance = ComplianceOscal()
c2p_config.compliance.component_definition = args.component_definition
c2p_config.pvp_name = 'Kyverno'
c2p_config.result_title = 'Kyverno Assessment Results'
c2p_config.result_description = 'OSCAL Assessment Results from Kyverno'
# Construct C2P
c2p = C2P(c2p_config)
# Transform OSCAL (Compliance) to Policy
policy_template_dir = f'{TEST_DATA_DIR}/policy-resources'
config = PluginConfigKyverno(policy_template_dir=policy_template_dir, deliverable_policy_dir=tmpdirname)
PluginKyverno(config).generate_pvp_policy(c2p.get_policy())
def tree(path: pathlib.Path, texts: list[str] = [], depth=0) -> list[str]:
prefix = ''
if depth > 0:
for _ in range(depth):
prefix = prefix + '-'
prefix = prefix + ' '
for item in path.iterdir():
texts.append(f'{prefix}{item.name}')
if item.is_dir():
tree(item, texts, depth=depth + 1)
return texts
print('')
print(f'tree {tmpdirname}')
for text in tree(pathlib.Path(tmpdirname)):
print(text)