-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
131 lines (115 loc) · 4.57 KB
/
app.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
import sys
import pandas as pd
import plotly.graph_objects as go
import dash
import dash_bootstrap_components as dbc
from dash import dcc, html
from dash.dependencies import Input, Output
if len(sys.argv) < 2:
print("Usage: python script_name.py input.csv")
sys.exit(1)
input_csv = sys.argv[1]
# Load CSV data into a DataFrame
df = pd.read_csv(input_csv)
def generate_sankey_figure(selected_role, selected_user, selected_object_type):
if selected_role:
filtered_df = df[df['RoleDefinitionName'] == selected_role]
elif selected_user:
filtered_df = df[df['DisplayName'] == selected_user]
elif selected_object_type:
filtered_df = df[df['ObjectType'] == selected_object_type]
else:
filtered_df = df.copy()
filtered_df['Scope'] = filtered_df['Scope'].str.replace(r'^/subscriptions/[^/]+/', '', regex=True)
nodes_role = filtered_df['RoleDefinitionName'].unique().tolist()
nodes_display_signin = filtered_df.apply(lambda row: f"{row['DisplayName']} ({row['SignInName']})", axis=1).unique().tolist()
nodes_scope = filtered_df['Scope'].unique().tolist()
node_indices = {node: index for index, node in enumerate(nodes_role + nodes_display_signin + nodes_scope)}
source_indices_role = filtered_df['RoleDefinitionName'].apply(lambda x: node_indices[x])
source_indices_display_signin = filtered_df.apply(lambda row: node_indices[f"{row['DisplayName']} ({row['SignInName']})"], axis=1)
source_indices_scope = filtered_df['Scope'].apply(lambda x: node_indices[x])
values = [1] * len(filtered_df)
link_sources = source_indices_role.tolist() + source_indices_display_signin.tolist()
link_targets = source_indices_display_signin.tolist() + source_indices_scope.tolist()
link_values = values + values
fig = go.Figure(go.Sankey(
node=dict(
pad=15,
thickness=20,
line=dict(color="black", width=0.5),
label=nodes_role + nodes_display_signin + nodes_scope
),
link=dict(
source=link_sources,
target=link_targets,
value=link_values
)
))
fig.update_layout(title_text="Azure IAM Relationships")
return fig
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.DARKLY])
app.layout = dbc.Container([
dbc.Row([
dbc.Col(
dbc.Card([
dbc.CardHeader("Select a Role"),
dbc.CardBody(
dcc.Dropdown(
id='role-dropdown',
options=[{'label': role, 'value': role} for role in df['RoleDefinitionName'].unique()],
value=None,
placeholder="Select a role",
className="mb-3",
style={'color': 'black'}
)
)
]),
width=4
),
dbc.Col(
dbc.Card([
dbc.CardHeader("Select a User"),
dbc.CardBody(
dcc.Dropdown(
id='user-dropdown',
options=[{'label': user, 'value': user} for user in df['DisplayName'].unique()],
value=None,
placeholder="Select a user",
className="mb-3",
style={'color': 'black'}
)
)
]),
width=4
),
dbc.Col(
dbc.Card([
dbc.CardHeader("Select an Object Type"),
dbc.CardBody(
dcc.Dropdown(
id='object-type-dropdown',
options=[{'label': obj_type, 'value': obj_type} for obj_type in df['ObjectType'].unique()],
value=None,
placeholder="Select an object type",
className="mb-3",
style={'color': 'black'}
)
)
]),
width=4
)
]),
dbc.Row([
dbc.Col(dcc.Graph(id='sankey-graph', style={'height': 'calc(100vh - 200px)'}))
], style={'margin': 0, 'padding': 0}) # Remove any unwanted margin and padding
], fluid=True)
@app.callback(
Output('sankey-graph', 'figure'),
Input('role-dropdown', 'value'),
Input('user-dropdown', 'value'),
Input('object-type-dropdown', 'value')
)
def update_sankey(selected_role, selected_user, selected_object_type):
return generate_sankey_figure(selected_role, selected_user, selected_object_type)
if __name__ == '__main__':
app.run_server(debug=True)