Skip to content
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

Add property assumeMinServerVersion for jdbc postgresql passwordless connections #32304

Merged
merged 3 commits into from
Nov 24, 2022
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 @@ -15,6 +15,8 @@ private JdbcPropertyConstants() {
public static final String POSTGRESQL_PROPERTY_NAME_SSL_MODE = "sslmode";
public static final String POSTGRESQL_PROPERTY_VALUE_SSL_MODE = "require";
public static final String POSTGRESQL_PROPERTY_NAME_APPLICATION_NAME = "ApplicationName";
public static final String POSTGRESQL_PROPERTY_NAME_ASSUME_MIN_SERVER_VERSION = "assumeMinServerVersion";
public static final String POSTGRESQL_PROPERTY_VALUE_ASSUME_MIN_SERVER_VERSION = "9.0.0";
saragluna marked this conversation as resolved.
Show resolved Hide resolved

public static final String MYSQL_DRIVER_CLASS_NAME = "com.mysql.cj.jdbc.Driver";
public static final String MYSQL_AUTH_PLUGIN_CLASS_NAME = "com.azure.identity.providers.mysql.AzureIdentityMysqlAuthenticationPlugin";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import static com.azure.spring.cloud.autoconfigure.implementation.jdbc.JdbcPropertyConstants.MYSQL_PROPERTY_CONNECTION_ATTRIBUTES_KV_DELIMITER;
import static com.azure.spring.cloud.autoconfigure.implementation.jdbc.JdbcPropertyConstants.MYSQL_PROPERTY_NAME_CONNECTION_ATTRIBUTES;
import static com.azure.spring.cloud.autoconfigure.implementation.jdbc.JdbcPropertyConstants.POSTGRESQL_PROPERTY_NAME_APPLICATION_NAME;
import static com.azure.spring.cloud.autoconfigure.implementation.jdbc.JdbcPropertyConstants.POSTGRESQL_PROPERTY_NAME_ASSUME_MIN_SERVER_VERSION;
import static com.azure.spring.cloud.autoconfigure.implementation.jdbc.JdbcPropertyConstants.POSTGRESQL_PROPERTY_VALUE_ASSUME_MIN_SERVER_VERSION;
import static com.azure.spring.cloud.core.implementation.util.AzurePropertiesUtils.copyPropertiesIgnoreTargetNonNull;
import static com.azure.spring.cloud.service.implementation.identity.credential.provider.SpringTokenCredentialProvider.PASSWORDLESS_TOKEN_CREDENTIAL_BEAN_NAME;

Expand Down Expand Up @@ -114,6 +116,14 @@ private void enhanceUserAgent(DatabaseType databaseType, JdbcConnectionStringEnh
Map<String, String> enhancedProperties = new HashMap<>();
enhancedProperties.put(POSTGRESQL_PROPERTY_NAME_APPLICATION_NAME,
AzureSpringIdentifier.AZURE_SPRING_POSTGRESQL_OAUTH);
// Set property assumeMinServerVersion with value "9.0.0" here for the following reasons:
// 1. We need to set application_name in paramList to build connections with postgresql server, in order to do that, the number of assumeVersion must >= 9.0.0.
// https://github.com/pgjdbc/pgjdbc/blob/98c04a0c903e90f2d5d10a09baf1f753747b2556/pgjdbc/src/main/java/org/postgresql/core/v3/ConnectionFactoryImpl.java#L360
// 2. The minimum supported version of Azure postgresql, both single server and flexible server, is greater than 9.0.0.
// https://learn.microsoft.com/azure/postgresql/single-server/concepts-supported-versions
// https://learn.microsoft.com/azure/postgresql/flexible-server/concepts-supported-versions
enhancedProperties.put(POSTGRESQL_PROPERTY_NAME_ASSUME_MIN_SERVER_VERSION,
POSTGRESQL_PROPERTY_VALUE_ASSUME_MIN_SERVER_VERSION);
saragluna marked this conversation as resolved.
Show resolved Hide resolved
enhancer.enhanceProperties(enhancedProperties, true);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import org.springframework.mock.env.MockEnvironment;

import static com.azure.spring.cloud.autoconfigure.implementation.jdbc.JdbcConnectionStringUtils.enhanceJdbcUrl;
import static com.azure.spring.cloud.autoconfigure.implementation.jdbc.JdbcPropertyConstants.POSTGRESQL_PROPERTY_NAME_ASSUME_MIN_SERVER_VERSION;
import static com.azure.spring.cloud.autoconfigure.implementation.jdbc.JdbcPropertyConstants.POSTGRESQL_PROPERTY_VALUE_ASSUME_MIN_SERVER_VERSION;
import static com.azure.spring.cloud.autoconfigure.jdbc.MySqlAzureJdbcAutoConfigurationTest.MYSQL_USER_AGENT;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
Expand All @@ -33,6 +35,8 @@ class JdbcPropertiesBeanPostProcessorTest {
private static final String POSTGRESQL_CONNECTION_STRING = "jdbc:postgresql://host/database?enableSwitch1&property1=value1";
private static final String PASSWORD = "password";
private static final String US_AUTHORITY_HOST_STRING = AuthProperty.AUTHORITY_HOST.getPropertyKey() + "=" + "https://login.microsoftonline.us/";
private static final String POSTGRESQL_ASSUME_MIN_SERVER_VERSION = POSTGRESQL_PROPERTY_NAME_ASSUME_MIN_SERVER_VERSION + "="
+ POSTGRESQL_PROPERTY_VALUE_ASSUME_MIN_SERVER_VERSION;

private MockEnvironment mockEnvironment;

Expand Down Expand Up @@ -194,7 +198,8 @@ void postgreSqlUserAgentShouldConfigureIfNonApplicationNameProvided() {
DatabaseType.POSTGRESQL,
baseUrl,
AuthProperty.TOKEN_CREDENTIAL_PROVIDER_CLASS_NAME.getPropertyKey() + "=" + SpringTokenCredentialProvider.class.getName(),
APPLICATION_NAME.getName() + "=" + AzureSpringIdentifier.AZURE_SPRING_POSTGRESQL_OAUTH
APPLICATION_NAME.getName() + "=" + AzureSpringIdentifier.AZURE_SPRING_POSTGRESQL_OAUTH,
POSTGRESQL_ASSUME_MIN_SERVER_VERSION
);

assertEquals(expectedJdbcUrl, dataSourceProperties.getUrl());
Expand All @@ -214,7 +219,8 @@ void postgreSqlUserAgentShouldNotConfigureIfApplicationNameExists() {
String expectedJdbcUrl = enhanceJdbcUrl(
DatabaseType.POSTGRESQL,
baseUrl,
AuthProperty.TOKEN_CREDENTIAL_PROVIDER_CLASS_NAME.getPropertyKey() + "=" + SpringTokenCredentialProvider.class.getName()
AuthProperty.TOKEN_CREDENTIAL_PROVIDER_CLASS_NAME.getPropertyKey() + "=" + SpringTokenCredentialProvider.class.getName(),
POSTGRESQL_ASSUME_MIN_SERVER_VERSION
);

assertEquals(expectedJdbcUrl, dataSourceProperties.getUrl());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
import org.springframework.boot.test.context.FilteredClassLoader;

import static com.azure.spring.cloud.autoconfigure.implementation.jdbc.JdbcPropertyConstants.POSTGRESQL_PROPERTY_NAME_APPLICATION_NAME;
import static com.azure.spring.cloud.autoconfigure.implementation.jdbc.JdbcPropertyConstants.POSTGRESQL_PROPERTY_NAME_ASSUME_MIN_SERVER_VERSION;
import static com.azure.spring.cloud.autoconfigure.implementation.jdbc.JdbcPropertyConstants.POSTGRESQL_PROPERTY_NAME_AUTHENTICATION_PLUGIN_CLASSNAME;
import static com.azure.spring.cloud.autoconfigure.implementation.jdbc.JdbcPropertyConstants.POSTGRESQL_PROPERTY_NAME_SSL_MODE;
import static com.azure.spring.cloud.autoconfigure.implementation.jdbc.JdbcPropertyConstants.POSTGRESQL_PROPERTY_VALUE_ASSUME_MIN_SERVER_VERSION;
import static com.azure.spring.cloud.autoconfigure.implementation.jdbc.JdbcPropertyConstants.POSTGRESQL_PROPERTY_VALUE_SSL_MODE;
import static com.azure.spring.cloud.autoconfigure.implementation.jdbc.JdbcPropertyConstants.POSTGRES_AUTH_PLUGIN_CLASS_NAME;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand All @@ -30,7 +32,8 @@ class PostgreSqlAzureJdbcAutoConfigurationTest extends AbstractAzureJdbcAutoConf

private static final String POSTGRESQL_USER_AGENT = POSTGRESQL_PROPERTY_NAME_APPLICATION_NAME + "="
+ AzureSpringIdentifier.AZURE_SPRING_POSTGRESQL_OAUTH;

private static final String POSTGRESQL_ASSUME_MIN_SERVER_VERSION = POSTGRESQL_PROPERTY_NAME_ASSUME_MIN_SERVER_VERSION + "="
+ POSTGRESQL_PROPERTY_VALUE_ASSUME_MIN_SERVER_VERSION;

@Override
void pluginNotOnClassPath() {
Expand Down Expand Up @@ -71,7 +74,8 @@ void enhanceUrlWithDefaultCredential() {
connectionString,
PUBLIC_AUTHORITY_HOST_STRING,
POSTGRESQL_USER_AGENT,
AUTHPROPERTY_TOKENCREDENTIALPROVIDERCLASSNAME_PROPERTY
AUTHPROPERTY_TOKENCREDENTIALPROVIDERCLASSNAME_PROPERTY,
POSTGRESQL_ASSUME_MIN_SERVER_VERSION
);
assertEquals(expectedUrl, dataSourceProperties.getUrl());
});
Expand All @@ -95,7 +99,8 @@ void enhanceUrlWithCustomCredential() {
PUBLIC_AUTHORITY_HOST_STRING,
AUTHPROPERTY_CREDENTIAL_BEAN_NAME,
AUTHPROPERTY_TOKENCREDENTIALPROVIDERCLASSNAME_PROPERTY,
POSTGRESQL_USER_AGENT
POSTGRESQL_USER_AGENT,
POSTGRESQL_ASSUME_MIN_SERVER_VERSION
);
assertEquals(expectedUrl, dataSourceProperties.getUrl());
});
Expand Down