Merge pull request #222 from bijukunjummen/GH-213

Supports user info with special uri chars "%, :, +"
This commit is contained in:
Scott Frederick
2018-05-09 15:20:10 -05:00
committed by GitHub
11 changed files with 214 additions and 11 deletions

View File

@@ -2,6 +2,7 @@ package org.springframework.cloud.cloudfoundry;
import org.springframework.cloud.service.ServiceInfo;
import org.springframework.cloud.service.common.RelationalServiceInfo;
import org.springframework.cloud.util.UriInfo;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.junit.Assert.assertEquals;
@@ -19,8 +20,8 @@ public abstract class AbstractCloudFoundryConnectorRelationalServiceTest extends
payload = payload.replace("$serviceName", serviceName);
payload = payload.replace("$hostname", hostname);
payload = payload.replace("$port", Integer.toString(port));
payload = payload.replace("$user", user);
payload = payload.replace("$password", password);
payload = payload.replace("$user", UriInfo.urlEncode(user));
payload = payload.replace("$password", UriInfo.urlEncode(password));
payload = payload.replace("$name", name);
return payload;

View File

@@ -2,11 +2,14 @@ package org.springframework.cloud.cloudfoundry;
import org.junit.Test;
import org.springframework.cloud.service.ServiceInfo;
import org.springframework.cloud.service.common.MysqlServiceInfo;
import org.springframework.cloud.service.common.DB2ServiceInfo;
import org.springframework.cloud.service.common.MysqlServiceInfo;
import org.springframework.cloud.service.common.RelationalServiceInfo;
import org.springframework.cloud.util.UriInfo;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.when;
@@ -31,6 +34,22 @@ public class CloudFoundryConnectorDB2ServiceTest extends AbstractUserProvidedSer
assertUriBasedServiceInfoFields(info, DB2_SCHEME, hostname, port, username, password, INSTANCE_NAME);
}
@Test
public void db2ServiceCreationWithSpecialChars() {
String userWithSpecialChars = "u%u:u+";
String passwordWithSpecialChars = "p%p:p+";
when(mockEnvironment.getEnvValue("VCAP_SERVICES"))
.thenReturn(getServicesPayload(
getUserProvidedServicePayload(SERVICE_NAME, hostname, port, userWithSpecialChars,
passwordWithSpecialChars, INSTANCE_NAME, DB2_SCHEME + ":")));
List<ServiceInfo> serviceInfos = testCloudConnector.getServiceInfos();
ServiceInfo info = getServiceInfo(serviceInfos, SERVICE_NAME);
assertServiceFoundOfType(info, DB2ServiceInfo.class);
assertEquals(getJdbcUrl("db2", hostname, port, INSTANCE_NAME, userWithSpecialChars, passwordWithSpecialChars), ((RelationalServiceInfo) info).getJdbcUrl());
assertUriBasedServiceInfoFields(info, DB2_SCHEME, hostname, port, userWithSpecialChars, passwordWithSpecialChars, INSTANCE_NAME);
}
@Test
public void db2ServiceCreationWithNoUri() {
when(mockEnvironment.getEnvValue("VCAP_SERVICES"))
@@ -65,6 +84,12 @@ public class CloudFoundryConnectorDB2ServiceTest extends AbstractUserProvidedSer
}
protected String getJdbcUrl(String scheme, String name) {
return String.format("%s%s://%s:%d/%s:user=%s;password=%s;", JDBC_PREFIX, scheme, hostname, port, name, username, password);
return String.format("%s%s://%s:%d/%s:user=%s;password=%s;", JDBC_PREFIX, scheme, hostname, port, name,
UriInfo.urlEncode(username), UriInfo.urlEncode(password));
}
private String getJdbcUrl(String scheme, String hostname, int port, String name, String user, String password) {
return String.format("%s%s://%s:%d/%s:user=%s;password=%s;", JDBC_PREFIX, scheme, hostname, port, name,
UriInfo.urlEncode(user), UriInfo.urlEncode(password));
}
}

View File

