mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
feat(iam): check lambda:PassNetworkConnector from a table generated from the Service Reference (#6699)
* feat(lambdaweb): check lambda:PassNetworkConnector as AWS does A revision that sets egressNetworkConnectorArn needs lambda:PassNetworkConnector on the connector version ARN as given. Lambda checks it after the connector ARN checks and before it looks the connector up. A refusal names the first policy type that refuses, in the order identity policies, permissions boundary, session policy. An explicit deny in a managed policy or a boundary also names that policy's ARN. * fix(lambdaweb): apply service control policies to the account root when passing a connector * fix(lambdaweb): name the managed policy that denies a session token caller * feat(iam): check lambda:PassNetworkConnector from a generated table Move the check out of Lambda Web into the IAM enforcement filter, where iam:PassRole is checked. tools/aws/regen_iam_passed_network_connectors.py joins AWS's Service Reference and botocore's models into src/main/resources/aws/iam-passed-network-connectors.json, and the filter reads it for Web Functions, MicroVM images and MicroVMs. A MicroVM request that names no egress connector, or an empty list, passes the Lambda-managed INTERNET_EGRESS connector, and RunMicrovm with no ingress connector passes HTTP_INGRESS, as AWS checks them. AppSync's data source operations are listed too, but botocore's model has no member for their connector, so they are not read. The refusal text now comes from AccessDeniedMessages, which LambdaTaskAuthorizer and GetStackOutputRoleAuthorizer use as well.
This commit is contained in:
1 parent
8a3d769c70
commit
b0cd70401b
20 files changed
+1405
-85
No files matched your search
@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.core.common;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.services.cloudtrail.CloudTrailService;
|
||||
import io.github.hectorvent.floci.services.iam.AccessDeniedMessages;
|
||||
import io.github.hectorvent.floci.services.iam.IamActionRegistry;
|
||||
import io.github.hectorvent.floci.services.iam.IamAuthorizedActions;
|
||||
import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator;
|
||||
@@ -541,6 +542,10 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
|
||||
region, accountId, akid)) {
|
||||
return;
|
||||
}
|
||||
if (abortIfConnectorPassDenied(ctx, caller, callerArns.orElse(null), resolvedAuthorization.operation(),
|
||||
region, accountId, akid)) {
|
||||
return;
|
||||
}
|
||||
if (tagOnCreate.isPresent() && abortIfTagOnCreateDenied(ctx, caller, principal, callerArns.orElse(null),
|
||||
action, resolvedAuthorization.operation(), tagOnCreate.get(), credentialScope, resources, region,
|
||||
accountId, akid)) {
|
||||
@@ -958,6 +963,41 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* A request that names a Lambda network connector also needs {@code lambda:PassNetworkConnector} on it (see
|
||||
* {@link PassedNetworkConnectors}), checked as the caller on the connector as the request names it, with the
|
||||
* keys that say who is calling. A connector has no resource policy. The refusal is Lambda's, which names the
|
||||
* policy type that refused it and the ARN of a managed policy or boundary that denies. Returns true when the
|
||||
* request was aborted.
|
||||
*/
|
||||
private boolean abortIfConnectorPassDenied(ContainerRequestContext ctx, CallerContext caller,
|
||||
CallerArns callerArns, String operation, String region,
|
||||
String accountId, String akid) {
|
||||
String action = PassedNetworkConnectors.ACTION;
|
||||
for (String connector : PassedNetworkConnectors.of(operation, ctx, region)) {
|
||||
Map<String, List<String>> context = new HashMap<>();
|
||||
if (callerArns != null) {
|
||||
callerArns.putPrincipalKeys(context);
|
||||
}
|
||||
Map<String, List<String>> effectiveContext = IamConditionContextResolver.withGlobalContext(
|
||||
context, connector, region, accountId, accountId);
|
||||
if (evaluator.evaluate(caller, null, action, connector, effectiveContext) != Decision.DENY) {
|
||||
continue;
|
||||
}
|
||||
LOG.infov("IAM enforcement DENY: akid={0} action={1} resource={2}", akid, action, connector);
|
||||
String user = callerArns != null ? callerArns.callerArn()
|
||||
: AwsArnUtils.Arn.global(requestPartition(), "iam", accountId, "user/" + akid).toString();
|
||||
String reason = AccessDeniedMessages.firstRefusal(evaluator, caller,
|
||||
iamService.heldPolicies(akid).orElse(null), action, connector, effectiveContext);
|
||||
ctx.abortWith(Response.status(403).type(MediaType.APPLICATION_JSON)
|
||||
.header("X-Amzn-Errortype", "AccessDeniedException")
|
||||
.entity(Map.of("Message", AccessDeniedMessages.notAuthorized(user, action, connector, reason)))
|
||||
.build());
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* The context once for each set of tags the request carries, with that set's keys where the context has
|
||||
* none of its own, so each resource's tags are checked apart; the context alone when it carries none.
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
package io.github.hectorvent.floci.core.common;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import jakarta.ws.rs.container.ContainerRequestContext;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* The Lambda network connectors a request passes, which the caller also needs
|
||||
* {@code lambda:PassNetworkConnector} on. The Lambda developer guide: "The caller must have
|
||||
* {@code lambda:PassNetworkConnector} on the referenced connector whenever a {@code create-web-function} or
|
||||
* {@code create-web-function-revision} call sets {@code egressNetworkConnectorArn}."
|
||||
*
|
||||
* <p>Where each operation's request names them is read once from the vendored
|
||||
* {@code aws/iam-passed-network-connectors.json}, generated by
|
||||
* {@code tools/aws/regen_iam_passed_network_connectors.py} from AWS's Service Reference and botocore's models.
|
||||
* A member the request leaves out or gives as an empty list passes the Lambda-managed connector the file names
|
||||
* for it, as AWS checks a MicroVM's default connectors.
|
||||
*
|
||||
* <p>A missing or malformed resource is a build defect, so loading fails with
|
||||
* {@link IllegalStateException} rather than leaving every operation it reads unchecked.
|
||||
*/
|
||||
final class PassedNetworkConnectors {
|
||||
|
||||
static final String RESOURCE_NAME = "aws/iam-passed-network-connectors.json";
|
||||
static final String ACTION = "lambda:PassNetworkConnector";
|
||||
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
|
||||
/** A member of a request that names connectors, and the Lambda-managed connector it passes when it names none. */
|
||||
record Member(String path, List<PassedRoleMembers.Step> steps, String whenAbsent) {
|
||||
}
|
||||
|
||||
private static final class Holder {
|
||||
static final Map<String, List<Member>> TABLE = VendoredResources.read(RESOURCE_NAME,
|
||||
"IAM passed network connector table", PassedNetworkConnectors::parse);
|
||||
}
|
||||
|
||||
private PassedNetworkConnectors() {
|
||||
}
|
||||
|
||||
/** The connectors a request for {@code operation} passes, in the order AWS checks them; empty when it passes none. */
|
||||
static List<String> of(String operation, ContainerRequestContext ctx, String region) {
|
||||
List<Member> members = Holder.TABLE.get(operation);
|
||||
if (members == null) {
|
||||
return List.of();
|
||||
}
|
||||
JsonNode body = PassedRoles.jsonBody(ctx);
|
||||
List<String> connectors = new ArrayList<>();
|
||||
for (Member member : members) {
|
||||
List<String> named = new ArrayList<>();
|
||||
PassedRoles.collect(body, member.steps(), 0, named);
|
||||
if (named.isEmpty() && member.whenAbsent() != null) {
|
||||
named.add(AwsArnUtils.Arn.of("lambda", region, "aws", "network-connector:" + member.whenAbsent())
|
||||
.toString());
|
||||
}
|
||||
connectors.addAll(named);
|
||||
}
|
||||
return connectors;
|
||||
}
|
||||
|
||||
static Map<String, List<Member>> table() {
|
||||
return Holder.TABLE;
|
||||
}
|
||||
|
||||
static Map<String, List<Member>> parse(InputStream in) throws IOException {
|
||||
JsonNode operations = MAPPER.readTree(in).path("operations");
|
||||
if (!operations.isObject() || operations.isEmpty()) {
|
||||
throw new IllegalStateException(RESOURCE_NAME + " carries no operations");
|
||||
}
|
||||
Map<String, List<Member>> table = new LinkedHashMap<>();
|
||||
operations.properties().forEach(operation -> {
|
||||
JsonNode entry = operation.getValue();
|
||||
if (entry.has("notRead")) {
|
||||
return;
|
||||
}
|
||||
if (!entry.path("connectors").isArray() || entry.path("connectors").isEmpty()) {
|
||||
throw new IllegalStateException(RESOURCE_NAME + " says nothing usable for " + operation.getKey());
|
||||
}
|
||||
List<Member> members = new ArrayList<>();
|
||||
for (JsonNode connector : entry.path("connectors")) {
|
||||
String path = connector.path("member").asText("");
|
||||
List<PassedRoleMembers.Step> steps = PassedRoleMembers.steps(path);
|
||||
if (steps == null) {
|
||||
throw new IllegalStateException(RESOURCE_NAME + " reads " + operation.getKey() + " at " + path
|
||||
+ ", which is no member path");
|
||||
}
|
||||
members.add(new Member(path, steps, connector.hasNonNull("whenAbsent")
|
||||
? connector.get("whenAbsent").asText() : null));
|
||||
}
|
||||
table.put(operation.getKey(), List.copyOf(members));
|
||||
});
|
||||
return Map.copyOf(table);
|
||||
}
|
||||
}
|
||||
@@ -370,7 +370,7 @@ final class PassedRoles {
|
||||
return passed;
|
||||
}
|
||||
|
||||
private static void collect(JsonNode node, List<PassedRoleMembers.Step> steps, int index, List<String> found) {
|
||||
static void collect(JsonNode node, List<PassedRoleMembers.Step> steps, int index, List<String> found) {
|
||||
if (node == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
+8
-7
@@ -3,6 +3,8 @@ package io.github.hectorvent.floci.services.cloudformation;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.services.iam.AccessDeniedMessages;
|
||||
import io.github.hectorvent.floci.services.iam.AccessDeniedMessages.PolicyType;
|
||||
import io.github.hectorvent.floci.services.iam.AssumeRolePolicyEvaluator;
|
||||
import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator;
|
||||
import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator.SimulationDecision;
|
||||
@@ -58,8 +60,9 @@ public class GetStackOutputRoleAuthorizer {
|
||||
if (role.isEmpty() || (enforced() && !trustEvaluator.allowsAnyPrincipalOf(
|
||||
role.get().getAssumeRolePolicyDocument(), consumerAccount, partition))) {
|
||||
throw new AwsException("AccessDenied", CloudFormationTemplateEngine.sdkErrorMessage(
|
||||
"User: " + AwsArnUtils.Arn.global(partition, "iam", consumerAccount, "root")
|
||||
+ " is not authorized to perform: sts:AssumeRole on resource: " + roleArn,
|
||||
AccessDeniedMessages.notAuthorized(
|
||||
AwsArnUtils.Arn.global(partition, "iam", consumerAccount, "root").toString(),
|
||||
"sts:AssumeRole", roleArn, null),
|
||||
"AWSSecurityTokenService", 403, "AccessDenied"), 403);
|
||||
}
|
||||
return AwsArnUtils.parse(roleArn).accountId();
|
||||
@@ -83,14 +86,12 @@ public class GetStackOutputRoleAuthorizer {
|
||||
if (decision == SimulationDecision.ALLOWED) {
|
||||
return;
|
||||
}
|
||||
String reason = decision == SimulationDecision.EXPLICIT_DENY
|
||||
? "with an explicit deny in an identity-based policy"
|
||||
: "because no identity-based policy allows the " + DESCRIBE_STACKS + " action";
|
||||
String session = AwsArnUtils.Arn.global(partition, "sts", stack.accountId(),
|
||||
"assumed-role/" + role.getRoleName() + "/" + SESSION_NAME).toString();
|
||||
throw new AwsException("AccessDenied", CloudFormationTemplateEngine.sdkErrorMessage(
|
||||
"User: " + session + " is not authorized to perform: " + DESCRIBE_STACKS + " on resource: "
|
||||
+ stackId + " " + reason, "AmazonCloudFormation", 403, "AccessDenied"), 403);
|
||||
AccessDeniedMessages.notAuthorized(session, DESCRIBE_STACKS, stackId,
|
||||
PolicyType.IDENTITY_BASED_POLICY.refusal(decision, DESCRIBE_STACKS, null)),
|
||||
"AmazonCloudFormation", 403, "AccessDenied"), 403);
|
||||
}
|
||||
|
||||
private boolean enforced() {
|
||||
|
||||
@@ -0,0 +1,114 @@
|
||||
package io.github.hectorvent.floci.services.iam;
|
||||
|
||||
import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator.SimulationDecision;
|
||||
import io.github.hectorvent.floci.services.iam.IamService.HeldPolicies;
|
||||
import io.github.hectorvent.floci.services.iam.IamService.HeldPolicy;
|
||||
import io.github.hectorvent.floci.services.iam.model.CallerContext;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* The access denied message AWS returns when a policy refuses a call, and the end of it that names the
|
||||
* policy type that refused it (IAM User Guide, "Access denied error message examples").
|
||||
*/
|
||||
public final class AccessDeniedMessages {
|
||||
|
||||
/** A policy type the end of the message names, as AWS words it. */
|
||||
public enum PolicyType {
|
||||
SERVICE_CONTROL_POLICY("a", "service control policy"),
|
||||
RESOURCE_BASED_POLICY("a", "resource-based policy"),
|
||||
ROLE_TRUST_POLICY("the", "role trust policy"),
|
||||
IDENTITY_BASED_POLICY("an", "identity-based policy"),
|
||||
PERMISSIONS_BOUNDARY("a", "permissions boundary"),
|
||||
SESSION_POLICY("a", "session policy");
|
||||
|
||||
private final String article;
|
||||
private final String name;
|
||||
|
||||
PolicyType(String article, String name) {
|
||||
this.article = article;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String explicitDeny() {
|
||||
return explicitDeny(null);
|
||||
}
|
||||
|
||||
/** Names the policy by its ARN too when one is given, as AWS names a managed policy or a boundary. */
|
||||
public String explicitDeny(String policyArn) {
|
||||
return "with an explicit deny in " + article + " " + name + (policyArn == null ? "" : ": " + policyArn);
|
||||
}
|
||||
|
||||
public String noAllow(String action) {
|
||||
return "because no " + name + " allows the " + action + " action";
|
||||
}
|
||||
|
||||
public String refusal(SimulationDecision decision, String action, String policyArn) {
|
||||
return decision == SimulationDecision.EXPLICIT_DENY ? explicitDeny(policyArn) : noAllow(action);
|
||||
}
|
||||
}
|
||||
|
||||
private AccessDeniedMessages() {
|
||||
}
|
||||
|
||||
/** The message, ending with {@code reason} when there is one. */
|
||||
public static String notAuthorized(String callerArn, String action, String resource, String reason) {
|
||||
return "User: " + callerArn + " is not authorized to perform: " + action + " on resource: " + resource
|
||||
+ (reason == null ? "" : " " + reason);
|
||||
}
|
||||
|
||||
/**
|
||||
* Why {@code caller} may not perform {@code action} on {@code resource}, or null when it may, for a
|
||||
* resource with no policy of its own. Each policy type is checked in turn, service control policies,
|
||||
* identity policies, permissions boundary, then session policy, and the first that refuses names the
|
||||
* reason, by its explicit deny or by the allow it lacks. So identity policies with no allow win over
|
||||
* an explicit deny in the boundary (measured for {@code lambda:PassNetworkConnector}). AWS names one
|
||||
* of several denying identity policies at random, and this names the first in {@code held}, which is
|
||||
* null when the caller holds no policies by ARN.
|
||||
*/
|
||||
public static String firstRefusal(IamPolicyEvaluator evaluator, CallerContext caller,
|
||||
HeldPolicies held, String action, String resource,
|
||||
Map<String, List<String>> context) {
|
||||
for (List<String> level : caller.scpLevels() == null ? List.<List<String>>of() : caller.scpLevels()) {
|
||||
SimulationDecision decision = level.isEmpty()
|
||||
? SimulationDecision.ALLOWED : simulate(evaluator, level, action, resource, context);
|
||||
if (decision != SimulationDecision.ALLOWED) {
|
||||
return PolicyType.SERVICE_CONTROL_POLICY.refusal(decision, action, null);
|
||||
}
|
||||
}
|
||||
SimulationDecision identity = simulate(evaluator, caller.identityPolicies(), action, resource, context);
|
||||
if (identity != SimulationDecision.ALLOWED) {
|
||||
String denying = identity == SimulationDecision.EXPLICIT_DENY && held != null
|
||||
? held.identity().stream()
|
||||
.filter(policy -> simulate(evaluator, List.of(policy.document()), action, resource,
|
||||
context) == SimulationDecision.EXPLICIT_DENY)
|
||||
.findFirst()
|
||||
.map(HeldPolicy::arn)
|
||||
.orElse(null)
|
||||
: null;
|
||||
return PolicyType.IDENTITY_BASED_POLICY.refusal(identity, action, denying);
|
||||
}
|
||||
if (caller.boundaryPolicyDocument() != null) {
|
||||
SimulationDecision boundary = simulate(evaluator, List.of(caller.boundaryPolicyDocument()), action,
|
||||
resource, context);
|
||||
if (boundary != SimulationDecision.ALLOWED) {
|
||||
return PolicyType.PERMISSIONS_BOUNDARY.refusal(boundary, action,
|
||||
held == null ? null : held.permissionsBoundaryArn());
|
||||
}
|
||||
}
|
||||
if (caller.sessionPolicyDocument() != null) {
|
||||
SimulationDecision session = simulate(evaluator, List.of(caller.sessionPolicyDocument()), action,
|
||||
resource, context);
|
||||
if (session != SimulationDecision.ALLOWED) {
|
||||
return PolicyType.SESSION_POLICY.refusal(session, action, null);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static SimulationDecision simulate(IamPolicyEvaluator evaluator, List<String> documents, String action,
|
||||
String resource, Map<String, List<String>> context) {
|
||||
return evaluator.simulatePrincipalPolicy(CallerContext.of(documents), action, resource, context);
|
||||
}
|
||||
}
|
||||
@@ -6005,64 +6005,68 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
return passwordPolicies.get(ACCOUNT_PASSWORD_POLICY_KEY);
|
||||
}
|
||||
|
||||
/** An identity policy as an access denied message names it: a managed one by its ARN, an inline one by none. */
|
||||
public record HeldPolicy(String arn, String document) {}
|
||||
|
||||
/** The identity policies of an IAM user or role, in the order they are collected, and its permissions boundary. */
|
||||
public record HeldPolicies(List<HeldPolicy> identity, String permissionsBoundaryArn) {}
|
||||
|
||||
/** What the IAM user or role behind {@code accessKeyId} holds, or nothing for any other caller. */
|
||||
public Optional<HeldPolicies> heldPolicies(String accessKeyId) {
|
||||
Optional<AccessKey> key = accessKeys.get(accessKeyId);
|
||||
if (key.isPresent()) {
|
||||
return users.get(key.get().getUserName()).map(this::heldPolicies);
|
||||
}
|
||||
Optional<SessionCredential> session = findSessionForCallerContext(accessKeyId)
|
||||
.filter(found -> found.getDelegatedApproverArn() == null && !isFederatedUserSession(found));
|
||||
if (session.isPresent() && session.get().getRoleArn() == null) {
|
||||
// GetSessionToken credentials act with the policies of the user that minted them.
|
||||
return session.filter(found -> found.getPresignedAction() == null)
|
||||
.flatMap(this::issuingUser)
|
||||
.map(this::heldPolicies);
|
||||
}
|
||||
return session.flatMap(found -> roleFromArnOrName(found.getRoleArn()))
|
||||
.map(role -> new HeldPolicies(rolePolicies(role), role.getPermissionsBoundaryArn()));
|
||||
}
|
||||
|
||||
private HeldPolicies heldPolicies(IamUser user) {
|
||||
return new HeldPolicies(userPolicies(user), user.getPermissionsBoundaryArn());
|
||||
}
|
||||
|
||||
private List<String> collectUserPolicies(String userArnOrName) {
|
||||
Optional<IamUser> userOpt = userFromArnOrName(userArnOrName);
|
||||
if (userOpt.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
IamUser user = userOpt.get();
|
||||
|
||||
// User inline policies
|
||||
List<String> docs = new ArrayList<>(user.getInlinePolicies().values());
|
||||
|
||||
// User attached managed policies
|
||||
for (String arn : user.getAttachedPolicyArns()) {
|
||||
Optional<IamPolicy> p = resolvePolicy(arn);
|
||||
if (p.isPresent() && p.get().getDefaultDocument() != null) {
|
||||
docs.add(p.get().getDefaultDocument());
|
||||
}
|
||||
}
|
||||
|
||||
// Group policies
|
||||
for (String groupName : user.getGroupNames()) {
|
||||
Optional<IamGroup> groupOpt = groups.get(groupName);
|
||||
if (groupOpt.isEmpty()) continue;
|
||||
IamGroup group = groupOpt.get();
|
||||
docs.addAll(group.getInlinePolicies().values());
|
||||
for (String arn : group.getAttachedPolicyArns()) {
|
||||
Optional<IamPolicy> p = resolvePolicy(arn);
|
||||
if (p.isPresent() && p.get().getDefaultDocument() != null) {
|
||||
docs.add(p.get().getDefaultDocument());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return docs;
|
||||
return userFromArnOrName(userArnOrName).map(user -> documents(userPolicies(user))).orElse(null);
|
||||
}
|
||||
|
||||
private List<String> collectRolePolicies(String roleArn) {
|
||||
if (roleArn == null) {
|
||||
return null;
|
||||
}
|
||||
Optional<IamRole> roleOpt = roleFromArnOrName(roleArn);
|
||||
if (roleOpt.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
IamRole role = roleOpt.get();
|
||||
List<String> docs = new ArrayList<>();
|
||||
return roleFromArnOrName(roleArn).map(role -> documents(rolePolicies(role))).orElse(null);
|
||||
}
|
||||
|
||||
// Role inline policies
|
||||
docs.addAll(role.getInlinePolicies().values());
|
||||
|
||||
// Role attached managed policies
|
||||
for (String arn : role.getAttachedPolicyArns()) {
|
||||
Optional<IamPolicy> p = resolvePolicy(arn);
|
||||
if (p.isPresent() && p.get().getDefaultDocument() != null) {
|
||||
docs.add(p.get().getDefaultDocument());
|
||||
}
|
||||
/** The user's inline and managed policies, then each group's. */
|
||||
private List<HeldPolicy> userPolicies(IamUser user) {
|
||||
List<HeldPolicy> held = inlineThenManaged(user.getInlinePolicies(), user.getAttachedPolicyArns());
|
||||
for (String groupName : user.getGroupNames()) {
|
||||
groups.get(groupName).ifPresent(group ->
|
||||
held.addAll(inlineThenManaged(group.getInlinePolicies(), group.getAttachedPolicyArns())));
|
||||
}
|
||||
return held;
|
||||
}
|
||||
|
||||
return docs;
|
||||
private List<HeldPolicy> rolePolicies(IamRole role) {
|
||||
return inlineThenManaged(role.getInlinePolicies(), role.getAttachedPolicyArns());
|
||||
}
|
||||
|
||||
private List<HeldPolicy> inlineThenManaged(Map<String, String> inline, List<String> attachedArns) {
|
||||
List<HeldPolicy> held = new ArrayList<>();
|
||||
inline.values().forEach(document -> held.add(new HeldPolicy(null, document)));
|
||||
for (String arn : attachedArns) {
|
||||
resolvePolicy(arn).map(IamPolicy::getDefaultDocument)
|
||||
.ifPresent(document -> held.add(new HeldPolicy(arn, document)));
|
||||
}
|
||||
return held;
|
||||
}
|
||||
|
||||
private static List<String> documents(List<HeldPolicy> held) {
|
||||
return held.stream().map(HeldPolicy::document).collect(Collectors.toCollection(ArrayList::new));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+18
-19
@@ -2,6 +2,8 @@ package io.github.hectorvent.floci.services.stepfunctions;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.IamConditionContextResolver;
|
||||
import io.github.hectorvent.floci.services.iam.AccessDeniedMessages;
|
||||
import io.github.hectorvent.floci.services.iam.AccessDeniedMessages.PolicyType;
|
||||
import io.github.hectorvent.floci.services.iam.AssumeRolePolicyEvaluator;
|
||||
import io.github.hectorvent.floci.services.iam.AssumeRolePolicyEvaluator.TrustDecision;
|
||||
import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator;
|
||||
@@ -76,8 +78,7 @@ public class LambdaTaskAuthorizer {
|
||||
Optional<IamRole> invokingRole = role(roleArn);
|
||||
if (invokingRole.isEmpty()) {
|
||||
LOG.infov("Role {0} names no existing role, so it cannot invoke {1}", roleArn, resourceArn);
|
||||
return Optional.of(denied(roleArn, action, resourceArn, "because no identity-based policy allows the "
|
||||
+ action + " action"));
|
||||
return Optional.of(denied(roleArn, action, resourceArn, PolicyType.IDENTITY_BASED_POLICY.noAllow(action)));
|
||||
}
|
||||
// The role is evaluated as itself, under the ARN IAM holds for it, whichever form names it.
|
||||
String invokingRoleArn = invokingRole.get().getArn();
|
||||
@@ -122,8 +123,8 @@ public class LambdaTaskAuthorizer {
|
||||
Optional<IamRole> executionRole = role(executionRoleArn);
|
||||
if (executionRole.isEmpty()) {
|
||||
LOG.infov("Execution role {0} names no existing role, so it cannot assume {1}", executionRoleArn, roleArn);
|
||||
return Optional.of(denied(executionRoleArn, ASSUME_ROLE, roleArn, "because no identity-based policy allows the "
|
||||
+ ASSUME_ROLE + " action"));
|
||||
return Optional.of(denied(executionRoleArn, ASSUME_ROLE, roleArn,
|
||||
PolicyType.IDENTITY_BASED_POLICY.noAllow(ASSUME_ROLE)));
|
||||
}
|
||||
String callerRoleArn = executionRole.get().getArn();
|
||||
Optional<IamRole> target = role(roleArn);
|
||||
@@ -215,27 +216,27 @@ public class LambdaTaskAuthorizer {
|
||||
boolean crossAccount = relationship == ResourceAccountRelationship.CROSS_ACCOUNT;
|
||||
|
||||
if (scps.contains(SimulationDecision.EXPLICIT_DENY)) {
|
||||
return "with an explicit deny in a service control policy";
|
||||
return PolicyType.SERVICE_CONTROL_POLICY.explicitDeny();
|
||||
}
|
||||
if (grant.explicitDeny()) {
|
||||
return "with an explicit deny in " + grant.deniedIn();
|
||||
return grant.type().explicitDeny();
|
||||
}
|
||||
if (identity == SimulationDecision.EXPLICIT_DENY) {
|
||||
return "with an explicit deny in an identity-based policy";
|
||||
return PolicyType.IDENTITY_BASED_POLICY.explicitDeny();
|
||||
}
|
||||
if (boundary == SimulationDecision.EXPLICIT_DENY) {
|
||||
return "with an explicit deny in a permissions boundary";
|
||||
return PolicyType.PERMISSIONS_BOUNDARY.explicitDeny();
|
||||
}
|
||||
if (scps.contains(SimulationDecision.IMPLICIT_DENY)) {
|
||||
return "because no service control policy allows the " + action + " action";
|
||||
return PolicyType.SERVICE_CONTROL_POLICY.noAllow(action);
|
||||
}
|
||||
if ((grant.required() || crossAccount) && !grant.allows()) {
|
||||
return "because no " + grant.kind() + " allows the " + action + " action";
|
||||
return grant.type().noAllow(action);
|
||||
}
|
||||
if (identity != SimulationDecision.ALLOWED && (crossAccount || !grant.sufficient())) {
|
||||
return "because no identity-based policy allows the " + action + " action";
|
||||
return PolicyType.IDENTITY_BASED_POLICY.noAllow(action);
|
||||
}
|
||||
return "because no permissions boundary allows the " + action + " action";
|
||||
return PolicyType.PERMISSIONS_BOUNDARY.noAllow(action);
|
||||
}
|
||||
|
||||
private SimulationDecision simulate(List<String> documents, String action, String resource,
|
||||
@@ -244,8 +245,7 @@ public class LambdaTaskAuthorizer {
|
||||
}
|
||||
|
||||
private static String denied(String roleArn, String action, String resource, String reason) {
|
||||
return "User: " + sessionArn(roleArn) + " is not authorized to perform: " + action + " on resource: "
|
||||
+ resource + (reason == null ? "" : " " + reason);
|
||||
return AccessDeniedMessages.notAuthorized(sessionArn(roleArn), action, resource, reason);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -272,24 +272,23 @@ public class LambdaTaskAuthorizer {
|
||||
* caller's identity-based policies
|
||||
* @param required whether the call needs it to allow even within one account, as a role's
|
||||
* trust policy does
|
||||
* @param deniedIn the policy, as an explicit denial names it
|
||||
* @param kind the policy type, as an implicit denial names it
|
||||
* @param type the policy type, as the access denied message names it
|
||||
*/
|
||||
private record ResourceGrant(boolean explicitDeny, boolean allows, boolean sufficient, boolean required,
|
||||
String deniedIn, String kind) {
|
||||
PolicyType type) {
|
||||
|
||||
static ResourceGrant of(ResourcePolicyDecision functionPolicy) {
|
||||
boolean sufficient = functionPolicy == ResourcePolicyDecision.ALLOW
|
||||
|| functionPolicy == ResourcePolicyDecision.ALLOW_DIRECT_IAM_USER;
|
||||
return new ResourceGrant(functionPolicy == ResourcePolicyDecision.EXPLICIT_DENY,
|
||||
sufficient || functionPolicy == ResourcePolicyDecision.ALLOW_ACCOUNT, sufficient, false,
|
||||
"a resource-based policy", "resource-based policy");
|
||||
PolicyType.RESOURCE_BASED_POLICY);
|
||||
}
|
||||
|
||||
static ResourceGrant of(TrustDecision trust) {
|
||||
return new ResourceGrant(trust == TrustDecision.EXPLICIT_DENY,
|
||||
trust == TrustDecision.ALLOW || trust == TrustDecision.ALLOW_ACCOUNT,
|
||||
trust == TrustDecision.ALLOW, true, "the role trust policy", "role trust policy");
|
||||
trust == TrustDecision.ALLOW, true, PolicyType.ROLE_TRUST_POLICY);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user