Skip to content

Improve Azure App Configuration error handling #196

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,14 @@ public Resource<TokenCredential> request(ParameterSet parameterSet) {
* used are configured by the parameters of the given {@code parameters}.
* Supported parameters are defined by the class variables in
* {@link TokenCredentialFactory}.
* @param parameters parameters that configure credentials. Not null.
* @param parameterSet parameters that configure credentials. Not null.
* @return Credentials configured by parameters
*/
private static TokenCredential getCredential(ParameterSet parameterSet) {

AzureAuthenticationMethod authenticationMethod =
parameterSet.getRequired(AUTHENTICATION_METHOD);
parameterSet.contains(AUTHENTICATION_METHOD)
? parameterSet.getRequired(AUTHENTICATION_METHOD) : AzureAuthenticationMethod.DEFAULT;

switch (authenticationMethod) {
case DEFAULT:
Expand Down Expand Up @@ -227,7 +228,7 @@ private static InteractiveBrowserCredential interactiveCredentials(
* the Azure SDK's
* {@link com.azure.identity.EnvironmentCredential} class.
* </p>
* @param parameterSet Optional parameters. Not null.
* @param parameters Optional parameters. Not null.
* @return Credentials for a service principal. Not null.
* @throws IllegalStateException If a required parameter is not configured.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.sql.SQLException;
import java.util.Objects;
import java.util.Properties;

Expand Down Expand Up @@ -110,37 +112,42 @@ public class AzureAppConfigurationProvider
* Configuration
*/
@Override
public Properties getConnectionProperties(String location) {
public Properties getConnectionProperties(String location) throws SQLException {
// If the location was already consulted, re-use the properties
Properties cachedProp = CACHE.get(location);
if (Objects.nonNull(cachedProp)) {
return cachedProp;
}

Properties properties = getRemoteProperties(location);
if (properties.containsKey(CONFIG_TTL_JSON_OBJECT_NAME)) {
// Remove the TTL information from the properties, if presents
long configTimeToLive = Long.parseLong(
properties.getProperty(CONFIG_TTL_JSON_OBJECT_NAME));

properties.remove(CONFIG_TTL_JSON_OBJECT_NAME);

CACHE.put(
location,
properties,
configTimeToLive,
() -> this.refreshProperties(location),
MS_REFRESH_TIMEOUT,
MS_RETRY_INTERVAL);
} else {
CACHE.put(location,
properties,
() -> this.refreshProperties(location),
MS_REFRESH_TIMEOUT,
MS_RETRY_INTERVAL);
}
try {
Properties properties = getRemoteProperties(location);
if (properties.containsKey(CONFIG_TTL_JSON_OBJECT_NAME)) {
// Remove the TTL information from the properties, if presents
long configTimeToLive = Long.parseLong(
properties.getProperty(CONFIG_TTL_JSON_OBJECT_NAME));

properties.remove(CONFIG_TTL_JSON_OBJECT_NAME);

CACHE.put(
location,
properties,
configTimeToLive,
() -> this.refreshProperties(location),
MS_REFRESH_TIMEOUT,
MS_RETRY_INTERVAL);
} else {
CACHE.put(location,
properties,
() -> this.refreshProperties(location),
MS_REFRESH_TIMEOUT,
MS_RETRY_INTERVAL);
}

return properties;
return properties;
} catch (UncheckedIOException | AzureException e) {
throw new SQLException("Unable to load Azure App Configuration at " + location, e
);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@ public void testCachePurged() throws SQLException {
}
}

@Test
public void testInvalidConfiguration() {
String invalidUrl = "jdbc:oracle:thin:@config-azure://invalid-config";
assertThrows(SQLException.class, () -> tryConnection(invalidUrl), "Should throw an SQLException");
}

/**
* Helper function: try to get connection form specified url
*/
Expand Down