diff --git a/crates/server/src/console/pages/role_pages.rs b/crates/server/src/console/pages/role_pages.rs index f2ffe4697..b56f65ca7 100755 --- a/crates/server/src/console/pages/role_pages.rs +++ b/crates/server/src/console/pages/role_pages.rs @@ -39,6 +39,12 @@ pub async fn new_role_form( (&account_id, Some(&format!("/console/accounts/{eid}"))), ("New Role", None), ]); + // The default principal is the account's root, which every principal of + // the account matches. `create_role` refuses a principal ARN that holds a + // wildcard, so the default must not carry one. + let root_principal = html::escape( + &serde_json::Value::String(format!("arn:aws:iam::{account_id}:root")).to_string(), + ); let content = format!( r#"{crumbs}

Create Role

@@ -51,7 +57,7 @@ pub async fn new_role_form( "Version": "2012-10-17", "Statement": [{{ "Effect": "Allow", - "Principal": {{"AWS": "arn:aws:iam::*:user/*"}}, + "Principal": {{"AWS": {root_principal}}}, "Action": "sts:AssumeRole" }}] }} diff --git a/tests/test_console_integration.py b/tests/test_console_integration.py index 402d9b2d4..0f23a0d0a 100755 --- a/tests/test_console_integration.py +++ b/tests/test_console_integration.py @@ -23,6 +23,7 @@ from __future__ import annotations +import html import json import os import re @@ -418,6 +419,31 @@ def test_create_and_view_role(self): self.console.post(f"/accounts/{self.account_id}/roles/{role}/delete") + def test_create_role_with_the_form_default_trust_policy(self): + resp = self.console.get(f"/accounts/{self.account_id}/roles/new") + assert resp.status_code == 200 + m = re.search( + r'', resp.text, re.S + ) + assert m, "the New Role form has a trust policy box" + default_policy = html.unescape(m.group(1)) + assert json.loads(default_policy)["Statement"][0]["Principal"] == { + "AWS": f"arn:aws:iam::{self.account_id}:root" + } + + role = f"df-{uuid.uuid4().hex[:8]}" + resp = self.console.post(f"/accounts/{self.account_id}/roles/new", { + "role_name": role, + "trust_policy": default_policy, + }) + assert resp.status_code == 303, resp.text + + resp = self.console.get(f"/accounts/{self.account_id}/roles/{role}") + assert resp.status_code == 200 + assert role in resp.text + + self.console.post(f"/accounts/{self.account_id}/roles/{role}/delete") + def test_delete_role(self): role = f"dr-{uuid.uuid4().hex[:8]}" self.console.post(f"/accounts/{self.account_id}/roles/new", {