mirror of
https://github.com/floci-io/floci.git
synced 2026-12-02 09:47:56 -05:00
fix(dsql): evaluate the cluster's resource-based policy on connect (#6319)
DSQL evaluates a cluster's resource-based policy when a client connects (measured, eu-west-1): an explicit Deny that applies to the caller refuses the connection with the hint "User: <caller> is not authorized to perform: dsql:<action> on resource: <cluster ARN> with an explicit deny in a resource-based policy", under the statement's conditions, aws:PrincipalArn and aws:SourceIp among them, and a statement that does not apply leaves the caller's own policies to decide. With IAM enforcement on, the endpoint checked only the caller's identity policies. DsqlResourcePolicyProvider supplies a cluster's policy through ResourcePolicyProviders, as the SNS and Lambda providers do, and the endpoint evaluates it with the caller's policies and SCPs as LambdaTaskAuthorizer and LambdaWebIamAuth evaluate a function's policy: evaluateResourcePolicyFor, then evaluateResolvedResourcePolicy, with the client's address as aws:SourceIp. With enforcement off nothing is evaluated, as for every other policy in Floci. DsqlService already built the cluster policy's context for the lockout check and the "is not authorized" sentence for the tag refusals; both now come from DsqlPrincipals (policyContext, notAuthorized), which the endpoint uses too, instead of a third copy. The policy also governs control plane calls on DSQL (measured), but Floci's IAM enforcement names DSQL's control plane resources as *, so that is left as it is.
This commit is contained in:
1 parent
212a424ed2
commit
f60cf120c6
10 files changed
+329
-45
No files matched your search
@@ -1,6 +1,7 @@
|
||||
package io.github.hectorvent.floci.services.dsql;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.IamConditionContextResolver;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.common.RequestContext;
|
||||
import io.github.hectorvent.floci.services.iam.IamService;
|
||||
@@ -10,6 +11,10 @@ import jakarta.enterprise.context.ContextNotActiveException;
|
||||
import jakarta.enterprise.inject.Instance;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Who is calling, and whether an IAM principal a cluster policy names exists: what DSQL checks a
|
||||
* policy against. A credential that names no IAM identity is its account's root, as for STS.
|
||||
@@ -17,6 +22,9 @@ import jakarta.inject.Inject;
|
||||
@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;
|
||||
@@ -30,12 +38,36 @@ public class DsqlPrincipals {
|
||||
}
|
||||
|
||||
public CallerArns caller() {
|
||||
return callerOf(accessKeyId());
|
||||
}
|
||||
|
||||
/** Who signs with {@code accessKeyId}, as {@link #caller()} names the caller of a request. */
|
||||
public CallerArns callerOf(String accessKeyId) {
|
||||
String accountId = regionResolver.getAccountId();
|
||||
return iamService.resolveCallerArns(accessKeyId())
|
||||
return iamService.resolveCallerArns(accessKeyId)
|
||||
.orElseGet(() -> CallerArns.accountRoot(regionResolver.buildGlobalArn("iam", accountId, "root"),
|
||||
accountId));
|
||||
}
|
||||
|
||||
/**
|
||||
* The context a cluster policy is read in for {@code caller}: its principal keys, the global
|
||||
* keys of the cluster in {@code region}, and {@code keys}, such as a connection's
|
||||
* {@code aws:SourceIp}.
|
||||
*/
|
||||
public Map<String, List<String>> policyContext(CallerArns caller, String clusterArn, String region,
|
||||
Map<String, List<String>> keys) {
|
||||
Map<String, List<String>> context = new HashMap<>(keys);
|
||||
caller.putPrincipalKeys(context);
|
||||
String accountId = regionResolver.getAccountId();
|
||||
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.
|
||||
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package io.github.hectorvent.floci.services.dsql;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.services.iam.ResourcePolicyProvider;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Supplies a DSQL cluster's resource-based policy, when it has one, to IAM policy evaluation, with
|
||||
* the account that owns the cluster: the cluster endpoint evaluates it on connect, as DSQL does
|
||||
* (measured).
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class DsqlResourcePolicyProvider implements ResourcePolicyProvider {
|
||||
|
||||
private final DsqlService dsqlService;
|
||||
|
||||
@Inject
|
||||
public DsqlResourcePolicyProvider(DsqlService dsqlService) {
|
||||
this.dsqlService = dsqlService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ResourcePolicy> getResourcePolicies(String credentialScope, String resourceArn) {
|
||||
return dsqlService.clusterOfArn(resourceArn)
|
||||
.filter(cluster -> cluster.getPolicy() != null)
|
||||
.map(cluster -> List.of(new ResourcePolicy(cluster.getPolicy(),
|
||||
AwsArnUtils.parse(resourceArn).accountId())))
|
||||
.orElse(List.of());
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,6 @@ 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.core.common.Digests;
|
||||
import io.github.hectorvent.floci.core.common.IamConditionContextResolver;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.common.Resettable;
|
||||
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
@@ -28,7 +27,6 @@ import java.time.format.DateTimeParseException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
@@ -332,13 +330,9 @@ public class DsqlService implements Resettable {
|
||||
|
||||
private boolean deniesCaller(String policy, String clusterArn, String region, String action) {
|
||||
CallerArns caller = principals.caller();
|
||||
Map<String, List<String>> context = new HashMap<>();
|
||||
caller.putPrincipalKeys(context);
|
||||
String accountId = regionResolver.getAccountId();
|
||||
Map<String, List<String>> effective = IamConditionContextResolver.withGlobalContext(
|
||||
context, clusterArn, region, accountId, accountId);
|
||||
return policyEvaluator.evaluateResourcePolicyFor(List.of(policy), RequestPrincipal.caller(caller),
|
||||
action, clusterArn, effective) == ResourcePolicyDecision.EXPLICIT_DENY;
|
||||
return policyEvaluator.evaluateResourcePolicyFor(List.of(policy), RequestPrincipal.caller(caller), action,
|
||||
clusterArn, principals.policyContext(caller, clusterArn, region, Map.of()))
|
||||
== ResourcePolicyDecision.EXPLICIT_DENY;
|
||||
}
|
||||
|
||||
// ── Tags ──────────────────────────────────────────────────────────────────
|
||||
@@ -472,7 +466,8 @@ public class DsqlService implements Resettable {
|
||||
}
|
||||
}
|
||||
|
||||
private Optional<DsqlCluster> clusterOfArn(String arn) {
|
||||
/** The cluster {@code arn} names in the caller's account, if there is one. */
|
||||
public Optional<DsqlCluster> clusterOfArn(String arn) {
|
||||
AwsArnUtils.Arn parsed = clusterArnOrNull(arn);
|
||||
if (parsed == null || !regionResolver.getAccountId().equals(parsed.accountId())) {
|
||||
return Optional.empty();
|
||||
@@ -491,15 +486,14 @@ public class DsqlService implements Resettable {
|
||||
if (parsed == null) {
|
||||
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.User: " + caller
|
||||
+ " is not authorized to perform: dsql:" + action + " on resource: * with an explicit deny in a"
|
||||
+ " resource-based policy", 403);
|
||||
+ " 'arn:partition:dsql:region:account-id:cluster/cluster-id' and try again."
|
||||
+ DsqlPrincipals.notAuthorized(caller, "dsql:" + action, "*",
|
||||
DsqlPrincipals.EXPLICIT_RESOURCE_DENY), 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", "User: " + caller + " is not authorized to perform: dsql:"
|
||||
+ action + " on resource: " + arn + " because no resource-based policy allows the dsql:" + action
|
||||
+ " action", 403);
|
||||
throw new AwsException("AccessDeniedException", DsqlPrincipals.notAuthorized(caller, "dsql:" + action, arn,
|
||||
"because no resource-based policy allows the dsql:" + action + " action"), 403);
|
||||
}
|
||||
if (!region.equals(parsed.region())) {
|
||||
throw new AwsException("BadRequestException", "Invalid input resource arn: expected region " + region, 400);
|
||||
|
||||
+54
-23
@@ -6,9 +6,17 @@ import io.github.hectorvent.floci.core.common.AwsRegions;
|
||||
import io.github.hectorvent.floci.core.common.PostgresWire;
|
||||
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.IamPolicyEvaluator;
|
||||
import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator.Decision;
|
||||
import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator.ResourceAccountRelationship;
|
||||
import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator.ResourcePolicyDecision;
|
||||
import io.github.hectorvent.floci.services.iam.IamService;
|
||||
import io.github.hectorvent.floci.services.iam.IamService.CallerArns;
|
||||
import io.github.hectorvent.floci.services.iam.RequestPrincipal;
|
||||
import io.github.hectorvent.floci.services.iam.ResourcePolicyProvider;
|
||||
import io.github.hectorvent.floci.services.iam.ResourcePolicyProviders;
|
||||
import io.github.hectorvent.floci.services.iam.ScpProvider;
|
||||
import io.github.hectorvent.floci.services.iam.model.CallerContext;
|
||||
import jakarta.annotation.PreDestroy;
|
||||
@@ -53,9 +61,11 @@ import java.util.concurrent.atomic.AtomicBoolean;
|
||||
* eu-west-1, 2026-10-16): the password is a SigV4 token for the endpoint's host, {@code admin}
|
||||
* signs {@code DbConnectAdmin} and every other role {@code DbConnect}, a token is taken up to 5
|
||||
* seconds past its expiry, and a role other than {@code admin} logs in only for an IAM role that
|
||||
* {@code AWS IAM GRANT} mapped to it. Every refusal is the same {@code access denied}, with a hint
|
||||
* only where DSQL gives one. DSQL lets clients in while a cluster is still CREATING; Floci's
|
||||
* clusters are CREATING only while their database starts, and serve once it answers.
|
||||
* {@code AWS IAM GRANT} mapped to it. With IAM enforcement on, the caller's policies and the
|
||||
* cluster's resource-based policy have to let it connect. Every refusal is the same
|
||||
* {@code access denied}, with a hint only where DSQL gives one. DSQL lets clients in while a
|
||||
* cluster is still CREATING; Floci's clusters are CREATING only while their database starts, and
|
||||
* serve once it answers.
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class DsqlEndpoint {
|
||||
@@ -71,6 +81,8 @@ public class DsqlEndpoint {
|
||||
private final IamService iamService;
|
||||
private final IamPolicyEvaluator policyEvaluator;
|
||||
private final Instance<ScpProvider> scpProvider;
|
||||
private final Instance<ResourcePolicyProvider> resourcePolicyProviders;
|
||||
private final DsqlPrincipals principals;
|
||||
private final SigV4RequestValidator validator;
|
||||
private final boolean enforcement;
|
||||
private final int configuredPort;
|
||||
@@ -112,12 +124,15 @@ public class DsqlEndpoint {
|
||||
|
||||
@Inject
|
||||
public DsqlEndpoint(DsqlTlsCertificates certificates, IamService iamService, IamPolicyEvaluator policyEvaluator,
|
||||
Instance<ScpProvider> scpProvider, EmulatorConfig config, Clock clock) {
|
||||
Instance<ScpProvider> scpProvider, Instance<ResourcePolicyProvider> resourcePolicyProviders,
|
||||
DsqlPrincipals principals, EmulatorConfig config, Clock clock) {
|
||||
this.clock = clock;
|
||||
this.certificates = certificates;
|
||||
this.iamService = iamService;
|
||||
this.policyEvaluator = policyEvaluator;
|
||||
this.scpProvider = scpProvider;
|
||||
this.resourcePolicyProviders = resourcePolicyProviders;
|
||||
this.principals = principals;
|
||||
this.validator = new SigV4RequestValidator(iamService);
|
||||
this.enforcement = config.services().iam().enforcementEnabled();
|
||||
this.configuredPort = config.services().dsql().port();
|
||||
@@ -275,11 +290,12 @@ public class DsqlEndpoint {
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether {@code token} lets {@code user} in, checked in the order DSQL reports: the cluster
|
||||
* the client named as its TLS server, the action the user needs, the token's expiry, then its
|
||||
* signature and, for a role other than {@code admin}, the IAM role mapped to it.
|
||||
* Whether {@code token} lets {@code user} in from {@code sourceIp}, checked in the order DSQL
|
||||
* reports: the cluster the client named as its TLS server, the action the user needs, the
|
||||
* token's expiry, then its signature, the policies with IAM enforcement on and, for a role other
|
||||
* than {@code admin}, the IAM role mapped to it.
|
||||
*/
|
||||
Admission admit(String token, String user, String serverName) {
|
||||
Admission admit(String token, String user, String serverName, String sourceIp) {
|
||||
URI uri;
|
||||
try {
|
||||
uri = URI.create("https://" + token);
|
||||
@@ -331,16 +347,16 @@ public class DsqlEndpoint {
|
||||
}
|
||||
// The endpoint runs outside any request: the caller's identity and policies are read in the
|
||||
// cluster's account, as the enforcement filter reads them in the request's.
|
||||
String principal = RequestScopes.callAs(route.accountId(), route.region(), () -> {
|
||||
if (enforcement && !allowedByIam(accessKeyId, "dsql:" + needed, route)) {
|
||||
return null;
|
||||
}
|
||||
return iamService.resolvePrincipalArn(accessKeyId).orElseGet(() -> AwsArnUtils.Arn.global(
|
||||
AwsRegions.partitionFor(route.region()), "iam", route.accountId(), "root").toString());
|
||||
Admission identified = RequestScopes.callAs(route.accountId(), route.region(), () -> {
|
||||
Admission refused = enforcement ? refusedByIam(accessKeyId, "dsql:" + needed, route, sourceIp) : null;
|
||||
return refused != null ? refused : new Admission(true, null, null, route,
|
||||
iamService.resolvePrincipalArn(accessKeyId).orElseGet(() -> AwsArnUtils.Arn.global(
|
||||
AwsRegions.partitionFor(route.region()), "iam", route.accountId(), "root").toString()));
|
||||
});
|
||||
if (principal == null) {
|
||||
return Admission.refused(null);
|
||||
if (!identified.allowed()) {
|
||||
return identified;
|
||||
}
|
||||
String principal = identified.principalArn();
|
||||
if (!ADMIN.equals(user)) {
|
||||
try {
|
||||
if (!mapped(route.backend(), principal, user)) {
|
||||
@@ -374,22 +390,37 @@ public class DsqlEndpoint {
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the caller's policies let it {@code action} the cluster, under its organization's
|
||||
* service control policies as the enforcement filter applies them. A known credential with no
|
||||
* policies to evaluate is let through, as the filter lets it.
|
||||
* The refusal DSQL gives unless the caller's policies, under its organization's service control
|
||||
* policies, and the cluster's resource-based policy let it {@code action} the cluster, or null when
|
||||
* they do. DSQL evaluates the cluster's policy on connect, with the client's address as
|
||||
* {@code aws:SourceIp}, and hints only at an explicit Deny in it (measured). A known credential
|
||||
* with no policies to evaluate is let through, as the enforcement filter lets it.
|
||||
*/
|
||||
private boolean allowedByIam(String accessKeyId, String action, Route route) {
|
||||
private Admission refusedByIam(String accessKeyId, String action, Route route, String sourceIp) {
|
||||
CallerContext caller = iamService.resolveCallerContext(accessKeyId);
|
||||
if (caller == null) {
|
||||
return true;
|
||||
return null;
|
||||
}
|
||||
List<List<String>> scpLevels = scpProvider.isResolvable()
|
||||
? scpProvider.get().effectiveScpLevels(route.accountId()) : null;
|
||||
if (scpLevels != null) {
|
||||
caller = caller.withScpLevels(scpLevels);
|
||||
}
|
||||
return policyEvaluator.simulatePrincipalPolicy(caller, action, route.arn(), Map.of())
|
||||
== IamPolicyEvaluator.SimulationDecision.ALLOWED;
|
||||
CallerArns arns = principals.callerOf(accessKeyId);
|
||||
List<String> clusterPolicies = ResourcePolicyProviders.policiesFor(resourcePolicyProviders, "dsql",
|
||||
route.arn()).stream().map(ResourcePolicyProvider.ResourcePolicy::policyDocument).toList();
|
||||
Map<String, List<String>> context = principals.policyContext(arns, route.arn(), route.region(),
|
||||
Map.of("aws:SourceIp", List.of(sourceIp)));
|
||||
ResourcePolicyDecision clusterPolicy = policyEvaluator.evaluateResourcePolicyFor(clusterPolicies,
|
||||
RequestPrincipal.caller(arns), action, route.arn(), context);
|
||||
if (policyEvaluator.evaluateResolvedResourcePolicy(caller, clusterPolicy,
|
||||
ResourceAccountRelationship.SAME_ACCOUNT, action, route.arn(), context) == Decision.ALLOW) {
|
||||
return null;
|
||||
}
|
||||
return Admission.refused(clusterPolicy == ResourcePolicyDecision.EXPLICIT_DENY
|
||||
? DsqlPrincipals.notAuthorized(arns.callerArn(), action, route.arn(),
|
||||
DsqlPrincipals.EXPLICIT_RESOURCE_DENY)
|
||||
: null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -206,7 +206,8 @@ final class DsqlSession {
|
||||
return;
|
||||
}
|
||||
String token = PostgresWire.cString(password.body(), 0).value();
|
||||
DsqlEndpoint.Admission admission = endpoint.admit(token, user, serverName[0]);
|
||||
DsqlEndpoint.Admission admission = endpoint.admit(token, user, serverName[0],
|
||||
socket.getInetAddress().getHostAddress());
|
||||
if (!admission.allowed()) {
|
||||
refuse(out, admission.state(), ACCESS_DENIED, admission.hint());
|
||||
return;
|
||||
|
||||
Reference in new issue
Block a user