Skip to content

Commit 0eb0f2b

Browse files
committed
inject PolarisMetaStoreManager into DefaultOAuth2ApiService
this avoids `TokenBrokerFactory` impls having to call `MetaStoreManagerFactory.createMetaStoreManager`
1 parent ccbb4ce commit 0eb0f2b

File tree

12 files changed

+54
-119
lines changed

12 files changed

+54
-119
lines changed

runtime/service/src/main/java/org/apache/polaris/service/auth/internal/broker/JWTBroker.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import java.util.Optional;
2828
import java.util.UUID;
2929
import org.apache.iceberg.exceptions.NotAuthorizedException;
30-
import org.apache.polaris.core.PolarisCallContext;
3130
import org.apache.polaris.core.entity.PolarisEntityType;
3231
import org.apache.polaris.core.entity.PrincipalEntity;
3332
import org.apache.polaris.core.persistence.PolarisMetaStoreManager;
@@ -50,11 +49,9 @@ public abstract class JWTBroker implements TokenBroker {
5049
private static final String CLAIM_KEY_PRINCIPAL_ID = "principalId";
5150
private static final String CLAIM_KEY_SCOPE = "scope";
5251

53-
private final PolarisMetaStoreManager metaStoreManager;
5452
private final int maxTokenGenerationInSeconds;
5553

56-
JWTBroker(PolarisMetaStoreManager metaStoreManager, int maxTokenGenerationInSeconds) {
57-
this.metaStoreManager = metaStoreManager;
54+
JWTBroker(int maxTokenGenerationInSeconds) {
5855
this.maxTokenGenerationInSeconds = maxTokenGenerationInSeconds;
5956
}
6057

@@ -88,7 +85,7 @@ public TokenResponse generateFromToken(
8885
String subjectToken,
8986
String grantType,
9087
String scope,
91-
PolarisCallContext polarisCallContext,
88+
PolarisMetaStoreManager metaStoreManager,
9289
TokenType requestedTokenType) {
9390
if (requestedTokenType != null && !TokenType.ACCESS_TOKEN.equals(requestedTokenType)) {
9491
return TokenResponse.of(OAuthError.invalid_request);
@@ -128,7 +125,7 @@ public TokenResponse generateFromClientSecrets(
128125
String clientSecret,
129126
String grantType,
130127
String scope,
131-
PolarisCallContext polarisCallContext,
128+
PolarisMetaStoreManager metaStoreManager,
132129
TokenType requestedTokenType) {
133130
// Initial sanity checks
134131
TokenRequestValidator validator = new TokenRequestValidator();
@@ -138,7 +135,8 @@ public TokenResponse generateFromClientSecrets(
138135
return TokenResponse.of(initialValidationResponse.get());
139136
}
140137

141-
Optional<PrincipalEntity> principal = findPrincipalEntity(clientId, clientSecret);
138+
Optional<PrincipalEntity> principal =
139+
findPrincipalEntity(metaStoreManager, clientId, clientSecret);
142140
if (principal.isEmpty()) {
143141
return TokenResponse.of(OAuthError.unauthorized_client);
144142
}
@@ -178,7 +176,8 @@ private String scopes(String scope) {
178176
return scope == null || scope.isBlank() ? DefaultAuthenticator.PRINCIPAL_ROLE_ALL : scope;
179177
}
180178

181-
private Optional<PrincipalEntity> findPrincipalEntity(String clientId, String clientSecret) {
179+
private Optional<PrincipalEntity> findPrincipalEntity(
180+
PolarisMetaStoreManager metaStoreManager, String clientId, String clientSecret) {
182181
// Validate the principal is present and secrets match
183182
PrincipalSecretsResult principalSecrets = metaStoreManager.loadPrincipalSecrets(clientId);
184183
if (!principalSecrets.isSuccess()) {

runtime/service/src/main/java/org/apache/polaris/service/auth/internal/broker/RSAKeyPairJWTBroker.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,14 @@
2121
import com.auth0.jwt.algorithms.Algorithm;
2222
import java.security.interfaces.RSAPrivateKey;
2323
import java.security.interfaces.RSAPublicKey;
24-
import org.apache.polaris.core.persistence.PolarisMetaStoreManager;
2524

2625
/** Generates a JWT using a Public/Private RSA Key */
2726
public class RSAKeyPairJWTBroker extends JWTBroker {
2827

2928
private final KeyProvider keyProvider;
3029

31-
RSAKeyPairJWTBroker(
32-
PolarisMetaStoreManager metaStoreManager,
33-
int maxTokenGenerationInSeconds,
34-
KeyProvider keyProvider) {
35-
super(metaStoreManager, maxTokenGenerationInSeconds);
30+
RSAKeyPairJWTBroker(int maxTokenGenerationInSeconds, KeyProvider keyProvider) {
31+
super(maxTokenGenerationInSeconds);
3632
this.keyProvider = keyProvider;
3733
}
3834

runtime/service/src/main/java/org/apache/polaris/service/auth/internal/broker/RSAKeyPairJWTBrokerFactory.java

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
import java.util.concurrent.ConcurrentHashMap;
2727
import java.util.concurrent.ConcurrentMap;
2828
import org.apache.polaris.core.context.RealmContext;
29-
import org.apache.polaris.core.persistence.MetaStoreManagerFactory;
30-
import org.apache.polaris.core.persistence.PolarisMetaStoreManager;
3129
import org.apache.polaris.service.auth.AuthenticationConfiguration;
3230
import org.apache.polaris.service.auth.AuthenticationRealmConfiguration;
3331
import org.apache.polaris.service.auth.AuthenticationRealmConfiguration.TokenBrokerConfiguration.RSAKeyPairConfiguration;
@@ -36,21 +34,17 @@
3634
@Identifier("rsa-key-pair")
3735
public class RSAKeyPairJWTBrokerFactory implements TokenBrokerFactory {
3836

39-
private final MetaStoreManagerFactory metaStoreManagerFactory;
4037
private final AuthenticationConfiguration authenticationConfiguration;
4138

4239
private final ConcurrentMap<String, RSAKeyPairJWTBroker> tokenBrokers = new ConcurrentHashMap<>();
4340

4441
@Inject
45-
public RSAKeyPairJWTBrokerFactory(
46-
MetaStoreManagerFactory metaStoreManagerFactory,
47-
AuthenticationConfiguration authenticationConfiguration) {
48-
this.metaStoreManagerFactory = metaStoreManagerFactory;
42+
public RSAKeyPairJWTBrokerFactory(AuthenticationConfiguration authenticationConfiguration) {
4943
this.authenticationConfiguration = authenticationConfiguration;
5044
}
5145

5246
@Override
53-
public TokenBroker apply(RealmContext realmContext) {
47+
public TokenBroker newTokenBroker(RealmContext realmContext) {
5448
return tokenBrokers.computeIfAbsent(
5549
realmContext.getRealmIdentifier(), k -> createTokenBroker(realmContext));
5650
}
@@ -64,10 +58,7 @@ private RSAKeyPairJWTBroker createTokenBroker(RealmContext realmContext) {
6458
.rsaKeyPair()
6559
.map(this::fileSystemKeyPair)
6660
.orElseGet(this::generateEphemeralKeyPair);
67-
PolarisMetaStoreManager metaStoreManager =
68-
metaStoreManagerFactory.createMetaStoreManager(realmContext, null);
69-
return new RSAKeyPairJWTBroker(
70-
metaStoreManager, (int) maxTokenGeneration.toSeconds(), keyProvider);
61+
return new RSAKeyPairJWTBroker((int) maxTokenGeneration.toSeconds(), keyProvider);
7162
}
7263

7364
private KeyProvider fileSystemKeyPair(RSAKeyPairConfiguration config) {

runtime/service/src/main/java/org/apache/polaris/service/auth/internal/broker/SymmetricKeyJWTBroker.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,13 @@
2020

2121
import com.auth0.jwt.algorithms.Algorithm;
2222
import java.util.function.Supplier;
23-
import org.apache.polaris.core.persistence.PolarisMetaStoreManager;
2423

2524
/** Generates a JWT using a Symmetric Key. */
2625
public class SymmetricKeyJWTBroker extends JWTBroker {
2726
private final Supplier<String> secretSupplier;
2827

29-
public SymmetricKeyJWTBroker(
30-
PolarisMetaStoreManager metaStoreManager,
31-
int maxTokenGenerationInSeconds,
32-
Supplier<String> secretSupplier) {
33-
super(metaStoreManager, maxTokenGenerationInSeconds);
28+
public SymmetricKeyJWTBroker(int maxTokenGenerationInSeconds, Supplier<String> secretSupplier) {
29+
super(maxTokenGenerationInSeconds);
3430
this.secretSupplier = secretSupplier;
3531
}
3632

runtime/service/src/main/java/org/apache/polaris/service/auth/internal/broker/SymmetricKeyJWTBrokerFactory.java

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@
3131
import java.util.concurrent.ConcurrentMap;
3232
import java.util.function.Supplier;
3333
import org.apache.polaris.core.context.RealmContext;
34-
import org.apache.polaris.core.persistence.MetaStoreManagerFactory;
35-
import org.apache.polaris.core.persistence.PolarisMetaStoreManager;
3634
import org.apache.polaris.service.auth.AuthenticationConfiguration;
3735
import org.apache.polaris.service.auth.AuthenticationRealmConfiguration;
3836
import org.apache.polaris.service.auth.AuthenticationRealmConfiguration.TokenBrokerConfiguration.SymmetricKeyConfiguration;
@@ -41,22 +39,18 @@
4139
@Identifier("symmetric-key")
4240
public class SymmetricKeyJWTBrokerFactory implements TokenBrokerFactory {
4341

44-
private final MetaStoreManagerFactory metaStoreManagerFactory;
4542
private final AuthenticationConfiguration authenticationConfiguration;
4643

4744
private final ConcurrentMap<String, SymmetricKeyJWTBroker> tokenBrokers =
4845
new ConcurrentHashMap<>();
4946

5047
@Inject
51-
public SymmetricKeyJWTBrokerFactory(
52-
MetaStoreManagerFactory metaStoreManagerFactory,
53-
AuthenticationConfiguration authenticationConfiguration) {
54-
this.metaStoreManagerFactory = metaStoreManagerFactory;
48+
public SymmetricKeyJWTBrokerFactory(AuthenticationConfiguration authenticationConfiguration) {
5549
this.authenticationConfiguration = authenticationConfiguration;
5650
}
5751

5852
@Override
59-
public TokenBroker apply(RealmContext realmContext) {
53+
public TokenBroker newTokenBroker(RealmContext realmContext) {
6054
return tokenBrokers.computeIfAbsent(
6155
realmContext.getRealmIdentifier(), k -> createTokenBroker(realmContext));
6256
}
@@ -73,10 +67,7 @@ private SymmetricKeyJWTBroker createTokenBroker(RealmContext realmContext) {
7367
Path file = symmetricKeyConfiguration.file().orElse(null);
7468
checkState(secret != null || file != null, "Either file or secret must be set");
7569
Supplier<String> secretSupplier = secret != null ? () -> secret : readSecretFromDisk(file);
76-
PolarisMetaStoreManager metaStoreManager =
77-
metaStoreManagerFactory.createMetaStoreManager(realmContext, null);
78-
return new SymmetricKeyJWTBroker(
79-
metaStoreManager, (int) maxTokenGeneration.toSeconds(), secretSupplier);
70+
return new SymmetricKeyJWTBroker((int) maxTokenGeneration.toSeconds(), secretSupplier);
8071
}
8172

8273
private static Supplier<String> readSecretFromDisk(Path file) {

runtime/service/src/main/java/org/apache/polaris/service/auth/internal/broker/TokenBroker.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919
package org.apache.polaris.service.auth.internal.broker;
2020

21-
import org.apache.polaris.core.PolarisCallContext;
21+
import org.apache.polaris.core.persistence.PolarisMetaStoreManager;
2222
import org.apache.polaris.service.auth.PolarisCredential;
2323
import org.apache.polaris.service.types.TokenType;
2424

@@ -39,7 +39,7 @@ TokenResponse generateFromClientSecrets(
3939
final String clientSecret,
4040
final String grantType,
4141
final String scope,
42-
PolarisCallContext polarisCallContext,
42+
PolarisMetaStoreManager metaStoreManager,
4343
TokenType requestedTokenType);
4444

4545
/**
@@ -52,7 +52,7 @@ TokenResponse generateFromToken(
5252
String subjectToken,
5353
final String grantType,
5454
final String scope,
55-
PolarisCallContext polarisCallContext,
55+
PolarisMetaStoreManager metaStoreManager,
5656
TokenType requestedTokenType);
5757

5858
/** Decodes and verifies the token, then returns the associated {@link PolarisCredential}. */

runtime/service/src/main/java/org/apache/polaris/service/auth/internal/broker/TokenBrokerFactory.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@
1818
*/
1919
package org.apache.polaris.service.auth.internal.broker;
2020

21-
import java.util.function.Function;
2221
import org.apache.polaris.core.context.RealmContext;
2322

2423
/**
2524
* Factory that creates a {@link TokenBroker} for generating and parsing. The {@link TokenBroker} is
2625
* created based on the realm context.
2726
*/
28-
public interface TokenBrokerFactory extends Function<RealmContext, TokenBroker> {}
27+
public interface TokenBrokerFactory {
28+
29+
TokenBroker newTokenBroker(RealmContext realmContext);
30+
}

runtime/service/src/main/java/org/apache/polaris/service/auth/internal/service/DefaultOAuth2ApiService.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
import jakarta.ws.rs.core.SecurityContext;
2828
import java.util.Base64;
2929
import org.apache.iceberg.rest.responses.OAuthTokenResponse;
30-
import org.apache.polaris.core.context.CallContext;
3130
import org.apache.polaris.core.context.RealmContext;
31+
import org.apache.polaris.core.persistence.PolarisMetaStoreManager;
3232
import org.apache.polaris.service.auth.internal.broker.TokenBroker;
3333
import org.apache.polaris.service.auth.internal.broker.TokenResponse;
3434
import org.apache.polaris.service.catalog.api.IcebergRestOAuth2ApiService;
@@ -49,12 +49,13 @@ public class DefaultOAuth2ApiService implements IcebergRestOAuth2ApiService {
4949
private static final String BEARER = "bearer";
5050

5151
private final TokenBroker tokenBroker;
52-
private final CallContext callContext;
52+
private final PolarisMetaStoreManager metaStoreManager;
5353

5454
@Inject
55-
public DefaultOAuth2ApiService(TokenBroker tokenBroker, CallContext callContext) {
55+
public DefaultOAuth2ApiService(
56+
TokenBroker tokenBroker, PolarisMetaStoreManager metaStoreManager) {
5657
this.tokenBroker = tokenBroker;
57-
this.callContext = callContext;
58+
this.metaStoreManager = metaStoreManager;
5859
}
5960

6061
@Override
@@ -104,20 +105,15 @@ public Response getToken(
104105
if (clientSecret != null) {
105106
tokenResponse =
106107
tokenBroker.generateFromClientSecrets(
107-
clientId,
108-
clientSecret,
109-
grantType,
110-
scope,
111-
callContext.getPolarisCallContext(),
112-
requestedTokenType);
108+
clientId, clientSecret, grantType, scope, metaStoreManager, requestedTokenType);
113109
} else if (subjectToken != null) {
114110
tokenResponse =
115111
tokenBroker.generateFromToken(
116112
subjectTokenType,
117113
subjectToken,
118114
grantType,
119115
scope,
120-
callContext.getPolarisCallContext(),
116+
metaStoreManager,
121117
requestedTokenType);
122118
} else {
123119
return OAuthUtils.getResponseFromError(OAuthError.invalid_request);

runtime/service/src/main/java/org/apache/polaris/service/config/ServiceProducers.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ public TokenBroker tokenBroker(
353353
config.type() == AuthenticationType.EXTERNAL ? "none" : config.tokenBroker().type();
354354
TokenBrokerFactory tokenBrokerFactory =
355355
tokenBrokerFactories.select(Identifier.Literal.of(type)).get();
356-
return tokenBrokerFactory.apply(realmContext);
356+
return tokenBrokerFactory.newTokenBroker(realmContext);
357357
}
358358

359359
// other beans

runtime/service/src/test/java/org/apache/polaris/service/auth/internal/broker/JWTSymmetricKeyGeneratorTest.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import com.auth0.jwt.JWTVerifier;
2525
import com.auth0.jwt.algorithms.Algorithm;
2626
import com.auth0.jwt.interfaces.DecodedJWT;
27-
import org.apache.polaris.core.PolarisCallContext;
2827
import org.apache.polaris.core.entity.PolarisBaseEntity;
2928
import org.apache.polaris.core.entity.PolarisEntitySubType;
3029
import org.apache.polaris.core.entity.PolarisEntityType;
@@ -41,7 +40,6 @@ public class JWTSymmetricKeyGeneratorTest {
4140
/** Sanity test to verify that we can generate a token */
4241
@Test
4342
public void testJWTSymmetricKeyGenerator() {
44-
PolarisCallContext polarisCallContext = new PolarisCallContext(null, null);
4543
PolarisMetaStoreManager metastoreManager = Mockito.mock(PolarisMetaStoreManager.class);
4644
String mainSecret = "test_secret";
4745
String clientId = "test_client_id";
@@ -59,14 +57,14 @@ public void testJWTSymmetricKeyGenerator() {
5957
"principal");
6058
Mockito.when(metastoreManager.loadEntity(0L, 1L, PolarisEntityType.PRINCIPAL))
6159
.thenReturn(new EntityResult(principal));
62-
TokenBroker generator = new SymmetricKeyJWTBroker(metastoreManager, 666, () -> "polaris");
60+
TokenBroker generator = new SymmetricKeyJWTBroker(666, () -> "polaris");
6361
TokenResponse token =
6462
generator.generateFromClientSecrets(
6563
clientId,
6664
mainSecret,
6765
TokenRequestValidator.CLIENT_CREDENTIALS,
6866
"PRINCIPAL_ROLE:TEST",
69-
polarisCallContext,
67+
metastoreManager,
7068
TokenType.ACCESS_TOKEN);
7169
assertThat(token).isNotNull();
7270

0 commit comments

Comments
 (0)