Skip to content

Fix jump role policy generation when bootstrapping > 361 accounts #751

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/lambda_codebase/jump_role_manager/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@
INCLUDE_NEW_ACCOUNTS_IF_JOINED_IN_LAST_HOURS = 2

MAX_MANAGED_POLICY_LENGTH = 6144
ZERO_ACCOUNTS_POLICY_LENGTH = 265
CHARS_PER_ACCOUNT_ID = 15
MAX_ROLE_NAME_LENGTH = 64
ZERO_ACCOUNTS_POLICY_LENGTH = 289 + MAX_ROLE_NAME_LENGTH
CHARS_PER_ACCOUNT_ID = 16
MAX_NUMBER_OF_ACCOUNTS = math.floor(
(
MAX_MANAGED_POLICY_LENGTH
Expand Down
49 changes: 47 additions & 2 deletions src/lambda_codebase/jump_role_manager/tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@
from main import (
ADF_JUMP_MANAGED_POLICY_ARN,
ADF_TEST_BOOTSTRAP_ROLE_NAME,
CHARS_PER_ACCOUNT_ID,
CROSS_ACCOUNT_ACCESS_ROLE_NAME,
INCLUDE_NEW_ACCOUNTS_IF_JOINED_IN_LAST_HOURS,
MAX_MANAGED_POLICY_LENGTH,
MAX_NUMBER_OF_ACCOUNTS,
MAX_POLICY_VERSIONS,
MAX_ROLE_NAME_LENGTH,
POLICY_VALID_DURATION_IN_HOURS,
ZERO_ACCOUNTS_POLICY_LENGTH,
_build_summary,
_delete_old_policy_versions,
_generate_policy_document,
Expand Down Expand Up @@ -65,7 +69,7 @@ def mock_organizations():


def test_max_number_of_accounts():
assert MAX_NUMBER_OF_ACCOUNTS == 391
assert MAX_NUMBER_OF_ACCOUNTS == 361


def test_max_policy_versions():
Expand Down Expand Up @@ -674,6 +678,7 @@ def test_generate_policy_document_no_accounts_to_bootstrap(get_mock):
assert policy == expected_policy


@patch("main.CROSS_ACCOUNT_ACCESS_ROLE_NAME", "z" * MAX_ROLE_NAME_LENGTH)
@patch("main._get_valid_until")
def test_generate_policy_document(get_mock):
end_time = '2024-04-03T14:00:00Z'
Expand All @@ -683,6 +688,7 @@ def test_generate_policy_document(get_mock):
'222222222222',
'333333333333',
]
role_name = "z" * MAX_ROLE_NAME_LENGTH
expected_policy = {
"Version": "2012-10-17",
"Statement": [
Expand All @@ -691,7 +697,7 @@ def test_generate_policy_document(get_mock):
"Effect": "Allow",
"Action": ["sts:AssumeRole"],
"Resource": [
f"arn:aws:iam::*:role/{CROSS_ACCOUNT_ACCESS_ROLE_NAME}",
f"arn:aws:iam::*:role/{role_name}",
],
"Condition": {
"DateLessThan": {
Expand All @@ -707,6 +713,45 @@ def test_generate_policy_document(get_mock):

policy = _generate_policy_document(non_bootstrapped_account_ids)
assert policy == expected_policy
assert len(json.dumps(policy)) == (
ZERO_ACCOUNTS_POLICY_LENGTH
+ CHARS_PER_ACCOUNT_ID * len(non_bootstrapped_account_ids)
- 2 # characters for the last account, as that does not include ", "
)


@patch("main.CROSS_ACCOUNT_ACCESS_ROLE_NAME", "z" * MAX_ROLE_NAME_LENGTH)
@patch("main._get_valid_until")
def test_generate_policy_document_max_length(get_mock):
end_time = '2024-04-03T14:00:00Z'
get_mock.return_value = end_time
non_bootstrapped_account_ids = ['111111111111'] * MAX_NUMBER_OF_ACCOUNTS
role_name = "z" * MAX_ROLE_NAME_LENGTH
expected_policy = {
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowNonBootstrappedAccounts",
"Effect": "Allow",
"Action": ["sts:AssumeRole"],
"Resource": [
f"arn:aws:iam::*:role/{role_name}",
],
"Condition": {
"DateLessThan": {
"aws:CurrentTime": end_time,
},
"StringEquals": {
"aws:ResourceAccount": non_bootstrapped_account_ids,
},
}
}
]
}

policy = _generate_policy_document(non_bootstrapped_account_ids)
assert policy == expected_policy
assert len(json.dumps(policy)) < MAX_MANAGED_POLICY_LENGTH
# ---------------------------------------------------------


Expand Down