Skip to content

Commit

Permalink
Remove CallContext
Browse files Browse the repository at this point in the history
  • Loading branch information
adutra committed Dec 24, 2024
1 parent 8a94c56 commit ea2175d
Show file tree
Hide file tree
Showing 47 changed files with 1,548 additions and 1,791 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import java.security.Principal;
import java.util.Optional;
import org.apache.polaris.core.auth.AuthenticatedPolarisPrincipal;
import org.apache.polaris.core.context.CallContext;
import org.apache.polaris.core.context.RealmContext;
import org.apache.polaris.service.auth.Authenticator;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.jboss.resteasy.reactive.server.ServerRequestFilter;
Expand All @@ -50,7 +50,7 @@ public class QuarkusOAuthFilter {
public static final String OAUTH_ACCESS_TOKEN_PARAM = "access_token";

@Inject Authenticator<String, AuthenticatedPolarisPrincipal> authenticator;
@Inject CallContext callContext;
@Inject RealmContext realmContext;

@ServerRequestFilter(priority = Priorities.AUTHENTICATION)
public void authenticate(ContainerRequestContext requestContext) {
Expand All @@ -71,7 +71,7 @@ public void authenticate(ContainerRequestContext requestContext) {
if (!authenticate(requestContext, credentials, SecurityContext.BASIC_AUTH)) {
throw new NotAuthorizedException(
"Credentials are required to access this resource.",
String.format(CHALLENGE_FORMAT, callContext.getRealmContext().getRealmIdentifier()));
String.format(CHALLENGE_FORMAT, realmContext.getRealmIdentifier()));
}
}

Expand Down Expand Up @@ -115,8 +115,6 @@ protected boolean authenticate(
return false;
}

CallContext.setCurrentContext(callContext);

Optional<AuthenticatedPolarisPrincipal> principal = authenticator.authenticate(credentials);
if (principal.isEmpty()) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import org.apache.polaris.core.auth.AuthenticatedPolarisPrincipal;
import org.apache.polaris.core.auth.PolarisAuthorizer;
import org.apache.polaris.core.auth.PolarisAuthorizerImpl;
import org.apache.polaris.core.context.CallContext;
import org.apache.polaris.core.context.RealmContext;
import org.apache.polaris.core.persistence.MetaStoreManagerFactory;
import org.apache.polaris.core.persistence.PolarisEntityManager;
Expand Down Expand Up @@ -107,16 +106,6 @@ public RealmContext realmContext(
.collect(HashMap::new, (m, e) -> m.put(e.getKey(), e.getValue()), HashMap::putAll));
}

@Produces
@RequestScoped
public CallContext callContext(RealmContext realmContext) {
return CallContext.of(realmContext);
}

public void closeCallContext(@Disposes CallContext callContext) {
callContext.close();
}