@@ -1,5 +1,6 @@
package org.springframework.cloud.cloudfoundry;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;
import static org.springframework.cloud.service.common.MysqlServiceInfo.MYSQL_SCHEME;
@@ -8,6 +9,8 @@ import java.util.List;
import org.junit.Test;
import org.springframework.cloud.service.ServiceInfo;
import org.springframework.cloud.service.common.MysqlServiceInfo;
import org.springframework.cloud.service.common.RelationalServiceInfo;
import org.springframework.cloud.util.UriInfo;
/**
*
@@ -81,6 +84,27 @@ public class CloudFoundryConnectorMysqlServiceTest extends AbstractCloudFoundryC
assertUriBasedServiceInfoFields(info2, MYSQL_SCHEME, hostname, port, username, password, name2);
}
@Test
public void mysqlServiceCreationNoLabelNoTagsWithSpecialChars() {
String name = "database";
String userWithSpecialChars = "u%u:u+";
String passwordWithSpecialChars = "p%p:p+";
when(mockEnvironment.getEnvValue("VCAP_SERVICES"))
.thenReturn(getServicesPayload(
getMysqlServicePayloadNoLabelNoTags("mysql", hostname, port, userWithSpecialChars, passwordWithSpecialChars, name)));
List<ServiceInfo> serviceInfos = testCloudConnector.getServiceInfos();
ServiceInfo info = getServiceInfo(serviceInfos, "mysql");
assertServiceFoundOfType(info, MysqlServiceInfo.class);
assertEquals(getJdbcUrl(hostname, port, name, userWithSpecialChars, passwordWithSpecialChars),
((RelationalServiceInfo)info).getJdbcUrl());
assertUriBasedServiceInfoFields(info, MYSQL_SCHEME, hostname, port, userWithSpecialChars, passwordWithSpecialChars, name);
}
@Test
public void mysqlServiceCreationWithLabelNoUri() {
String name1 = "database-1";
@@ -127,6 +151,26 @@ public class CloudFoundryConnectorMysqlServiceTest extends AbstractCloudFoundryC
assertUriBasedServiceInfoFields(info2, MYSQL_SCHEME, hostname, port, username, password, name2);
}
@Test
public void mysqlServiceCreationWithJdbcUrlAndSpecialChars() {
String userWithSpecialChars = "u%u:u+";
String passwordWithSpecialChars = "p%p:p+";
String name = "database";
when(mockEnvironment.getEnvValue("VCAP_SERVICES"))
.thenReturn(getServicesPayload(
getMysqlServicePayloadWithJdbcUrl("mysql", hostname, port, userWithSpecialChars, passwordWithSpecialChars, name)));
List<ServiceInfo> serviceInfos = testCloudConnector.getServiceInfos();
ServiceInfo info = getServiceInfo(serviceInfos, "mysql");
assertServiceFoundOfType(info, MysqlServiceInfo.class);
assertEquals(getJdbcUrl(hostname, port, name, userWithSpecialChars, passwordWithSpecialChars),
((RelationalServiceInfo)info).getJdbcUrl());
assertUriBasedServiceInfoFields(info, MYSQL_SCHEME, hostname, port, userWithSpecialChars, passwordWithSpecialChars, name);
}
@Test
public void mysqlServiceCreationWithJdbcUrlOnly() {
String name1 = "database-1";
@@ -153,6 +197,25 @@ public class CloudFoundryConnectorMysqlServiceTest extends AbstractCloudFoundryC
assertJdbcShemeSpecificPartEqual(info2, MYSQL_SCHEME, name2);
}
@Test
public void mysqlServiceCreationWithJdbcUrlOnlyWithSpecialChars() {
String name = "database";
String userWithSpecialChars = "u%u:u+";
String passwordWithSpecialChars = "p%p:p+";
when(mockEnvironment.getEnvValue("VCAP_SERVICES"))
.thenReturn(getServicesPayload(
getMysqlServicePayloadWithJdbcUrlOnly("mysql", hostname, port, userWithSpecialChars, passwordWithSpecialChars, name)));
List<ServiceInfo> serviceInfos = testCloudConnector.getServiceInfos();
ServiceInfo info = getServiceInfo(serviceInfos, "mysql");
assertServiceFoundOfType(info, MysqlServiceInfo.class);
assertEquals(getJdbcUrl(hostname, port, name, userWithSpecialChars, passwordWithSpecialChars),
((RelationalServiceInfo)info).getJdbcUrl());
}
private String getMysqlServicePayload(String serviceName,
String hostname, int port,
String user, String password, String name) {
@@ -194,4 +257,10 @@ public class CloudFoundryConnectorMysqlServiceTest extends AbstractCloudFoundryC
return getRelationalPayload("test-mysql-info-jdbc-url-only.json", serviceName,
hostname, port, user, password, name);
}
private String getJdbcUrl(String hostname, int port, String name, String user,
String password) {
return String.format("jdbc:mysql://%s:%d/%s?user=%s&password=%s", hostname, port,
name, UriInfo.urlEncode(user), UriInfo.urlEncode(password));
}
}

View File

@@ -2,11 +2,13 @@ package org.springframework.cloud.cloudfoundry;
import org.junit.Test;
import org.springframework.cloud.service.ServiceInfo;
import org.springframework.cloud.service.common.MysqlServiceInfo;
import org.springframework.cloud.service.common.OracleServiceInfo;
import org.springframework.cloud.service.common.RelationalServiceInfo;
import org.springframework.cloud.util.UriInfo;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.when;
@@ -31,6 +33,21 @@ public class CloudFoundryConnectorOracleServiceTest extends AbstractUserProvided
assertUriBasedServiceInfoFields(info, ORACLE_SCHEME, hostname, port, username, password, INSTANCE_NAME);
}
@Test
public void oracleServiceCreationWithSpecialChars() {
String userWithSpecialChars = "u%u:u+";
String passwordWithSpecialChars = "p%p:p+";
when(mockEnvironment.getEnvValue("VCAP_SERVICES"))
.thenReturn(getServicesPayload(
getUserProvidedServicePayload(SERVICE_NAME, hostname, port, userWithSpecialChars, passwordWithSpecialChars, INSTANCE_NAME, ORACLE_SCHEME + ":")));
List<ServiceInfo> serviceInfos = testCloudConnector.getServiceInfos();
ServiceInfo info = getServiceInfo(serviceInfos, SERVICE_NAME);
assertServiceFoundOfType(info, OracleServiceInfo.class);
assertEquals(getJdbcUrl(hostname, port, INSTANCE_NAME, userWithSpecialChars, passwordWithSpecialChars), ((RelationalServiceInfo)info).getJdbcUrl());
assertUriBasedServiceInfoFields(info, ORACLE_SCHEME, hostname, port, userWithSpecialChars, passwordWithSpecialChars, INSTANCE_NAME);
}
@Test
public void oracleServiceCreationWithNoUri() {
when(mockEnvironment.getEnvValue("VCAP_SERVICES"))
@@ -40,7 +57,7 @@ public class CloudFoundryConnectorOracleServiceTest extends AbstractUserProvided
ServiceInfo info = getServiceInfo(serviceInfos, SERVICE_NAME);
assertNotNull(info);
assertFalse(MysqlServiceInfo.class.isAssignableFrom(info.getClass())); // service was not detected as MySQL
assertFalse(OracleServiceInfo.class.isAssignableFrom(info.getClass())); // service was not detected as MySQL
assertNotNull(info);
}
@@ -67,4 +84,8 @@ public class CloudFoundryConnectorOracleServiceTest extends AbstractUserProvided
protected String getJdbcUrl(String scheme, String name) {
return String.format("%s%s:thin:%s/%s@%s:%d/%s", JDBC_PREFIX, scheme, username, password, hostname, port, name);
}
private String getJdbcUrl(String hostname, int port, String name, String user, String password) {
return String.format("%s%s:thin:%s/%s@%s:%d/%s", JDBC_PREFIX, "oracle", UriInfo.urlEncode(user), UriInfo.urlEncode(password), hostname, port, name);
}
}

View File

@@ -1,5 +1,6 @@
package org.springframework.cloud.cloudfoundry;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;
import static org.springframework.cloud.service.common.PostgresqlServiceInfo.POSTGRES_SCHEME;
@@ -8,6 +9,8 @@ import java.util.List;
import org.junit.Test;
import org.springframework.cloud.service.ServiceInfo;
import org.springframework.cloud.service.common.PostgresqlServiceInfo;
import org.springframework.cloud.service.common.RelationalServiceInfo;
import org.springframework.cloud.util.UriInfo;
/**
* @author Ramnivas Laddad
@@ -37,6 +40,29 @@ public class CloudFoundryConnectorPostgresqlServiceTest extends AbstractCloudFou
assertUriBasedServiceInfoFields(info2, POSTGRES_SCHEME, hostname, port, username, password, name2);
}
@Test
public void postgresqlWithSpecialCharsServiceCreation() {
String userWithSpecialChars = "u%u:u+";
String passwordWithSpecialChars = "p%p:p+";
String name1 = "database-1";
when(mockEnvironment.getEnvValue("VCAP_SERVICES")).thenReturn(
getServicesPayload(getPostgresqlServicePayload("postgresql-1", hostname,
port, userWithSpecialChars, passwordWithSpecialChars, name1)));
List<ServiceInfo> serviceInfos = testCloudConnector.getServiceInfos();
ServiceInfo info1 = getServiceInfo(serviceInfos, "postgresql-1");
assertServiceFoundOfType(info1, PostgresqlServiceInfo.class);
assertEquals(getJdbcUrl(hostname, port, userWithSpecialChars,
passwordWithSpecialChars, name1),
((RelationalServiceInfo) info1).getJdbcUrl());
assertUriBasedServiceInfoFields(info1, POSTGRES_SCHEME, hostname, port,
userWithSpecialChars, passwordWithSpecialChars, name1);
}
@Test
public void postgresqlServiceCreationNoLabelNoTags() {
String name1 = "database-1";
@@ -104,4 +130,9 @@ public class CloudFoundryConnectorPostgresqlServiceTest extends AbstractCloudFou
hostname, port, user, password, name);
}
private String getJdbcUrl(String hostname, int port, String user, String password,
String name) {
return String.format("jdbc:postgresql://%s:%d/%s?user=%s&password=%s", hostname,
port, name, UriInfo.urlEncode(user), UriInfo.urlEncode(password));
}
}

View File

@@ -2,10 +2,13 @@ package org.springframework.cloud.cloudfoundry;
import org.junit.Test;
import org.springframework.cloud.service.ServiceInfo;
import org.springframework.cloud.service.common.RelationalServiceInfo;
import org.springframework.cloud.service.common.SqlServerServiceInfo;
import org.springframework.cloud.util.UriInfo;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.when;
@@ -30,6 +33,23 @@ public class CloudFoundryConnectorSqlServerServiceTest extends AbstractUserProvi
assertUriBasedServiceInfoFields(info, SQLSERVER_SCHEME, hostname, port, username, password, INSTANCE_NAME);
}
@Test
public void sqlServerServiceCreationWithSpecialChars() {
String userWithSpecialChars = "u%u:u+";
String passwordWithSpecialChars = "p%p:p+";
when(mockEnvironment.getEnvValue("VCAP_SERVICES"))
.thenReturn(getServicesPayload(
getUserProvidedServicePayload(SERVICE_NAME, hostname, port, userWithSpecialChars, passwordWithSpecialChars, INSTANCE_NAME, SQLSERVER_SCHEME + ":")));
List<ServiceInfo> serviceInfos = testCloudConnector.getServiceInfos();
ServiceInfo info = getServiceInfo(serviceInfos, SERVICE_NAME);
assertServiceFoundOfType(info, SqlServerServiceInfo.class);
assertEquals(getJdbcUrl(hostname, port, SQLSERVER_SCHEME, INSTANCE_NAME,
userWithSpecialChars, passwordWithSpecialChars), ((RelationalServiceInfo)info).getJdbcUrl());
assertUriBasedServiceInfoFields(info, SQLSERVER_SCHEME, hostname, port, userWithSpecialChars, passwordWithSpecialChars, INSTANCE_NAME);
}
@Test
public void sqlServerServiceCreationWithNoUri() {
when(mockEnvironment.getEnvValue("VCAP_SERVICES"))
@@ -55,6 +75,23 @@ public class CloudFoundryConnectorSqlServerServiceTest extends AbstractUserProvi
assertUriBasedServiceInfoFields(info, SQLSERVER_SCHEME, hostname, port, username, password, INSTANCE_NAME);
}
@Test
public void sqlServerServiceCreationWithJdbcUrlAndSpecialChars() {
String userWithSpecialChars = "u%u:u+";
String passwordWithSpecialChars = "p%p:p+";
when(mockEnvironment.getEnvValue("VCAP_SERVICES"))
.thenReturn(getServicesPayload(
getSqlServerServicePayloadWithJdbcurl(SERVICE_NAME, hostname, port, userWithSpecialChars, passwordWithSpecialChars, INSTANCE_NAME, SQLSERVER_SCHEME + ":")));
List<ServiceInfo> serviceInfos = testCloudConnector.getServiceInfos();
ServiceInfo info = getServiceInfo(serviceInfos, SERVICE_NAME);
assertServiceFoundOfType(info, SqlServerServiceInfo.class);
assertEquals(getJdbcUrl(hostname, port, SQLSERVER_SCHEME, INSTANCE_NAME,
userWithSpecialChars, passwordWithSpecialChars), ((RelationalServiceInfo)info).getJdbcUrl());
assertUriBasedServiceInfoFields(info, SQLSERVER_SCHEME, hostname, port, userWithSpecialChars, passwordWithSpecialChars, INSTANCE_NAME);
}
protected String getSqlServerServicePayloadWithJdbcurl(String serviceName, String hostname, int port,
String user, String password, String name, String scheme) {
String payload = getRelationalPayload("test-sqlserver-info-jdbc-url.json", serviceName,
@@ -65,4 +102,9 @@ public class CloudFoundryConnectorSqlServerServiceTest extends AbstractUserProvi
protected String getJdbcUrl(String scheme, String name) {
return String.format("%s%s://%s:%d;database=%s;user=%s;password=%s", JDBC_PREFIX, scheme, hostname, port, name, username, password);
}
private String getJdbcUrl(String hostname, int port, String scheme, String name, String username, String password) {
return String.format("%s%s://%s:%d;database=%s;user=%s;password=%s", JDBC_PREFIX, scheme, hostname, port,
name, UriInfo.urlEncode(username), UriInfo.urlEncode(password));
}
}

View File

@@ -1,6 +1,7 @@
package org.springframework.cloud.service.common;
import org.springframework.cloud.service.ServiceInfo;
import org.springframework.cloud.util.UriInfo;
@ServiceInfo.ServiceLabel("db2")
public class DB2ServiceInfo extends RelationalServiceInfo {
@@ -18,6 +19,6 @@ public class DB2ServiceInfo extends RelationalServiceInfo {
protected String buildJdbcUrl() {
return String.format("jdbc:%s://%s:%d/%s:user=%s;password=%s;",
jdbcUrlDatabaseType,
getHost(), getPort(), getPath(), getUserName(), getPassword());
getHost(), getPort(), getPath(), UriInfo.urlEncode(getUserName()), UriInfo.urlEncode(getPassword()));
}
}

View File

@@ -1,6 +1,7 @@
package org.springframework.cloud.service.common;
import org.springframework.cloud.service.ServiceInfo;
import org.springframework.cloud.util.UriInfo;
@ServiceInfo.ServiceLabel("oracle")
public class OracleServiceInfo extends RelationalServiceInfo {
@@ -17,7 +18,7 @@ public class OracleServiceInfo extends RelationalServiceInfo {
@Override
protected String buildJdbcUrl() {
return String.format("jdbc:%s:thin:%s/%s@%s:%d/%s",
jdbcUrlDatabaseType, getUserName(), getPassword(),
jdbcUrlDatabaseType, UriInfo.urlEncode(getUserName()), UriInfo.urlEncode(getPassword()),
getHost(), getPort(), getPath());
}
}

View File

@@ -1,6 +1,7 @@
package org.springframework.cloud.service.common;
import org.springframework.cloud.service.UriBasedServiceInfo;
import org.springframework.cloud.util.UriInfo;
/**
* @author Ramnivas Laddad
@@ -42,10 +43,10 @@ public abstract class RelationalServiceInfo extends UriBasedServiceInfo {
private String formatUserinfo() {
if (getUserName() != null && getPassword() != null) {
return String.format("?user=%s&password=%s", getUserName(), getPassword());
return String.format("?user=%s&password=%s", UriInfo.urlEncode(getUserName()), UriInfo.urlEncode(getPassword()));
}
if (getUserName() != null) {
return String.format("?user=%s", getUserName());
return String.format("?user=%s", UriInfo.urlEncode(getUserName()));
}
return "";
}

View File

@@ -1,6 +1,7 @@
package org.springframework.cloud.service.common;
import org.springframework.cloud.service.ServiceInfo;
import org.springframework.cloud.util.UriInfo;
@ServiceInfo.ServiceLabel("sqlserver")
public class SqlServerServiceInfo extends RelationalServiceInfo {
@@ -18,6 +19,6 @@ public class SqlServerServiceInfo extends RelationalServiceInfo {
protected String buildJdbcUrl() {
return String.format("jdbc:%s://%s:%d;database=%s;user=%s;password=%s",
jdbcUrlDatabaseType,
getHost(), getPort(), getPath(), getUserName(), getPassword());
getHost(), getPort(), getPath(), UriInfo.urlEncode(getUserName()), UriInfo.urlEncode(getPassword()));
}
}

View File

@@ -1,8 +1,10 @@
package org.springframework.cloud.util;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLDecoder;
import java.net.URLEncoder;
/**
* Utility class that allows expressing URIs in alternative forms: individual fields or a URI string
@@ -158,6 +160,14 @@ public class UriInfo {
throw new RuntimeException(e);
}
}
public static String urlEncode(String s) {
try {
return URLEncoder.encode(s, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
@Override
public String toString() {