aboutsummaryrefslogtreecommitdiffstats
path: root/old/tools/policies/generate_opst_policy.py
blob: dd01d1c11aa71df19e0b0043771a6a4398355ff6 (plain)
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
import json
import os
import logging
import argparse


FILES = [
    "cinder.policy.json",
    "glance.policy.json",
    "keystone.policy.json",
    "neutron.policy.json",
    "nova.policy.json",
]
policy = {
    "pdps": [{
        "name": "external_pdp",
        "keystone_project_id": "",
        "description": "",
        "policies": [{"name": "OpenStack RBAC Policy"}]}
    ],

    "policies": [{
        "name": "OpenStack RBAC Policy",
        "genre": "authz",
        "description": "A RBAC policy similar of what you can find through policy.json files",
        "model": {"name": "OPST_RBAC"}, "mandatory": True, "override": True}
    ],

    "models": [{"name": "OPST_RBAC", "description": "", "meta_rules": [{"name": "rbac"}], "override": True}],

    "subjects": [
        {"name": "admin", "description": "", "extra": {}, "policies": [{"name": "OpenStack RBAC Policy"}]},
        {"name": "demo", "description": "", "extra": {}, "policies": [{"name": "OpenStack RBAC Policy"}]}
    ],

    "subject_categories": [{"name": "role", "description": "a role in OpenStack"}],

    "subject_data": [
        {"name": "admin", "description": "the admin role", "policies": [], "category": {"name": "role"}},
        {"name": "member", "description": "the member role", "policies": [], "category": {"name": "role"}}
    ],

    "subject_assignments": [
        {"subject": {"name": "admin"}, "category": {"name": "role"}, "assignments": [{"name": "admin"}, {"name": "member"}]},
        {"subject": {"name": "demo"}, "category": {"name": "role"}, "assignments": [{"name": "member"}]}
    ],

    "objects": [],

    "object_categories": [{"name": "id", "description": "the UID of each virtual machine"}],

    "object_data": [
        {
            "name": "all_vm",
            "description": "represents all virtual machines in this project",
            "policies": [],
            "category": {"name": "id"}},
    ],

    "object_assignments": [],

    "actions": [],

    "action_categories": [{"name": "action_id", "description": ""}],

    "action_data": [],

    "action_assignments": [],

    "meta_rules": [
        {
            "name": "rbac", "description": "",
            "subject_categories": [{"name": "role"}],
            "object_categories": [{"name": "id"}],
            "action_categories": [{"name": "action_id"}]
        }
    ],

    "rules": [],

}
logger = logging.getLogger(__name__)


def init():
    parser = argparse.ArgumentParser()
    parser.add_argument("--verbose", '-v', action='store_true', help='verbose mode')
    parser.add_argument("--debug", '-d', action='store_true', help='debug mode')
    parser.add_argument("--dir", help='directory containing policy files', default="./policy.json.d")
    parser.add_argument("--indent", '-i', help='indent the output (default:None)', type=int, default=None)
    parser.add_argument("--output", '-o', help='output name', type=str, default="opst_default_policy.json")
    args = parser.parse_args()
    logging_format = "%(levelname)s: %(message)s"
    if args.verbose:
        logging.basicConfig(level=logging.INFO, format=logging_format)
    if args.debug:
        logging.basicConfig(level=logging.DEBUG, format=logging_format)
    else:
        logging.basicConfig(format=logging_format)
    return args


def get_rules(args):
    results = {}
    for f in FILES:
        _json_file = json.loads(open(os.path.join(args.dir, f)).read())
        keys = list(_json_file.keys())
        values = list(_json_file.values())
        for value in values:
            if value in keys:
                keys.remove(value)
        component = os.path.basename(f).split(".")[0]
        results[component] = keys
    return results


def build_dict(results):
    for key in results:
        for rule in results[key]:
            _output = {
                "name": rule,
                "description": "{} action for {}".format(rule, key),
                "extra": {"component": key},
                "policies": []
            }
            policy['actions'].append(_output)
            _output = {
                "name": rule,
                "description": "{} action for {}".format(rule, key),
                "policies": [],
                "category": {"name": "action_id"}
            }
            policy['action_data'].append(_output)
            _output = {
                "action": {"name": rule},
                "category": {"name": "action_id"},
                "assignments": [{"name": rule}, ]}
            policy['action_assignments'].append(_output)
            _output = {
                "meta_rule": {"name": "rbac"},
                "rule": {
                    "subject_data": [{"name": "admin"}],
                    "object_data": [{"name": "all_vm"}],
                    "action_data": [{"name": rule}]
                },
                "policy": {"name": "OpenStack RBAC Policy"},
                "instructions": {"decision": "grant"},
                "enabled": True
              }
            policy['rules'].append(_output)
            # TODO: add rules for member only
            # TODO: add rules for everyone


def write_dict(args):
    json.dump(policy, open(args.output, "w"), indent=args.indent)


def main():
    args = init()
    rules = get_rules(args)
    build_dict(rules)
    write_dict(args)


if __name__ == "__main__":
    main()