Skip to content

Commit

Permalink
KNOX-2924 - Added MariaDB support in JDBCTokenStateService (#820)
Browse files Browse the repository at this point in the history
  • Loading branch information
smolnar82 authored Nov 17, 2023
1 parent 083dc89 commit 78278bf
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
4 changes: 4 additions & 0 deletions gateway-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,10 @@
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
</dependency>
<dependency>
<groupId>de.thetaphi</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.apache.knox.gateway.services.security.AliasService;
import org.apache.knox.gateway.services.security.AliasServiceException;
import org.hsqldb.jdbc.JDBCDataSource;
import org.mariadb.jdbc.MariaDbDataSource;
import org.postgresql.ds.PGSimpleDataSource;
import org.postgresql.jdbc.SslMode;
import org.postgresql.ssl.NonValidatingFactory;
Expand All @@ -46,6 +47,7 @@
public class JDBCUtils {
public static final String POSTGRESQL_DB_TYPE = "postgresql";
public static final String MYSQL_DB_TYPE = "mysql";
public static final String MARIA_DB_TYPE = "mariadb";
public static final String DERBY_DB_TYPE = "derbydb";
public static final String HSQL = "hsql";
public static final String DATABASE_USER_ALIAS_NAME = "gateway_database_user";
Expand All @@ -61,6 +63,8 @@ public static DataSource getDataSource(GatewayConfig gatewayConfig, AliasService
return createHsqlDatasource(gatewayConfig, aliasService);
} else if (MYSQL_DB_TYPE.equalsIgnoreCase(gatewayConfig.getDatabaseType())) {
return createMySqlDataSource(gatewayConfig, aliasService);
} else if (MARIA_DB_TYPE.equalsIgnoreCase(gatewayConfig.getDatabaseType())) {
return createMariaDbDataSource(gatewayConfig);
}
throw new IllegalArgumentException("Invalid database type: " + gatewayConfig.getDatabaseType());
}
Expand Down Expand Up @@ -155,6 +159,14 @@ private static void configureMysqlSsl(GatewayConfig gatewayConfig, AliasService
}
}

private static DataSource createMariaDbDataSource(GatewayConfig gatewayConfig) throws SQLException {
if (gatewayConfig.getDatabaseConnectionUrl() != null) {
return new MariaDbDataSource(gatewayConfig.getDatabaseConnectionUrl());
} else {
throw new IllegalArgumentException("MariaDB Java Datasource requires a connection string!");
}
}

private static String getDatabaseUser(AliasService aliasService) throws AliasServiceException {
return getDatabaseAlias(aliasService, DATABASE_USER_ALIAS_NAME);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.apache.knox.gateway.services.security.AliasServiceException;
import org.easymock.EasyMock;
import org.junit.Test;
import org.mariadb.jdbc.MariaDbDataSource;
import org.postgresql.ds.PGSimpleDataSource;
import org.postgresql.ssl.NonValidatingFactory;

Expand Down Expand Up @@ -198,4 +199,42 @@ public void testGetMySqlDatasourceFromJdbcConnectionUrl() throws Exception {
assertEquals(connectionUrl, dataSource.getUrl());
EasyMock.verify(gatewayConfig);
}

@Test
public void shouldReturnMariaDbDatasource() throws Exception {
final GatewayConfig gatewayConfig = EasyMock.createNiceMock(GatewayConfig.class);
EasyMock.expect(gatewayConfig.getDatabaseType()).andReturn(JDBCUtils.MARIA_DB_TYPE).anyTimes();
EasyMock.expect(gatewayConfig.getDatabaseConnectionUrl()).andReturn("jdbc:mariadb://localhost:1234").anyTimes();
EasyMock.replay(gatewayConfig);
assertTrue(JDBCUtils.getDataSource(gatewayConfig, null) instanceof MariaDbDataSource);
}

@Test
public void shoulFailToReturnMariaDbDatasourceIfConnectionUrlIsMissing() throws Exception {
testMariaDbFailure("MariaDB Java Datasource requires a connection string!", null);
}

@Test
public void shoulFailToReturnMariaDbDatasourceIfConnectionUrlIsWrong() throws Exception {
final String wrongConnectionUrl = "jdbc:mariadbb://localhost:1234"; // typo in the protocol
testMariaDbFailure("Wrong mariaDB url: " + wrongConnectionUrl, wrongConnectionUrl);
}

private void testMariaDbFailure(String expectedError, String connectionUrl) {
boolean error = false;
try {
final GatewayConfig gatewayConfig = EasyMock.createNiceMock(GatewayConfig.class);
EasyMock.expect(gatewayConfig.getDatabaseType()).andReturn(JDBCUtils.MARIA_DB_TYPE).anyTimes();
if (connectionUrl != null) {
EasyMock.expect(gatewayConfig.getDatabaseConnectionUrl()).andReturn(connectionUrl).anyTimes();
}
EasyMock.replay(gatewayConfig);
JDBCUtils.getDataSource(gatewayConfig, null);
} catch (Exception e) {
error = true;
assertEquals(expectedError, e.getMessage());
}
assertTrue(error);
}

}
13 changes: 13 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@
<pac4j.version>4.5.6</pac4j.version>
<postgresql.version>42.4.1</postgresql.version>
<mysql.version>8.0.28</mysql.version>
<mariadb.connector.version>3.3.0</mariadb.connector.version>
<protobuf.version>3.16.3</protobuf.version>
<powermock.version>2.0.9</powermock.version>
<purejavacomm.version>0.0.11.1</purejavacomm.version>
Expand Down Expand Up @@ -2207,6 +2208,18 @@
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>3.3.0</version>
<exclusions>
<exclusion>
<groupId>com.github.waffle</groupId>
<artifactId>waffle-jna</artifactId>
</exclusion>
</exclusions>
</dependency>

<!-- Webshell support -->
<dependency>
<groupId>org.jetbrains.pty4j</groupId>
Expand Down

0 comments on commit 78278bf

Please sign in to comment.