mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(redshift): enforce Lake Formation SELECT on Spectrum queries (#8414)
This commit is contained in:
1 parent
dc74687f73
commit
00183c0bf1
14 files changed
+654
-12
No files matched your search
@@ -0,0 +1,204 @@
|
||||
package io.floci.aws.services.lakeformation;
|
||||
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.services.lakeformation.model.ColumnWildcard;
|
||||
import io.floci.aws.services.lakeformation.model.DataLakePrincipal;
|
||||
import io.floci.aws.services.lakeformation.model.DataLakeSettings;
|
||||
import io.floci.aws.services.lakeformation.model.PrincipalPermissions;
|
||||
import io.floci.aws.services.lakeformation.model.PrincipalResourcePermissions;
|
||||
import io.floci.aws.services.lakeformation.model.Resource;
|
||||
import io.floci.aws.services.lakeformation.model.TableResource;
|
||||
import io.floci.aws.services.lakeformation.model.TableWildcard;
|
||||
import io.floci.aws.services.lakeformation.model.TableWithColumnsResource;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.ArgumentMatchers.isNull;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class LakeFormationAuthorizerTest {
|
||||
|
||||
private static final String ACCOUNT = "000000000000";
|
||||
private static final String ROLE = "arn:aws:iam::000000000000:role/SpectrumRole";
|
||||
|
||||
private LakeFormationStorage storage;
|
||||
private LakeFormationAuthorizer authorizer;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
storage = mock(LakeFormationStorage.class);
|
||||
RegionResolver regionResolver = new RegionResolver("us-east-1", ACCOUNT);
|
||||
authorizer = new LakeFormationAuthorizer(storage, regionResolver);
|
||||
storeSettings(List.of());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aCatalogWithoutStoredSettingsLeavesEveryTableToIam() {
|
||||
when(storage.getDataLakeSettings(anyString(), anyString())).thenReturn(Optional.empty());
|
||||
assertTrue(authorizer.canSelect(ACCOUNT, ROLE, "lake", "events"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void defaultTablePermissionsNamingIamAllowedPrincipalsLeaveTablesToIam() {
|
||||
storeSettings(List.of(defaultPermissionFor(LakeFormationAuthorizer.IAM_ALLOWED_PRINCIPALS)));
|
||||
assertTrue(authorizer.canSelect(ACCOUNT, ROLE, "lake", "events"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void defaultTablePermissionsForAnotherPrincipalDoNotLeaveTablesToIam() {
|
||||
storeSettings(List.of(defaultPermissionFor("arn:aws:iam::000000000000:role/Other")));
|
||||
assertFalse(authorizer.canSelect(ACCOUNT, ROLE, "lake", "events"));
|
||||
}
|
||||
|
||||
private void storeSettings(List<PrincipalPermissions> createTableDefaults) {
|
||||
DataLakeSettings settings = new DataLakeSettings();
|
||||
settings.setCreateTableDefaultPermissions(createTableDefaults);
|
||||
when(storage.getDataLakeSettings(anyString(), anyString())).thenReturn(Optional.of(settings));
|
||||
}
|
||||
|
||||
private static PrincipalPermissions defaultPermissionFor(String principalId) {
|
||||
DataLakePrincipal principal = new DataLakePrincipal();
|
||||
principal.setDataLakePrincipalIdentifier(principalId);
|
||||
PrincipalPermissions permissions = new PrincipalPermissions();
|
||||
permissions.setPrincipal(principal);
|
||||
permissions.setPermissions(List.of("ALL"));
|
||||
return permissions;
|
||||
}
|
||||
|
||||
@Test
|
||||
void noGrantDenies() {
|
||||
assertFalse(authorizer.canSelect(ACCOUNT, ROLE, "lake", "events"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void selectOnTheTableAllows() {
|
||||
grant(ROLE, tableResource("lake", "events"), "SELECT");
|
||||
assertTrue(authorizer.canSelect(ACCOUNT, ROLE, "lake", "events"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void allOnTheTableAllows() {
|
||||
grant(ROLE, tableResource("lake", "events"), "ALL");
|
||||
assertTrue(authorizer.canSelect(ACCOUNT, ROLE, "lake", "events"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void namesMatchIgnoringCaseLikeGlue() {
|
||||
grant(ROLE, tableResource("salesdb", "events"), "SELECT");
|
||||
assertTrue(authorizer.canSelect(ACCOUNT, ROLE, "SalesDB", "EVENTS"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anotherTableDenies() {
|
||||
grant(ROLE, tableResource("lake", "orders"), "SELECT");
|
||||
assertFalse(authorizer.canSelect(ACCOUNT, ROLE, "lake", "events"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anotherDatabaseDenies() {
|
||||
grant(ROLE, tableResource("other", "events"), "SELECT");
|
||||
assertFalse(authorizer.canSelect(ACCOUNT, ROLE, "lake", "events"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void describeAloneDenies() {
|
||||
grant(ROLE, tableResource("lake", "events"), "DESCRIBE");
|
||||
assertFalse(authorizer.canSelect(ACCOUNT, ROLE, "lake", "events"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void tableWildcardCoversEveryTableOfTheDatabase() {
|
||||
TableResource table = new TableResource();
|
||||
table.setDatabaseName("lake");
|
||||
table.setTableWildcard(new TableWildcard());
|
||||
Resource resource = new Resource();
|
||||
resource.setTable(table);
|
||||
grant(ROLE, resource, "SELECT");
|
||||
assertTrue(authorizer.canSelect(ACCOUNT, ROLE, "lake", "events"));
|
||||
assertFalse(authorizer.canSelect(ACCOUNT, ROLE, "other", "events"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void columnWildcardCoversTheWholeTable() {
|
||||
grant(ROLE, columnsResource(new ColumnWildcard()), "SELECT");
|
||||
assertTrue(authorizer.canSelect(ACCOUNT, ROLE, "lake", "events"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void columnWildcardWithExclusionsDoesNotCoverTheWholeTable() {
|
||||
ColumnWildcard wildcard = new ColumnWildcard();
|
||||
wildcard.setExcludedColumnNames(List.of("secret"));
|
||||
grant(ROLE, columnsResource(wildcard), "SELECT");
|
||||
assertFalse(authorizer.canSelect(ACCOUNT, ROLE, "lake", "events"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void namedColumnsDoNotCoverTheWholeTable() {
|
||||
grant(ROLE, columnsResource(null), "SELECT");
|
||||
assertFalse(authorizer.canSelect(ACCOUNT, ROLE, "lake", "events"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void iamAllowedPrincipalsLeavesTheTableToIam() {
|
||||
grant(LakeFormationAuthorizer.IAM_ALLOWED_PRINCIPALS, tableResource("lake", "events"), "ALL");
|
||||
assertTrue(authorizer.canSelect(ACCOUNT, ROLE, "lake", "events"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aGrantToAnotherPrincipalDenies() {
|
||||
grant("arn:aws:iam::000000000000:role/Other", tableResource("lake", "events"), "SELECT");
|
||||
assertFalse(authorizer.canSelect(ACCOUNT, ROLE, "lake", "events"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void databaseAndTableNamesMatchIgnoringCase() {
|
||||
grant(ROLE, tableResource("salesdb", "orders"), "SELECT");
|
||||
assertTrue(authorizer.canSelect(ACCOUNT, ROLE, "SalesDB", "Orders"));
|
||||
}
|
||||
|
||||
private void grant(String principalId, Resource resource, String permission) {
|
||||
DataLakePrincipal principal = new DataLakePrincipal();
|
||||
principal.setDataLakePrincipalIdentifier(principalId);
|
||||
PrincipalResourcePermissions grant = new PrincipalResourcePermissions();
|
||||
grant.setPrincipal(principal);
|
||||
grant.setResource(resource);
|
||||
grant.setPermissions(List.of(permission));
|
||||
when(storage.listPermissions(anyString(), anyString(), any(DataLakePrincipal.class), isNull(), isNull(),
|
||||
eq(false), isNull(), isNull()))
|
||||
.thenAnswer(invocation -> {
|
||||
DataLakePrincipal asked = invocation.getArgument(2);
|
||||
return principalId.equals(asked.getDataLakePrincipalIdentifier()) ? List.of(grant) : List.of();
|
||||
});
|
||||
}
|
||||
|
||||
private static Resource tableResource(String database, String name) {
|
||||
TableResource table = new TableResource();
|
||||
table.setDatabaseName(database);
|
||||
table.setName(name);
|
||||
Resource resource = new Resource();
|
||||
resource.setTable(table);
|
||||
return resource;
|
||||
}
|
||||
|
||||
private static Resource columnsResource(ColumnWildcard wildcard) {
|
||||
TableWithColumnsResource columns = new TableWithColumnsResource();
|
||||
columns.setDatabaseName("lake");
|
||||
columns.setName("events");
|
||||
columns.setColumnWildcard(wildcard);
|
||||
if (wildcard == null) {
|
||||
columns.setColumnNames(List.of("id"));
|
||||
}
|
||||
Resource resource = new Resource();
|
||||
resource.setTableWithColumns(columns);
|
||||
return resource;
|
||||
}
|
||||
}
|
||||
+182
@@ -0,0 +1,182 @@
|
||||
package io.floci.aws.services.redshift;
|
||||
|
||||
import io.floci.aws.services.glue.GlueService;
|
||||
import io.floci.aws.services.glue.model.Database;
|
||||
import io.floci.aws.services.iam.IamService;
|
||||
import io.floci.aws.services.lakeformation.LakeFormationService;
|
||||
import io.floci.aws.services.lakeformation.model.DataLakePrincipal;
|
||||
import io.floci.aws.services.lakeformation.model.DataLakeSettings;
|
||||
import io.floci.aws.services.lakeformation.model.GrantPermissionsRequest;
|
||||
import io.floci.aws.services.lakeformation.model.PutDataLakeSettingsRequest;
|
||||
import io.floci.aws.services.lakeformation.model.Resource;
|
||||
import io.floci.aws.services.lakeformation.model.RevokePermissionsRequest;
|
||||
import io.floci.aws.services.lakeformation.model.TableResource;
|
||||
import io.floci.aws.services.redshift.model.Cluster;
|
||||
import io.floci.aws.services.redshift.spectrum.ExternalStatement;
|
||||
import io.floci.aws.services.redshift.spectrum.ExternalStatementParser;
|
||||
import io.floci.aws.services.redshift.spectrum.GlueTableBuilder;
|
||||
import io.floci.aws.services.s3.S3Service;
|
||||
import io.floci.aws.testing.SidecarContainersProfile;
|
||||
import io.floci.aws.testing.SidecarSpectrumLakeFormationProfile;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.TestProfile;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.Timeout;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.List;
|
||||
|
||||
import static io.floci.aws.services.redshift.RedshiftConnections.waitForConnection;
|
||||
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 Spectrum query is refused until the external schema's role holds a Lake Formation {@code SELECT}
|
||||
* grant on the table, and a revoke stops the next query even though the table is already cached.
|
||||
*/
|
||||
@QuarkusTest
|
||||
@TestProfile(SidecarSpectrumLakeFormationProfile.class)
|
||||
class RedshiftSpectrumLakeFormationIntegrationTest {
|
||||
|
||||
private static final String REGION = "us-east-1";
|
||||
private static final String ROLE_NAME = "SpectrumLfItRole";
|
||||
private static final String ROLE_ARN = "arn:aws:iam::000000000000:role/" + ROLE_NAME;
|
||||
private static final String TRUST_POLICY = """
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Allow",
|
||||
"Principal":{"Service":"redshift.amazonaws.com"},"Action":"sts:AssumeRole"}]}""";
|
||||
|
||||
@Inject
|
||||
RedshiftService redshiftService;
|
||||
|
||||
@Inject
|
||||
S3Service s3Service;
|
||||
|
||||
@Inject
|
||||
IamService iamService;
|
||||
|
||||
@Inject
|
||||
GlueService glueService;
|
||||
|
||||
@Inject
|
||||
LakeFormationService lakeFormationService;
|
||||
|
||||
private String clusterId;
|
||||
private String bucket;
|
||||
private String glueDatabase;
|
||||
|
||||
@BeforeAll
|
||||
static void requireDocker() throws ClassNotFoundException {
|
||||
SidecarContainersProfile.requireDocker();
|
||||
Class.forName("org.postgresql.Driver");
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void cleanUp() {
|
||||
if (clusterId != null) {
|
||||
redshiftService.deleteCluster(clusterId);
|
||||
}
|
||||
if (bucket != null) {
|
||||
s3Service.deleteObject(bucket, "events/part-1.csv");
|
||||
s3Service.deleteBucket(bucket);
|
||||
}
|
||||
if (glueDatabase != null) {
|
||||
glueService.deleteDatabase(glueDatabase);
|
||||
}
|
||||
iamService.deleteRolePolicy(ROLE_NAME, "AllowS3");
|
||||
iamService.deleteRole(ROLE_NAME);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Timeout(180)
|
||||
void aGrantOpensTheTableAndARevokeClosesItAgainOnceCached() throws Exception {
|
||||
iamService.createRole(ROLE_NAME, "/", TRUST_POLICY, null, 0, null);
|
||||
iamService.putRolePolicy(ROLE_NAME, "AllowS3", """
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Allow",
|
||||
"Action":["s3:GetObject","s3:ListBucket"],"Resource":"*"}]}""");
|
||||
bucket = "spectrum-lf-" + System.nanoTime();
|
||||
s3Service.createBucket(bucket, REGION);
|
||||
s3Service.putObject(bucket, "events/part-1.csv", "1,10\n2,7\n".getBytes(StandardCharsets.UTF_8),
|
||||
"text/csv", null);
|
||||
glueDatabase = "spectrum_lf_" + System.nanoTime();
|
||||
Database database = new Database();
|
||||
database.setName(glueDatabase);
|
||||
glueService.createDatabase(database);
|
||||
clusterId = "it-lf-" + System.nanoTime();
|
||||
Cluster cluster = redshiftService.createCluster(clusterId, "dc2.large", "admin", "Secret123",
|
||||
null, List.of(), List.of(ROLE_ARN));
|
||||
try (Connection connection = waitForConnection(cluster);
|
||||
Statement statement = connection.createStatement()) {
|
||||
statement.execute("CREATE EXTERNAL SCHEMA lake FROM DATA CATALOG DATABASE '" + glueDatabase
|
||||
+ "' IAM_ROLE '" + ROLE_ARN + "'");
|
||||
String externalDdl = "CREATE EXTERNAL TABLE lake.events (id INTEGER, amount INTEGER) "
|
||||
+ "ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' STORED AS TEXTFILE LOCATION 's3://"
|
||||
+ bucket + "/events/'";
|
||||
ExternalStatement.CreateTable external = (ExternalStatement.CreateTable)
|
||||
new ExternalStatementParser().parse(externalDdl).orElseThrow();
|
||||
glueService.createTable(glueDatabase, GlueTableBuilder.toGlueTable(external));
|
||||
|
||||
DataLakeSettings settings = new DataLakeSettings();
|
||||
settings.setCreateTableDefaultPermissions(List.of());
|
||||
PutDataLakeSettingsRequest managed = new PutDataLakeSettingsRequest();
|
||||
managed.setDataLakeSettings(settings);
|
||||
lakeFormationService.putDataLakeSettings(REGION, managed);
|
||||
|
||||
SQLException denied = assertThrows(SQLException.class, () -> sumAmount(statement));
|
||||
assertEquals("42501", denied.getSQLState());
|
||||
|
||||
lakeFormationService.grantPermissions(REGION, grant());
|
||||
assertEquals(17L, sumAmount(statement));
|
||||
assertEquals(17L, sumAmount(statement));
|
||||
|
||||
lakeFormationService.revokePermissions(REGION, revoke());
|
||||
SQLException revoked = assertThrows(SQLException.class, () -> sumAmount(statement));
|
||||
assertEquals("42501", revoked.getSQLState());
|
||||
}
|
||||
}
|
||||
|
||||
private GrantPermissionsRequest grant() {
|
||||
GrantPermissionsRequest request = new GrantPermissionsRequest();
|
||||
request.setPrincipal(principal());
|
||||
request.setResource(tableResource());
|
||||
request.setPermissions(List.of("SELECT"));
|
||||
return request;
|
||||
}
|
||||
|
||||
private RevokePermissionsRequest revoke() {
|
||||
RevokePermissionsRequest request = new RevokePermissionsRequest();
|
||||
request.setPrincipal(principal());
|
||||
request.setResource(tableResource());
|
||||
request.setPermissions(List.of("SELECT"));
|
||||
return request;
|
||||
}
|
||||
|
||||
private static DataLakePrincipal principal() {
|
||||
DataLakePrincipal principal = new DataLakePrincipal();
|
||||
principal.setDataLakePrincipalIdentifier(ROLE_ARN);
|
||||
return principal;
|
||||
}
|
||||
|
||||
private Resource tableResource() {
|
||||
TableResource table = new TableResource();
|
||||
table.setDatabaseName(glueDatabase);
|
||||
table.setName("events");
|
||||
Resource resource = new Resource();
|
||||
resource.setTable(table);
|
||||
return resource;
|
||||
}
|
||||
|
||||
private static long sumAmount(Statement statement) throws SQLException {
|
||||
try (ResultSet rows = statement.executeQuery("SELECT SUM(amount) FROM lake.events")) {
|
||||
assertTrue(rows.next());
|
||||
return rows.getLong(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
+45
-2
@@ -11,6 +11,7 @@ import io.floci.aws.services.glue.model.Table;
|
||||
import io.floci.aws.services.iam.IamService;
|
||||
import io.floci.aws.services.iam.model.CallerContext;
|
||||
import io.floci.aws.services.iam.model.IamRole;
|
||||
import io.floci.aws.services.lakeformation.LakeFormationAuthorizer;
|
||||
import io.floci.aws.services.s3.S3Service;
|
||||
import io.floci.aws.services.s3.model.S3Object;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
@@ -77,6 +78,8 @@ class ExternalTableMaterializerTest {
|
||||
private IamService iam;
|
||||
private RecordingBackend backend;
|
||||
private ExternalTableMaterializer materializer;
|
||||
private EmulatorConfig config;
|
||||
private LakeFormationAuthorizer lakeFormation;
|
||||
|
||||
private static final class RecordingBackend implements BackendSql {
|
||||
private final List<String> statements = new ArrayList<>();
|
||||
@@ -115,12 +118,13 @@ class ExternalTableMaterializerTest {
|
||||
when(role.getArn()).thenReturn("arn:aws:iam::000000000000:role/R");
|
||||
when(role.getAssumeRolePolicyDocument()).thenReturn(TRUST_POLICY);
|
||||
when(iam.findRole(ACCOUNT, "R")).thenReturn(Optional.of(role));
|
||||
EmulatorConfig config = mock(EmulatorConfig.class, Answers.RETURNS_DEEP_STUBS);
|
||||
config = mock(EmulatorConfig.class, Answers.RETURNS_DEEP_STUBS);
|
||||
lakeFormation = mock(LakeFormationAuthorizer.class);
|
||||
when(config.services().redshift().spectrumMaxRows()).thenReturn(1000L);
|
||||
when(config.defaultRegion()).thenReturn("us-east-1");
|
||||
when(duck.query(anyString(), any(), anyString())).thenReturn(List.of());
|
||||
backend = new RecordingBackend();
|
||||
materializer = new ExternalTableMaterializer(duck, glue, s3, iam, config);
|
||||
materializer = new ExternalTableMaterializer(duck, glue, s3, iam, config, lakeFormation);
|
||||
when(s3.openObjectStream(eq(ExternalTableMaterializer.SCRATCH_BUCKET), anyString(), isNull()))
|
||||
.thenReturn(new ByteArrayInputStream("id,name\n1,Alice\n2,Bob\n".getBytes(StandardCharsets.UTF_8)));
|
||||
S3Object icebergMetadata = new S3Object("bucket", "events/metadata/v1.json",
|
||||
@@ -139,6 +143,45 @@ class ExternalTableMaterializerTest {
|
||||
.thenReturn(new S3Service.ListObjectsResult(List.of(), List.of(), false, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void lakeFormationIsNotConsultedWhenEnforcementIsOff() {
|
||||
when(glue.getTable("lake", "events")).thenReturn(csvTable());
|
||||
materializer.ensureCurrent(backend, session(false), BINDING, "events");
|
||||
verify(lakeFormation, never()).canSelect(anyString(), anyString(), anyString(), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void lakeFormationGrantAllowsTheQuery() {
|
||||
when(config.services().redshift().spectrumLakeFormationEnforcement()).thenReturn(true);
|
||||
when(glue.getTable("lake", "events")).thenReturn(csvTable());
|
||||
when(lakeFormation.canSelect(ACCOUNT, BINDING.iamRoleArn(), "lake", "events")).thenReturn(true);
|
||||
assertEquals(ExternalTableMaterializer.Outcome.LOADED,
|
||||
materializer.ensureCurrent(backend, session(false), BINDING, "events"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void lakeFormationRefusalDeniesWithInsufficientPrivilege() {
|
||||
when(config.services().redshift().spectrumLakeFormationEnforcement()).thenReturn(true);
|
||||
when(glue.getTable("lake", "events")).thenReturn(csvTable());
|
||||
SpectrumSqlException denied = assertThrows(SpectrumSqlException.class,
|
||||
() -> materializer.ensureCurrent(backend, session(false), BINDING, "events"));
|
||||
assertEquals("42501", denied.sqlState());
|
||||
assertThat(denied.getMessage(), containsString("Lake Formation"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void revokeDeniesATableAlreadyLoadedIntoTheBackend() {
|
||||
when(config.services().redshift().spectrumLakeFormationEnforcement()).thenReturn(true);
|
||||
when(glue.getTable("lake", "events")).thenReturn(csvTable());
|
||||
when(lakeFormation.canSelect(ACCOUNT, BINDING.iamRoleArn(), "lake", "events")).thenReturn(true);
|
||||
materializer.ensureCurrent(backend, session(false), BINDING, "events");
|
||||
assertEquals(ExternalTableMaterializer.Outcome.CURRENT,
|
||||
materializer.ensureCurrent(backend, session(false), BINDING, "events"));
|
||||
when(lakeFormation.canSelect(ACCOUNT, BINDING.iamRoleArn(), "lake", "events")).thenReturn(false);
|
||||
assertThrows(SpectrumSqlException.class,
|
||||
() -> materializer.ensureCurrent(backend, session(false), BINDING, "events"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void confirmedExtendedCommitPublishesDefinitionsAndFingerprint() {
|
||||
when(glue.getTable("lake", "events")).thenReturn(csvTable());
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package io.floci.aws.services.redshift.spectrum;
|
||||
|
||||
import io.floci.aws.config.EmulatorConfig;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@@ -10,9 +11,12 @@ import java.util.Optional;
|
||||
import static io.floci.aws.testing.InlineMocks.inlineMock;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@@ -73,4 +77,25 @@ class SpectrumInterceptorTest {
|
||||
interceptor.cleanup(backend, materialization);
|
||||
verify(materializer).cleanup(backend, materialization);
|
||||
}
|
||||
|
||||
@Test
|
||||
void aLegacyCatalogTableIsRefusedWhileLakeFormationIsEnforced() {
|
||||
EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
|
||||
when(config.services().redshift().spectrumLakeFormationEnforcement()).thenReturn(true);
|
||||
SpectrumInterceptor enforcing = new SpectrumInterceptor(catalog, new SpectrumStatementParser(),
|
||||
new SpectrumQueryClassifier(), null, inlineMock(SpectrumS3Reader.class), materializer, null, config);
|
||||
SpectrumExternalTable table = new SpectrumExternalTable("000000000000", "dev", "analytics", "events",
|
||||
List.of(new SpectrumColumn("id", SpectrumColumn.Type.INTEGER)), "s3://b/events/", ',', '"', '\\', "\\N", 0);
|
||||
SpectrumExternalSchema schema = new SpectrumExternalSchema("000000000000", "dev", "analytics",
|
||||
"s3://b/root/", null);
|
||||
when(catalog.table("000000000000", "dev", "analytics", "events")).thenReturn(Optional.of(table));
|
||||
when(catalog.schema("000000000000", "dev", "analytics")).thenReturn(Optional.of(schema));
|
||||
when(materializer.nextIdentifier()).thenReturn("spectrum_tmp_x");
|
||||
|
||||
SpectrumSqlException refused = assertThrows(SpectrumSqlException.class,
|
||||
() -> enforcing.intercept("SELECT * FROM analytics.events", "000000000000", "dev", null));
|
||||
|
||||
assertEquals("42501", refused.sqlState());
|
||||
verify(materializer, never()).materialize(any(), any(), any(), any(), any());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package io.floci.aws.testing;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* {@link SidecarContainersProfile} with Lake Formation enforced on Spectrum queries, so an external
|
||||
* table is readable only by a role that holds a Lake Formation {@code SELECT} grant on it.
|
||||
*/
|
||||
public class SidecarSpectrumLakeFormationProfile extends SidecarContainersProfile {
|
||||
|
||||
@Override
|
||||
public Map<String, String> getConfigOverrides() {
|
||||
Map<String, String> overrides = new HashMap<>(super.getConfigOverrides());
|
||||
overrides.put("floci.services.redshift.spectrum-lake-formation-enforcement", "true");
|
||||
return overrides;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user