Skip to content

Commit ef3d859

Browse files
Improve Azure App Configuration error handling (#196)
1 parent b12a688 commit ef3d859

File tree

3 files changed

+41
-27
lines changed

3 files changed

+41
-27
lines changed

ojdbc-provider-azure/src/main/java/oracle/jdbc/provider/azure/authentication/TokenCredentialFactory.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,14 @@ public Resource<TokenCredential> request(ParameterSet parameterSet) {
139139
* used are configured by the parameters of the given {@code parameters}.
140140
* Supported parameters are defined by the class variables in
141141
* {@link TokenCredentialFactory}.
142-
* @param parameters parameters that configure credentials. Not null.
142+
* @param parameterSet parameters that configure credentials. Not null.
143143
* @return Credentials configured by parameters
144144
*/
145145
private static TokenCredential getCredential(ParameterSet parameterSet) {
146146

147147
AzureAuthenticationMethod authenticationMethod =
148-
parameterSet.getRequired(AUTHENTICATION_METHOD);
148+
parameterSet.contains(AUTHENTICATION_METHOD)
149+
? parameterSet.getRequired(AUTHENTICATION_METHOD) : AzureAuthenticationMethod.DEFAULT;
149150

150151
switch (authenticationMethod) {
151152
case DEFAULT:
@@ -227,7 +228,7 @@ private static InteractiveBrowserCredential interactiveCredentials(
227228
* the Azure SDK's
228229
* {@link com.azure.identity.EnvironmentCredential} class.
229230
* </p>
230-
* @param parameterSet Optional parameters. Not null.
231+
* @param parameters Optional parameters. Not null.
231232
* @return Credentials for a service principal. Not null.
232233
* @throws IllegalStateException If a required parameter is not configured.
233234
*/

ojdbc-provider-azure/src/main/java/oracle/jdbc/provider/azure/configuration/AzureAppConfigurationProvider.java

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@
5858

5959
import java.io.ByteArrayInputStream;
6060
import java.io.InputStream;
61+
import java.io.UncheckedIOException;
62+
import java.sql.SQLException;
6163
import java.util.Objects;
6264
import java.util.Properties;
6365

@@ -110,37 +112,42 @@ public class AzureAppConfigurationProvider
110112
* Configuration
111113
*/
112114
@Override
113-
public Properties getConnectionProperties(String location) {
115+
public Properties getConnectionProperties(String location) throws SQLException {
114116
// If the location was already consulted, re-use the properties
115117
Properties cachedProp = CACHE.get(location);
116118
if (Objects.nonNull(cachedProp)) {
117119
return cachedProp;
118120
}
119121

120-
Properties properties = getRemoteProperties(location);
121-
if (properties.containsKey(CONFIG_TTL_JSON_OBJECT_NAME)) {
122-
// Remove the TTL information from the properties, if presents
123-
long configTimeToLive = Long.parseLong(
124-
properties.getProperty(CONFIG_TTL_JSON_OBJECT_NAME));
125-
126-
properties.remove(CONFIG_TTL_JSON_OBJECT_NAME);
127-
128-
CACHE.put(
129-
location,
130-
properties,
131-
configTimeToLive,
132-
() -> this.refreshProperties(location),
133-
MS_REFRESH_TIMEOUT,
134-
MS_RETRY_INTERVAL);
135-
} else {
136-
CACHE.put(location,
137-
properties,
138-
() -> this.refreshProperties(location),
139-
MS_REFRESH_TIMEOUT,
140-
MS_RETRY_INTERVAL);
141-
}
122+
try {
123+
Properties properties = getRemoteProperties(location);
124+
if (properties.containsKey(CONFIG_TTL_JSON_OBJECT_NAME)) {
125+
// Remove the TTL information from the properties, if presents
126+
long configTimeToLive = Long.parseLong(
127+
properties.getProperty(CONFIG_TTL_JSON_OBJECT_NAME));
128+
129+
properties.remove(CONFIG_TTL_JSON_OBJECT_NAME);
130+
131+
CACHE.put(
132+
location,
133+
properties,
134+
configTimeToLive,
135+
() -> this.refreshProperties(location),
136+
MS_REFRESH_TIMEOUT,
137+
MS_RETRY_INTERVAL);
138+
} else {
139+
CACHE.put(location,
140+
properties,
141+
() -> this.refreshProperties(location),
142+
MS_REFRESH_TIMEOUT,
143+
MS_RETRY_INTERVAL);
144+
}
142145

143-
return properties;
146+
return properties;
147+
} catch (UncheckedIOException | AzureException e) {
148+
throw new SQLException("Unable to load Azure App Configuration at " + location, e
149+
);
150+
}
144151
}
145152

146153
/**

ojdbc-provider-azure/src/test/java/oracle/jdbc/provider/azure/configuration/AzureAppConfigurationProviderTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,12 @@ public void testCachePurged() throws SQLException {
146146
}
147147
}
148148

149+
@Test
150+
public void testInvalidConfiguration() {
151+
String invalidUrl = "jdbc:oracle:thin:@config-azure://invalid-config";
152+
assertThrows(SQLException.class, () -> tryConnection(invalidUrl), "Should throw an SQLException");
153+
}
154+
149155
/**
150156
* Helper function: try to get connection form specified url
151157
*/

0 commit comments

Comments
 (0)