@@ -82,7 +82,7 @@ def policy_document_from_jinja(c, policy_name, model):
82
82
return (template_json )
83
83
84
84
85
- def build_role_trust (c , trusts ):
85
+ def build_role_trust (c , trusts , SamlDirect = False ):
86
86
policy = {
87
87
"Version" : "2012-10-17" ,
88
88
"Statement" : [],
@@ -124,14 +124,23 @@ def build_role_trust(c, trusts):
124
124
aws_principals .append (
125
125
GetAtt (scrub_name ("{}Role" .format (trust )), "Arn" )
126
126
)
127
- # Next see if we match our SAML trust.
127
+ # Next see if we match our SAML trust if saml_direct: False
128
128
elif trust == c .saml_provider :
129
129
saml_principals .append (
130
130
"arn:aws:iam::"
131
131
+ c .parent_account_id
132
132
+ ":saml-provider/"
133
133
+ c .saml_provider
134
134
)
135
+ # Check if this is a SAML Direct config (if saml_direct: True)
136
+ elif SamlDirect :
137
+ saml_principals .append (
138
+ "arn:aws:iam::"
139
+ + c .current_account_id
140
+ + ":saml-provider/"
141
+ + trust
142
+ )
143
+
135
144
# See if we match a user or role ARN Principal
136
145
elif re .match ("arn:aws:iam::\d{12}:(user|role)/.*?" , trust ) or \
137
146
re .match ("arn:aws:sts::\d{12}:assumed-role/.*?/.*?" , trust ):
@@ -360,11 +369,11 @@ def create_instance_profile(c, RoleName, model, named=False):
360
369
])
361
370
362
371
363
- def add_role (c , RoleName , model , named = False ):
372
+ def add_role (c , RoleName , model , named = False , SamlDirect = False ):
364
373
cfn_name = scrub_name (RoleName + "Role" )
365
374
kw_args = {
366
375
"Path" : "/" ,
367
- "AssumeRolePolicyDocument" : build_role_trust (c , model ['trusts' ]),
376
+ "AssumeRolePolicyDocument" : build_role_trust (c , model ['trusts' ], SamlDirect ),
368
377
"ManagedPolicyArns" : [],
369
378
"Policies" : []
370
379
}
@@ -499,7 +508,8 @@ def add_user(c, UserName, model, named=False):
499
508
"users" : True ,
500
509
"groups" : True
501
510
},
502
- "template_outputs" : "enabled"
511
+ "template_outputs" : "enabled" ,
512
+ "saml_direct" : False
503
513
}
504
514
505
515
# Policies
@@ -540,19 +550,32 @@ def add_user(c, UserName, model, named=False):
540
550
)
541
551
542
552
# Roles
553
+
554
+
555
+
543
556
if "roles" in c .config :
557
+ # first check if this is a saml_direct config - we do it this way instead of c.config["global"]["saml_direct"] so that existing configs (pre saml_direct support) will work without error
558
+ saml_direct = False
559
+ try :
560
+ if c .config ['global' ]['saml_direct' ] is True :
561
+ saml_direct = True
562
+ except Exception :
563
+ pass
564
+
544
565
for role_name in c .config ["roles" ]:
545
566
context = ["all" ]
546
567
if "in_accounts" in c .config ["roles" ][role_name ]:
547
568
context = c .config ["roles" ][role_name ]["in_accounts" ]
548
569
549
570
for account in c .search_accounts (context ):
550
571
c .current_account = account
572
+ c .current_account_id = str (c .config ['accounts' ][account ]['id' ])
551
573
add_role (
552
574
c ,
553
575
role_name ,
554
576
c .config ["roles" ][role_name ],
555
- c .config ["global" ]["names" ]["roles" ]
577
+ c .config ["global" ]["names" ]["roles" ],
578
+ saml_direct
556
579
)
557
580
558
581
# See if we need to add an instance profile too with an ec2 trust.
0 commit comments