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
@@ -15,6 +15,8 @@ import java.util.Properties;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.awaitility.Awaitility.await;
|
||||
import static org.hamcrest.Matchers.anyOf;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.jupiter.api.Assumptions.assumeTrue;
|
||||
|
||||
/**
|
||||
@@ -65,6 +67,20 @@ public final class DsqlClusterFixture {
|
||||
return host;
|
||||
}
|
||||
|
||||
public String arn() {
|
||||
return "arn:aws:dsql:" + REGION + ":" + ACCOUNT + ":cluster/" + identifier;
|
||||
}
|
||||
|
||||
/** Gives the cluster {@code policy} through the control plane, as the account's root. */
|
||||
public void putPolicy(String policy) {
|
||||
dsql().body(Map.of("policy", policy)).post("/cluster/" + identifier + "/policy").then().statusCode(200);
|
||||
}
|
||||
|
||||
/** Takes the cluster's policy away, if it has one. */
|
||||
public void removePolicy() {
|
||||
dsql().delete("/cluster/" + identifier + "/policy").then().statusCode(anyOf(is(200), is(404)));
|
||||
}
|
||||
|
||||
public String url() {
|
||||
return "jdbc:postgresql://127.0.0.1:" + endpoint.port() + "/postgres";
|
||||
}
|
||||
|
||||
+152
@@ -0,0 +1,152 @@
|
||||
package io.github.hectorvent.floci.services.dsql;
|
||||
|
||||
import com.github.dockerjava.api.DockerClient;
|
||||
import io.github.hectorvent.floci.services.dsql.dataplane.DsqlEndpoint;
|
||||
import io.github.hectorvent.floci.testing.EnforcementFixtures;
|
||||
import io.github.hectorvent.floci.testing.PartitionCleanup;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.TestProfile;
|
||||
import jakarta.inject.Inject;
|
||||
import org.eclipse.microprofile.config.inject.ConfigProperty;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Tag;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestInstance;
|
||||
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||
import org.postgresql.util.PSQLException;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.Statement;
|
||||
import java.time.Instant;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* A cluster's resource-based policy on connect, with IAM enforcement on, as Aurora DSQL evaluates it
|
||||
* (measured, eu-west-1, 2026-10-22): an explicit Deny that applies to the caller refuses the
|
||||
* connection, with a hint naming the caller, the action and the cluster, under the conditions the
|
||||
* statement sets; a statement that does not apply leaves the decision to the caller's own policies.
|
||||
*/
|
||||
@QuarkusTest
|
||||
@TestProfile(DsqlEnforcementProfile.class)
|
||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||
@Tag("docker")
|
||||
class DsqlClusterPolicyIntegrationTest {
|
||||
|
||||
private static final String ACCOUNT = DsqlClusterFixture.ACCOUNT;
|
||||
|
||||
@Inject
|
||||
DockerClient dockerClient;
|
||||
|
||||
@Inject
|
||||
DsqlEndpoint endpoint;
|
||||
|
||||
@ConfigProperty(name = "quarkus.http.test-port", defaultValue = "0")
|
||||
int httpPort;
|
||||
|
||||
@RegisterExtension
|
||||
final PartitionCleanup cleanup = new PartitionCleanup();
|
||||
|
||||
private DsqlClusterFixture cluster;
|
||||
|
||||
@BeforeAll
|
||||
void createCluster() {
|
||||
cluster = new DsqlClusterFixture(httpPort, endpoint);
|
||||
cluster.create(dockerClient);
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
void deleteCluster() {
|
||||
cluster.delete();
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void removePolicy() {
|
||||
cluster.removePolicy();
|
||||
}
|
||||
|
||||
@Test
|
||||
void anExplicitDenyThatAppliesToTheCallerRefusesItsConnection() throws Exception {
|
||||
String userName = "dsql-" + Long.toString(System.nanoTime(), 36);
|
||||
EnforcementFixtures.UserCredentials user = adminUser(userName);
|
||||
String userArn = "arn:aws:iam::" + ACCOUNT + ":user/" + userName;
|
||||
signsIn(user);
|
||||
|
||||
String[] denials = {
|
||||
statement("Deny", "{\"AWS\":\"arn:aws:iam::" + ACCOUNT + ":root\"}", "dsql:DbConnectAdmin", "*", null),
|
||||
statement("Deny", "\"*\"", "dsql:DbConnectAdmin", "*",
|
||||
"{\"ArnEquals\":{\"aws:PrincipalArn\":\"" + userArn + "\"}}"),
|
||||
statement("Deny", "\"*\"", "dsql:DbConnectAdmin", "*",
|
||||
"{\"IpAddress\":{\"aws:SourceIp\":\"127.0.0.1/32\"}}"),
|
||||
};
|
||||
for (String denial : denials) {
|
||||
cluster.putPolicy(denial);
|
||||
PSQLException refused = assertThrows(PSQLException.class, () -> connect(user).close(), denial);
|
||||
assertEquals("08006", refused.getSQLState(), denial);
|
||||
assertEquals("unable to accept connection, access denied", refused.getServerErrorMessage().getMessage());
|
||||
assertTrue(refused.getServerErrorMessage().getDetail().startsWith("Session Id: "));
|
||||
assertEquals("User: " + userArn + " is not authorized to perform: dsql:DbConnectAdmin on resource: "
|
||||
+ cluster.arn() + " with an explicit deny in a resource-based policy",
|
||||
refused.getServerErrorMessage().getHint(), denial);
|
||||
}
|
||||
|
||||
cluster.removePolicy();
|
||||
signsIn(user);
|
||||
}
|
||||
|
||||
@Test
|
||||
void aStatementThatDoesNotApplyLeavesTheCallersOwnPoliciesToDecide() throws Exception {
|
||||
EnforcementFixtures.UserCredentials user = adminUser("dsql-" + Long.toString(System.nanoTime(), 36));
|
||||
String[] others = {
|
||||
statement("Deny", "\"*\"", "dsql:DbConnectAdmin", "*",
|
||||
"{\"IpAddress\":{\"aws:SourceIp\":\"192.0.2.0/24\"}}"),
|
||||
statement("Deny", "{\"AWS\":\"arn:aws:iam::" + ACCOUNT + ":root\"}", "dsql:DbConnect", "*", null),
|
||||
statement("Deny", "{\"AWS\":\"arn:aws:iam::" + ACCOUNT + ":root\"}", "dsql:DbConnectAdmin",
|
||||
"arn:aws:dsql:" + DsqlClusterFixture.REGION + ":" + ACCOUNT + ":cluster/abcdefghijklmnopqrstuvwxyz",
|
||||
null),
|
||||
statement("Allow", "{\"AWS\":\"arn:aws:iam::" + ACCOUNT + ":root\"}", "dsql:DbConnectAdmin", "*",
|
||||
"{\"ArnEquals\":{\"aws:PrincipalArn\":\"arn:aws:iam::" + ACCOUNT + ":role/someone-else\"}}"),
|
||||
};
|
||||
for (String other : others) {
|
||||
cluster.putPolicy(other);
|
||||
signsIn(user);
|
||||
}
|
||||
}
|
||||
|
||||
/** A new IAM user whose own policy lets it connect as {@code admin}. */
|
||||
private EnforcementFixtures.UserCredentials adminUser(String userName) {
|
||||
String authorization = EnforcementFixtures.auth(ACCOUNT, "iam");
|
||||
EnforcementFixtures.UserCredentials user = EnforcementFixtures.createUser(cleanup, httpPort, authorization,
|
||||
userName);
|
||||
EnforcementFixtures.putUserPolicy(httpPort, authorization, userName, "connect", """
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":"dsql:DbConnectAdmin",
|
||||
"Resource":"*"}]}""");
|
||||
return user;
|
||||
}
|
||||
|
||||
private Connection connect(EnforcementFixtures.UserCredentials user) throws Exception {
|
||||
return cluster.connect("admin", cluster.token("DbConnectAdmin", user.accessKeyId(), user.secretAccessKey(),
|
||||
Instant.now(), 900, null));
|
||||
}
|
||||
|
||||
private void signsIn(EnforcementFixtures.UserCredentials user) throws Exception {
|
||||
try (Connection connection = connect(user);
|
||||
Statement statement = connection.createStatement();
|
||||
ResultSet row = statement.executeQuery("SELECT current_user")) {
|
||||
assertTrue(row.next());
|
||||
assertEquals("admin", row.getString(1));
|
||||
}
|
||||
}
|
||||
|
||||
private static String statement(String effect, String principal, String action, String resource,
|
||||
String condition) {
|
||||
return "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"" + effect + "\",\"Principal\":" + principal
|
||||
+ ",\"Action\":\"" + action + "\",\"Resource\":\"" + resource + "\""
|
||||
+ (condition == null ? "" : ",\"Condition\":" + condition) + "}]}";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package io.github.hectorvent.floci.services.dsql;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTestProfile;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/** {@link DsqlDatabasesProfile}'s clusters with IAM enforcement on, for what a cluster's policy decides on connect. */
|
||||
public final class DsqlEnforcementProfile implements QuarkusTestProfile {
|
||||
@Override
|
||||
public Map<String, String> getConfigOverrides() {
|
||||
Map<String, String> overrides = new HashMap<>(new DsqlDatabasesProfile().getConfigOverrides());
|
||||
overrides.put("floci.services.iam.enforcement-enabled", "true");
|
||||
return overrides;
|
||||
}
|
||||
}
|
||||
+6
-1
@@ -1,8 +1,10 @@
|
||||
package io.github.hectorvent.floci.services.dsql.dataplane;
|
||||
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.services.dsql.DsqlPrincipals;
|
||||
import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator;
|
||||
import io.github.hectorvent.floci.services.iam.IamService;
|
||||
import io.github.hectorvent.floci.services.iam.ResourcePolicyProvider;
|
||||
import io.github.hectorvent.floci.services.iam.ScpProvider;
|
||||
import io.github.hectorvent.floci.testing.MutableClock;
|
||||
import jakarta.enterprise.inject.Instance;
|
||||
@@ -26,8 +28,11 @@ class DsqlEndpointTest {
|
||||
void createEndpoint() {
|
||||
@SuppressWarnings("unchecked")
|
||||
Instance<ScpProvider> scpProvider = mock(Instance.class);
|
||||
@SuppressWarnings("unchecked")
|
||||
Instance<ResourcePolicyProvider> resourcePolicyProviders = mock(Instance.class);
|
||||
endpoint = new DsqlEndpoint(mock(DsqlTlsCertificates.class), mock(IamService.class),
|
||||
mock(IamPolicyEvaluator.class), scpProvider, mock(EmulatorConfig.class, RETURNS_DEEP_STUBS), clock);
|
||||
mock(IamPolicyEvaluator.class), scpProvider, resourcePolicyProviders, mock(DsqlPrincipals.class),
|
||||
mock(EmulatorConfig.class, RETURNS_DEEP_STUBS), clock);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
|
||||
Reference in new issue
Block a user