Skip to content

Commit

Permalink
test: update e2e coverage for policy update on auth removal (#13538)
Browse files Browse the repository at this point in the history
  • Loading branch information
edwardfoyle authored Jan 22, 2024
1 parent 7060a95 commit 97b1530
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/amplify-e2e-tests/src/__tests__/auth_3a.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('amplify add auth...a', () => {
await removeAuthWithDefault(projRoot);
await amplifyPushAuth(projRoot);

expect(AuthRoleName).not.toHaveValidPolicyConditionMatchingIdpId(idpId);
expect(UnauthRoleName).not.toHaveValidPolicyConditionMatchingIdpId(idpId);
expect(AuthRoleName).toHaveDenyAssumeRolePolicy();
expect(UnauthRoleName).toHaveDenyAssumeRolePolicy();
});
});
27 changes: 27 additions & 0 deletions packages/amplify-e2e-tests/src/aws-matchers/iamMatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,30 @@ export const toHaveValidPolicyConditionMatchingIdpId = async (roleName: string,
pass,
};
};

export const toHaveDenyAssumeRolePolicy = async (roleName: string) => {
let pass = false;
let message = '';

try {
const iam = new IAM({
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
sessionToken: process.env.AWS_SESSION_TOKEN,
});

const { Role: role } = await iam.getRole({ RoleName: roleName }).promise();
const assumeRolePolicyDocument = JSON.parse(decodeURIComponent(role.AssumeRolePolicyDocument));

pass = assumeRolePolicyDocument?.Statement?.length === 1 && assumeRolePolicyDocument?.Statement?.[0]?.Effect === 'Deny';

message = pass ? 'Assume role policy has Effect: Deny' : `Assume role policy does not exist or does not have Effect: Deny.`;
} catch (e) {
message = 'IAM GetRole threw Error: ' + e.message;
}

return {
message: () => message,
pass,
};
};
3 changes: 2 additions & 1 deletion packages/amplify-e2e-tests/src/setup-tests.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { toBeIAMRoleWithArn, toHaveValidPolicyConditionMatchingIdpId, toBeAS3Bucket } from './aws-matchers';
import { toBeIAMRoleWithArn, toHaveValidPolicyConditionMatchingIdpId, toBeAS3Bucket, toHaveDenyAssumeRolePolicy } from './aws-matchers';

const removeYarnPaths = () => {
// Remove yarn's temporary PATH modifications as they affect the yarn version used by jest tests when building the lambda functions
Expand All @@ -9,6 +9,7 @@ const removeYarnPaths = () => {

expect.extend({ toBeIAMRoleWithArn });
expect.extend({ toHaveValidPolicyConditionMatchingIdpId });
expect.extend({ toHaveDenyAssumeRolePolicy });
expect.extend({ toBeAS3Bucket });

removeYarnPaths();
Expand Down
1 change: 1 addition & 0 deletions packages/amplify-e2e-tests/typings/aws-matchers.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ namespace jest {
toBeIAMRoleWithArn(roleName: string, arn?: string): R;
toBeAS3Bucket(bucketName: string): R;
toHaveValidPolicyConditionMatchingIdpId(idpId: string): R;
toHaveDenyAssumeRolePolicy(): R;
}
}

0 comments on commit 97b1530

Please sign in to comment.