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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
|
import numpy as np import pandas as pd import matplotlib.pyplot as plt import matplotlib.patches as mpatches from matplotlib.colors import LinearSegmentedColormap from mpl_toolkits.mplot3d import Axes3D from itertools import combinations, chain import warnings warnings.filterwarnings('ignore')
try: import seaborn as sns HAS_SNS = True except ImportError: HAS_SNS = False
try: from pulp import (LpProblem, LpVariable, LpMinimize, lpSum, value, LpStatus, PULP_CBC_CMD) HAS_PULP = True except ImportError: HAS_PULP = False
plt.rcParams.update({'figure.dpi': 120, 'font.size': 10})
RESOURCES = [ 'SourceCode', 'FinancialDB', 'EmployeeRecords', 'CRM', 'EmailServer', 'Analytics', 'Payroll', 'CustomerData', 'InternalWiki', 'DeployTools' ] R = {r: i for i, r in enumerate(RESOURCES)} N_RES = len(RESOURCES)
ROLE_PERMISSIONS = { 'Admin': set(range(N_RES)), 'Developer': {R['SourceCode'], R['InternalWiki'], R['DeployTools'], R['EmailServer']}, 'Analyst': {R['Analytics'], R['CustomerData'], R['CRM'], R['InternalWiki']}, 'Manager': {R['FinancialDB'], R['Analytics'], R['CRM'], R['InternalWiki'], R['EmailServer']}, 'HRSpecialist':{R['EmployeeRecords'], R['Payroll'], R['InternalWiki'], R['EmailServer']}, 'SalesRep': {R['CRM'], R['CustomerData'], R['EmailServer'], R['InternalWiki']}, } ROLES = list(ROLE_PERMISSIONS.keys()) N_ROLES = len(ROLES)
USERS = { 'Alice': {R['SourceCode'], R['DeployTools'], R['InternalWiki']}, 'Bob': {R['FinancialDB'], R['Analytics'], R['InternalWiki']}, 'Carol': {R['EmployeeRecords'], R['Payroll'], R['InternalWiki']}, 'Dave': {R['CRM'], R['CustomerData'], R['EmailServer']}, 'Eve': {R['Analytics'], R['CRM'], R['InternalWiki'], R['EmailServer']}, 'Frank': {R['SourceCode'], R['DeployTools'], R['Analytics'], R['InternalWiki']}, 'Grace': {R['FinancialDB'], R['Payroll'], R['Analytics'], R['EmailServer'], R['InternalWiki']}, 'Heidi': {R['SourceCode'], R['InternalWiki'], R['DeployTools'], R['EmailServer'], R['CRM']}, 'Ivan': {R['EmployeeRecords'], R['Analytics'], R['InternalWiki']}, 'Judy': {R['CRM'], R['CustomerData'], R['FinancialDB'], R['Analytics'], R['InternalWiki']}, } USERNAMES = list(USERS.keys()) N_USERS = len(USERNAMES)
def cover_check(role_combo, required): """Return True if union of permissions in role_combo covers required.""" granted = set().union(*[ROLE_PERMISSIONS[r] for r in role_combo]) return required.issubset(granted)
def over_privilege(role_combo, required): """Count excess permissions beyond what is required.""" granted = set().union(*[ROLE_PERMISSIONS[r] for r in role_combo]) return len(granted - required)
def optimize_user(username): """Find minimum-role assignments that cover the user's requirements.""" required = USERS[username] best_combos = [] best_op = float('inf') best_size = float('inf')
for size in range(1, N_ROLES + 1): for combo in combinations(ROLES, size): if cover_check(combo, required): op = over_privilege(combo, required) if op < best_op or (op == best_op and size < best_size): best_op = op best_size = size best_combos = [combo] elif op == best_op and size == best_size: best_combos.append(combo) if best_combos: break
return best_combos, best_op
results = {} for user in USERNAMES: combos, op = optimize_user(user) results[user] = { 'required': USERS[user], 'best_roles': combos[0], 'over_privilege': op, 'role_count': len(combos[0]), 'granted': set().union(*[ROLE_PERMISSIONS[r] for r in combos[0]]) }
baseline = {} for user in USERNAMES: req = USERS[user] granted_admin = ROLE_PERMISSIONS['Admin'] baseline[user] = { 'over_privilege': len(granted_admin - req), 'role_count': 1 }
rows = [] for user in USERNAMES: r_opt = results[user] r_base = baseline[user] rows.append({ 'User': user, 'Required Perms': len(r_opt['required']), 'Optimal Roles': ', '.join(r_opt['best_roles']), 'Roles #': r_opt['role_count'], 'Granted Perms': len(r_opt['granted']), 'OverPriv (Opt)': r_opt['over_privilege'], 'OverPriv (Admin)': r_base['over_privilege'], 'Reduction': r_base['over_privilege'] - r_opt['over_privilege'], })
df = pd.DataFrame(rows) print("=" * 80) print("ACCESS CONTROL OPTIMIZATION RESULTS") print("=" * 80) print(df.to_string(index=False)) print(f"\nTotal over-privilege (Admin baseline): {df['OverPriv (Admin)'].sum()}") print(f"Total over-privilege (Optimized) : {df['OverPriv (Opt)'].sum()}") print(f"Total reduction : {df['Reduction'].sum()}")
role_perm_matrix = np.zeros((N_ROLES, N_RES), dtype=int) for i, role in enumerate(ROLES): for res_idx in ROLE_PERMISSIONS[role]: role_perm_matrix[i, res_idx] = 1
user_req_matrix = np.zeros((N_USERS, N_RES), dtype=int) user_grant_matrix = np.zeros((N_USERS, N_RES), dtype=int) for i, user in enumerate(USERNAMES): for res_idx in results[user]['required']: user_req_matrix[i, res_idx] = 1 for res_idx in results[user]['granted']: user_grant_matrix[i, res_idx] = 1
over_priv_matrix = user_grant_matrix - user_req_matrix
op_opt = np.array([results[u]['over_privilege'] for u in USERNAMES]) op_admin = np.array([baseline[u]['over_privilege'] for u in USERNAMES]) reduction = op_admin - op_opt
fig = plt.figure(figsize=(22, 30)) fig.patch.set_facecolor('#0f0f1a') text_col = '#e0e0e0'
def ax_style(ax, title): ax.set_facecolor('#1a1a2e') ax.tick_params(colors=text_col, labelsize=8) for sp in ax.spines.values(): sp.set_edgecolor('#444') ax.set_title(title, color=text_col, fontsize=11, pad=8, fontweight='bold') ax.title.set_color(text_col)
cmap_rb = LinearSegmentedColormap.from_list('rb', ['#1a1a2e','#e94560']) cmap_gb = LinearSegmentedColormap.from_list('gb', ['#1a1a2e','#00b4d8']) cmap_yb = LinearSegmentedColormap.from_list('yb', ['#1a1a2e','#f0c040']) cmap_div = LinearSegmentedColormap.from_list('dv', ['#00b4d8','#1a1a2e','#e94560'])
short_res = ['Src','FinDB','EmpRec','CRM','Email', 'Anlyt','Payrl','CustD','Wiki','Deploy']
ax1 = fig.add_subplot(4, 3, 1) ax_style(ax1, 'Role–Permission Matrix') im1 = ax1.imshow(role_perm_matrix, cmap=cmap_rb, aspect='auto', vmin=0, vmax=1) ax1.set_xticks(range(N_RES)); ax1.set_xticklabels(short_res, rotation=45, ha='right', color=text_col) ax1.set_yticks(range(N_ROLES)); ax1.set_yticklabels(ROLES, color=text_col) for i in range(N_ROLES): for j in range(N_RES): ax1.text(j, i, '✓' if role_perm_matrix[i,j] else '', ha='center', va='center', fontsize=7, color='white') plt.colorbar(im1, ax=ax1, fraction=0.03)
ax2 = fig.add_subplot(4, 3, 2) ax_style(ax2, 'User Required Permissions') im2 = ax2.imshow(user_req_matrix, cmap=cmap_gb, aspect='auto', vmin=0, vmax=1) ax2.set_xticks(range(N_RES)); ax2.set_xticklabels(short_res, rotation=45, ha='right', color=text_col) ax2.set_yticks(range(N_USERS)); ax2.set_yticklabels(USERNAMES, color=text_col) plt.colorbar(im2, ax=ax2, fraction=0.03)
ax3 = fig.add_subplot(4, 3, 3) ax_style(ax3, 'Over-Privilege Map (Optimized)') im3 = ax3.imshow(over_priv_matrix, cmap=cmap_yb, aspect='auto', vmin=0, vmax=1) ax3.set_xticks(range(N_RES)); ax3.set_xticklabels(short_res, rotation=45, ha='right', color=text_col) ax3.set_yticks(range(N_USERS)); ax3.set_yticklabels(USERNAMES, color=text_col) plt.colorbar(im3, ax=ax3, fraction=0.03) for i in range(N_USERS): for j in range(N_RES): if over_priv_matrix[i,j] == 1: ax3.text(j, i, '!', ha='center', va='center', fontsize=8, color='black', fontweight='bold')
ax4 = fig.add_subplot(4, 3, 4) ax_style(ax4, 'Over-Privilege: Admin vs Optimized') x = np.arange(N_USERS); w = 0.35 b1 = ax4.bar(x - w/2, op_admin, w, label='Admin (Baseline)', color='#e94560', alpha=0.85) b2 = ax4.bar(x + w/2, op_opt, w, label='Optimized', color='#00b4d8', alpha=0.85) ax4.set_xticks(x); ax4.set_xticklabels(USERNAMES, rotation=45, ha='right', color=text_col) ax4.set_ylabel('Excess Permissions', color=text_col) ax4.legend(facecolor='#1a1a2e', labelcolor=text_col, fontsize=8) for bar in chain(b1, b2): h = bar.get_height() ax4.text(bar.get_x() + bar.get_width()/2, h + 0.1, str(int(h)), ha='center', va='bottom', fontsize=7, color=text_col)
ax5 = fig.add_subplot(4, 3, 5) ax_style(ax5, 'Privilege Reduction per User') colors_bar = ['#2ecc71' if v > 0 else '#e74c3c' for v in reduction] bars = ax5.bar(USERNAMES, reduction, color=colors_bar, alpha=0.9, edgecolor='#333') ax5.set_xticklabels(USERNAMES, rotation=45, ha='right', color=text_col) ax5.set_ylabel('Reduction in Excess Permissions', color=text_col) ax5.axhline(0, color='#888', lw=0.8, ls='--') for bar, v in zip(bars, reduction): ax5.text(bar.get_x() + bar.get_width()/2, v + 0.05, str(int(v)), ha='center', va='bottom', fontsize=8, color=text_col)
ax6 = fig.add_subplot(4, 3, 6) ax_style(ax6, 'Optimal Role Assignment Frequency') role_count = {role: 0 for role in ROLES} for user in USERNAMES: for role in results[user]['best_roles']: role_count[role] += 1 rc_vals = [role_count[r] for r in ROLES] wedge_colors = ['#e94560','#00b4d8','#f0c040','#2ecc71','#9b59b6','#e67e22'] wedges, texts, autotexts = ax6.pie( rc_vals, labels=ROLES, autopct='%1.0f%%', colors=wedge_colors, startangle=140, textprops={'color': text_col, 'fontsize': 8}, wedgeprops={'edgecolor': '#0f0f1a', 'linewidth': 1.5} ) for at in autotexts: at.set_color('#0f0f1a'); at.set_fontsize(7); at.set_fontweight('bold')
ax7 = fig.add_subplot(4, 3, 7, projection='3d') ax7.set_facecolor('#1a1a2e') ax7.set_title('3D Over-Privilege Surface\n(Admin Baseline)', color=text_col, fontsize=10, pad=6)
admin_grant = np.ones((N_USERS, N_RES), dtype=int) admin_op_mat = admin_grant - user_req_matrix
_x = np.arange(N_RES) _y = np.arange(N_USERS) _xx, _yy = np.meshgrid(_x, _y) _zz = admin_op_mat.astype(float)
surf = ax7.plot_surface(_xx, _yy, _zz, cmap='plasma', edgecolor='none', alpha=0.88) ax7.set_xticks(range(N_RES)); ax7.set_xticklabels(short_res, rotation=60, ha='right', fontsize=6, color=text_col) ax7.set_yticks(range(N_USERS)); ax7.set_yticklabels(USERNAMES, fontsize=7, color=text_col) ax7.set_zlabel('Excess', color=text_col, fontsize=8) ax7.tick_params(colors=text_col) fig.colorbar(surf, ax=ax7, fraction=0.025, pad=0.1)
ax8 = fig.add_subplot(4, 3, 8, projection='3d') ax8.set_facecolor('#1a1a2e') ax8.set_title('3D Over-Privilege Surface\n(Optimized)', color=text_col, fontsize=10, pad=6)
_zz2 = over_priv_matrix.astype(float) surf2 = ax8.plot_surface(_xx, _yy, _zz2, cmap='cool', edgecolor='none', alpha=0.88) ax8.set_xticks(range(N_RES)); ax8.set_xticklabels(short_res, rotation=60, ha='right', fontsize=6, color=text_col) ax8.set_yticks(range(N_USERS)); ax8.set_yticklabels(USERNAMES, fontsize=7, color=text_col) ax8.set_zlabel('Excess', color=text_col, fontsize=8) ax8.tick_params(colors=text_col) fig.colorbar(surf2, ax=ax8, fraction=0.025, pad=0.1)
ax9 = fig.add_subplot(4, 3, 9, projection='3d') ax9.set_facecolor('#1a1a2e') ax9.set_title('3D Bar: Over-Privilege Score\n(Baseline vs Optimized)', color=text_col, fontsize=10, pad=6)
_xpos = np.arange(N_USERS) _dx = _dy = 0.35 for xi, (oa, oo) in enumerate(zip(op_admin, op_opt)): ax9.bar3d(xi - _dx/2 - 0.02, 0, 0, _dx, _dy, oa, color='#e94560', alpha=0.8, zsort='min') ax9.bar3d(xi - _dx/2 + 0.02, _dy*1.2, 0, _dx, _dy, oo, color='#00b4d8', alpha=0.8, zsort='min') ax9.set_xticks(_xpos) ax9.set_xticklabels(USERNAMES, rotation=45, ha='right', fontsize=6, color=text_col) ax9.set_yticks([0.17, 0.62]) ax9.set_yticklabels(['Admin', 'Opt'], fontsize=8, color=text_col) ax9.set_zlabel('Score', color=text_col, fontsize=8) ax9.tick_params(colors=text_col)
ax10 = fig.add_subplot(4, 3, 10, polar=True) ax10.set_facecolor('#1a1a2e') ax10.set_title('Resource Coverage Radar\n(Admin vs Optimized)', color=text_col, fontsize=10, pad=14)
angles = np.linspace(0, 2 * np.pi, N_RES, endpoint=False).tolist() angles += angles[:1] opt_cover = user_grant_matrix.sum(axis=0) / N_USERS admin_cover = np.ones(N_RES) opt_vals = opt_cover.tolist() + [opt_cover[0]] adm_vals = admin_cover.tolist() + [admin_cover[0]]
ax10.plot(angles, adm_vals, color='#e94560', lw=2, label='Admin') ax10.fill(angles, adm_vals, color='#e94560', alpha=0.20) ax10.plot(angles, opt_vals, color='#00b4d8', lw=2, label='Optimized') ax10.fill(angles, opt_vals, color='#00b4d8', alpha=0.25) ax10.set_xticks(angles[:-1]) ax10.set_xticklabels(short_res, color=text_col, fontsize=7) ax10.tick_params(colors=text_col) ax10.set_facecolor('#1a1a2e') ax10.spines['polar'].set_color('#444') ax10.yaxis.label.set_color(text_col) ax10.tick_params(axis='y', colors='#666', labelsize=6) ax10.legend(loc='upper right', bbox_to_anchor=(1.3, 1.1), facecolor='#1a1a2e', labelcolor=text_col, fontsize=8)
ax11 = fig.add_subplot(4, 3, 11) ax_style(ax11, 'Privilege Efficiency Score\n(Required / Granted)')
eff_opt = np.array([len(results[u]['required']) / max(len(results[u]['granted']), 1) for u in USERNAMES]) eff_admin = np.array([len(USERS[u]) / N_RES for u in USERNAMES])
ax11.plot(USERNAMES, eff_admin, 'o--', color='#e94560', lw=2, label='Admin Baseline', ms=6) ax11.plot(USERNAMES, eff_opt, 's-', color='#00b4d8', lw=2, label='Optimized', ms=6) ax11.fill_between(range(N_USERS), eff_admin, eff_opt, alpha=0.15, color='#f0c040') ax11.set_ylim(0, 1.1) ax11.set_xticks(range(N_USERS)) ax11.set_xticklabels(USERNAMES, rotation=45, ha='right', color=text_col) ax11.set_ylabel('Efficiency (1.0 = perfect)', color=text_col) ax11.axhline(1.0, color='#2ecc71', ls=':', lw=1.2, label='Perfect (1.0)') ax11.legend(facecolor='#1a1a2e', labelcolor=text_col, fontsize=8) ax11.yaxis.label.set_color(text_col)
ax12 = fig.add_subplot(4, 3, 12) ax_style(ax12, 'Optimal Role Assignments per User')
role_assign_matrix = np.zeros((N_USERS, N_ROLES), dtype=int) for i, user in enumerate(USERNAMES): for role in results[user]['best_roles']: j = ROLES.index(role) role_assign_matrix[i, j] = 1
im12 = ax12.imshow(role_assign_matrix, cmap=cmap_div, aspect='auto', vmin=-1, vmax=1) ax12.set_xticks(range(N_ROLES)) ax12.set_xticklabels(ROLES, rotation=45, ha='right', color=text_col, fontsize=8) ax12.set_yticks(range(N_USERS)) ax12.set_yticklabels(USERNAMES, color=text_col) for i in range(N_USERS): for j in range(N_ROLES): if role_assign_matrix[i, j]: ax12.text(j, i, '✓', ha='center', va='center', fontsize=9, color='white', fontweight='bold') plt.colorbar(im12, ax=ax12, fraction=0.03)
plt.suptitle('Access Control Role Design Optimization — Principle of Least Privilege', fontsize=14, color=text_col, y=1.002, fontweight='bold') plt.tight_layout(pad=2.5) plt.savefig('polp_optimization.png', dpi=130, bbox_inches='tight', facecolor=fig.get_facecolor()) plt.show() print("\n[Chart saved as polp_optimization.png]")
|