|
55 | 55 | get_azp, |
56 | 56 | get_user_token |
57 | 57 | ) |
| 58 | +from .iam import iam_root, iam_alt_root |
| 59 | +from .utils import assert_raises, _get_status_and_error_code |
58 | 60 |
|
59 | 61 | log = logging.getLogger(__name__) |
60 | 62 |
|
@@ -2164,3 +2166,124 @@ def test_get_caller_identity_after_assume_role(): |
2164 | 2166 | assert response['Account'] == account_id |
2165 | 2167 | assert response['Arn'] == assume_role['AssumedRoleUser']['Arn'] |
2166 | 2168 |
|
| 2169 | +@pytest.mark.test_of_sts |
| 2170 | +@pytest.mark.iam_account |
| 2171 | +def test_same_account_user_policy_assume_role(iam_root): |
| 2172 | + path = get_iam_path_prefix() |
| 2173 | + user_name = make_iam_name('MyUser') |
| 2174 | + role_name = make_iam_name('MyRole') |
| 2175 | + session_name = 'MySession' |
| 2176 | + |
| 2177 | + # role trust policy does not allow owning account principal |
| 2178 | + trust_policy = json.dumps({ |
| 2179 | + 'Version': '2012-10-17', |
| 2180 | + 'Statement': [{ |
| 2181 | + 'Effect': 'Allow', |
| 2182 | + 'Action': 'sts:AssumeRole', |
| 2183 | + 'Principal': {'AWS': 'OTHERACCOUNT'} |
| 2184 | + }] |
| 2185 | + }) |
| 2186 | + |
| 2187 | + role = iam_root.create_role(RoleName=role_name, Path=path, AssumeRolePolicyDocument=trust_policy)['Role'] |
| 2188 | + role_arn = role['Arn'] |
| 2189 | + |
| 2190 | + user = iam_root.create_user(UserName=user_name, Path=path)['User'] |
| 2191 | + user_arn = user['Arn'] |
| 2192 | + |
| 2193 | + key = iam_root.create_access_key(UserName=user_name)['AccessKey'] |
| 2194 | + sts = get_sts_client(aws_access_key_id=key['AccessKeyId'], |
| 2195 | + aws_secret_access_key=key['SecretAccessKey']) |
| 2196 | + |
| 2197 | + # reject AssumeRole due to lack of identity policy |
| 2198 | + e = assert_raises(ClientError, sts.assume_role, RoleArn=role_arn, RoleSessionName=session_name) |
| 2199 | + assert (403, 'AccessDenied') == _get_status_and_error_code(e.response) |
| 2200 | + |
| 2201 | + user_policy_name = 'AllowAssumeRole' |
| 2202 | + user_policy = json.dumps({ |
| 2203 | + 'Version': '2012-10-17', |
| 2204 | + 'Statement': [{ |
| 2205 | + 'Effect': 'Allow', |
| 2206 | + 'Action': 'sts:AssumeRole', |
| 2207 | + 'Resource': role_arn |
| 2208 | + }] |
| 2209 | + }) |
| 2210 | + iam_root.put_user_policy(UserName=user_name, PolicyName=user_policy_name, PolicyDocument=user_policy) |
| 2211 | + |
| 2212 | + sts.assume_role(RoleArn=role_arn, RoleSessionName=session_name) |
| 2213 | + |
| 2214 | +@pytest.mark.test_of_sts |
| 2215 | +@pytest.mark.iam_account |
| 2216 | +def test_same_account_trust_policy_assume_role(iam_root): |
| 2217 | + path = get_iam_path_prefix() |
| 2218 | + user_name = make_iam_name('MyUser') |
| 2219 | + role_name = make_iam_name('MyRole') |
| 2220 | + session_name = 'MySession' |
| 2221 | + |
| 2222 | + user = iam_root.create_user(UserName=user_name, Path=path)['User'] |
| 2223 | + user_arn = user['Arn'] |
| 2224 | + |
| 2225 | + trust_policy = json.dumps({ |
| 2226 | + 'Version': '2012-10-17', |
| 2227 | + 'Statement': [{ |
| 2228 | + 'Effect': 'Allow', |
| 2229 | + 'Action': 'sts:AssumeRole', |
| 2230 | + 'Principal': {'AWS': user_arn} |
| 2231 | + }] |
| 2232 | + }) |
| 2233 | + |
| 2234 | + role = iam_root.create_role(RoleName=role_name, Path=path, AssumeRolePolicyDocument=trust_policy)['Role'] |
| 2235 | + role_arn = role['Arn'] |
| 2236 | + |
| 2237 | + key = iam_root.create_access_key(UserName=user_name)['AccessKey'] |
| 2238 | + sts = get_sts_client(aws_access_key_id=key['AccessKeyId'], |
| 2239 | + aws_secret_access_key=key['SecretAccessKey']) |
| 2240 | + |
| 2241 | + # AssumeRole is granted by role trust policy alone |
| 2242 | + sts.assume_role(RoleArn=role_arn, RoleSessionName=session_name) |
| 2243 | + |
| 2244 | +@pytest.mark.test_of_sts |
| 2245 | +@pytest.mark.iam_account |
| 2246 | +@pytest.mark.iam_cross_account |
| 2247 | +def test_cross_account_user_policy_assume_role(iam_root, iam_alt_root): |
| 2248 | + path = get_iam_path_prefix() |
| 2249 | + user_name = make_iam_name('MyUser') |
| 2250 | + role_name = make_iam_name('MyRole') |
| 2251 | + session_name = 'MySession' |
| 2252 | + |
| 2253 | + # create user with alt account |
| 2254 | + user = iam_alt_root.create_user(UserName=user_name, Path=path)['User'] |
| 2255 | + user_arn = user['Arn'] |
| 2256 | + |
| 2257 | + key = iam_alt_root.create_access_key(UserName=user_name)['AccessKey'] |
| 2258 | + sts = get_sts_client(aws_access_key_id=key['AccessKeyId'], |
| 2259 | + aws_secret_access_key=key['SecretAccessKey']) |
| 2260 | + |
| 2261 | + trust_policy = json.dumps({ |
| 2262 | + 'Version': '2012-10-17', |
| 2263 | + 'Statement': [{ |
| 2264 | + 'Effect': 'Allow', |
| 2265 | + 'Action': 'sts:AssumeRole', |
| 2266 | + 'Principal': {'AWS': user_arn} |
| 2267 | + }] |
| 2268 | + }) |
| 2269 | + |
| 2270 | + # create role with main account |
| 2271 | + role = iam_root.create_role(RoleName=role_name, Path=path, AssumeRolePolicyDocument=trust_policy)['Role'] |
| 2272 | + role_arn = role['Arn'] |
| 2273 | + |
| 2274 | + # reject AssumeRole due to lack of identity policy |
| 2275 | + e = assert_raises(ClientError, sts.assume_role, RoleArn=role_arn, RoleSessionName=session_name) |
| 2276 | + assert (403, 'AccessDenied') == _get_status_and_error_code(e.response) |
| 2277 | + |
| 2278 | + user_policy_name = 'AllowAssumeRole' |
| 2279 | + user_policy = json.dumps({ |
| 2280 | + 'Version': '2012-10-17', |
| 2281 | + 'Statement': [{ |
| 2282 | + 'Effect': 'Allow', |
| 2283 | + 'Action': 'sts:AssumeRole', |
| 2284 | + 'Resource': role_arn |
| 2285 | + }] |
| 2286 | + }) |
| 2287 | + iam_alt_root.put_user_policy(UserName=user_name, PolicyName=user_policy_name, PolicyDocument=user_policy) |
| 2288 | + |
| 2289 | + sts.assume_role(RoleArn=role_arn, RoleSessionName=session_name) |
0 commit comments