Skip to content

Commit 6fed971

Browse files
committed
Use try catch block to catch any issues during mutiplexing
1 parent b882865 commit 6fed971

File tree

2 files changed

+19
-26
lines changed

2 files changed

+19
-26
lines changed

runtime/service/src/main/java/org/apache/polaris/service/credentials/DefaultPolarisCredentialManager.java

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -78,32 +78,24 @@ public RealmContext getRealmContext() {
7878
public @Nonnull ConnectionCredentials getConnectionCredentials(
7979
@Nonnull ConnectionConfigInfoDpo connectionConfig) {
8080

81-
// Select the appropriate vendor based on authentication type
8281
AuthenticationType authType =
8382
connectionConfig.getAuthenticationParameters().getAuthenticationType();
84-
Instance<ConnectionCredentialVendor> selectedVendor =
85-
credentialVendors.select(AuthType.Literal.of(authType));
8683

87-
if (selectedVendor.isUnsatisfied()) {
88-
// TODO: Add credential vendors for other authentication types
89-
LOGGER.warn("No connection credential vendor found for authentication type: {}", authType);
90-
return ConnectionCredentials.builder().build();
91-
}
92-
93-
if (selectedVendor.isAmbiguous()) {
94-
LOGGER.error(
95-
"Multiple connection credential vendors found for authentication type: {}. "
96-
+ "Use @Priority to specify which vendor should be used. "
97-
+ "Higher priority values take precedence.",
98-
authType);
84+
// Use CDI to select the appropriate vendor based on the authentication type
85+
ConnectionCredentialVendor selectedVendor;
86+
try {
87+
selectedVendor = credentialVendors.select(AuthType.Literal.of(authType)).get();
88+
} catch (jakarta.enterprise.inject.UnsatisfiedResolutionException e) {
89+
// No vendor registered for this authentication type
90+
throw new IllegalStateException(
91+
"No credential vendor available for authentication type: " + authType, e);
92+
} catch (jakarta.enterprise.inject.AmbiguousResolutionException e) {
93+
// Multiple vendors found - need @Priority to disambiguate
9994
throw new IllegalStateException(
100-
String.format(
101-
"Ambiguous connection credential vendor for authentication type: %s. "
102-
+ "Multiple implementations found without @Priority annotation.",
103-
authType));
95+
"Ambiguous connection credential vendor for authentication type: " + authType, e);
10496
}
10597

106-
// Delegate to the vendor to generate credentials
107-
return selectedVendor.get().getConnectionCredentials(connectionConfig);
98+
// Delegate credential generation to the selected vendor
99+
return selectedVendor.getConnectionCredentials(connectionConfig);
108100
}
109101
}

runtime/service/src/test/java/org/apache/polaris/service/credentials/DefaultPolarisCredentialManagerTest.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,17 +170,18 @@ public void testDelegatesToOAuthVendor() {
170170
}
171171

172172
@Test
173-
public void testUnsupportedAuthTypeReturnsEmpty() {
173+
public void testUnsupportedAuthTypeThrowsException() {
174174
// Use a mock connection config with an unsupported authentication type
175175
ConnectionConfigInfoDpo mockConfig = Mockito.mock(ConnectionConfigInfoDpo.class);
176176
AuthenticationParametersDpo mockAuthParams = Mockito.mock(AuthenticationParametersDpo.class);
177177

178178
when(mockConfig.getAuthenticationParameters()).thenReturn(mockAuthParams);
179179
when(mockAuthParams.getAuthenticationType()).thenReturn(AuthenticationType.NULL_TYPE);
180180

181-
// Should return empty credentials since no vendor supports NULL_TYPE
182-
ConnectionCredentials credentials = credentialManager.getConnectionCredentials(mockConfig);
183-
184-
Assertions.assertThat(credentials.credentials()).isEmpty();
181+
// Should throw IllegalStateException since no vendor supports NULL_TYPE
182+
Assertions.assertThatThrownBy(() -> credentialManager.getConnectionCredentials(mockConfig))
183+
.isInstanceOf(IllegalStateException.class)
184+
.hasMessageContaining("No credential vendor available for authentication type: NULL_TYPE")
185+
.hasCauseInstanceOf(jakarta.enterprise.inject.UnsatisfiedResolutionException.class);
185186
}
186187
}

0 commit comments

Comments
 (0)