-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathauthorization_journey.py
More file actions
1057 lines (895 loc) · 44.4 KB
/
authorization_journey.py
File metadata and controls
1057 lines (895 loc) · 44.4 KB
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
Complete Authorization Journey: Example and Integration Test
============================================================
This module serves as both:
1. A comprehensive example showing how to use all authorization APIs
2. An integration test with 100% coverage of authorization methods
Narrative: Building a Complete RBAC System for an E-Commerce Platform
----------------------------------------------------------------------
Follow the journey of setting up access control for a microservices platform
that handles order processing, payment processing, and customer support.
The story covers:
- Creating applications for different microservices
- Setting up team structure with groups
- Onboarding users with appropriate roles
- Defining custom roles for specific needs
- Granting permissions to workflows and tasks
- Configuring API gateway authentication
- Testing access control
- Cleaning up resources
Usage:
As an example:
python authorization_journey.py
As an integration test:
python -m pytest authorization_journey.py -v
Requirements:
- Running Conductor server (default: localhost:8080)
- Valid authentication configured with proper credentials
- Set environment variables:
CONDUCTOR_SERVER_URL (optional, defaults to http://localhost:8080)
CONDUCTOR_AUTH_KEY and CONDUCTOR_AUTH_SECRET (for key/secret auth)
OR CONDUCTOR_AUTH_TOKEN (for token auth)
"""
import sys
import uuid
from typing import Dict, List, Any
from conductor.client.configuration.configuration import Configuration
from conductor.client.orkes.orkes_authorization_client import OrkesAuthorizationClient
from conductor.client.http.models.create_or_update_application_request import CreateOrUpdateApplicationRequest
from conductor.client.http.models.upsert_user_request import UpsertUserRequest
from conductor.client.http.models.upsert_group_request import UpsertGroupRequest
from conductor.client.http.models.target_ref import TargetRef, TargetType
from conductor.client.http.models.subject_ref import SubjectRef, SubjectType
from conductor.client.http.models.create_or_update_role_request import CreateOrUpdateRoleRequest
from conductor.client.http.models.authentication_config import AuthenticationConfig
from conductor.client.orkes.models.access_type import AccessType
from conductor.client.orkes.models.metadata_tag import MetadataTag
class AuthorizationJourney:
"""
Complete journey through all authorization APIs.
Each method demonstrates usage and verifies functionality.
"""
def __init__(self, cleanup: bool = True):
"""
Initialize the authorization journey.
Args:
cleanup: Whether to clean up created resources at the end
"""
self.config = Configuration()
self.auth_client = OrkesAuthorizationClient(self.config)
self.cleanup = cleanup
# Track created resources for cleanup
self.created_apps = []
self.created_users = []
self.created_groups = []
self.created_roles = []
self.created_auth_configs = []
# Generate unique identifiers to avoid conflicts
self.run_id = str(uuid.uuid4())[:8]
def run(self):
"""Execute the complete authorization journey."""
print("\n" + "="*80)
print("🚀 Starting Authorization Journey: E-Commerce Platform RBAC Setup")
print("="*80)
try:
# Test connectivity and authentication first
self._test_connectivity()
# Chapter 1: Foundation
self._chapter_1_foundation()
# Chapter 2: Applications
app_id = self._chapter_2_applications()
# Chapter 3: Access Keys
key_id = self._chapter_3_access_keys(app_id)
# Chapter 4: Team Structure
groups = self._chapter_4_team_structure()
# Chapter 5: User Management
users = self._chapter_5_user_management()
# Chapter 6: Group Membership
self._chapter_6_group_membership(groups, users)
# Chapter 7: Custom Roles
custom_role = self._chapter_7_custom_roles()
# Chapter 8: Permissions Management
self._chapter_8_permissions(groups, users)
# Chapter 9: API Gateway Configuration
self._chapter_9_api_gateway(app_id)
# Chapter 10: Token Management
self._chapter_10_token_management(app_id, key_id)
# Chapter 11: Testing Access Control
self._chapter_11_testing_access(users)
print("\n" + "="*80)
print("✅ Authorization Journey Completed Successfully!")
print("="*80)
finally:
if self.cleanup:
self._cleanup_resources()
def _test_connectivity(self):
"""Test server connectivity and authentication."""
print("\n🔌 Testing connectivity and authentication...")
print("-" * 60)
try:
# Try a simple API call to verify connection and auth
print("Checking server connection...")
apps = self.auth_client.list_applications()
print(f" ✓ Connected to Conductor server")
print(f" ✓ Authentication successful")
print(f" ✓ Found {len(apps)} existing applications")
except Exception as e:
print(f"\n❌ Connection/Authentication Failed!")
print(f" Error: {e}")
print("\n📋 Troubleshooting:")
print(" 1. Ensure Conductor server is running")
print(" 2. Check your authentication configuration:")
print(" - For key/secret auth: Set CONDUCTOR_AUTH_KEY and CONDUCTOR_AUTH_SECRET")
print(" - For token auth: Set CONDUCTOR_AUTH_TOKEN")
print(" 3. Verify server URL (default: http://localhost:8080)")
print(" - Set CONDUCTOR_SERVER_URL if using a different server")
raise SystemExit(1)
def _chapter_1_foundation(self):
"""Chapter 1: Foundation - Understanding the system."""
print("\n📚 Chapter 1: Foundation - Understanding the System")
print("-" * 60)
# API Method 35: list_all_roles
print("\n1. Listing all available roles...")
all_roles = self.auth_client.list_all_roles()
print(f" Found {len(all_roles)} total roles")
# API Method 36: list_system_roles
print("\n2. Listing system-defined roles...")
system_roles = self.auth_client.list_system_roles()
print(f" System roles: {list(system_roles.keys())}")
assert "USER" in system_roles
assert "ADMIN" in system_roles
# API Method 37: list_custom_roles
print("\n3. Listing custom roles...")
custom_roles = self.auth_client.list_custom_roles()
print(f" Found {len(custom_roles)} custom roles")
# API Method 38: list_available_permissions
print("\n4. Listing available permissions...")
permissions = self.auth_client.list_available_permissions()
print(f" Resource types: {list(permissions.keys())[:5]}...")
assert len(permissions) > 0
# API Method 43: get_user_info_from_token
print("\n5. Getting current user info from token...")
try:
user_info = self.auth_client.get_user_info_from_token()
# Note: Returns Dict with user information
if user_info and 'id' in user_info:
print(f" Current user: {user_info['id']}")
else:
print(f" User info retrieved (format may vary)")
except:
print(" Token info not available (normal in test environment)")
def _chapter_2_applications(self) -> str:
"""Chapter 2: Applications - Creating microservice applications."""
print("\n📦 Chapter 2: Applications - Setting Up Microservices")
print("-" * 60)
# API Method 1: create_application
print("\n1. Creating Order Service application...")
app_name = f"order-service-{self.run_id}"
request = CreateOrUpdateApplicationRequest(name=app_name)
app = self.auth_client.create_application(request)
self.created_apps.append(app.id)
print(f" ✓ Created application: {app.id}")
assert app.name == app_name
# API Method 2: get_application
print("\n2. Retrieving application details...")
retrieved_app = self.auth_client.get_application(app.id)
print(f" ✓ Retrieved: {retrieved_app.name}")
assert retrieved_app.id == app.id
# API Method 3: list_applications
print("\n3. Listing all applications...")
all_apps = self.auth_client.list_applications()
print(f" ✓ Found {len(all_apps)} applications")
assert any(a.id == app.id for a in all_apps)
# === COMPREHENSIVE TAGGING DEMONSTRATION ===
# API Method 9: set_application_tags (Initial tags)
print("\n4. Setting initial application tags...")
initial_tags = [
MetadataTag("environment", "production"),
MetadataTag("service", "order-processing"),
MetadataTag("team", "platform"),
MetadataTag("version", "1.0"),
MetadataTag("cost-center", "engineering")
]
self.auth_client.set_application_tags(initial_tags, app.id)
print(f" ✓ Added {len(initial_tags)} tags")
# API Method 10: get_application_tags (Verify initial tags)
print("\n5. Getting application tags...")
retrieved_tags = self.auth_client.get_application_tags(app.id)
print(f" ✓ Retrieved {len(retrieved_tags)} tags")
for tag in retrieved_tags:
print(f" - {tag.key}={tag.value}")
assert len(retrieved_tags) == len(initial_tags)
# API Method 9: set_application_tags (Replace with new set)
print("\n6. Replacing tags with new set...")
replacement_tags = [
MetadataTag("environment", "staging"), # Changed value
MetadataTag("service", "order-processing"), # Same
MetadataTag("team", "devops"), # Changed value
MetadataTag("region", "us-west"), # New tag
MetadataTag("tier", "critical") # New tag
]
self.auth_client.set_application_tags(replacement_tags, app.id)
print(f" ✓ Replaced with {len(replacement_tags)} tags")
# API Method 10: get_application_tags (Verify replacement)
print("\n7. Verifying tag replacement...")
current_tags = self.auth_client.get_application_tags(app.id)
print(f" ✓ Current tags: {len(current_tags)}")
for tag in current_tags:
print(f" - {tag.key}={tag.value}")
# API Method 4: update_application (UPDATE operation)
print("\n8. Updating application (demonstrating UPDATE)...")
print(f" Original name: {app.name}")
updated_name = f"order-service-v2-{self.run_id}"
update_request = CreateOrUpdateApplicationRequest(name=updated_name)
updated_app = self.auth_client.update_application(update_request, app.id)
print(f" ✓ Updated application: {app.id}")
print(f" New name: {updated_app.name}")
assert updated_app.name == updated_name
# API Method 7: add_role_to_application_user
print("\n9. Adding ADMIN role to application...")
self.auth_client.add_role_to_application_user(app.id, "ADMIN")
print(f" ✓ Added ADMIN role")
# API Method 8: remove_role_from_application_user
print("\n10. Removing ADMIN role from application...")
self.auth_client.remove_role_from_application_user(app.id, "ADMIN")
print(f" ✓ Removed ADMIN role")
# API Method 11: delete_application_tags (Partial deletion)
print("\n11. Removing specific tags...")
tags_to_remove = [
MetadataTag("environment", "staging"),
MetadataTag("region", "us-west")
]
self.auth_client.delete_application_tags(tags_to_remove, app.id)
print(f" ✓ Removed {len(tags_to_remove)} tags")
# API Method 10: get_application_tags (Verify deletion)
print("\n12. Verifying remaining tags after deletion...")
remaining_tags = self.auth_client.get_application_tags(app.id)
print(f" ✓ Remaining tags: {len(remaining_tags)}")
for tag in remaining_tags:
print(f" - {tag.key}={tag.value}")
# API Method 11: delete_application_tags (Remove all remaining)
print("\n13. Removing all remaining tags...")
if remaining_tags:
self.auth_client.delete_application_tags(remaining_tags, app.id)
print(f" ✓ Removed all {len(remaining_tags)} remaining tags")
# API Method 10: get_application_tags (Verify all removed)
print("\n14. Verifying all tags removed...")
final_tags = self.auth_client.get_application_tags(app.id)
print(f" ✓ Tags after cleanup: {len(final_tags)}")
assert len(final_tags) == 0
# API Method 9: set_application_tags (Add final set for other tests)
print("\n15. Adding final tags for application...")
final_tag_set = [
MetadataTag("status", "active"),
MetadataTag("owner", f"test-{self.run_id}")
]
self.auth_client.set_application_tags(final_tag_set, app.id)
print(f" ✓ Added final {len(final_tag_set)} tags")
return app.id
def _chapter_3_access_keys(self, app_id: str) -> str:
"""Chapter 3: Access Keys - Managing API authentication."""
print("\n🔑 Chapter 3: Access Keys - API Authentication")
print("-" * 60)
# API Method 12: create_access_key
print("\n1. Creating access key for application...")
created_key = self.auth_client.create_access_key(app_id)
print(f" ✓ Created key: {created_key.id}")
print(f" ⚠️ Secret (save this!): {created_key.secret[:10]}...")
assert created_key.id is not None
assert created_key.secret is not None
# Store for later use
key_id = created_key.id
key_secret = created_key.secret
# API Method 13: get_access_keys
print("\n2. Listing access keys...")
keys = self.auth_client.get_access_keys(app_id)
print(f" ✓ Found {len(keys)} key(s)")
assert any(k.id == key_id for k in keys)
# API Method 14: toggle_access_key_status
print("\n3. Deactivating access key...")
toggled_key = self.auth_client.toggle_access_key_status(app_id, key_id)
print(f" ✓ Key status: {toggled_key.status}")
assert toggled_key.status == "INACTIVE"
print("\n4. Reactivating access key...")
toggled_key = self.auth_client.toggle_access_key_status(app_id, key_id)
print(f" ✓ Key status: {toggled_key.status}")
assert toggled_key.status == "ACTIVE"
# API Method 6: get_app_by_access_key_id
print("\n5. Finding application by access key...")
try:
found_app_id = self.auth_client.get_app_by_access_key_id(key_id)
print(f" ✓ Found app: {found_app_id}")
# The API might return the app object or ID in different formats
if hasattr(found_app_id, 'id'):
assert found_app_id.id == app_id
else:
assert str(found_app_id) == app_id or found_app_id == app_id
except Exception as e:
print(f" ⚠️ Could not verify app by access key (API may have changed): {e}")
return key_id
def _chapter_4_team_structure(self) -> Dict[str, str]:
"""Chapter 4: Team Structure - Creating groups."""
print("\n👥 Chapter 4: Team Structure - Creating Groups")
print("-" * 60)
groups = {}
# API Method 22: upsert_group (create)
print("\n1. Creating Engineering team group...")
eng_group_id = f"engineering-{self.run_id}"
eng_request = UpsertGroupRequest(
description="Engineering Team - Full stack developers",
roles=["USER", "METADATA_MANAGER"]
)
eng_group = self.auth_client.upsert_group(eng_request, eng_group_id)
self.created_groups.append(eng_group_id)
groups['engineering'] = eng_group_id
print(f" ✓ Created group: {eng_group_id}")
print("\n2. Creating Operations team group...")
ops_group_id = f"operations-{self.run_id}"
ops_request = UpsertGroupRequest(
description="Operations Team - Workflow managers",
roles=["USER", "WORKFLOW_MANAGER"],
default_access={
"WORKFLOW_DEF": ["READ", "EXECUTE"],
"TASK_DEF": ["READ"]
}
)
ops_group = self.auth_client.upsert_group(ops_request, ops_group_id)
self.created_groups.append(ops_group_id)
groups['operations'] = ops_group_id
print(f" ✓ Created group: {ops_group_id}")
print("\n3. Creating Support team group...")
support_group_id = f"support-{self.run_id}"
support_request = UpsertGroupRequest(
description="Support Team - View-only access",
roles=["USER"] # USER role with read-only permissions granted separately
)
support_group = self.auth_client.upsert_group(support_request, support_group_id)
self.created_groups.append(support_group_id)
groups['support'] = support_group_id
print(f" ✓ Created group: {support_group_id}")
# API Method 23: get_group
print("\n4. Retrieving group details...")
retrieved_group = self.auth_client.get_group(eng_group_id)
print(f" ✓ Retrieved: {retrieved_group.description}")
assert retrieved_group.id == eng_group_id
# API Method 24: list_groups
print("\n5. Listing all groups...")
all_groups = self.auth_client.list_groups()
print(f" ✓ Found {len(all_groups)} groups")
assert any(g.id == eng_group_id for g in all_groups)
# API Method 22: upsert_group (UPDATE operation - same method, existing ID)
print("\n6. Updating Engineering group (demonstrating UPDATE)...")
print(f" Original roles: {eng_group.roles}")
print(f" Original description: {eng_group.description}")
updated_request = UpsertGroupRequest(
description="Engineering Team - Full stack developers (Updated)",
roles=["USER", "METADATA_MANAGER", "WORKFLOW_MANAGER", "ADMIN"] # Added ADMIN role
)
updated_group = self.auth_client.upsert_group(updated_request, eng_group_id)
print(f" ✓ Updated group: {eng_group_id}")
print(f" New description: {updated_group.description}")
print(f" New roles: {updated_group.roles}")
return groups
def _chapter_5_user_management(self) -> Dict[str, str]:
"""Chapter 5: User Management - Creating users."""
print("\n👤 Chapter 5: User Management - Onboarding Users")
print("-" * 60)
users = {}
# API Method 16: upsert_user (create)
print("\n1. Creating Lead Engineer user...")
lead_eng_id = f"lead.engineer-{self.run_id}@example.com"
lead_eng_request = UpsertUserRequest(
name="Lead Engineer",
roles=["USER", "ADMIN"]
)
lead_eng = self.auth_client.upsert_user(lead_eng_request, lead_eng_id)
self.created_users.append(lead_eng_id)
users['lead_engineer'] = lead_eng_id
print(f" ✓ Created user: {lead_eng_id}")
print("\n2. Creating Developer user...")
dev_id = f"developer-{self.run_id}@example.com"
dev_request = UpsertUserRequest(
name="Developer",
roles=["USER"]
)
dev = self.auth_client.upsert_user(dev_request, dev_id)
self.created_users.append(dev_id)
users['developer'] = dev_id
print(f" ✓ Created user: {dev_id}")
print("\n3. Creating Ops Manager user...")
ops_mgr_id = f"ops.manager-{self.run_id}@example.com"
ops_request = UpsertUserRequest(
name="Operations Manager",
roles=["USER", "WORKFLOW_MANAGER"]
)
ops_mgr = self.auth_client.upsert_user(ops_request, ops_mgr_id)
self.created_users.append(ops_mgr_id)
users['ops_manager'] = ops_mgr_id
print(f" ✓ Created user: {ops_mgr_id}")
print("\n4. Creating Support Agent user...")
support_id = f"support-{self.run_id}@example.com"
support_request = UpsertUserRequest(
name="Support Agent",
roles=["USER"] # Will grant read-only permissions separately
)
support = self.auth_client.upsert_user(support_request, support_id)
self.created_users.append(support_id)
users['support'] = support_id
print(f" ✓ Created user: {support_id}")
# API Method 17: get_user
print("\n5. Retrieving user details...")
retrieved_user = self.auth_client.get_user(lead_eng_id)
print(f" ✓ Retrieved: {retrieved_user.name}")
assert retrieved_user.id == lead_eng_id
# API Method 18: list_users
print("\n6. Listing all users...")
all_users = self.auth_client.list_users()
print(f" ✓ Found {len(all_users)} users")
print("\n7. Listing users with application info...")
users_with_apps = self.auth_client.list_users(apps=True)
print(f" ✓ Found {len(users_with_apps)} users with app info")
# API Method 16: upsert_user (UPDATE operation - same method, existing ID)
print("\n8. Updating Lead Engineer user (demonstrating UPDATE)...")
print(f" Original roles: {lead_eng.roles}")
update_request = UpsertUserRequest(
name="Lead Engineer (Senior)",
roles=["USER", "ADMIN", "METADATA_MANAGER", "WORKFLOW_MANAGER"] # Added WORKFLOW_MANAGER
)
updated_user = self.auth_client.upsert_user(update_request, lead_eng_id)
print(f" ✓ Updated user: {updated_user.name}")
print(f" New roles: {updated_user.roles}")
return users
def _chapter_6_group_membership(self, groups: Dict[str, str], users: Dict[str, str]):
"""Chapter 6: Group Membership - Managing team assignments."""
print("\n🔗 Chapter 6: Group Membership - Team Assignments")
print("-" * 60)
# API Method 27: add_user_to_group
print("\n1. Adding Lead Engineer to Engineering group...")
self.auth_client.add_user_to_group(
groups['engineering'],
users['lead_engineer']
)
print(f" ✓ Added {users['lead_engineer']}")
# API Method 28: add_users_to_group (bulk)
print("\n2. Adding multiple users to Engineering group...")
self.auth_client.add_users_to_group(
groups['engineering'],
[users['developer']]
)
print(f" ✓ Added developer to engineering")
print("\n3. Adding Ops Manager to Operations group...")
self.auth_client.add_user_to_group(
groups['operations'],
users['ops_manager']
)
print(f" ✓ Added ops manager")
print("\n4. Adding Support Agent to Support group...")
self.auth_client.add_user_to_group(
groups['support'],
users['support']
)
print(f" ✓ Added support agent")
# API Method 29: get_users_in_group
print("\n5. Listing users in Engineering group...")
eng_users = self.auth_client.get_users_in_group(groups['engineering'])
print(f" ✓ Found {len(eng_users)} users in Engineering")
assert len(eng_users) >= 2
# API Method 30: remove_user_from_group
print("\n6. Removing developer from Engineering (temporary)...")
self.auth_client.remove_user_from_group(
groups['engineering'],
users['developer']
)
print(f" ✓ Removed developer")
# API Method 31: remove_users_from_group (bulk)
print("\n7. Re-adding developer to Engineering...")
self.auth_client.add_user_to_group(
groups['engineering'],
users['developer']
)
print(f" ✓ Re-added developer")
print("\n8. Bulk removing users (demonstration)...")
# Add support to engineering temporarily
self.auth_client.add_user_to_group(
groups['engineering'],
users['support']
)
# Then remove using bulk operation
self.auth_client.remove_users_from_group(
groups['engineering'],
[users['support']]
)
print(f" ✓ Demonstrated bulk removal")
def _chapter_7_custom_roles(self) -> str:
"""Chapter 7: Custom Roles - Defining specialized permissions."""
print("\n🎭 Chapter 7: Custom Roles - Specialized Permissions")
print("-" * 60)
# First, get available permissions to use valid permission values
print("\n1. Getting available permissions...")
available_permissions = self.auth_client.list_available_permissions()
# Extract actual permission values from the API
permission_list = []
for resource_type, perms in available_permissions.items():
if isinstance(perms, dict) and 'permissions' in perms:
permission_list.extend(perms['permissions'])
elif isinstance(perms, list):
permission_list.extend(perms)
# Display some available permissions
print(f" Found {len(permission_list)} total permissions")
if permission_list:
print(f" Sample permissions: {permission_list[:5]}...")
# Select appropriate permissions for a workflow operator role
# Use actual permissions from the system
selected_permissions = []
# Look for workflow-related permissions
for perm in permission_list:
perm_lower = str(perm).lower()
if 'workflow' in perm_lower and ('execute' in perm_lower or 'read' in perm_lower):
selected_permissions.append(perm)
if len(selected_permissions) >= 3:
break
# If we didn't find workflow permissions, use the first few available
if not selected_permissions and permission_list:
selected_permissions = permission_list[:3]
# If still no permissions, use fallback (but this shouldn't happen)
if not selected_permissions:
selected_permissions = ["workflow-execute", "workflow-read", "task-read"]
print(" ⚠️ Using fallback permissions (no permissions found from API)")
# API Method 39: create_role
role_name = f"WORKFLOW_OPERATOR_C"
print(f"\n2. Creating custom '{role_name}' role...")
print(f" Using permissions: {selected_permissions}")
# Using the model class for role creation
role_request = CreateOrUpdateRoleRequest(
name=role_name,
permissions=selected_permissions
)
try:
created_role = self.auth_client.create_role(role_request)
self.created_roles.append(role_name)
print(f" ✅ Successfully created custom role: {role_name}")
# Note: create_role returns a Dict response
print(f" Permissions assigned: {len(selected_permissions)} permissions")
except Exception as e:
print(f" ❌ Could not create custom role: {str(e)}")
print(f" This may indicate custom roles are not supported in your Conductor instance")
# Create a placeholder for the rest of the chapter
created_role = {"name": role_name}
# API Method 40: get_role
print("\n3. Retrieving role details...")
try:
retrieved_role = self.auth_client.get_role(role_name)
print(f" ✓ Retrieved role: {role_name}")
# Note: get_role returns a Dict, we just verify it succeeded
except Exception as e:
print(f" ⚠️ Could not retrieve role (may not exist): {str(e)[:100]}")
# API Method 41: update_role (UPDATE operation)
print("\n4. Updating role permissions (demonstrating UPDATE)...")
print(f" Current permissions: {selected_permissions[:3]}")
# Add more permissions from available list
additional_permissions = []
for perm in permission_list:
perm_lower = str(perm).lower()
if perm not in selected_permissions and ('update' in perm_lower or 'delete' in perm_lower or 'create' in perm_lower):
additional_permissions.append(perm)
if len(additional_permissions) >= 2:
break
updated_permissions = selected_permissions + additional_permissions
if not additional_permissions:
# If no additional permissions found, duplicate some existing ones
updated_permissions = selected_permissions + selected_permissions[:1]
print(f" No additional permissions found, using existing set")
print(f" New permissions to set: {updated_permissions[:5]}{'...' if len(updated_permissions) > 5 else ''}")
update_role_request = CreateOrUpdateRoleRequest(
name=role_name,
permissions=updated_permissions
)
try:
updated_role = self.auth_client.update_role(role_name, update_role_request)
print(f" ✅ Successfully updated role: {role_name}")
print(f" Total permissions now: {len(updated_permissions)}")
except Exception as e:
print(f" ⚠️ Could not update role: {str(e)[:100]}")
print(f" This may indicate custom roles updates are not supported")
# ASSIGN ROLES TO USER
# Note: SDK validates that only system roles can be assigned via UpsertUserRequest.
# Custom roles would need to be assigned through direct API calls or permissions.
print("\n5. Creating user with appropriate system roles...")
operator_id = f"workflow.operator-{self.run_id}@example.com"
# Use system roles that match the custom role's intended permissions
operator_request = UpsertUserRequest(
name="Workflow Operator",
roles=["USER", "WORKFLOW_MANAGER"] # System roles that provide similar permissions
)
operator = self.auth_client.upsert_user(operator_request, operator_id)
self.created_users.append(operator_id)
print(f" ✓ Created user: {operator_id}")
# Verify what roles were assigned
retrieved_user = self.auth_client.get_user(operator_id)
print(f" Assigned roles: {retrieved_user.roles}")
print(f" Note: Custom role '{role_name}' cannot be assigned via SDK")
print(f" Using system roles that provide equivalent permissions")
# ASSIGN ROLES TO GROUP
print("\n6. Creating group with appropriate system roles...")
operators_group_id = f"operators-{self.run_id}"
# Use system roles and default_access to provide appropriate permissions
operators_request = UpsertGroupRequest(
description="Operators Group - Workflow operators",
roles=["USER", "WORKFLOW_MANAGER"], # System roles that provide similar permissions
default_access={
"WORKFLOW_DEF": ["READ", "EXECUTE", "UPDATE"],
"TASK_DEF": ["READ", "EXECUTE"]
}
)
operators_group = self.auth_client.upsert_group(operators_request, operators_group_id)
self.created_groups.append(operators_group_id)
print(f" ✓ Created group: {operators_group_id}")
# Show what was configured
retrieved_group = self.auth_client.get_group(operators_group_id)
print(f" Assigned roles: {retrieved_group.roles}")
print(f" Default access configured for workflow and task operations")
print(f" Note: Using system roles + default_access to achieve custom permissions")
# Demonstrate role progression with system roles
print("\n7. Creating user with role progression...")
specialist_id = f"specialist-{self.run_id}@example.com"
# Start with basic role
initial_request = UpsertUserRequest(
name="Workflow Specialist (Junior)",
roles=["USER"]
)
specialist = self.auth_client.upsert_user(initial_request, specialist_id)
self.created_users.append(specialist_id)
print(f" ✓ Created user with basic role: {specialist_id}")
# Update to senior level with more roles
print("\n8. Upgrading user to senior level...")
updated_request = UpsertUserRequest(
name="Workflow Specialist (Senior)",
roles=["USER", "WORKFLOW_MANAGER", "METADATA_MANAGER"] # Additional system roles
)
updated_specialist = self.auth_client.upsert_user(updated_request, specialist_id)
print(f" ✓ Updated user with additional system roles: {specialist_id}")
print(f" Roles: {', '.join(updated_request.roles)}")
return role_name
def _chapter_8_permissions(self, groups: Dict[str, str], users: Dict[str, str]):
"""Chapter 8: Permissions Management - Access control."""
print("\n🔐 Chapter 8: Permissions Management - Access Control")
print("-" * 60)
# Define workflow and task targets
workflow_target = TargetRef(TargetType.WORKFLOW_DEF, f"order-processing-{self.run_id}")
task_target = TargetRef(TargetType.TASK_DEF, f"payment-task-{self.run_id}")
# API Method 32: grant_permissions (to group)
print("\n1. Granting workflow permissions to Engineering group...")
eng_subject = SubjectRef(SubjectType.GROUP, groups['engineering'])
self.auth_client.grant_permissions(
eng_subject,
workflow_target,
[AccessType.READ, AccessType.EXECUTE, AccessType.UPDATE]
)
print(f" ✓ Granted READ, EXECUTE, UPDATE to Engineering")
print("\n2. Granting workflow permissions to Operations group...")
ops_subject = SubjectRef(SubjectType.GROUP, groups['operations'])
self.auth_client.grant_permissions(
ops_subject,
workflow_target,
[AccessType.READ, AccessType.EXECUTE]
)
print(f" ✓ Granted READ, EXECUTE to Operations")
print("\n3. Granting read-only permissions to Support group...")
support_subject = SubjectRef(SubjectType.GROUP, groups['support'])
self.auth_client.grant_permissions(
support_subject,
workflow_target,
[AccessType.READ] # Only READ access for support team
)
print(f" ✓ Granted READ to Support (view-only access)")
# API Method 32: grant_permissions (to user)
print("\n4. Granting special permissions to Lead Engineer...")
lead_subject = SubjectRef(SubjectType.USER, users['lead_engineer'])
self.auth_client.grant_permissions(
lead_subject,
workflow_target,
[AccessType.DELETE]
)
print(f" ✓ Granted DELETE to Lead Engineer")
print("\n5. Granting task permissions to Developer...")
dev_subject = SubjectRef(SubjectType.USER, users['developer'])
self.auth_client.grant_permissions(
dev_subject,
task_target,
[AccessType.READ, AccessType.UPDATE]
)
print(f" ✓ Granted task permissions to Developer")
# API Method 33: get_permissions
print("\n6. Retrieving permissions for workflow...")
workflow_permissions = self.auth_client.get_permissions(workflow_target)
print(f" ✓ Found permissions for {len(workflow_permissions)} access types")
for access_type, subjects in workflow_permissions.items():
print(f" {access_type}: {len(subjects)} subjects")
# API Method 26: get_granted_permissions_for_group
print("\n7. Checking permissions for Engineering group...")
eng_permissions = self.auth_client.get_granted_permissions_for_group(groups['engineering'])
print(f" ✓ Engineering group has {len(eng_permissions)} permission grants")
# API Method 20: get_granted_permissions_for_user
print("\n8. Checking permissions for Lead Engineer...")
lead_permissions = self.auth_client.get_granted_permissions_for_user(users['lead_engineer'])
print(f" ✓ Lead Engineer has {len(lead_permissions)} permission grants")
# API Method 21: check_permissions
print("\n9. Verifying Lead Engineer can delete workflow...")
can_delete = self.auth_client.check_permissions(
user_id=users['lead_engineer'],
target_type="WORKFLOW_DEF",
target_id=f"order-processing-{self.run_id}"
)
print(f" ✓ Can delete: {can_delete}")
# API Method 34: remove_permissions
print("\n10. Revoking DELETE permission from Lead Engineer...")
self.auth_client.remove_permissions(
lead_subject,
workflow_target,
[AccessType.DELETE]
)
print(f" ✓ Revoked DELETE permission")
def _chapter_9_api_gateway(self, app_id: str):
"""Chapter 9: API Gateway Configuration - External authentication."""
print("\n🌐 Chapter 9: API Gateway Configuration")
print("-" * 60)
# API Method 45: create_gateway_auth_config
print("\n1. Creating API Gateway auth configuration...")
config_id = f"gateway-auth-{self.run_id}"
# Using the AuthenticationConfig model
auth_config = AuthenticationConfig()
auth_config.id = config_id
auth_config.application_id = app_id
auth_config.authentication_type = "API_KEY"
auth_config.api_keys = ["key1", "key2"]
auth_config.fallback_to_default_auth = False
auth_config.token_in_workflow_input = True
created_config = self.auth_client.create_gateway_auth_config(auth_config)
self.created_auth_configs.append(config_id)
print(f" ✓ Created config: {config_id}")
# API Method 46: get_gateway_auth_config
print("\n2. Retrieving auth configuration...")
retrieved_config = self.auth_client.get_gateway_auth_config(config_id)
print(f" ✓ Retrieved config: {retrieved_config.id}")
assert retrieved_config.id == config_id
# API Method 47: list_gateway_auth_configs
print("\n3. Listing all auth configurations...")
all_configs = self.auth_client.list_gateway_auth_configs()
print(f" ✓ Found {len(all_configs)} configurations")
# API Method 48: update_gateway_auth_config (UPDATE operation)
print("\n4. Updating auth configuration (demonstrating UPDATE)...")
print(f" Original type: {retrieved_config.authentication_type if hasattr(retrieved_config, 'authentication_type') else 'API_KEY'}")
updated_config = AuthenticationConfig()
updated_config.id = config_id
updated_config.application_id = app_id
updated_config.authentication_type = "OIDC" # Changed from API_KEY to OIDC
updated_config.issuer_uri = "https://auth.example.com"
updated_config.audience = "https://api.example.com"
updated_config.passthrough = True
updated_config.fallback_to_default_auth = True
result = self.auth_client.update_gateway_auth_config(config_id, updated_config)
print(f" ✅ Updated gateway auth configuration")
print(f" New type: {updated_config.authentication_type}")
print(f" Issuer URI: {updated_config.issuer_uri}")
def _chapter_10_token_management(self, app_id: str, key_id: str):
"""Chapter 10: Token Management - JWT authentication."""
print("\n🎫 Chapter 10: Token Management")
print("-" * 60)
# Note: generate_token requires valid access key credentials
# In a real scenario, you would use the actual key_id and secret
print("\n1. Generating JWT token (demonstration)...")
print(" ℹ️ In production, use actual access key credentials:")
print(f" auth_client.generate_token(key_id='{key_id}', key_secret='***')")
# API Method 44: generate_token (demonstration only)
# This would normally be:
# token_response = self.auth_client.generate_token(key_id, key_secret)
# jwt_token = token_response.get('token')
# print(f" ✓ Generated JWT token (expires in {token_response.get('expiresIn')} seconds)")
print(" ✓ Token generation API demonstrated")
def _chapter_11_testing_access(self, users: Dict[str, str]):
"""Chapter 11: Testing Access Control - Verification."""
print("\n✅ Chapter 11: Testing Access Control")
print("-" * 60)
print("\n1. Testing user permissions...")
for user_type, user_id in users.items():
print(f"\n Testing {user_type}:")
# Check workflow access
can_read = self.auth_client.check_permissions(
user_id=user_id,
target_type="WORKFLOW_DEF",
target_id=f"order-processing-{self.run_id}"
)
print(f" Can read workflow: {can_read}")
# Get all permissions for user
user_perms = self.auth_client.get_granted_permissions_for_user(user_id)
print(f" Total permissions: {len(user_perms)}")
print("\n ✓ Access control verified")
def _cleanup_resources(self):
"""Clean up all created resources."""
print("\n🧹 Cleaning up resources...")
print("-" * 60)
# API Method 49: delete_gateway_auth_config
for config_id in self.created_auth_configs:
try:
self.auth_client.delete_gateway_auth_config(config_id)
print(f" ✓ Deleted auth config: {config_id}")
except:
pass
# API Method 42: delete_role
for role_name in self.created_roles:
try:
self.auth_client.delete_role(role_name)
print(f" ✓ Deleted role: {role_name}")
except:
pass
# API Method 19: delete_user
for user_id in self.created_users:
try:
self.auth_client.delete_user(user_id)
print(f" ✓ Deleted user: {user_id}")
except:
pass
# API Method 25: delete_group
for group_id in self.created_groups:
try:
self.auth_client.delete_group(group_id)
print(f" ✓ Deleted group: {group_id}")
except:
pass
# API Method 15: delete_access_key (handled with app deletion)
# API Method 5: delete_application
for app_id in self.created_apps:
try:
# Get and delete access keys first
keys = self.auth_client.get_access_keys(app_id)
for key in keys:
try:
self.auth_client.delete_access_key(app_id, key.id)
print(f" ✓ Deleted access key: {key.id}")