Skip to content

Commit

Permalink
Split DefaultContextResolver in 2 beans (#533)
Browse files Browse the repository at this point in the history
This refactoring is a no-op in terms of functionality.

This is required as `RealmContext` and `CallContext` may be treated
differently in the future (e.g. different scopes).
  • Loading branch information
adutra authored Dec 12, 2024
1 parent 32174be commit 85f0beb
Show file tree
Hide file tree
Showing 17 changed files with 116 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,13 @@ contract={org.apache.polaris.service.auth.TokenBrokerFactory}
name=rsa-key-pair
qualifier={io.smallrye.common.annotation.Identifier}

[org.apache.polaris.service.context.DefaultContextResolver]S
contract={org.apache.polaris.service.context.CallContextResolver,org.apache.polaris.service.context.RealmContextResolver}
[org.apache.polaris.service.context.DefaultRealmContextResolver]S
contract={org.apache.polaris.service.context.RealmContextResolver}
name=default
qualifier={io.smallrye.common.annotation.Identifier}

[org.apache.polaris.service.context.DefaultCallContextResolver]S
contract={org.apache.polaris.service.context.CallContextResolver}
name=default
qualifier={io.smallrye.common.annotation.Identifier}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
package org.apache.polaris.service;

import static org.apache.polaris.service.context.DefaultContextResolver.REALM_PROPERTY_KEY;
import static org.apache.polaris.service.context.DefaultRealmContextResolver.REALM_PROPERTY_KEY;
import static org.apache.polaris.service.dropwizard.throttling.RequestThrottlingErrorResponse.RequestThrottlingErrorType.REQUEST_TOO_LARGE;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
package org.apache.polaris.service;

import static org.apache.polaris.service.context.DefaultContextResolver.REALM_PROPERTY_KEY;
import static org.apache.polaris.service.context.DefaultRealmContextResolver.REALM_PROPERTY_KEY;
import static org.apache.polaris.service.dropwizard.TimedApplicationEventListener.SINGLETON_METRIC_NAME;
import static org.apache.polaris.service.dropwizard.TimedApplicationEventListener.TAG_API_NAME;
import static org.apache.polaris.service.dropwizard.monitor.PolarisMetricRegistry.SUFFIX_COUNTER;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
package org.apache.polaris.service.admin;

import static org.apache.polaris.service.context.DefaultContextResolver.REALM_PROPERTY_KEY;
import static org.apache.polaris.service.context.DefaultRealmContextResolver.REALM_PROPERTY_KEY;
import static org.assertj.core.api.Assertions.assertThat;

import io.dropwizard.testing.ConfigOverride;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
package org.apache.polaris.service.admin;

import static org.apache.polaris.service.admin.PolarisAuthzTestBase.SCHEMA;
import static org.apache.polaris.service.context.DefaultContextResolver.REALM_PROPERTY_KEY;
import static org.apache.polaris.service.context.DefaultRealmContextResolver.REALM_PROPERTY_KEY;
import static org.assertj.core.api.Assertions.assertThat;

import io.dropwizard.testing.ConfigOverride;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
package org.apache.polaris.service.admin;

import static org.apache.polaris.service.context.DefaultContextResolver.REALM_PROPERTY_KEY;
import static org.apache.polaris.service.context.DefaultRealmContextResolver.REALM_PROPERTY_KEY;
import static org.assertj.core.api.Assertions.assertThat;

import io.dropwizard.core.setup.Environment;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
package org.apache.polaris.service.admin;

import static io.dropwizard.jackson.Jackson.newObjectMapper;
import static org.apache.polaris.service.context.DefaultContextResolver.REALM_PROPERTY_KEY;
import static org.apache.polaris.service.context.DefaultRealmContextResolver.REALM_PROPERTY_KEY;
import static org.assertj.core.api.Assertions.assertThat;

import com.auth0.jwt.JWT;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
package org.apache.polaris.service.auth;

import static org.apache.polaris.service.context.DefaultContextResolver.REALM_PROPERTY_KEY;
import static org.apache.polaris.service.context.DefaultRealmContextResolver.REALM_PROPERTY_KEY;
import static org.assertj.core.api.Assertions.assertThat;

import jakarta.ws.rs.client.Client;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
package org.apache.polaris.service.catalog;

import static org.apache.polaris.service.context.DefaultContextResolver.REALM_PROPERTY_KEY;
import static org.apache.polaris.service.context.DefaultRealmContextResolver.REALM_PROPERTY_KEY;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
package org.apache.polaris.service.catalog;

import static org.apache.polaris.service.context.DefaultContextResolver.REALM_PROPERTY_KEY;
import static org.apache.polaris.service.context.DefaultRealmContextResolver.REALM_PROPERTY_KEY;

import io.dropwizard.testing.ConfigOverride;
import io.dropwizard.testing.ResourceHelpers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
package org.apache.polaris.service.catalog;

import static org.apache.polaris.service.context.DefaultContextResolver.REALM_PROPERTY_KEY;
import static org.apache.polaris.service.context.DefaultRealmContextResolver.REALM_PROPERTY_KEY;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
package org.apache.polaris.service.catalog;

import static org.apache.polaris.service.context.DefaultContextResolver.REALM_PROPERTY_KEY;
import static org.apache.polaris.service.context.DefaultRealmContextResolver.REALM_PROPERTY_KEY;
import static org.assertj.core.api.Assertions.assertThat;

import com.google.common.collect.ImmutableMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
package org.apache.polaris.service.ratelimiter;

import static org.apache.polaris.service.context.DefaultContextResolver.REALM_PROPERTY_KEY;
import static org.apache.polaris.service.context.DefaultRealmContextResolver.REALM_PROPERTY_KEY;
import static org.assertj.core.api.Assertions.assertThat;

import io.dropwizard.testing.junit5.DropwizardAppExtension;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
package org.apache.polaris.service.test;

import static org.apache.polaris.service.context.DefaultContextResolver.REALM_PROPERTY_KEY;
import static org.apache.polaris.service.context.DefaultRealmContextResolver.REALM_PROPERTY_KEY;
import static org.apache.polaris.service.test.DropwizardTestEnvironmentResolver.findDropwizardExtension;

import com.fasterxml.jackson.core.JsonProcessingException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
package org.apache.polaris.service.test;

import static org.apache.polaris.service.context.DefaultContextResolver.REALM_PROPERTY_KEY;
import static org.apache.polaris.service.context.DefaultRealmContextResolver.REALM_PROPERTY_KEY;
import static org.assertj.core.api.Assertions.assertThat;

import jakarta.ws.rs.client.Entity;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.polaris.service.context;

import static org.apache.polaris.service.context.DefaultRealmContextResolver.parseBearerTokenAsKvPairs;

import io.smallrye.common.annotation.Identifier;
import jakarta.inject.Inject;
import java.time.Clock;
import java.time.ZoneId;
import java.util.Map;
import org.apache.polaris.core.PolarisCallContext;
import org.apache.polaris.core.PolarisConfigurationStore;
import org.apache.polaris.core.PolarisDefaultDiagServiceImpl;
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.core.persistence.PolarisMetaStoreSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* For local/dev testing, this resolver simply expects a custom bearer-token format that is a
* semicolon-separated list of colon-separated key/value pairs that constitute the realm properties.
*
* <p>Example: principal:data-engineer;password:test;realm:acct123
*/
@Identifier("default")
public class DefaultCallContextResolver implements CallContextResolver {
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultCallContextResolver.class);

public static final String PRINCIPAL_PROPERTY_KEY = "principal";
public static final String PRINCIPAL_PROPERTY_DEFAULT_VALUE = "default-principal";

@Inject private MetaStoreManagerFactory metaStoreManagerFactory;
@Inject private PolarisConfigurationStore configurationStore;

@Override
public CallContext resolveCallContext(
final RealmContext realmContext,
String method,
String path,
Map<String, String> queryParams,
Map<String, String> headers) {
LOGGER
.atDebug()
.addKeyValue("realmContext", realmContext.getRealmIdentifier())
.addKeyValue("method", method)
.addKeyValue("path", path)
.addKeyValue("queryParams", queryParams)
.addKeyValue("headers", headers)
.log("Resolving CallContext");
final Map<String, String> parsedProperties = parseBearerTokenAsKvPairs(headers);

if (!parsedProperties.containsKey(PRINCIPAL_PROPERTY_KEY)) {
LOGGER.warn(
"Failed to parse {} from headers ({}); using {}",
PRINCIPAL_PROPERTY_KEY,
headers,
PRINCIPAL_PROPERTY_DEFAULT_VALUE);
parsedProperties.put(PRINCIPAL_PROPERTY_KEY, PRINCIPAL_PROPERTY_DEFAULT_VALUE);
}

PolarisDiagnostics diagServices = new PolarisDefaultDiagServiceImpl();
PolarisMetaStoreSession metaStoreSession =
metaStoreManagerFactory.getOrCreateSessionSupplier(realmContext).get();
PolarisCallContext polarisContext =
new PolarisCallContext(
metaStoreSession,
diagServices,
configurationStore,
Clock.system(ZoneId.systemDefault()));
return CallContext.of(realmContext, polarisContext);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,9 @@

import com.google.common.base.Splitter;
import io.smallrye.common.annotation.Identifier;
import jakarta.inject.Inject;
import java.time.Clock;
import java.time.ZoneId;
import java.util.HashMap;
import java.util.Map;
import org.apache.polaris.core.PolarisCallContext;
import org.apache.polaris.core.PolarisConfigurationStore;
import org.apache.polaris.core.PolarisDefaultDiagServiceImpl;
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.core.persistence.PolarisMetaStoreSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -43,16 +33,11 @@
* <p>Example: principal:data-engineer;password:test;realm:acct123
*/
@Identifier("default")
public class DefaultContextResolver implements RealmContextResolver, CallContextResolver {
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultContextResolver.class);
public class DefaultRealmContextResolver implements RealmContextResolver {
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultRealmContextResolver.class);

public static final String REALM_PROPERTY_KEY = "realm";

public static final String PRINCIPAL_PROPERTY_KEY = "principal";
public static final String PRINCIPAL_PROPERTY_DEFAULT_VALUE = "default-principal";

@Inject private MetaStoreManagerFactory metaStoreManagerFactory;
@Inject private PolarisConfigurationStore configurationStore;
private String defaultRealm = "default-realm";

@Override
Expand Down Expand Up @@ -96,49 +81,11 @@ public String getDefaultRealm() {
return this.defaultRealm;
}

@Override
public CallContext resolveCallContext(
final RealmContext realmContext,
String method,
String path,
Map<String, String> queryParams,
Map<String, String> headers) {
LOGGER
.atDebug()
.addKeyValue("realmContext", realmContext.getRealmIdentifier())
.addKeyValue("method", method)
.addKeyValue("path", path)
.addKeyValue("queryParams", queryParams)
.addKeyValue("headers", headers)
.log("Resolving CallContext");
final Map<String, String> parsedProperties = parseBearerTokenAsKvPairs(headers);

if (!parsedProperties.containsKey(PRINCIPAL_PROPERTY_KEY)) {
LOGGER.warn(
"Failed to parse {} from headers ({}); using {}",
PRINCIPAL_PROPERTY_KEY,
headers,
PRINCIPAL_PROPERTY_DEFAULT_VALUE);
parsedProperties.put(PRINCIPAL_PROPERTY_KEY, PRINCIPAL_PROPERTY_DEFAULT_VALUE);
}

PolarisDiagnostics diagServices = new PolarisDefaultDiagServiceImpl();
PolarisMetaStoreSession metaStoreSession =
metaStoreManagerFactory.getOrCreateSessionSupplier(realmContext).get();
PolarisCallContext polarisContext =
new PolarisCallContext(
metaStoreSession,
diagServices,
configurationStore,
Clock.system(ZoneId.systemDefault()));
return CallContext.of(realmContext, polarisContext);
}

/**
* Returns kv pairs parsed from the "Authorization: Bearer k1:v1;k2:v2;k3:v3" header if it exists;
* if missing, returns empty map.
*/
private static Map<String, String> parseBearerTokenAsKvPairs(Map<String, String> headers) {
static Map<String, String> parseBearerTokenAsKvPairs(Map<String, String> headers) {
Map<String, String> parsedProperties = new HashMap<>();
if (headers != null) {
String authHeader = headers.get("Authorization");
Expand Down

0 comments on commit 85f0beb

Please sign in to comment.