@Produces
@RequestScoped
public PolarisMetaStoreSession metaStoreSession(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import java.util.concurrent.ExecutorService;
import org.apache.polaris.core.PolarisConfigurationStore;
import org.apache.polaris.core.PolarisDiagnostics;
import org.apache.polaris.core.context.CallContext;
import org.apache.polaris.core.context.RealmContext;
import org.apache.polaris.core.persistence.MetaStoreManagerFactory;
import org.apache.polaris.service.dropwizard.tracing.QuarkusTracingFilter;
import org.apache.polaris.service.task.TaskExecutorImpl;
Expand Down Expand Up @@ -71,19 +71,18 @@ public void init() {
}

@Override
protected void handleTask(long taskEntityId, CallContext callContext, int attempt) {
protected void handleTask(long taskEntityId, RealmContext realmContext, int attempt) {
Span span =
tracer
.spanBuilder("polaris.task")
.setParent(Context.current())
.setAttribute(
QuarkusTracingFilter.REALM_ID_ATTRIBUTE,
callContext.getRealmContext().getRealmIdentifier())
QuarkusTracingFilter.REALM_ID_ATTRIBUTE, realmContext.getRealmIdentifier())
.setAttribute("polaris.task.entity.id", taskEntityId)
.setAttribute("polaris.task.attempt", attempt)
.startSpan();
try (Scope ignored = span.makeCurrent()) {
super.handleTask(taskEntityId, callContext, attempt);
super.handleTask(taskEntityId, realmContext, attempt);
} finally {
span.end();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ private PolarisAdminService newTestAdminService(Set<String> activatedPrincipalRo
final AuthenticatedPolarisPrincipal authenticatedPrincipal =
new AuthenticatedPolarisPrincipal(principalEntity, activatedPrincipalRoles);
return new PolarisAdminService(
realmContext,
entityManager,
metaStoreManager,
metaStoreSession,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
import org.apache.polaris.core.auth.AuthenticatedPolarisPrincipal;
import org.apache.polaris.core.auth.PolarisAuthorizer;
import org.apache.polaris.core.auth.PolarisAuthorizerImpl;
import org.apache.polaris.core.context.CallContext;
import org.apache.polaris.core.context.RealmContext;
import org.apache.polaris.core.entity.CatalogEntity;
import org.apache.polaris.core.entity.CatalogRoleEntity;
Expand Down Expand Up @@ -163,7 +162,6 @@ public Map<String, String> getConfigOverrides() {

@Inject protected MetaStoreManagerFactory managerFactory;
@Inject protected RealmEntityManagerFactory realmEntityManagerFactory;
// @Inject protected CallContextCatalogFactory callContextCatalogFactory;
@Inject protected PolarisConfigurationStore configurationStore;
@Inject protected PolarisDiagnostics diagServices;

Expand All @@ -174,7 +172,7 @@ public Map<String, String> getConfigOverrides() {
protected PolarisMetaStoreSession metaStoreSession;
protected PolarisBaseEntity catalogEntity;
protected PrincipalEntity principalEntity;
protected CallContext callContext;
protected RealmContext realmContext;
protected AuthenticatedPolarisPrincipal authenticatedRoot;

@BeforeAll
Expand All @@ -189,14 +187,11 @@ public static void setUpMocks() {

@BeforeEach
public void before(TestInfo testInfo) {
RealmContext realmContext = testInfo::getDisplayName;
realmContext = testInfo::getDisplayName;
metaStoreManager = managerFactory.getOrCreateMetaStoreManager(realmContext);
metaStoreSession = managerFactory.getOrCreateSessionSupplier(realmContext).get();
entityManager = realmEntityManagerFactory.getOrCreateEntityManager(realmContext);

callContext = CallContext.of(realmContext);
CallContext.setCurrentContext(callContext);

PrincipalEntity rootEntity =
new PrincipalEntity(
PolarisEntity.of(
Expand All @@ -213,6 +208,7 @@ public void before(TestInfo testInfo) {

this.adminService =
new PolarisAdminService(
realmContext,
entityManager,
metaStoreManager,
metaStoreSession,
Expand Down Expand Up @@ -373,12 +369,12 @@ private void initBaseCatalog() {
entityManager, metaStoreSession, authenticatedRoot, CATALOG_NAME);
this.baseCatalog =
new BasePolarisCatalog(
realmContext,
entityManager,
metaStoreManager,
metaStoreSession,
configurationStore,
diagServices,
callContext,
passthroughView,
authenticatedRoot,
Mockito.mock(),
Expand Down Expand Up @@ -419,13 +415,14 @@ public TestPolarisCallContextCatalogFactory(

@Override
public Catalog createCallContextCatalog(
CallContext context,
RealmContext realmContext,
AuthenticatedPolarisPrincipal authenticatedPolarisPrincipal,
final PolarisResolutionManifest resolvedManifest) {
// This depends on the BasePolarisCatalog allowing calling initialize multiple times
// to override the previous config.
Catalog catalog =
super.createCallContextCatalog(context, authenticatedPolarisPrincipal, resolvedManifest);
super.createCallContextCatalog(
realmContext, authenticatedPolarisPrincipal, resolvedManifest);
catalog.initialize(
CATALOG_NAME,
ImmutableMap.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import org.apache.polaris.core.context.CallContext;
import org.apache.polaris.core.entity.PolarisBaseEntity;
import org.apache.polaris.core.entity.PolarisEntitySubType;
import org.apache.polaris.core.entity.PolarisEntityType;
Expand All @@ -43,7 +42,6 @@ public class JWTSymmetricKeyGeneratorTest {
/** Sanity test to verify that we can generate a token */
@Test
public void testJWTSymmetricKeyGenerator() {
CallContext.setCurrentContext(CallContext.of(() -> "realm"));
PolarisMetaStoreManager metastoreManager = Mockito.mock(PolarisMetaStoreManager.class);
PolarisMetaStoreSession metaStoreSession = Mockito.mock(PolarisMetaStoreSession.class);
String mainSecret = "test_secret";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
import org.apache.polaris.core.auth.AuthenticatedPolarisPrincipal;
import org.apache.polaris.core.auth.PolarisAuthorizerImpl;
import org.apache.polaris.core.auth.PolarisSecretsManager.PrincipalSecretsResult;
import org.apache.polaris.core.context.CallContext;
import org.apache.polaris.core.context.RealmContext;
import org.apache.polaris.core.entity.CatalogEntity;
import org.apache.polaris.core.entity.PolarisBaseEntity;
Expand Down Expand Up @@ -139,7 +138,6 @@ public class BasePolarisCatalogTest extends CatalogTests<BasePolarisCatalog> {
@Inject Clock clock;

private BasePolarisCatalog catalog;
private CallContext callContext;
private RealmContext realmContext;
private PolarisMetaStoreManager metaStoreManager;
private PolarisMetaStoreSession metaStoreSession;
Expand Down Expand Up @@ -168,9 +166,6 @@ public void before(TestInfo testInfo) {
metaStoreSession = managerFactory.getOrCreateSessionSupplier(realmContext).get();
entityManager = entityManagerFactory.getOrCreateEntityManager(realmContext);

callContext = CallContext.of(realmContext);
CallContext.setCurrentContext(callContext);

PrincipalEntity rootEntity =
new PrincipalEntity(
PolarisEntity.of(
Expand All @@ -187,6 +182,7 @@ public void before(TestInfo testInfo) {

adminService =
new PolarisAdminService(
realmContext,
entityManager,
metaStoreManager,
metaStoreSession,
Expand Down Expand Up @@ -221,12 +217,12 @@ public void before(TestInfo testInfo) {
TaskExecutor taskExecutor = Mockito.mock();
this.catalog =
new BasePolarisCatalog(
realmContext,
entityManager,
metaStoreManager,
metaStoreSession,
configurationStore,
diagServices,
callContext,
passthroughView,
authenticatedRoot,
taskExecutor,
Expand Down Expand Up @@ -490,12 +486,12 @@ public void testValidateNotificationFailToCreateFileIO() throws IOException {
FileIOFactory fileIoFactory = spy(new DefaultFileIOFactory());
BasePolarisCatalog catalog =
new BasePolarisCatalog(
realmContext,
entityManager,
metaStoreManager,
metaStoreSession,
configurationStore,
diagServices,
callContext,
passthroughView,
authenticatedRoot,
Mockito.mock(),
Expand Down Expand Up @@ -806,26 +802,24 @@ public void testUpdateNotificationCreateTableWithLocalFilePrefix() {
// The location of the metadata JSON file specified in the create will be forbidden.
final String metadataLocation = "file:///etc/metadata.json/../passwd";
String catalogWithoutStorage = "catalogWithoutStorage";
PolarisEntity catalogEntity =
adminService.createCatalog(
new CatalogEntity.Builder()
.setDefaultBaseLocation("file://")
.setName(catalogWithoutStorage)
.build());
adminService.createCatalog(
new CatalogEntity.Builder()
.setDefaultBaseLocation("file://")
.setName(catalogWithoutStorage)
.build());

CallContext callContext = CallContext.getCurrentContext();
PolarisPassthroughResolutionView passthroughView =
new PolarisPassthroughResolutionView(
entityManager, metaStoreSession, authenticatedRoot, catalogWithoutStorage);
TaskExecutor taskExecutor = Mockito.mock();
BasePolarisCatalog catalog =
new BasePolarisCatalog(
realmContext,
entityManager,
metaStoreManager,
metaStoreSession,
configurationStore,
diagServices,
callContext,
passthroughView,
authenticatedRoot,
taskExecutor,
Expand Down Expand Up @@ -854,7 +848,7 @@ public void testUpdateNotificationCreateTableWithLocalFilePrefix() {
TableMetadataParser.toJson(createSampleTableMetadata(metadataLocation)).getBytes(UTF_8));

if (!configurationStore
.getConfiguration(PolarisConfiguration.SUPPORTED_CATALOG_STORAGE_TYPES)
.getConfiguration(realmContext, PolarisConfiguration.SUPPORTED_CATALOG_STORAGE_TYPES)
.contains("FILE")) {
Assertions.assertThatThrownBy(() -> catalog.sendNotification(table, request))
.isInstanceOf(ForbiddenException.class)
Expand All @@ -879,19 +873,18 @@ public void testUpdateNotificationCreateTableWithHttpPrefix() {
.setName(catalogName)
.build());

CallContext callContext = CallContext.getCurrentContext();
PolarisPassthroughResolutionView passthroughView =
new PolarisPassthroughResolutionView(
entityManager, metaStoreSession, authenticatedRoot, catalogName);
TaskExecutor taskExecutor = Mockito.mock();
BasePolarisCatalog catalog =
new BasePolarisCatalog(
realmContext,
entityManager,
metaStoreManager,
metaStoreSession,
configurationStore,
diagServices,
callContext,
passthroughView,
authenticatedRoot,
taskExecutor,
Expand Down Expand Up @@ -922,7 +915,7 @@ public void testUpdateNotificationCreateTableWithHttpPrefix() {
TableMetadataParser.toJson(createSampleTableMetadata(metadataLocation)).getBytes(UTF_8));

if (!configurationStore
.getConfiguration(PolarisConfiguration.SUPPORTED_CATALOG_STORAGE_TYPES)
.getConfiguration(realmContext, PolarisConfiguration.SUPPORTED_CATALOG_STORAGE_TYPES)
.contains("FILE")) {
Assertions.assertThatThrownBy(() -> catalog.sendNotification(table, request))
.isInstanceOf(ForbiddenException.class)
Expand All @@ -942,7 +935,7 @@ public void testUpdateNotificationCreateTableWithHttpPrefix() {
TableMetadataParser.toJson(createSampleTableMetadata(metadataLocation)).getBytes(UTF_8));

if (!configurationStore
.getConfiguration(PolarisConfiguration.SUPPORTED_CATALOG_STORAGE_TYPES)
.getConfiguration(realmContext, PolarisConfiguration.SUPPORTED_CATALOG_STORAGE_TYPES)
.contains("FILE")) {
Assertions.assertThatThrownBy(() -> catalog.sendNotification(table, newRequest))
.isInstanceOf(ForbiddenException.class)
Expand Down Expand Up @@ -1417,19 +1410,17 @@ public void testDropTableWithPurgeDisabled() {
.addProperty(PolarisConfiguration.DROP_WITH_PURGE_ENABLED.catalogConfig(), "false")
.setStorageConfigurationInfo(noPurgeStorageConfigModel, storageLocation)
.build());
realmContext = () -> "realm";
CallContext callContext = CallContext.of(realmContext);
PolarisPassthroughResolutionView passthroughView =
new PolarisPassthroughResolutionView(
entityManager, metaStoreSession, authenticatedRoot, noPurgeCatalogName);
BasePolarisCatalog noPurgeCatalog =
new BasePolarisCatalog(
realmContext,
entityManager,
metaStoreManager,
metaStoreSession,
configurationStore,
diagServices,
callContext,
passthroughView,
authenticatedRoot,
Mockito.mock(),
Expand Down Expand Up @@ -1501,21 +1492,19 @@ public void testRetriableException() {

@Test
public void testFileIOWrapper() {
CallContext callContext = CallContext.of(realmContext);
CallContext.setCurrentContext(callContext);
PolarisPassthroughResolutionView passthroughView =
new PolarisPassthroughResolutionView(
entityManager, metaStoreSession, authenticatedRoot, CATALOG_NAME);

TestFileIOFactory measured = new TestFileIOFactory();
BasePolarisCatalog catalog =
new BasePolarisCatalog(
realmContext,
entityManager,
metaStoreManager,
metaStoreSession,
configurationStore,
diagServices,
callContext,
passthroughView,
authenticatedRoot,
Mockito.mock(),
Expand Down
Loading

0 comments on commit ea2175d

Please sign in to comment.