Skip to content

Commit

Permalink
Refactor RealmContext to RealmId
Browse files Browse the repository at this point in the history
`RealmContext` is not an actual context but just a container for the ID of the realm. This change makes this explicit.
  • Loading branch information
snazy committed Jan 14, 2025
1 parent 8aa8fc3 commit f4352b5
Show file tree
Hide file tree
Showing 84 changed files with 599 additions and 636 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import java.time.Clock;
import org.apache.polaris.core.PolarisConfigurationStore;
import org.apache.polaris.core.PolarisDiagnostics;
import org.apache.polaris.core.context.RealmContext;
import org.apache.polaris.core.context.RealmId;
import org.apache.polaris.core.persistence.LocalPolarisMetaStoreManagerFactory;
import org.apache.polaris.core.persistence.PolarisMetaStoreManager;
import org.apache.polaris.core.persistence.PolarisMetaStoreSession;
Expand Down Expand Up @@ -69,15 +69,15 @@ protected PolarisEclipseLinkStore createBackingStore(@Nonnull PolarisDiagnostics
@Override
protected PolarisMetaStoreSession createMetaStoreSession(
@Nonnull PolarisEclipseLinkStore store,
@Nonnull RealmContext realmContext,
@Nonnull RealmId realmId,
@Nonnull PolarisDiagnostics diagnostics) {
return new PolarisEclipseLinkMetaStoreSessionImpl(
store,
storageIntegrationProvider,
realmContext,
realmId,
configurationFile(),
persistenceUnitName(),
secretsGenerator(realmContext),
secretsGenerator(realmId),
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.RealmContext;
import org.apache.polaris.core.context.RealmId;
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,22 +88,21 @@ public class PolarisEclipseLinkMetaStoreSessionImpl implements PolarisMetaStoreS
*
* @param store Backing store of EclipseLink implementation
* @param storageIntegrationProvider Storage integration provider
* @param realmContext Realm context used to communicate with different database.
* @param realmId 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 RealmContext realmContext,
@Nonnull RealmId realmId,
@Nullable String confFile,
@Nullable String persistenceUnitName,
@Nonnull PrincipalSecretsGenerator secretsGenerator,
@Nonnull PolarisDiagnostics diagnostics) {
this.diagnostics = diagnostics;
LOGGER.debug(
"Creating EclipseLink Meta Store Session for realm {}", realmContext.getRealmIdentifier());
emf = createEntityManagerFactory(realmContext, confFile, persistenceUnitName);
LOGGER.debug("Creating EclipseLink Meta Store Session for realm {}", realmId.id());
emf = createEntityManagerFactory(realmId, confFile, persistenceUnitName);

// init store
this.store = store;
Expand All @@ -121,18 +120,16 @@ public PolarisEclipseLinkMetaStoreSessionImpl(
* realm.
*/
private EntityManagerFactory createEntityManagerFactory(
@Nonnull RealmContext realmContext,
@Nullable String confFile,
@Nullable String persistenceUnitName) {
String realm = realmContext.getRealmIdentifier();
@Nonnull RealmId realmId, @Nullable String confFile, @Nullable String persistenceUnitName) {
String realm = realmId.id();
return realmFactories.computeIfAbsent(
realm,
key -> {
try {
PolarisEclipseLinkPersistenceUnit persistenceUnit =
PolarisEclipseLinkPersistenceUnit.locatePersistenceUnit(
confFile, persistenceUnitName);
return persistenceUnit.createEntityManagerFactory(realmContext);
return persistenceUnit.createEntityManagerFactory(realmId);
} 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.RealmContext;
import org.apache.polaris.core.context.RealmId;
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,16 +57,15 @@ sealed interface PolarisEclipseLinkPersistenceUnit
FileSystemPolarisEclipseLinkPersistenceUnit,
JarFilePolarisEclipseLinkPersistenceUnit {

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

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

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

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

@Override
public EntityManagerFactory createEntityManagerFactory(RealmContext realmContext)
throws IOException {
Map<String, String> properties = loadProperties(confUrl, persistenceUnitName, realmContext);
public EntityManagerFactory createEntityManagerFactory(RealmId realmId) throws IOException {
Map<String, String> properties = loadProperties(confUrl, persistenceUnitName, realmId);
properties.put(ECLIPSELINK_PERSISTENCE_XML, descriptorPath);
ClassLoader prevClassLoader = Thread.currentThread().getContextClassLoader();
try (URLClassLoader currentClassLoader =
Expand Down Expand Up @@ -183,9 +180,7 @@ 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 RealmContext realmContext)
@Nonnull URL confFile, @Nonnull String persistenceUnitName, @Nonnull RealmId realmId)
throws IOException {
try (InputStream input = confFile.openStream()) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
Expand All @@ -205,9 +200,7 @@ 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}", realmContext.getRealmIdentifier()));
properties.put(JDBC_URL, properties.get(JDBC_URL).replace("{realm}", realmId.id()));
}
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.RealmContext;
import org.apache.polaris.core.context.RealmId;
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);
RealmContext realmContext = () -> "realm";
RealmId realmId = RealmId.newRealmId("realm");
PolarisMetaStoreSession session =
new PolarisEclipseLinkMetaStoreSessionImpl(
store, Mockito.mock(), realmContext, null, "polaris", RANDOM_SECRETS, diagServices);
store, Mockito.mock(), realmId, null, "polaris", RANDOM_SECRETS, diagServices);
return new PolarisTestMetaStoreManager(
new PolarisMetaStoreManagerImpl(
realmContext,
realmId,
diagServices,
new PolarisConfigurationStore() {},
timeSource.withZone(ZoneId.systemDefault())),
Expand All @@ -128,7 +128,7 @@ void testCreateStoreSession(String confFile, boolean success) {
new PolarisEclipseLinkMetaStoreSessionImpl(
store,
Mockito.mock(),
() -> "realm",
RealmId.newRealmId("realm"),
confFile,
"polaris",
RANDOM_SECRETS,
Expand Down
2 changes: 1 addition & 1 deletion helm/polaris/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ $ helm uninstall --namespace polaris polaris
| podAnnotations | object | `{}` | Annotations to apply to polaris pods. |
| podLabels | object | `{}` | Additional Labels to apply to polaris pods. |
| podSecurityContext | object | `{}` | Security context for the polaris pod. See https://kubernetes.io/docs/tasks/configure-pod-container/security-context/. |
| polarisServerConfig | object | `{"authenticator":{"class":"org.apache.polaris.service.auth.TestInlineBearerTokenPolarisAuthenticator"},"callContextResolver":{"type":"default"},"cors":{"allowed-credentials":true,"allowed-headers":["*"],"allowed-methods":["PATCH","POST","DELETE","GET","PUT"],"allowed-origins":["http://localhost:8080"],"allowed-timing-origins":["http://localhost:8080"],"exposed-headers":["*"],"preflight-max-age":600},"defaultRealms":["default-realm"],"featureConfiguration":{"ENFORCE_PRINCIPAL_CREDENTIAL_ROTATION_REQUIRED_CHECKING":false,"SUPPORTED_CATALOG_STORAGE_TYPES":["S3","GCS","AZURE","FILE"]},"io":{"factoryType":"default"},"logging":{"appenders":[{"logFormat":"%-5p [%d{ISO8601} - %-6r] [%t] [%X{aid}%X{sid}%X{tid}%X{wid}%X{oid}%X{srv}%X{job}%X{rid}] %c{30}: %m %kvp%n%ex","threshold":"ALL","type":"console"}],"level":"INFO","loggers":{"org.apache.iceberg.rest":"DEBUG","org.apache.polaris":"DEBUG"}},"maxRequestBodyBytes":-1,"metaStoreManager":{"type":"in-memory"},"oauth2":{"type":"test"},"rateLimiter":{"type":"no-op"},"realmContextResolver":{"type":"default"},"server":{"adminConnectors":[{"port":8182,"type":"http"}],"applicationConnectors":[{"port":8181,"type":"http"}],"maxThreads":200,"minThreads":10,"requestLog":{"appenders":[{"type":"console"}]}}}` | Configures for polaris-server.yml |
| polarisServerConfig | object | `{"authenticator":{"class":"org.apache.polaris.service.auth.TestInlineBearerTokenPolarisAuthenticator"},"callContextResolver":{"type":"default"},"cors":{"allowed-credentials":true,"allowed-headers":["*"],"allowed-methods":["PATCH","POST","DELETE","GET","PUT"],"allowed-origins":["http://localhost:8080"],"allowed-timing-origins":["http://localhost:8080"],"exposed-headers":["*"],"preflight-max-age":600},"defaultRealms":["default-realm"],"featureConfiguration":{"ENFORCE_PRINCIPAL_CREDENTIAL_ROTATION_REQUIRED_CHECKING":false,"SUPPORTED_CATALOG_STORAGE_TYPES":["S3","GCS","AZURE","FILE"]},"io":{"factoryType":"default"},"logging":{"appenders":[{"logFormat":"%-5p [%d{ISO8601} - %-6r] [%t] [%X{aid}%X{sid}%X{tid}%X{wid}%X{oid}%X{srv}%X{job}%X{rid}] %c{30}: %m %kvp%n%ex","threshold":"ALL","type":"console"}],"level":"INFO","loggers":{"org.apache.iceberg.rest":"DEBUG","org.apache.polaris":"DEBUG"}},"maxRequestBodyBytes":-1,"metaStoreManager":{"type":"in-memory"},"oauth2":{"type":"test"},"rateLimiter":{"type":"no-op"},"realmIdResolver":{"type":"default"},"server":{"adminConnectors":[{"port":8182,"type":"http"}],"applicationConnectors":[{"port":8181,"type":"http"}],"maxThreads":200,"minThreads":10,"requestLog":{"appenders":[{"type":"console"}]}}}` | Configures for polaris-server.yml |
| readinessProbe | object | `{"failureThreshold":3,"initialDelaySeconds":5,"periodSeconds":10,"successThreshold":1,"timeoutSeconds":10}` | Configures the readiness probe for polaris pods. |
| readinessProbe.failureThreshold | int | `3` | Minimum consecutive failures for the probe to be considered failed after having succeeded. Minimum value is 1. |
| readinessProbe.initialDelaySeconds | int | `5` | Number of seconds after the container has started before readiness probes are initiated. Minimum value is 0. |
Expand Down
3 changes: 3 additions & 0 deletions polaris-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ 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.RealmContext;
import org.apache.polaris.core.context.RealmId;
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 realmContext the realm context to check for overrides; may be null.
* @param realmId 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 RealmContext realmContext, String configName) {
default <T> @Nullable T getConfiguration(@Nullable RealmId realmId, String configName) {
return null;
}

Expand All @@ -48,15 +48,15 @@ public interface PolarisConfigurationStore {
* value.
*
* @param <T> the type of the configuration value
* @param realmContext the realm context to check for overrides; may be null.
* @param realmId 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 RealmContext realmContext, String configName, @Nonnull T defaultValue) {
@Nullable RealmId realmId, String configName, @Nonnull T defaultValue) {
Preconditions.checkNotNull(defaultValue, "Cannot pass null as a default value");
T configValue = getConfiguration(realmContext, configName);
T configValue = getConfiguration(realmId, 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 realmContext the realm context to check for overrides; may be null.
* @param realmId 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 RealmContext realmContext, PolarisConfiguration<T> config) {
T result = getConfiguration(realmContext, config.key, config.defaultValue);
@Nullable RealmId realmId, PolarisConfiguration<T> config) {
T result = getConfiguration(realmId, 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 realmContext the realm context to check for overrides; may be null.
* @param realmId 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 RealmContext realmContext,
@Nullable RealmId realmId,
@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(realmContext, config);
return getConfiguration(realmId, 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.RealmContext;
import org.apache.polaris.core.context.RealmId;
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 RealmContext realmContext,
@Nonnull RealmId realmId,
@Nonnull AuthenticatedPolarisPrincipal authenticatedPrincipal,
@Nonnull Set<PolarisBaseEntity> activatedEntities,
@Nonnull PolarisAuthorizableOperation authzOp,
@Nullable PolarisResolvedPathWrapper target,
@Nullable PolarisResolvedPathWrapper secondary);

void authorizeOrThrow(
@Nonnull RealmContext realmContext,
@Nonnull RealmId realmId,
@Nonnull AuthenticatedPolarisPrincipal authenticatedPrincipal,
@Nonnull Set<PolarisBaseEntity> activatedEntities,
@Nonnull PolarisAuthorizableOperation authzOp,
Expand Down
Loading

0 comments on commit f4352b5

Please sign in to comment.