Skip to content

Commit

Permalink
Revert "Refactor RealmContext to RealmId (#741)" (#920)
Browse files Browse the repository at this point in the history
* Revert "Copy RealmId when passing it to TaskExecutorImpl (#879)"

This reverts commit febe4e8.

* Revert #741
  • Loading branch information
flyrain authored Feb 3, 2025
1 parent 143d6e5 commit f0c99a6
Show file tree
Hide file tree
Showing 88 changed files with 694 additions and 657 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import java.time.Clock;
import org.apache.polaris.core.PolarisConfigurationStore;
import org.apache.polaris.core.PolarisDiagnostics;
import org.apache.polaris.core.context.RealmId;
import org.apache.polaris.core.context.RealmContext;
import org.apache.polaris.core.persistence.LocalPolarisMetaStoreManagerFactory;
import org.apache.polaris.core.persistence.PolarisCredentialsBootstrap;
import org.apache.polaris.core.persistence.PolarisMetaStoreManager;
Expand Down Expand Up @@ -71,16 +71,16 @@ protected PolarisEclipseLinkStore createBackingStore(@Nonnull PolarisDiagnostics
@Override
protected PolarisMetaStoreSession createMetaStoreSession(
@Nonnull PolarisEclipseLinkStore store,
@Nonnull RealmId realmId,
@Nonnull RealmContext realmContext,
@Nullable PolarisCredentialsBootstrap credentialsBootstrap,
@Nonnull PolarisDiagnostics diagnostics) {
return new PolarisEclipseLinkMetaStoreSessionImpl(
store,
storageIntegrationProvider,
realmId,
realmContext,
configurationFile(),
persistenceUnitName(),
secretsGenerator(realmId, credentialsBootstrap),
secretsGenerator(realmContext, credentialsBootstrap),
diagnostics);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import java.util.function.Supplier;
import java.util.stream.Collectors;
import org.apache.polaris.core.PolarisDiagnostics;
import org.apache.polaris.core.context.RealmId;
import org.apache.polaris.core.context.RealmContext;
import org.apache.polaris.core.entity.PolarisBaseEntity;
import org.apache.polaris.core.entity.PolarisChangeTrackingVersions;
import org.apache.polaris.core.entity.PolarisEntitiesActiveKey;
Expand Down Expand Up @@ -88,21 +88,22 @@ public class PolarisEclipseLinkMetaStoreSessionImpl implements PolarisMetaStoreS
*
* @param store Backing store of EclipseLink implementation
* @param storageIntegrationProvider Storage integration provider
* @param realmId Realm context used to communicate with different database.
* @param realmContext Realm context used to communicate with different database.
* @param confFile Optional EclipseLink configuration file. Default to 'META-INF/persistence.xml'.
* @param persistenceUnitName Optional persistence-unit name in confFile. Default to 'polaris'.
*/
public PolarisEclipseLinkMetaStoreSessionImpl(
@Nonnull PolarisEclipseLinkStore store,
@Nonnull PolarisStorageIntegrationProvider storageIntegrationProvider,
@Nonnull RealmId realmId,
@Nonnull RealmContext realmContext,
@Nullable String confFile,
@Nullable String persistenceUnitName,
@Nonnull PrincipalSecretsGenerator secretsGenerator,
@Nonnull PolarisDiagnostics diagnostics) {
this.diagnostics = diagnostics;
LOGGER.debug("Creating EclipseLink Meta Store Session for realm {}", realmId.id());
emf = createEntityManagerFactory(realmId, confFile, persistenceUnitName);
LOGGER.debug(
"Creating EclipseLink Meta Store Session for realm {}", realmContext.getRealmIdentifier());
emf = createEntityManagerFactory(realmContext, confFile, persistenceUnitName);

// init store
this.store = store;
Expand All @@ -120,16 +121,18 @@ public PolarisEclipseLinkMetaStoreSessionImpl(
* realm.
*/
private EntityManagerFactory createEntityManagerFactory(
@Nonnull RealmId realmId, @Nullable String confFile, @Nullable String persistenceUnitName) {
String realm = realmId.id();
@Nonnull RealmContext realmContext,
@Nullable String confFile,
@Nullable String persistenceUnitName) {
String realm = realmContext.getRealmIdentifier();
return realmFactories.computeIfAbsent(
realm,
key -> {
try {
PolarisEclipseLinkPersistenceUnit persistenceUnit =
PolarisEclipseLinkPersistenceUnit.locatePersistenceUnit(
confFile, persistenceUnitName);
return persistenceUnit.createEntityManagerFactory(realmId);
return persistenceUnit.createEntityManagerFactory(realmContext);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.apache.polaris.core.context.RealmId;
import org.apache.polaris.core.context.RealmContext;
import org.apache.polaris.extension.persistence.impl.eclipselink.PolarisEclipseLinkPersistenceUnit.ClasspathResourcePolarisEclipseLinkPersistenceUnit;
import org.apache.polaris.extension.persistence.impl.eclipselink.PolarisEclipseLinkPersistenceUnit.FileSystemPolarisEclipseLinkPersistenceUnit;
import org.apache.polaris.extension.persistence.impl.eclipselink.PolarisEclipseLinkPersistenceUnit.JarFilePolarisEclipseLinkPersistenceUnit;
Expand All @@ -57,15 +57,16 @@ sealed interface PolarisEclipseLinkPersistenceUnit
FileSystemPolarisEclipseLinkPersistenceUnit,
JarFilePolarisEclipseLinkPersistenceUnit {

EntityManagerFactory createEntityManagerFactory(RealmId realmId) throws IOException;
EntityManagerFactory createEntityManagerFactory(RealmContext realmContext) throws IOException;

record ClasspathResourcePolarisEclipseLinkPersistenceUnit(
URL resource, String resourceName, String persistenceUnitName)
implements PolarisEclipseLinkPersistenceUnit {

@Override
public EntityManagerFactory createEntityManagerFactory(RealmId realmId) throws IOException {
Map<String, String> properties = loadProperties(resource, persistenceUnitName, realmId);
public EntityManagerFactory createEntityManagerFactory(RealmContext realmContext)
throws IOException {
Map<String, String> properties = loadProperties(resource, persistenceUnitName, realmContext);
properties.put(ECLIPSELINK_PERSISTENCE_XML, resourceName);
return Persistence.createEntityManagerFactory(persistenceUnitName, properties);
}
Expand All @@ -75,9 +76,10 @@ record FileSystemPolarisEclipseLinkPersistenceUnit(Path path, String persistence
implements PolarisEclipseLinkPersistenceUnit {

@Override
public EntityManagerFactory createEntityManagerFactory(RealmId realmId) throws IOException {
public EntityManagerFactory createEntityManagerFactory(RealmContext realmContext)
throws IOException {
Map<String, String> properties =
loadProperties(path.toUri().toURL(), persistenceUnitName, realmId);
loadProperties(path.toUri().toURL(), persistenceUnitName, realmContext);
Path archiveDirectory = path.getParent();
String descriptorPath = archiveDirectory.getParent().relativize(path).toString();
properties.put(ECLIPSELINK_PERSISTENCE_XML, descriptorPath);
Expand All @@ -99,8 +101,9 @@ record JarFilePolarisEclipseLinkPersistenceUnit(
implements PolarisEclipseLinkPersistenceUnit {

@Override
public EntityManagerFactory createEntityManagerFactory(RealmId realmId) throws IOException {
Map<String, String> properties = loadProperties(confUrl, persistenceUnitName, realmId);
public EntityManagerFactory createEntityManagerFactory(RealmContext realmContext)
throws IOException {
Map<String, String> properties = loadProperties(confUrl, persistenceUnitName, realmContext);
properties.put(ECLIPSELINK_PERSISTENCE_XML, descriptorPath);
ClassLoader prevClassLoader = Thread.currentThread().getContextClassLoader();
try (URLClassLoader currentClassLoader =
Expand Down Expand Up @@ -180,7 +183,9 @@ private static URL classpathResource(String resourceName) throws IOException {

/** Load the persistence unit properties from a given configuration file */
private static Map<String, String> loadProperties(
@Nonnull URL confFile, @Nonnull String persistenceUnitName, @Nonnull RealmId realmId)
@Nonnull URL confFile,
@Nonnull String persistenceUnitName,
@Nonnull RealmContext realmContext)
throws IOException {
try (InputStream input = confFile.openStream()) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
Expand All @@ -200,7 +205,9 @@ private static Map<String, String> loadProperties(
}
// Replace database name in JDBC URL with realm
if (properties.containsKey(JDBC_URL)) {
properties.put(JDBC_URL, properties.get(JDBC_URL).replace("{realm}", realmId.id()));
properties.put(
JDBC_URL,
properties.get(JDBC_URL).replace("{realm}", realmContext.getRealmIdentifier()));
}
return properties;
} catch (XPathExpressionException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import org.apache.polaris.core.PolarisConfigurationStore;
import org.apache.polaris.core.PolarisDefaultDiagServiceImpl;
import org.apache.polaris.core.PolarisDiagnostics;
import org.apache.polaris.core.context.RealmId;
import org.apache.polaris.core.context.RealmContext;
import org.apache.polaris.core.entity.PolarisPrincipalSecrets;
import org.apache.polaris.core.persistence.BasePolarisMetaStoreManagerTest;
import org.apache.polaris.core.persistence.PolarisMetaStoreManagerImpl;
Expand Down Expand Up @@ -101,13 +101,13 @@ static void deleteConfFiles() throws IOException {
protected PolarisTestMetaStoreManager createPolarisTestMetaStoreManager() {
PolarisDiagnostics diagServices = new PolarisDefaultDiagServiceImpl();
PolarisEclipseLinkStore store = new PolarisEclipseLinkStore(diagServices);
RealmId realmId = RealmId.newRealmId("realm");
RealmContext realmContext = () -> "realm";
PolarisMetaStoreSession session =
new PolarisEclipseLinkMetaStoreSessionImpl(
store, Mockito.mock(), realmId, null, "polaris", RANDOM_SECRETS, diagServices);
store, Mockito.mock(), realmContext, null, "polaris", RANDOM_SECRETS, diagServices);
return new PolarisTestMetaStoreManager(
new PolarisMetaStoreManagerImpl(
realmId,
realmContext,
diagServices,
new PolarisConfigurationStore() {},
timeSource.withZone(ZoneId.systemDefault())),
Expand All @@ -128,7 +128,7 @@ void testCreateStoreSession(String confFile, boolean success) {
new PolarisEclipseLinkMetaStoreSessionImpl(
store,
Mockito.mock(),
RealmId.newRealmId("realm"),
() -> "realm",
confFile,
"polaris",
RANDOM_SECRETS,
Expand Down
3 changes: 0 additions & 3 deletions polaris-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ dependencies {
compileOnly(libs.jetbrains.annotations)
compileOnly(libs.spotbugs.annotations)

compileOnly(project(":polaris-immutables"))
annotationProcessor(project(":polaris-immutables", configuration = "processor"))

constraints {
implementation("org.xerial.snappy:snappy-java:1.1.10.7") {
because("Vulnerability detected in 1.1.8.2")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import jakarta.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import org.apache.polaris.core.context.RealmId;
import org.apache.polaris.core.context.RealmContext;
import org.apache.polaris.core.entity.CatalogEntity;

/**
Expand All @@ -35,11 +35,11 @@ public interface PolarisConfigurationStore {
* Retrieve the current value for a configuration key. May be null if not set.
*
* @param <T> the type of the configuration value
* @param realmId the realm context to check for overrides; may be null.
* @param realmContext the realm context to check for overrides; may be null.
* @param configName the name of the configuration key to check
* @return the current value set for the configuration key or null if not set
*/
default <T> @Nullable T getConfiguration(@Nullable RealmId realmId, String configName) {
default <T> @Nullable T getConfiguration(@Nullable RealmContext realmContext, String configName) {
return null;
}

Expand All @@ -48,15 +48,15 @@ public interface PolarisConfigurationStore {
* value.
*
* @param <T> the type of the configuration value
* @param realmId the realm context to check for overrides; may be null.
* @param realmContext the realm context to check for overrides; may be null.
* @param configName the name of the configuration key to check
* @param defaultValue the default value if the configuration key has no value
* @return the current value or the supplied default value
*/
default <T> @Nonnull T getConfiguration(
@Nullable RealmId realmId, String configName, @Nonnull T defaultValue) {
@Nullable RealmContext realmContext, String configName, @Nonnull T defaultValue) {
Preconditions.checkNotNull(defaultValue, "Cannot pass null as a default value");
T configValue = getConfiguration(realmId, configName);
T configValue = getConfiguration(realmContext, configName);
return configValue != null ? configValue : defaultValue;
}

Expand Down Expand Up @@ -88,13 +88,13 @@ public interface PolarisConfigurationStore {
* Retrieve the current value for a configuration.
*
* @param <T> the type of the configuration value
* @param realmId the realm context to check for overrides; may be null.
* @param realmContext the realm context to check for overrides; may be null.
* @param config the configuration to load
* @return the current value set for the configuration key or null if not set
*/
default <T> @Nonnull T getConfiguration(
@Nullable RealmId realmId, PolarisConfiguration<T> config) {
T result = getConfiguration(realmId, config.key, config.defaultValue);
@Nullable RealmContext realmContext, PolarisConfiguration<T> config) {
T result = getConfiguration(realmContext, config.key, config.defaultValue);
return tryCast(config, result);
}

Expand All @@ -103,20 +103,20 @@ public interface PolarisConfigurationStore {
* present.
*
* @param <T> the type of the configuration value
* @param realmId the realm context to check for overrides; may be null.
* @param realmContext the realm context to check for overrides; may be null.
* @param catalogEntity the catalog to check for an override
* @param config the configuration to load
* @return the current value set for the configuration key or null if not set
*/
default <T> @Nonnull T getConfiguration(
@Nullable RealmId realmId,
@Nullable RealmContext realmContext,
@Nonnull CatalogEntity catalogEntity,
PolarisConfiguration<T> config) {
if (config.hasCatalogConfig()
&& catalogEntity.getPropertiesAsMap().containsKey(config.catalogConfig())) {
return tryCast(config, catalogEntity.getPropertiesAsMap().get(config.catalogConfig()));
} else {
return getConfiguration(realmId, config);
return getConfiguration(realmContext, config);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,23 @@
import jakarta.annotation.Nullable;
import java.util.List;
import java.util.Set;
import org.apache.polaris.core.context.RealmId;
import org.apache.polaris.core.context.RealmContext;
import org.apache.polaris.core.entity.PolarisBaseEntity;
import org.apache.polaris.core.persistence.PolarisResolvedPathWrapper;

/** Interface for invoking authorization checks. */
public interface PolarisAuthorizer {

void authorizeOrThrow(
@Nonnull RealmId realmId,
@Nonnull RealmContext realmContext,
@Nonnull AuthenticatedPolarisPrincipal authenticatedPrincipal,
@Nonnull Set<PolarisBaseEntity> activatedEntities,
@Nonnull PolarisAuthorizableOperation authzOp,
@Nullable PolarisResolvedPathWrapper target,
@Nullable PolarisResolvedPathWrapper secondary);

void authorizeOrThrow(
@Nonnull RealmId realmId,
@Nonnull RealmContext realmContext,
@Nonnull AuthenticatedPolarisPrincipal authenticatedPrincipal,
@Nonnull Set<PolarisBaseEntity> activatedEntities,
@Nonnull PolarisAuthorizableOperation authzOp,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
import org.apache.iceberg.exceptions.ForbiddenException;
import org.apache.polaris.core.PolarisConfiguration;
import org.apache.polaris.core.PolarisConfigurationStore;
import org.apache.polaris.core.context.RealmId;
import org.apache.polaris.core.context.RealmContext;
import org.apache.polaris.core.entity.PolarisBaseEntity;
import org.apache.polaris.core.entity.PolarisEntityConstants;
import org.apache.polaris.core.entity.PolarisEntityCore;
Expand Down Expand Up @@ -487,14 +487,14 @@ public boolean matchesOrIsSubsumedBy(

@Override
public void authorizeOrThrow(
@Nonnull RealmId realmId,
@Nonnull RealmContext realmContext,
@Nonnull AuthenticatedPolarisPrincipal authenticatedPrincipal,
@Nonnull Set<PolarisBaseEntity> activatedEntities,
@Nonnull PolarisAuthorizableOperation authzOp,
@Nullable PolarisResolvedPathWrapper target,
@Nullable PolarisResolvedPathWrapper secondary) {
authorizeOrThrow(
realmId,
realmContext,
authenticatedPrincipal,
activatedEntities,
authzOp,
Expand All @@ -504,15 +504,16 @@ public void authorizeOrThrow(

@Override
public void authorizeOrThrow(
@Nonnull RealmId realmId,
@Nonnull RealmContext realmContext,
@Nonnull AuthenticatedPolarisPrincipal authenticatedPrincipal,
@Nonnull Set<PolarisBaseEntity> activatedEntities,
@Nonnull PolarisAuthorizableOperation authzOp,
@Nullable List<PolarisResolvedPathWrapper> targets,
@Nullable List<PolarisResolvedPathWrapper> secondaries) {
boolean enforceCredentialRotationRequiredState =
featureConfig.getConfiguration(
realmId, PolarisConfiguration.ENFORCE_PRINCIPAL_CREDENTIAL_ROTATION_REQUIRED_CHECKING);
realmContext,
PolarisConfiguration.ENFORCE_PRINCIPAL_CREDENTIAL_ROTATION_REQUIRED_CHECKING);
if (enforceCredentialRotationRequiredState
&& authenticatedPrincipal
.getPrincipalEntity()
Expand Down
Loading

0 comments on commit f0c99a6

Please sign in to comment.