Light, fluffy, and always free - The AWS Local Emulator alternative
fix(iam): hold a root approval of a delegation request to four hours (#6321)
Priamo Germosena8b5e8b8ec86
diff --git a/docs/services/iam.md b/docs/services/iam.mdindex 63ca7a73be..4f9afd60cf 100644--- a/docs/services/iam.md+++ b/docs/services/iam.md@@ -1145,6 +1145,18 @@ and `AcceptDelegationRequest` models only `NoSuchEntity`, `ServiceFailure` and`ConcurrentModification`, so the conflict is the closest of the three. The same lock serializesthose transitions, so two callers claiming one request leave a single owner.+Two ceilings apply to a request's length, and only one of them belongs to the request. The first is+the twelve hours `sessionDurationType` declares, which the create holds a `SessionDuration` to. The+second is the approver's: "root users can only approve delegation requests with a duration of 4+hours or less. If a request specifies more than 4 hours, you must use a non-root identity to approve+the request", so a longer request is refused at the approval rather than at the filing, and the same+request is approvable by an IAM user or role of that account. Floci reads root as a caller whose ARN+ends in `:root`, which is also what a key naming no IAM user resolves to, so approving a long request+locally means signing as a user rather than with a bare account id. `AcceptDelegationRequest` models+nothing for this and no source gives the code AWS answers, so `AccessDenied` is chosen, the guide+stating the rule as which identity may approve and the release's own owner rule answering the same+way.+The exchange token is the other half. `SendDelegationToken` releases it, and STS`GetDelegatedAccessToken` exchanges it for credentials in the customer's account, acting as theidentity that approved the request rather than the one that claimed it: accepting a request recordsdiff --git a/src/main/java/io/github/hectorvent/floci/services/iam/DelegationRequestService.java b/src/main/java/io/github/hectorvent/floci/services/iam/DelegationRequestService.javaindex b235e35e11..ca46c55cbe 100644--- a/src/main/java/io/github/hectorvent/floci/services/iam/DelegationRequestService.java+++ b/src/main/java/io/github/hectorvent/floci/services/iam/DelegationRequestService.java@@ -90,6 +90,15 @@ public class DelegationRequestService {private static final int SESSION_DURATION_MIN = 300;private static final int SESSION_DURATION_MAX = 43200;+ /**+ * The longest delegation the account root may approve. Not a declared constraint but a+ * documented one, and it belongs to the approval rather than to the request: the user guide's+ * own note reads "The maximum duration for delegated access is 12 hours. However, root users+ * can only approve delegation requests with a duration of 4 hours or less. If a request+ * specifies more than 4 hours, you must use a non-root identity to approve the request."+ */+ private static final int ROOT_APPROVAL_DURATION_MAX = 14400;+/*** {@code summaryStateType}, of which {@code FAILED} is unreachable here: it reports a* generation that ran and did not finish, and nothing is generated asynchronously.@@ -277,6 +286,7 @@ public class DelegationRequestService {DelegationRequest request = visible(callerAccountId, id, now, notices);requireState(request, DelegationRequestState.ASSIGNED,DelegationRequestState.PENDING_APPROVAL);+ requireApproverMayHoldItThatLong(request, callerArn);request.setApproverId(callerArn);return moveTo(request, DelegationRequestState.ACCEPTED, now, notices);}@@ -648,6 +658,35 @@ public class DelegationRequestService {+ " and cannot be changed from that state.", 409);}+ /**+ * The four-hour cap on what the account root may approve, which {@code create} cannot apply:+ * the twelve hours it enforces belong to the request, and this one belongs to whoever+ * approves it, so it is read here where the approver is known.+ *+ * <p>Root is the caller whose ARN ends in {@code :root}, which is what this family already+ * names an approver by, and covers a root credential itself as well as a key resolving to no+ * IAM user, which this service answers with the account-root stand-in.+ *+ * <p>{@code AcceptDelegationRequest} models {@code NoSuchEntity}, {@code ServiceFailure} and+ * {@code ConcurrentModification} and nothing for this, and no source gives the code AWS+ * answers, so the code is chosen: the guide states the rule as which identity may approve,+ * which is what {@code SendDelegationToken}'s own owner rule answers {@code AccessDenied}+ * for. A duration the record does not carry is nothing to hold to a cap.+ */+ private static void requireApproverMayHoldItThatLong(DelegationRequest request,+ String callerArn) {+ if (callerArn == null || !callerArn.endsWith(":root")+ || request.getSessionDuration() == null+ || request.getSessionDuration() <= ROOT_APPROVAL_DURATION_MAX) {+ return;+ }+ throw new AwsException("AccessDenied", "User is not authorized to perform: "+ + "iam:AcceptDelegationRequest on " + request.getDelegationRequestId()+ + ", which asks for " + request.getSessionDuration() + " seconds: the root user may"+ + " only approve a delegation of " + ROOT_APPROVAL_DURATION_MAX / 3600+ + " hours or less. Approve it as an IAM user or role instead.", 403);+ }+private void put(DelegationRequest request) {requests.putForAccount(request.getRequestorId(), request.getDelegationRequestId(), request);}diff --git a/src/test/java/io/github/hectorvent/floci/services/iam/DelegationRequestIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/iam/DelegationRequestIntegrationTest.javaindex 0f1de0bf94..a950e8b89f 100644--- a/src/test/java/io/github/hectorvent/floci/services/iam/DelegationRequestIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/iam/DelegationRequestIntegrationTest.java@@ -568,6 +568,34 @@ class DelegationRequestIntegrationTest {.body("ErrorResponse.Error.Message", containsString("entityArn"));}+ @Test+ void theReleaseRefusesAnIdThatBreaksItsPatternAndNotOnlyAnAbsentOne() {+ // The other half of the check above, and the one a regression could lose quietly: an id+ // that arrived and cannot be a request id at all, rather than one that never arrived.+ iam(CUSTOMER_AUTH)+ .formParam("Action", "SendDelegationToken")+ .formParam("Version", "2010-05-08")+ .formParam("DelegationRequestId", "dr-0123456789abcdef!")+ .when().post("/").then().statusCode(400)+ .body("ErrorResponse.Error.Code", equalTo("ValidationError"))+ .body("ErrorResponse.Error.Message", containsString("delegationRequestId"));+ }++ @Test+ void theRootApprovalCapHoldsOverTheWireToo() {+ // The customer signs with its account id here, which names no IAM user, so this suite's+ // approver is the account root: a request longer than four hours cannot be approved with+ // it, which is the guide's rule rather than a local one.+ String id = create(workflowId(), null, "18000").statusCode(200)+ .extract().path("CreateDelegationRequestResponse"+ + ".CreateDelegationRequestResult.DelegationRequestId");+ byId(CUSTOMER_AUTH, "AssociateDelegationRequest", id).statusCode(200);++ byId(CUSTOMER_AUTH, "AcceptDelegationRequest", id).statusCode(403)+ .body("ErrorResponse.Error.Code", equalTo("AccessDenied"))+ .body("ErrorResponse.Error.Message", containsString("4 hours or less"));+ }+@Testvoid theReleaseNeedsARequestIdLikeEveryOtherCallOnOne() {// delegationRequestIdType declares a length and a pattern, so an omitted id breaks adiff --git a/src/test/java/io/github/hectorvent/floci/services/iam/DelegationRequestServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/iam/DelegationRequestServiceTest.javaindex 86cb78b141..bddb7e1475 100644--- a/src/test/java/io/github/hectorvent/floci/services/iam/DelegationRequestServiceTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/iam/DelegationRequestServiceTest.java@@ -49,6 +49,8 @@ class DelegationRequestServiceTest {private static final String CUSTOMER_ARN = "arn:aws:iam::000000000002:user/admin";/** A second identity in the customer's account, the privileged approver the reference names. */private static final String APPROVER_ARN = "arn:aws:iam::000000000002:user/security-admin";+ /** The customer's account root, which is also what a key naming no IAM user resolves to. */+ private static final String ROOT_ARN = "arn:aws:iam::000000000002:root";private static final String REGION = "us-east-1";private static final String TOPIC = "arn:aws:sns:us-east-1:000000000001:partner-notifications";@@ -102,6 +104,15 @@ class DelegationRequestServiceTest {ownerAccountId, null, null, onlySendByOwner));}+ /** A request of this length, every other member being the one the helpers above file. */+ private DelegationRequest createdFor(String workflowId, int sessionDuration) {+ return service.create(PARTNER, REGION, new DelegationRequestService.NewRequest(+ "Set up the integration", TOPIC, workflowId, sessionDuration,+ new DelegationPermission("arn:aws:iam::partner:policy/template/example",+ List.of()),+ null, null, null, null)).request();+ }+private DelegationRequest created(String workflowId, String ownerAccountId) {return createdIn(REGION, workflowId, ownerAccountId).request();}@@ -896,4 +907,46 @@ class DelegationRequestServiceTest {assertEquals(List.of("StateChange:EXPIRED"), announced());}++ /**+ * The user guide's own note: "The maximum duration for delegated access is 12 hours. However,+ * root users can only approve delegation requests with a duration of 4 hours or less. If a+ * request specifies more than 4 hours, you must use a non-root identity to approve the+ * request." The twelve hours are {@code create}'s; the four are the approver's.+ */+ @Test+ void theAccountRootMayOnlyApproveADelegationOfFourHoursOrLess() {+ String id = createdFor("workflow-root-long", 18000).getDelegationRequestId();+ service.associate(CUSTOMER, ROOT_ARN, id);++ AwsException refused = assertThrows(AwsException.class,+ () -> service.accept(CUSTOMER, ROOT_ARN, id));++ assertEquals("AccessDenied", refused.getErrorCode());+ assertEquals(403, refused.getHttpStatus());+ // Refused, not spent: the guide's own remedy is to approve it as a non-root identity, and+ // the request is still sitting in ASSIGNED waiting for one.+ assertEquals(DelegationRequestState.ACCEPTED,+ service.accept(CUSTOMER, APPROVER_ARN, id).getState());+ }++ @Test+ void rootMayApproveADelegationRightOnTheFourHourCap() {+ String id = createdFor("workflow-root-capped", 14400).getDelegationRequestId();+ service.associate(CUSTOMER, ROOT_ARN, id);++ // Four hours or less is what the note allows, so the cap itself is approvable.+ assertEquals(DelegationRequestState.ACCEPTED,+ service.accept(CUSTOMER, ROOT_ARN, id).getState());+ }++ @Test+ void theCapIsTheApproversRatherThanTheRequests() {+ String id = createdFor("workflow-user-long", 43200).getDelegationRequestId();+ service.associate(CUSTOMER, CUSTOMER_ARN, id);++ // Twelve hours is the ceiling create enforces, and an IAM user may approve all of it.+ assertEquals(DelegationRequestState.ACCEPTED,+ service.accept(CUSTOMER, APPROVER_ARN, id).getState());+ }}