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
@@ -1891,6 +1891,9 @@ public interface EmulatorConfig {
|
||||
|
||||
@WithDefault("1000000")
|
||||
long spectrumMaxRows();
|
||||
|
||||
@WithDefault("false")
|
||||
boolean spectrumLakeFormationEnforcement();
|
||||
}
|
||||
|
||||
interface RdsServiceConfig {
|
||||
|
||||
@@ -0,0 +1,109 @@
|
||||
package io.floci.aws.services.lakeformation;
|
||||
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.core.common.RequestScopes;
|
||||
import io.floci.aws.services.lakeformation.model.ColumnWildcard;
|
||||
import io.floci.aws.services.lakeformation.model.DataLakePrincipal;
|
||||
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.TableWithColumnsResource;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Decides whether a principal may read a table, from the grants Lake Formation stores.
|
||||
* Only table-level {@code SELECT} is evaluated: a grant on columns, an LF-Tag or a data cells filter
|
||||
* does not count, so a table reachable only through those is denied.
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class LakeFormationAuthorizer {
|
||||
|
||||
/** The group AWS grants to every Glue database that is not under Lake Formation control. */
|
||||
static final String IAM_ALLOWED_PRINCIPALS = "IAM_ALLOWED_PRINCIPALS";
|
||||
|
||||
private static final String SELECT = "SELECT";
|
||||
private static final String ALL = "ALL";
|
||||
|
||||
private final LakeFormationStorage storage;
|
||||
private final RegionResolver regionResolver;
|
||||
|
||||
public LakeFormationAuthorizer(LakeFormationStorage storage, RegionResolver regionResolver) {
|
||||
this.storage = storage;
|
||||
this.regionResolver = regionResolver;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether {@code principalArn} holds {@code SELECT} on the table, by a grant to the principal itself or
|
||||
* to {@code IAMAllowedPrincipals}, the group that leaves the table to IAM alone. Floci's Glue never stores
|
||||
* that grant, so a catalog that keeps AWS's initial settings (none stored, or
|
||||
* {@code CreateTableDefaultPermissions} naming {@code IAM_ALLOWED_PRINCIPALS}) leaves every table to IAM.
|
||||
*/
|
||||
public boolean canSelect(String accountId, String principalArn, String databaseName, String tableName) {
|
||||
return RequestScopes.callAs(accountId, () -> {
|
||||
String region = regionResolver.getRegion();
|
||||
return grants(region, accountId, principalArn, databaseName, tableName)
|
||||
|| grants(region, accountId, IAM_ALLOWED_PRINCIPALS, databaseName, tableName)
|
||||
|| newTablesAreLeftToIam(region, accountId);
|
||||
});
|
||||
}
|
||||
|
||||
private boolean newTablesAreLeftToIam(String region, String catalogId) {
|
||||
return storage.getDataLakeSettings(region, catalogId)
|
||||
.map(settings -> settings.getCreateTableDefaultPermissions() != null
|
||||
&& settings.getCreateTableDefaultPermissions().stream().anyMatch(permissions ->
|
||||
permissions.getPrincipal() != null
|
||||
&& IAM_ALLOWED_PRINCIPALS.equals(permissions.getPrincipal().getDataLakePrincipalIdentifier())))
|
||||
.orElse(true);
|
||||
}
|
||||
|
||||
private boolean grants(String region, String catalogId, String principalId, String databaseName,
|
||||
String tableName) {
|
||||
DataLakePrincipal principal = new DataLakePrincipal();
|
||||
principal.setDataLakePrincipalIdentifier(principalId);
|
||||
List<PrincipalResourcePermissions> held =
|
||||
storage.listPermissions(region, catalogId, principal, null, null, false, null, null);
|
||||
for (PrincipalResourcePermissions grant : held) {
|
||||
if (allowsSelect(grant.getPermissions()) && covers(grant.getResource(), catalogId, databaseName, tableName)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static boolean allowsSelect(List<String> permissions) {
|
||||
return permissions != null && (permissions.contains(SELECT) || permissions.contains(ALL));
|
||||
}
|
||||
|
||||
private static boolean covers(Resource resource, String catalogId, String databaseName, String tableName) {
|
||||
if (resource == null) {
|
||||
return false;
|
||||
}
|
||||
TableResource table = resource.getTable();
|
||||
if (table != null) {
|
||||
return sameCatalog(table.getCatalogId(), catalogId)
|
||||
&& sameName(table.getDatabaseName(), databaseName)
|
||||
&& (table.getTableWildcard() != null || sameName(table.getName(), tableName));
|
||||
}
|
||||
TableWithColumnsResource columns = resource.getTableWithColumns();
|
||||
if (columns != null) {
|
||||
ColumnWildcard wildcard = columns.getColumnWildcard();
|
||||
boolean wholeTable = wildcard != null
|
||||
&& (wildcard.getExcludedColumnNames() == null || wildcard.getExcludedColumnNames().isEmpty());
|
||||
return wholeTable && sameCatalog(columns.getCatalogId(), catalogId)
|
||||
&& sameName(columns.getDatabaseName(), databaseName)
|
||||
&& sameName(columns.getName(), tableName);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/** Glue resolves database and table names ignoring case, so a grant written in another case still applies. */
|
||||
private static boolean sameName(String left, String right) {
|
||||
return left != null && left.equalsIgnoreCase(right);
|
||||
}
|
||||
|
||||
private static boolean sameCatalog(String grantCatalogId, String catalogId) {
|
||||
return grantCatalogId == null || grantCatalogId.equals(catalogId);
|
||||
}
|
||||
}
|
||||
@@ -203,10 +203,14 @@ public class MemoryLakeFormationStorage implements LakeFormationStorage {
|
||||
// Deliberate omission: MaxResults, NextToken, ResourceType, and IncludeRelated are ignored.
|
||||
// All results fit on a single page, and SDKs tolerate unset optional filters.
|
||||
String filterResourceKey = resource != null ? getResourceKey(resource) : null;
|
||||
return permissionsStorage.scan(k -> k.startsWith(region + ":" + catalogId + ":")).stream()
|
||||
String principalId = principal != null ? principal.getDataLakePrincipalIdentifier() : null;
|
||||
String keyPrefix = principalId != null
|
||||
? region + ":" + catalogId + ":" + principalId + ":"
|
||||
: region + ":" + catalogId + ":";
|
||||
return permissionsStorage.scan(k -> k.startsWith(keyPrefix)).stream()
|
||||
.filter(p -> {
|
||||
if (principal != null && principal.getDataLakePrincipalIdentifier() != null) {
|
||||
if (p.getPrincipal() == null || !principal.getDataLakePrincipalIdentifier().equals(p.getPrincipal().getDataLakePrincipalIdentifier())) {
|
||||
if (principalId != null) {
|
||||
if (p.getPrincipal() == null || !principalId.equals(p.getPrincipal().getDataLakePrincipalIdentifier())) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
+21
-3
@@ -15,6 +15,7 @@ import io.floci.aws.services.glue.model.Partition;
|
||||
import io.floci.aws.services.glue.model.StorageDescriptor;
|
||||
import io.floci.aws.services.glue.model.Table;
|
||||
import io.floci.aws.services.iam.IamService;
|
||||
import io.floci.aws.services.lakeformation.LakeFormationAuthorizer;
|
||||
import io.floci.aws.services.redshift.proxy.RedshiftRoleAccess;
|
||||
import io.floci.aws.services.redshift.proxy.S3CopySimulator;
|
||||
import io.floci.aws.services.s3.S3ResourceArns;
|
||||
@@ -84,6 +85,7 @@ public class ExternalTableMaterializer {
|
||||
private final S3Service s3Service;
|
||||
private final IamService iamService;
|
||||
private final EmulatorConfig config;
|
||||
private final LakeFormationAuthorizer lakeFormation;
|
||||
private final ExternalCatalogLocks catalogLocks;
|
||||
private final ConcurrentHashMap<String, String> fingerprints = new ConcurrentHashMap<>();
|
||||
/** Column definitions each loaded table was created with, to tell a data reload from a schema change. */
|
||||
@@ -183,18 +185,21 @@ public class ExternalTableMaterializer {
|
||||
}
|
||||
|
||||
public ExternalTableMaterializer(FlociDuckClient duckClient, GlueService glueService, S3Service s3Service,
|
||||
IamService iamService, EmulatorConfig config) {
|
||||
this(duckClient, glueService, s3Service, iamService, config, new ExternalCatalogLocks());
|
||||
IamService iamService, EmulatorConfig config,
|
||||
LakeFormationAuthorizer lakeFormation) {
|
||||
this(duckClient, glueService, s3Service, iamService, config, lakeFormation, new ExternalCatalogLocks());
|
||||
}
|
||||
|
||||
@Inject
|
||||
public ExternalTableMaterializer(FlociDuckClient duckClient, GlueService glueService, S3Service s3Service,
|
||||
IamService iamService, EmulatorConfig config, ExternalCatalogLocks catalogLocks) {
|
||||
IamService iamService, EmulatorConfig config,
|
||||
LakeFormationAuthorizer lakeFormation, ExternalCatalogLocks catalogLocks) {
|
||||
this.duckClient = duckClient;
|
||||
this.glueService = glueService;
|
||||
this.s3Service = s3Service;
|
||||
this.iamService = iamService;
|
||||
this.config = config;
|
||||
this.lakeFormation = lakeFormation;
|
||||
this.catalogLocks = catalogLocks;
|
||||
}
|
||||
|
||||
@@ -243,6 +248,8 @@ public class ExternalTableMaterializer {
|
||||
throw exception;
|
||||
}
|
||||
String label = binding.schemaName() + "." + tableName;
|
||||
// Checked ahead of the cache probe below, so a revoke takes effect on data already loaded.
|
||||
authorizeLakeFormation(binding, session, table.getName(), label);
|
||||
// DuckDB reads as the account, so the bound role's access is enforced here, ahead of the read,
|
||||
// with the same signed authorization COPY uses: identity policy and bucket policy.
|
||||
RedshiftRoleAccess.RoleSession roleSession = openRoleSession(binding, session);
|
||||
@@ -588,6 +595,17 @@ public class ExternalTableMaterializer {
|
||||
}
|
||||
}
|
||||
|
||||
private void authorizeLakeFormation(ExternalSchemaBinding binding, SpectrumSession session, String glueTable,
|
||||
String label) {
|
||||
if (!config.services().redshift().spectrumLakeFormationEnforcement()) {
|
||||
return;
|
||||
}
|
||||
if (!lakeFormation.canSelect(session.accountId(), binding.iamRoleArn(), binding.glueDatabase(), glueTable)) {
|
||||
throw new SpectrumSqlException(SQLSTATE_INSUFFICIENT_PRIVILEGE,
|
||||
"Insufficient Lake Formation permission(s) on external table \"" + label + "\"");
|
||||
}
|
||||
}
|
||||
|
||||
private void authorizeObjects(String accountId, ExternalSchemaBinding binding,
|
||||
RedshiftRoleAccess.RoleSession roleSession, List<ReadSource> sources) {
|
||||
try {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package io.floci.aws.services.redshift.spectrum;
|
||||
|
||||
import io.floci.aws.config.EmulatorConfig;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
@@ -16,18 +17,20 @@ public final class SpectrumInterceptor {
|
||||
private final SpectrumS3Reader reader;
|
||||
private final SpectrumMaterializer materializer;
|
||||
private final SpectrumQueryPreparation preparation;
|
||||
private final EmulatorConfig config;
|
||||
|
||||
public SpectrumInterceptor(SpectrumCatalog catalog, SpectrumStatementParser statementParser,
|
||||
SpectrumQueryClassifier queryClassifier, SpectrumQueryRewriter queryRewriter,
|
||||
SpectrumS3Reader reader, SpectrumMaterializer materializer) {
|
||||
this(catalog, statementParser, queryClassifier, queryRewriter, reader, materializer, null);
|
||||
this(catalog, statementParser, queryClassifier, queryRewriter, reader, materializer, null, null);
|
||||
}
|
||||
|
||||
@Inject
|
||||
public SpectrumInterceptor(SpectrumCatalog catalog, SpectrumStatementParser statementParser,
|
||||
SpectrumQueryClassifier queryClassifier, SpectrumQueryRewriter queryRewriter,
|
||||
SpectrumS3Reader reader, SpectrumMaterializer materializer,
|
||||
SpectrumQueryPreparation preparation) {
|
||||
SpectrumQueryPreparation preparation, EmulatorConfig config) {
|
||||
this.config = config;
|
||||
this.catalog = catalog;
|
||||
this.statementParser = statementParser;
|
||||
this.queryClassifier = queryClassifier;
|
||||
@@ -106,6 +109,7 @@ public final class SpectrumInterceptor {
|
||||
return new Decision.Handled();
|
||||
}
|
||||
case Plan.Query(SpectrumQuery query, SpectrumExternalTable table, SpectrumExternalSchema schema, String identifier) -> {
|
||||
requireLakeFormationFreeSchema(table);
|
||||
SpectrumMaterializer.Materialization materialized = materializer.materialize(backend, table, schema, reader, identifier);
|
||||
return new Decision.Rewritten(queryRewriter.rewrite(query, materialized.identifier()), materialized);
|
||||
}
|
||||
@@ -115,6 +119,17 @@ public final class SpectrumInterceptor {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A legacy-catalog table has no Glue resource, so no Lake Formation grant can ever cover it. With
|
||||
* enforcement on it is refused rather than read unchecked.
|
||||
*/
|
||||
private void requireLakeFormationFreeSchema(SpectrumExternalTable table) {
|
||||
if (config != null && config.services().redshift().spectrumLakeFormationEnforcement()) {
|
||||
throw new SpectrumSqlException("42501", "Insufficient Lake Formation permission(s) on external table \""
|
||||
+ table.schemaName() + "." + table.tableName() + "\": the table is not in a Glue database");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Drops the temp table a {@link Decision.Rewritten} materialized, once the client has finished
|
||||
* reading its rows (or failed while doing so).
|
||||
|
||||
Reference in new issue
Block a user