-
Notifications
You must be signed in to change notification settings - Fork 15
/
irisiam-mapper.py
54 lines (41 loc) · 1.38 KB
/
irisiam-mapper.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
#!/usr/bin/python3
# SPDX-License-Identifier: Apache-2.0
# Copyright (c) 2023 United Kingdom Research and Innovation
import sys
import os
import json
from subprocess import check_output, Popen, PIPE
from shlex import quote
import cgi
project_id = sys.argv[1]
env = os.environ.copy()
sourcecmd = "source /etc/openstack-utils/username-openrc.sh;"
def cl(c):
p = Popen(sourcecmd+c, shell=True, stdout=PIPE, env=env)
return p.communicate()[0]
print("Content-Type: application/json")
print("")
arguments = cgi.FieldStorage()
for i in arguments.keys():
print(i + ": " + arguments[i].value)
assignment_out = cl("openstack role assignment list --project " + quote(project_id) + " -f json")
assignments = json.loads(assignment_out)
mapping_out = cl("openstack mapping show irisiam -f json")
mapping_json = json.loads(mapping_out)
rules = mapping_json['rules']
groups_in_project = []
iam_allowed_groups = {
"groups": []
}
for assignment in assignments:
if assignment['Group'] != "":
groups_in_project.append(assignment['Group'])
for rule in rules:
locals = rule['local']
remotes = rule['remote']
for local in locals:
if local['group']['id'] in groups_in_project:
for remote in remotes:
if remote['type'] == "OIDC-groups":
iam_allowed_groups['groups'].extend(remote['any_one_of'])
print(json.dumps(iam_allowed_groups))