Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

chore(iam): build the remaining access denied reasons with AccessDeniedMessages (#6715)

TATSUNO “Taz” Yasuhiro1f9b3993e273
diff --git a/src/main/java/io/github/hectorvent/floci/core/common/IamEnforcementFilter.java b/src/main/java/io/github/hectorvent/floci/core/common/IamEnforcementFilter.java
index 813bd92ab5..c59edf717c 100644
--- a/src/main/java/io/github/hectorvent/floci/core/common/IamEnforcementFilter.java
+++ b/src/main/java/io/github/hectorvent/floci/core/common/IamEnforcementFilter.java
@@ -4,6 +4,7 @@ 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.AccessDeniedMessages.PolicyType;
import io.github.hectorvent.floci.services.iam.IamActionRegistry;
import io.github.hectorvent.floci.services.iam.IamAuthorizedActions;
import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator;
@@ -1199,7 +1200,7 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
String denyMessage = "User: " + AwsArnUtils.Arn.global(requestPartition(), "iam", accountId, "user/" + akid)
+ " is not authorized to perform: " + action
+ " on resource: \"" + resource + "\""
- + " because no identity-based policy allows the " + action + " action";
+ + " " + PolicyType.IDENTITY_BASED_POLICY.noAllow(action);
emitS3DenialIfApplicable(akid, operationAction, resource, ctx, region, denyMessage);
ctx.abortWith(accessDeniedForRequest(action, credentialScope, ctx, resource));
return true;
@@ -1487,7 +1488,7 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
String denyMessage = "User: " + AwsArnUtils.Arn.global(requestPartition(), "iam", accountId, "user/" + akid)
+ " is not authorized to perform: " + action
+ " on resource: \"" + resource + "\""
- + " because no identity-based policy allows the " + action + " action";
+ + " " + PolicyType.IDENTITY_BASED_POLICY.noAllow(action);
if (deniedEvent != null) {
emitS3Denial(akid, deniedEvent.name(), resource, deniedEvent.region(), denyMessage);
}
diff --git a/src/main/java/io/github/hectorvent/floci/services/dsql/DsqlPrincipals.java b/src/main/java/io/github/hectorvent/floci/services/dsql/DsqlPrincipals.java
index 50c2d20f91..602cf87366 100644
--- a/src/main/java/io/github/hectorvent/floci/services/dsql/DsqlPrincipals.java
+++ b/src/main/java/io/github/hectorvent/floci/services/dsql/DsqlPrincipals.java
@@ -22,9 +22,6 @@ import java.util.Map;
@ApplicationScoped
public class DsqlPrincipals {
- /** How IAM ends a refusal that an explicit Deny in the cluster's policy decided. */
- public static final String EXPLICIT_RESOURCE_DENY = "with an explicit deny in a resource-based policy";
-
private final IamService iamService;
private final RegionResolver regionResolver;
private final Instance<RequestContext> requestContext;
@@ -62,12 +59,6 @@ public class DsqlPrincipals {
return IamConditionContextResolver.withGlobalContext(context, clusterArn, region, accountId, accountId);
}
- /** IAM's refusal of {@code callerArn} for {@code action} on {@code resource}, as DSQL words it. */
- 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;
- }
-
/**
* Whether the IAM principal {@code arn} names exists. Floci takes every account to exist, so an
* account's root always does; a role or a user is looked up in its account by its name.
diff --git a/src/main/java/io/github/hectorvent/floci/services/dsql/DsqlService.java b/src/main/java/io/github/hectorvent/floci/services/dsql/DsqlService.java
index 1c70c75278..799a458687 100644
--- a/src/main/java/io/github/hectorvent/floci/services/dsql/DsqlService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/dsql/DsqlService.java
@@ -12,6 +12,8 @@ import io.github.hectorvent.floci.core.common.Resettable;
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import io.github.hectorvent.floci.services.dsql.model.DsqlCluster;
+import io.github.hectorvent.floci.services.iam.AccessDeniedMessages;
+import io.github.hectorvent.floci.services.iam.AccessDeniedMessages.PolicyType;
import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator;
import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator.ResourcePolicyDecision;
import io.github.hectorvent.floci.services.iam.IamService.CallerArns;
@@ -488,13 +490,13 @@ public class DsqlService implements Resettable {
throw new AwsException("AccessDeniedException", "Invalid input for 'resourceArn'. The cluster ARN that you"
+ " specified isn't valid. Provide the cluster ARN in the format"
+ " 'arn:partition:dsql:region:account-id:cluster/cluster-id' and try again."
- + DsqlPrincipals.notAuthorized(caller, "dsql:" + action, "*",
- DsqlPrincipals.EXPLICIT_RESOURCE_DENY), 403);
+ + AccessDeniedMessages.notAuthorized(caller, "dsql:" + action, "*",
+ PolicyType.RESOURCE_BASED_POLICY.explicitDeny()), 403);
}
if (!regionResolver.getAccountId().equals(parsed.accountId())) {
// Measured: another account's cluster is refused, not looked up in the caller's.
- throw new AwsException("AccessDeniedException", DsqlPrincipals.notAuthorized(caller, "dsql:" + action, arn,
- "because no resource-based policy allows the dsql:" + action + " action"), 403);
+ throw new AwsException("AccessDeniedException", AccessDeniedMessages.notAuthorized(caller, "dsql:" + action,
+ arn, PolicyType.RESOURCE_BASED_POLICY.noAllow("dsql:" + action)), 403);
}
if (!region.equals(parsed.region())) {
throw new AwsException("BadRequestException", "Invalid input resource arn: expected region " + region, 400);
diff --git a/src/main/java/io/github/hectorvent/floci/services/dsql/dataplane/DsqlEndpoint.java b/src/main/java/io/github/hectorvent/floci/services/dsql/dataplane/DsqlEndpoint.java
index 5215343a59..e67f28b92a 100644
--- a/src/main/java/io/github/hectorvent/floci/services/dsql/dataplane/DsqlEndpoint.java
+++ b/src/main/java/io/github/hectorvent/floci/services/dsql/dataplane/DsqlEndpoint.java
@@ -8,6 +8,8 @@ import io.github.hectorvent.floci.core.common.RequestScopes;
import io.github.hectorvent.floci.core.common.auth.SigV4RequestValidator;
import io.github.hectorvent.floci.services.dsql.DsqlPrincipals;
import io.github.hectorvent.floci.services.dsql.model.DsqlCluster;
+import io.github.hectorvent.floci.services.iam.AccessDeniedMessages;
+import io.github.hectorvent.floci.services.iam.AccessDeniedMessages.PolicyType;
import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator;
import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator.Decision;
import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator.ResourceAccountRelationship;
@@ -418,8 +420,8 @@ public class DsqlEndpoint {
return null;
}
return Admission.refused(clusterPolicy == ResourcePolicyDecision.EXPLICIT_DENY
- ? DsqlPrincipals.notAuthorized(arns.callerArn(), action, route.arn(),
- DsqlPrincipals.EXPLICIT_RESOURCE_DENY)
+ ? AccessDeniedMessages.notAuthorized(arns.callerArn(), action, route.arn(),
+ PolicyType.RESOURCE_BASED_POLICY.explicitDeny())
: null);
}
diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java
index 744cd22448..96aff16fc4 100644
--- a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java
@@ -23,6 +23,7 @@ import io.github.hectorvent.floci.core.storage.StorageBackedMap;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import io.github.hectorvent.floci.services.ec2.Ec2Service;
import io.github.hectorvent.floci.services.ec2.model.Subnet;
+import io.github.hectorvent.floci.services.iam.AccessDeniedMessages.PolicyType;
import io.github.hectorvent.floci.services.lambda.durable.DurableExecutionService;
import io.github.hectorvent.floci.services.lambda.durable.DurableWire;
import io.github.hectorvent.floci.services.lambda.durable.model.DurableErrorObject;
@@ -342,8 +343,8 @@ public class LambdaService implements ResourceProvider {
if (!acceptable) {
throw new AwsException("AccessDeniedException",
"User is not authorized to perform: lambda:GetLayerVersion on resource: "
- + arn + " because no resource-based policy allows the"
- + " lambda:GetLayerVersion action", 403);
+ + arn + " " + PolicyType.RESOURCE_BASED_POLICY.noAllow("lambda:GetLayerVersion"),
+ 403);
}
LOG.warnv("Layer {0} belongs to another account; recorded on the function but its"
+ " content will not be mounted at /opt", arn);
diff --git a/src/main/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebService.java b/src/main/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebService.java
index c93edc353b..838cc26818 100644
--- a/src/main/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebService.java
@@ -20,6 +20,8 @@ import io.github.hectorvent.floci.core.common.ServicePrincipals;
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
import io.github.hectorvent.floci.core.storage.StorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
+import io.github.hectorvent.floci.services.iam.AccessDeniedMessages;
+import io.github.hectorvent.floci.services.iam.AccessDeniedMessages.PolicyType;
import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator.Decision;
import io.github.hectorvent.floci.services.iam.PublicAccessConditions;
import io.github.hectorvent.floci.services.lambdamicrovms.LambdaMicrovmsService;
@@ -1630,9 +1632,8 @@ public class LambdaWebService implements Resettable {
String callerAccount = regionResolver.getAccountId();
if (!callerAccount.equals(arn.group(2))) {
String user = regionResolver.buildGlobalArn("iam", callerAccount, "user/" + accessKeyId());
- throw new AwsException("AccessDeniedException", "User: " + user + " is not authorized to perform: lambda:"
- + action + " on resource: " + givenArn + " because no resource-based policy allows the lambda:"
- + action + " action", 403);
+ throw new AwsException("AccessDeniedException", AccessDeniedMessages.notAuthorized(user, "lambda:" + action,
+ givenArn, PolicyType.RESOURCE_BASED_POLICY.noAllow("lambda:" + action)), 403);
}
}