mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(iam): read the organization levels and scenario a simulation was handed (#8585)
Both policy simulators accepted OrderedOrganizationPolicyInputList and ResourceHandlingOption and answered as though neither had been sent. The levels now gate a simulation the way they gate the call it stands for, through the same walk a live request takes through service control policies: an action no level allows is an implicitDeny, a level that denies it is an explicitDeny whatever the levels below allow, and a level carrying no statements is skipped. A level whose document does not read as a policy is an InvalidInput naming the item's position. The levels a call hands in are the whole of the organization the simulation runs against, matching the console, which includes SCPs only when the box is ticked. A named scenario is held to the resources it requires, "to enforce the presence of the required resources to ensure reliable simulation results", and a value naming no published scenario is refused. A simulation that names no scenario is left alone. One difference from AWS's simulator is documented and open: AWS says its simulator does not evaluate SCPs that have global conditions, while this walk evaluates a condition in an SCP exactly as the live evaluation does.
This commit is contained in:
1 parent
4a7953bb0b
commit
bba2ac901e
5 files changed
+460
-10
No files matched your search
@@ -435,11 +435,22 @@ public class IamPolicyEvaluator {
|
||||
List<PolicyStatement> boundaryStmts = caller.boundaryPolicyDocument() == null
|
||||
? null : parseAll(List.of(caller.boundaryPolicyDocument()));
|
||||
|
||||
// Organization levels gate a simulation the way they gate the call it stands for: the
|
||||
// action must be allowed at every level and explicitly denied at none, before the input
|
||||
// policies are consulted. This is the live path's own walk, so a simulation cannot answer
|
||||
// differently from the request it models.
|
||||
ScpWalk scp = scpWalk(caller.scpLevels(), loweredAction, resource, ctx);
|
||||
if (scp.firstDeny() != null) {
|
||||
return SimulationDecision.EXPLICIT_DENY;
|
||||
}
|
||||
if (anyExplicitDeny(identityStmts, loweredAction, resource, ctx)
|
||||
|| (sessionStmts != null && anyExplicitDeny(sessionStmts, loweredAction, resource, ctx))
|
||||
|| (boundaryStmts != null && anyExplicitDeny(boundaryStmts, loweredAction, resource, ctx))) {
|
||||
return SimulationDecision.EXPLICIT_DENY;
|
||||
}
|
||||
if (!scp.allows()) {
|
||||
return SimulationDecision.IMPLICIT_DENY;
|
||||
}
|
||||
if (!anyExplicitAllow(identityStmts, loweredAction, resource, ctx)) {
|
||||
return SimulationDecision.IMPLICIT_DENY;
|
||||
}
|
||||
|
||||
@@ -2190,10 +2190,20 @@ public class IamQueryHandler {
|
||||
PolicyExclusions exclusions = PolicyExclusions.from(params);
|
||||
List<String> inputDocuments = readablePolicyInputList(params);
|
||||
String inputBoundary = inputBoundaryDocument(params);
|
||||
CallerContext caller = withInputDocuments(
|
||||
iamService.resolvePrincipalContext(policySourceArn, exclusions), inputDocuments, inputBoundary);
|
||||
List<String> actionNames = requireActionNames(params);
|
||||
// SCPs enter a simulation only when the call asks for them, the way the console includes
|
||||
// them only when "AWS Organizations SCPs" is ticked, so the levels the call handed in are
|
||||
// the whole of the organization the simulation runs against. They are read where the other
|
||||
// handed-in documents are, ahead of the lookup, so a principal that does not exist cannot
|
||||
// answer for an input this operation refuses: that is the rule above, and these two inputs
|
||||
// live under it as well.
|
||||
List<List<String>> scpLevels = organizationPolicyLevels(params);
|
||||
List<String> resourceArns = extractResourceArnsOrWildcard(params);
|
||||
SimulationScenarios.requireScenarioResources(params.getFirst("ResourceHandlingOption"),
|
||||
resourceArns);
|
||||
CallerContext caller = withInputDocuments(
|
||||
iamService.resolvePrincipalContext(policySourceArn, exclusions), inputDocuments, inputBoundary)
|
||||
.withScpLevels(scpLevels);
|
||||
List<String> actionNames = requireActionNames(params);
|
||||
// Keyed without regard to case, as the evaluator compares condition key names: a value
|
||||
// the call supplied has to shadow a populated one however the call spelled the key.
|
||||
Map<String, List<String>> context = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
|
||||
@@ -2218,12 +2228,57 @@ public class IamQueryHandler {
|
||||
private Response handleSimulateCustomPolicy(MultivaluedMap<String, String> params) {
|
||||
List<String> policyInputList = requirePolicyInputList(params);
|
||||
List<String> actionNames = requireActionNames(params);
|
||||
// Read before the scenario, as the principal simulation reads them, so a bad level and a
|
||||
// bad scenario are answered in the same order whichever operation was called.
|
||||
List<List<String>> scpLevels = organizationPolicyLevels(params);
|
||||
List<String> resourceArns = extractResourceArnsOrWildcard(params);
|
||||
SimulationScenarios.requireScenarioResources(params.getFirst("ResourceHandlingOption"),
|
||||
resourceArns);
|
||||
Map<String, List<String>> context = extractContextEntries(params);
|
||||
CallerContext caller = new CallerContext(policyInputList, null, inputBoundaryDocument(params));
|
||||
CallerContext caller = new CallerContext(policyInputList, null, inputBoundaryDocument(params),
|
||||
scpLevels);
|
||||
return simulationResults("SimulateCustomPolicy", params, caller, actionNames, resourceArns, context);
|
||||
}
|
||||
|
||||
/**
|
||||
* The service control policies a simulation was handed, one list per level of an
|
||||
* organization. The member's reference describes both the shape and the order: "An ordered
|
||||
* list of service control policies (SCPs) to include in the simulation. Each element
|
||||
* represents one level of an Organizations hierarchy, from the organization root to the
|
||||
* account. The simulator evaluates SCPs in the order that you provide, consistent with how
|
||||
* Organizations enforces SCPs." So the levels go to the evaluator as the caller's effective
|
||||
* SCPs, which is the walk a live request already takes through them, in the order the request
|
||||
* wrote. How many levels there may be is the model's to say, seven on this list, and the
|
||||
* declared constraints are read ahead of this.
|
||||
*
|
||||
* <p>Each document is held to being a policy document, as the documents of the other input
|
||||
* lists are, and a level naming none is no level: a request that carries the member and
|
||||
* nothing in it simulates with no organization above it rather than with an empty root.
|
||||
*/
|
||||
private List<List<String>> organizationPolicyLevels(MultivaluedMap<String, String> params) {
|
||||
List<List<String>> levels = new ArrayList<>();
|
||||
for (AwsQueryLists.IndexedMember level
|
||||
: AwsQueryLists.members(params, "OrderedOrganizationPolicyInputList", null)) {
|
||||
List<String> documents = new ArrayList<>();
|
||||
for (AwsQueryLists.IndexedMember document : AwsQueryLists.members(params,
|
||||
level.prefix() + ".ServiceControlPolicyInputList", null)) {
|
||||
String policy = params.getFirst(document.prefix());
|
||||
if (policy == null) {
|
||||
continue;
|
||||
}
|
||||
if (!policyEvaluator.readsAsPolicyDocument(policy)) {
|
||||
throw new AwsException("InvalidInput", "Ordered organization policy input list"
|
||||
+ " item " + level.index() + " has invalid content", 400);
|
||||
}
|
||||
documents.add(policy);
|
||||
}
|
||||
if (!documents.isEmpty()) {
|
||||
levels.add(List.copyOf(documents));
|
||||
}
|
||||
}
|
||||
return levels.isEmpty() ? null : List.copyOf(levels);
|
||||
}
|
||||
|
||||
/**
|
||||
* The documents a principal simulation was handed beside the principal's own.
|
||||
* {@code PolicyInputList} is "an optional list of additional policy documents to include in
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
package io.floci.aws.services.iam;
|
||||
|
||||
import io.floci.aws.core.common.AwsArnUtils;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* The simulation types {@code ResourceHandlingOption} names, and the resources each one has to be
|
||||
* given.
|
||||
*
|
||||
* <p>The member's reference says what it is for: "Specifies the type of simulation to run.
|
||||
* Different API operations that support resource-based policies require different combinations of
|
||||
* resources. By specifying the type of simulation to run, you enable the policy simulator to
|
||||
* enforce the presence of the required resources to ensure reliable simulation results." It then
|
||||
* publishes the four scenarios and what each requires, which is what this holds: "Each of the
|
||||
* Amazon EC2 scenarios requires that you specify instance, image, and security group resources. If
|
||||
* your scenario includes an EBS volume, then you must specify that volume as a resource. If the
|
||||
* Amazon EC2 scenario includes VPC, then you must supply the network interface resource. If it
|
||||
* includes an IP subnet, then you must specify the subnet resource."
|
||||
*
|
||||
* <p>Two refusals here are this emulator's own wording. AWS publishes no sentence for a scenario
|
||||
* whose resources are missing, nor for a value that names no scenario at all, and both are
|
||||
* {@code InvalidInput}, the error both simulators model for "an invalid or out-of-range value
|
||||
* supplied for an input parameter". The member's own length is held to the model ahead of this,
|
||||
* as every declared constraint of these operations is.
|
||||
*/
|
||||
final class SimulationScenarios {
|
||||
|
||||
/** Each published scenario and the EC2 resource types a simulation of it has to name. */
|
||||
private static final Map<String, List<String>> REQUIRED = Map.of(
|
||||
"EC2-VPC-InstanceStore", List.of("instance", "image", "security-group",
|
||||
"network-interface"),
|
||||
"EC2-VPC-InstanceStore-Subnet", List.of("instance", "image", "security-group",
|
||||
"network-interface", "subnet"),
|
||||
"EC2-VPC-EBS", List.of("instance", "image", "security-group", "network-interface",
|
||||
"volume"),
|
||||
"EC2-VPC-EBS-Subnet", List.of("instance", "image", "security-group",
|
||||
"network-interface", "subnet", "volume"));
|
||||
|
||||
private SimulationScenarios() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Holds a simulation to the resources its scenario requires, or passes a request that names no
|
||||
* scenario: "If your simulation does not match one of the following scenarios, then you can
|
||||
* omit this parameter." Omitting the member is what passes, not emptying it: a member the
|
||||
* request carries is a value rather than a member it left out, so a value of nothing but
|
||||
* spaces names no published scenario and is refused like any other value that names none. A
|
||||
* member carried empty never reaches here, the declared minimum of one answering it ahead of
|
||||
* this operation.
|
||||
*/
|
||||
static void requireScenarioResources(String option, List<String> resourceArns) {
|
||||
if (option == null) {
|
||||
return;
|
||||
}
|
||||
List<String> required = REQUIRED.get(option);
|
||||
if (required == null) {
|
||||
throw new AwsException("InvalidInput", "ResourceHandlingOption " + option
|
||||
+ " is not one of the simulation types this operation runs.", 400);
|
||||
}
|
||||
for (String type : required) {
|
||||
if (!namesResourceOfType(resourceArns, type)) {
|
||||
throw new AwsException("InvalidInput", "ResourceHandlingOption " + option
|
||||
+ " requires a resource of type " + type + " among the ResourceArns.", 400);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Whether any resource ARN names an EC2 resource of {@code type}. */
|
||||
private static boolean namesResourceOfType(List<String> resourceArns, String type) {
|
||||
if (resourceArns == null) {
|
||||
return false;
|
||||
}
|
||||
String prefix = type + "/";
|
||||
for (String arn : resourceArns) {
|
||||
AwsArnUtils.Arn parsed;
|
||||
try {
|
||||
parsed = AwsArnUtils.parse(arn);
|
||||
} catch (IllegalArgumentException ignored) {
|
||||
// A value the parser refuses names no resource of any type, whether it was meant
|
||||
// as an ARN or not, and the member that carried it is held to its shape elsewhere.
|
||||
continue;
|
||||
}
|
||||
if ("ec2".equals(parsed.service()) && parsed.resource() != null
|
||||
&& parsed.resource().startsWith(prefix)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user