6
6
from libs .python .helperCommandExecution import login_cf
7
7
from libs .python .helperJson import getJsonFromFile
8
8
import logging
9
+ import sys
10
+ import os
9
11
10
12
log = logging .getLogger (__name__ )
11
13
@@ -119,6 +121,32 @@ def assignUsergroupsToRoleCollection(btpUsecase, rolecollection):
119
121
)
120
122
if idp is not None :
121
123
command += " --of-idp '" + idp + "'"
124
+
125
+ # Additional mapping for custom IdP only relevant if custom IdP is used
126
+ (
127
+ groupForIdp ,
128
+ attributeForIdp ,
129
+ attributeValueForIdp ,
130
+ ) = getCustomIdpMapping (rolecollection )
131
+
132
+ if isMappingForIdpValid (
133
+ groupForIdp , attributeForIdp , attributeValueForIdp
134
+ ):
135
+
136
+ if groupForIdp is not None :
137
+ command += " --to-group '" + groupForIdp + "'"
138
+
139
+ if attributeForIdp is not None :
140
+ command += " --to-attribute '" + attributeForIdp + "'"
141
+ command += (
142
+ " --attribute-value '" + attributeValueForIdp + "'"
143
+ )
144
+ else :
145
+ log .error (
146
+ "Custom IdP configuration is not valid. Please check."
147
+ )
148
+ sys .exit (os .EX_DATAERR )
149
+
122
150
thisResult = runCommandAndGetJsonResult (
123
151
btpUsecase , command , "INFO" , message
124
152
)
@@ -189,6 +217,32 @@ def assignUsersToGlobalAndSubaccount(btpUsecase):
189
217
)
190
218
if idp is not None :
191
219
command += " --of-idp '" + idp + "'"
220
+
221
+ # Additional mapping for custom IdP only relevant if custom IdP is used
222
+ (
223
+ groupForIdp ,
224
+ attributeForIdp ,
225
+ attributeValueForIdp ,
226
+ ) = getCustomIdpMapping (rolecollection )
227
+
228
+ if isMappingForIdpValid (
229
+ groupForIdp , attributeForIdp , attributeValueForIdp
230
+ ):
231
+
232
+ if groupForIdp is not None :
233
+ command += " --to-group '" + groupForIdp + "'"
234
+
235
+ if attributeForIdp is not None :
236
+ command += " --to-attribute '" + attributeForIdp + "'"
237
+ command += (
238
+ " --attribute-value '" + attributeValueForIdp + "'"
239
+ )
240
+ else :
241
+ log .error (
242
+ "Custom IdP configuration is not valid. Please check."
243
+ )
244
+ sys .exit (os .EX_DATAERR )
245
+
192
246
runCommandAndGetJsonResult (btpUsecase , command , "INFO" , message )
193
247
194
248
log .header ("Set administrators for sub account" )
@@ -215,6 +269,32 @@ def assignUsersToGlobalAndSubaccount(btpUsecase):
215
269
)
216
270
if idp is not None :
217
271
command += " --of-idp '" + idp + "'"
272
+
273
+ # Additional mapping for custom IdP only relevant if custom IdP is used
274
+ (
275
+ groupForIdp ,
276
+ attributeForIdp ,
277
+ attributeValueForIdp ,
278
+ ) = getCustomIdpMapping (rolecollection )
279
+
280
+ if isMappingForIdpValid (
281
+ groupForIdp , attributeForIdp , attributeValueForIdp
282
+ ):
283
+
284
+ if groupForIdp is not None :
285
+ command += " --to-group '" + groupForIdp + "'"
286
+
287
+ if attributeForIdp is not None :
288
+ command += " --to-attribute '" + attributeForIdp + "'"
289
+ command += (
290
+ " --attribute-value '" + attributeValueForIdp + "'"
291
+ )
292
+ else :
293
+ log .error (
294
+ "Custom IdP configuration is not valid. Please check."
295
+ )
296
+ sys .exit (os .EX_DATAERR )
297
+
218
298
runCommandAndGetJsonResult (btpUsecase , command , "INFO" , message )
219
299
220
300
@@ -331,6 +411,32 @@ def assignUsersToCustomRoleCollections(btpUsecase):
331
411
)
332
412
if idp is not None :
333
413
command += " --of-idp '" + idp + "'"
414
+
415
+ # Additional mapping for custom IdP only relevant if custom IdP is used
416
+ (
417
+ groupForIdp ,
418
+ attributeForIdp ,
419
+ attributeValueForIdp ,
420
+ ) = getCustomIdpMapping (rolecollection )
421
+
422
+ if isMappingForIdpValid (
423
+ groupForIdp , attributeForIdp , attributeValueForIdp
424
+ ):
425
+
426
+ if groupForIdp is not None :
427
+ command += " --to-group '" + groupForIdp + "'"
428
+
429
+ if attributeForIdp is not None :
430
+ command += " --to-attribute '" + attributeForIdp + "'"
431
+ command += (
432
+ " --attribute-value '" + attributeValueForIdp + "'"
433
+ )
434
+ else :
435
+ log .error (
436
+ "Custom IdP configuration is not valid. Please check."
437
+ )
438
+ sys .exit (os .EX_DATAERR )
439
+
334
440
runCommandAndGetJsonResult (btpUsecase , command , "INFO" , message )
335
441
336
442
@@ -380,6 +486,7 @@ def assignUsersToEnvironments(btpUsecase):
380
486
)
381
487
if idp is not None :
382
488
command += " --origin '" + idp + "'"
489
+
383
490
p = runShellCommandFlex (
384
491
btpUsecase , command , "INFO" , message , False , False
385
492
)
@@ -438,3 +545,36 @@ def determineIdpForRoleCollection(btpUsecase, rolecollection):
438
545
idp = rolecollection .get ("idp" )
439
546
440
547
return idp
548
+
549
+
550
+ def getCustomIdpMapping (rolecollection ):
551
+ groupForIdp = None
552
+ attributeForIdp = None
553
+ attributeValueForIdp = None
554
+
555
+ if rolecollection .get ("group" ):
556
+ groupForIdp = rolecollection .get ("group" )
557
+
558
+ if rolecollection .get ("attribute" ):
559
+ attributeForIdp = rolecollection .get ("attribute" )
560
+
561
+ if rolecollection .get ("attributeValue" ):
562
+ attributeValueForIdp = rolecollection .get ("attributeValue" )
563
+
564
+ return groupForIdp , attributeForIdp , attributeValueForIdp
565
+
566
+
567
+ def isMappingForIdpValid (groupForIdp , attributeForIdp , attributeValueForIdp ):
568
+ if groupForIdp is not None and attributeForIdp is not None :
569
+ log .error (
570
+ "A group and an attribute is configured for the IdP mapping. Only one is allowed."
571
+ )
572
+ return False
573
+ if (attributeForIdp is None and attributeValueForIdp is not None ) or (
574
+ attributeForIdp is not None and attributeValueForIdp is None
575
+ ):
576
+ log .error (
577
+ "Attribute and attributeValue are both required for the IdP mapping. One is missing."
578
+ )
579
+ return False
580
+ return True
0 commit comments