This is an automated email from the ASF dual-hosted git repository.
hanicz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/knox.git
The following commit(s) were added to refs/heads/master by this push:
new 5cdf8e6ff KNOX-3202: Oracle DB support for TokeStateService,
Refactored JDBCUti… (#1134)
5cdf8e6ff is described below
commit 5cdf8e6ff09deefc6bc9e715a2615d6c0233a1ef
Author: hanicz <[email protected]>
AuthorDate: Fri Dec 19 17:39:49 2025 +0100
KNOX-3202: Oracle DB support for TokeStateService, Refactored JDBCUti…
(#1134)
* KNOX-3202: Oracle DB support for TokeStateService, Refactored JDBCUtils
into DataSourceFactory
* KNOX-3202: Oracle support for RemoteConfigDatabase, Removed IF NOT EXISTS
from Oracle sql for older DB support
* KNOX-3202: Changed ojdbc11 scope to provided
---
gateway-server/pom.xml | 5 +
.../knox/gateway/database/AbstractDataSource.java | 61 ++++++
.../knox/gateway/database/DataSourceFactory.java | 45 +++++
.../apache/knox/gateway/database/DatabaseType.java | 100 ++++++++++
.../knox/gateway/database/DerbyDataSource.java | 38 ++++
.../knox/gateway/database/HsqlDataSource.java | 38 ++++
.../apache/knox/gateway/database/JDBCUtils.java | 54 ++++++
.../knox/gateway/database/MariaDBDataSource.java | 38 ++++
.../knox/gateway/database/MysqlDataSource.java | 60 ++++++
.../knox/gateway/database/OracleDataSource.java | 56 ++++++
.../knox/gateway/database/PostgresDataSource.java | 74 ++++++++
.../token/impl/DerbyDBTokenStateService.java | 8 +-
.../services/token/impl/JDBCTokenStateService.java | 26 +--
.../services/token/impl/TokenStateDatabase.java | 26 +--
.../RemoteConfigurationMonitorServiceFactory.java | 16 +-
.../topology/monitor/db/RemoteConfigDatabase.java | 20 +-
.../org/apache/knox/gateway/util/JDBCUtils.java | 207 ---------------------
.../main/resources/createKnoxDescriptorsTable.sql | 4 +-
...ble.sql => createKnoxDescriptorsTableDerby.sql} | 4 +-
...le.sql => createKnoxDescriptorsTableOracle.sql} | 10 +-
.../main/resources/createKnoxProvidersTable.sql | 4 +-
...Table.sql => createKnoxProvidersTableDerby.sql} | 4 +-
...able.sql => createKnoxProvidersTableOracle.sql} | 10 +-
.../resources/createKnoxTokenDatabaseTable.sql | 4 +-
...e.sql => createKnoxTokenDatabaseTableDerby.sql} | 4 +-
....sql => createKnoxTokenDatabaseTableOracle.sql} | 12 +-
.../createKnoxTokenMetadataDatabaseTable.sql | 4 +-
... createKnoxTokenMetadataDatabaseTableDerby.sql} | 4 +-
...createKnoxTokenMetadataDatabaseTableOracle.sql} | 10 +-
.../DataSourceFactoryTest.java} | 128 +++++++++----
.../services/factory/ServiceFactoryTest.java | 47 ++---
.../token/impl/JDBCTokenStateServiceTest.java | 48 ++---
.../monitor/db/RemoteConfigDatabaseTest.java | 4 +-
pom.xml | 7 +
34 files changed, 800 insertions(+), 380 deletions(-)
diff --git a/gateway-server/pom.xml b/gateway-server/pom.xml
index 9b254163e..75ca1dfe5 100644
--- a/gateway-server/pom.xml
+++ b/gateway-server/pom.xml
@@ -435,6 +435,11 @@
<artifactId>mysql-connector-java</artifactId>
<scope>provided</scope>
</dependency>
+ <dependency>
+ <groupId>com.oracle.database.jdbc</groupId>
+ <artifactId>ojdbc11</artifactId>
+ <scope>provided</scope>
+ </dependency>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
diff --git
a/gateway-server/src/main/java/org/apache/knox/gateway/database/AbstractDataSource.java
b/gateway-server/src/main/java/org/apache/knox/gateway/database/AbstractDataSource.java
new file mode 100644
index 000000000..32edee732
--- /dev/null
+++
b/gateway-server/src/main/java/org/apache/knox/gateway/database/AbstractDataSource.java
@@ -0,0 +1,61 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.knox.gateway.database;
+
+import org.apache.knox.gateway.config.GatewayConfig;
+import org.apache.knox.gateway.services.security.AliasService;
+import org.apache.knox.gateway.services.security.AliasServiceException;
+
+import javax.sql.DataSource;
+import java.sql.SQLException;
+
+public abstract class AbstractDataSource {
+
+ public static final String TOKENS_TABLE_CREATE_SQL_FILE_NAME =
"createKnoxTokenDatabaseTable.sql";
+ public static final String TOKEN_METADATA_TABLE_CREATE_SQL_FILE_NAME =
"createKnoxTokenMetadataDatabaseTable.sql";
+ public static final String ORACLE_TOKENS_TABLE_CREATE_SQL_FILE_NAME =
"createKnoxTokenDatabaseTableOracle.sql";
+ public static final String
ORACLE_TOKEN_METADATA_TABLE_CREATE_SQL_FILE_NAME =
"createKnoxTokenMetadataDatabaseTableOracle.sql";
+ public static final String DERBY_TOKENS_TABLE_CREATE_SQL_FILE_NAME =
"createKnoxTokenDatabaseTableDerby.sql";
+ public static final String DERBY_TOKEN_METADATA_TABLE_CREATE_SQL_FILE_NAME
= "createKnoxTokenMetadataDatabaseTableDerby.sql";
+
+ public static final String KNOX_PROVIDERS_TABLE_CREATE_SQL_FILE_NAME =
"createKnoxProvidersTable.sql";
+ public static final String KNOX_DESCRIPTORS_TABLE_CREATE_SQL_FILE_NAME =
"createKnoxDescriptorsTable.sql";
+ public static final String
ORACLE_KNOX_PROVIDERS_TABLE_CREATE_SQL_FILE_NAME =
"createKnoxProvidersTableOracle.sql";
+ public static final String
ORACLE_KNOX_DESCRIPTORS_TABLE_CREATE_SQL_FILE_NAME =
"createKnoxDescriptorsTableOracle.sql";
+ public static final String DERBY_KNOX_PROVIDERS_TABLE_CREATE_SQL_FILE_NAME
= "createKnoxProvidersTableDerby.sql";
+ public static final String
DERBY_KNOX_DESCRIPTORS_TABLE_CREATE_SQL_FILE_NAME =
"createKnoxDescriptorsTableDerby.sql";
+
+ public static final String DATABASE_USER_ALIAS_NAME =
"gateway_database_user";
+ public static final String DATABASE_PASSWORD_ALIAS_NAME =
"gateway_database_password";
+ public static final String DATABASE_TRUSTSTORE_PASSWORD_ALIAS_NAME =
"gateway_database_ssl_truststore_password";
+
+ public abstract DataSource createDataSource(GatewayConfig gatewayConfig,
AliasService aliasService) throws AliasServiceException, SQLException;
+
+ protected String getDatabaseUser(AliasService aliasService) throws
AliasServiceException {
+ return getDatabaseAlias(aliasService, DATABASE_USER_ALIAS_NAME);
+ }
+
+ protected String getDatabasePassword(AliasService aliasService) throws
AliasServiceException {
+ return getDatabaseAlias(aliasService, DATABASE_PASSWORD_ALIAS_NAME);
+ }
+
+ protected String getDatabaseAlias(AliasService aliasService, String
aliasName) throws AliasServiceException {
+ final char[] value =
aliasService.getPasswordFromAliasForGateway(aliasName);
+ return value == null ? null : new String(value);
+ }
+}
diff --git
a/gateway-server/src/main/java/org/apache/knox/gateway/database/DataSourceFactory.java
b/gateway-server/src/main/java/org/apache/knox/gateway/database/DataSourceFactory.java
new file mode 100644
index 000000000..f30c2b211
--- /dev/null
+++
b/gateway-server/src/main/java/org/apache/knox/gateway/database/DataSourceFactory.java
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.knox.gateway.database;
+
+import org.apache.knox.gateway.config.GatewayConfig;
+import org.apache.knox.gateway.services.security.AliasService;
+import org.apache.knox.gateway.services.security.AliasServiceException;
+
+import javax.sql.DataSource;
+import java.sql.SQLException;
+
+public class DataSourceFactory {
+
+ public static DataSource getDataSource(GatewayConfig gatewayConfig,
AliasService aliasService) throws AliasServiceException, SQLException {
+ DatabaseType dbType =
DatabaseType.fromString(gatewayConfig.getDatabaseType());
+ AbstractDataSource dsFactory;
+
+ switch (dbType) {
+ case POSTGRESQL -> dsFactory = new PostgresDataSource();
+ case MYSQL -> dsFactory = new MysqlDataSource();
+ case MARIADB -> dsFactory = new MariaDBDataSource();
+ case DERBY -> dsFactory = new DerbyDataSource();
+ case HSQL -> dsFactory = new HsqlDataSource();
+ case ORACLE -> dsFactory = new OracleDataSource();
+ default -> throw new IllegalArgumentException("Invalid database
type: " + gatewayConfig.getDatabaseType());
+ }
+
+ return dsFactory.createDataSource(gatewayConfig, aliasService);
+ }
+}
diff --git
a/gateway-server/src/main/java/org/apache/knox/gateway/database/DatabaseType.java
b/gateway-server/src/main/java/org/apache/knox/gateway/database/DatabaseType.java
new file mode 100644
index 000000000..ba17ffcc3
--- /dev/null
+++
b/gateway-server/src/main/java/org/apache/knox/gateway/database/DatabaseType.java
@@ -0,0 +1,100 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.knox.gateway.database;
+
+public enum DatabaseType {
+ POSTGRESQL("postgresql",
+ AbstractDataSource.TOKENS_TABLE_CREATE_SQL_FILE_NAME,
+ AbstractDataSource.TOKEN_METADATA_TABLE_CREATE_SQL_FILE_NAME,
+ AbstractDataSource.KNOX_PROVIDERS_TABLE_CREATE_SQL_FILE_NAME,
+ AbstractDataSource.KNOX_DESCRIPTORS_TABLE_CREATE_SQL_FILE_NAME
+ ),
+ MYSQL("mysql",
+ AbstractDataSource.TOKENS_TABLE_CREATE_SQL_FILE_NAME,
+ AbstractDataSource.TOKEN_METADATA_TABLE_CREATE_SQL_FILE_NAME,
+ AbstractDataSource.KNOX_PROVIDERS_TABLE_CREATE_SQL_FILE_NAME,
+ AbstractDataSource.KNOX_DESCRIPTORS_TABLE_CREATE_SQL_FILE_NAME
+ ),
+ MARIADB("mariadb",
+ AbstractDataSource.TOKENS_TABLE_CREATE_SQL_FILE_NAME,
+ AbstractDataSource.TOKEN_METADATA_TABLE_CREATE_SQL_FILE_NAME,
+ AbstractDataSource.KNOX_PROVIDERS_TABLE_CREATE_SQL_FILE_NAME,
+ AbstractDataSource.KNOX_DESCRIPTORS_TABLE_CREATE_SQL_FILE_NAME
+ ),
+ HSQL("hsql",
+ AbstractDataSource.TOKENS_TABLE_CREATE_SQL_FILE_NAME,
+ AbstractDataSource.TOKEN_METADATA_TABLE_CREATE_SQL_FILE_NAME,
+ AbstractDataSource.KNOX_PROVIDERS_TABLE_CREATE_SQL_FILE_NAME,
+ AbstractDataSource.KNOX_DESCRIPTORS_TABLE_CREATE_SQL_FILE_NAME
+ ),
+ DERBY("derbydb",
+ AbstractDataSource.DERBY_TOKENS_TABLE_CREATE_SQL_FILE_NAME,
+ AbstractDataSource.DERBY_TOKEN_METADATA_TABLE_CREATE_SQL_FILE_NAME,
+ AbstractDataSource.DERBY_KNOX_PROVIDERS_TABLE_CREATE_SQL_FILE_NAME,
+
AbstractDataSource.DERBY_KNOX_DESCRIPTORS_TABLE_CREATE_SQL_FILE_NAME
+ ),
+ ORACLE("oracle",
+ AbstractDataSource.ORACLE_TOKENS_TABLE_CREATE_SQL_FILE_NAME,
+
AbstractDataSource.ORACLE_TOKEN_METADATA_TABLE_CREATE_SQL_FILE_NAME,
+
AbstractDataSource.ORACLE_KNOX_PROVIDERS_TABLE_CREATE_SQL_FILE_NAME,
+
AbstractDataSource.ORACLE_KNOX_DESCRIPTORS_TABLE_CREATE_SQL_FILE_NAME
+ );
+
+ private final String type;
+ private final String tokensTableSql;
+ private final String metadataTableSql;
+ private final String providersTableSql;
+ private final String descriptorsTableSql;
+
+ DatabaseType(String type, String tokensTableSql, String metadataTableSql,
String providersTableSql, String descriptorsTableSql) {
+ this.type = type;
+ this.tokensTableSql = tokensTableSql;
+ this.metadataTableSql = metadataTableSql;
+ this.providersTableSql = providersTableSql;
+ this.descriptorsTableSql = descriptorsTableSql;
+ }
+
+ public String type() {
+ return type;
+ }
+
+ public String tokensTableSql() {
+ return tokensTableSql;
+ }
+
+ public String metadataTableSql() {
+ return metadataTableSql;
+ }
+
+ public String providersTableSql() {
+ return providersTableSql;
+ }
+
+ public String descriptorsTableSql() {
+ return descriptorsTableSql;
+ }
+
+ public static DatabaseType fromString(String dbType) {
+ for (DatabaseType dt : values()) {
+ if (dt.type.equalsIgnoreCase(dbType)) {
+ return dt;
+ }
+ }
+ throw new IllegalArgumentException("Invalid database type: " + dbType);
+ }
+}
diff --git
a/gateway-server/src/main/java/org/apache/knox/gateway/database/DerbyDataSource.java
b/gateway-server/src/main/java/org/apache/knox/gateway/database/DerbyDataSource.java
new file mode 100644
index 000000000..d796a1c18
--- /dev/null
+++
b/gateway-server/src/main/java/org/apache/knox/gateway/database/DerbyDataSource.java
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.knox.gateway.database;
+
+import org.apache.derby.jdbc.EmbeddedDataSource;
+import org.apache.knox.gateway.config.GatewayConfig;
+import org.apache.knox.gateway.services.security.AliasService;
+import org.apache.knox.gateway.services.security.AliasServiceException;
+
+import javax.sql.DataSource;
+import java.sql.SQLException;
+
+public class DerbyDataSource extends AbstractDataSource {
+
+ @Override
+ public DataSource createDataSource(GatewayConfig gatewayConfig,
AliasService aliasService) throws AliasServiceException, SQLException {
+ final EmbeddedDataSource embeddedDataSource = new EmbeddedDataSource();
+ embeddedDataSource.setDatabaseName(gatewayConfig.getDatabaseName());
+ embeddedDataSource.setUser(getDatabaseUser(aliasService));
+ embeddedDataSource.setPassword(getDatabasePassword(aliasService));
+ return embeddedDataSource;
+ }
+}
diff --git
a/gateway-server/src/main/java/org/apache/knox/gateway/database/HsqlDataSource.java
b/gateway-server/src/main/java/org/apache/knox/gateway/database/HsqlDataSource.java
new file mode 100644
index 000000000..756770f17
--- /dev/null
+++
b/gateway-server/src/main/java/org/apache/knox/gateway/database/HsqlDataSource.java
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.knox.gateway.database;
+
+import org.apache.knox.gateway.config.GatewayConfig;
+import org.apache.knox.gateway.services.security.AliasService;
+import org.apache.knox.gateway.services.security.AliasServiceException;
+import org.hsqldb.jdbc.JDBCDataSource;
+
+import javax.sql.DataSource;
+import java.sql.SQLException;
+
+public class HsqlDataSource extends AbstractDataSource {
+
+ @Override
+ public DataSource createDataSource(GatewayConfig gatewayConfig,
AliasService aliasService) throws AliasServiceException, SQLException {
+ JDBCDataSource hsqlDatasource = new JDBCDataSource();
+ hsqlDatasource.setUrl(gatewayConfig.getDatabaseConnectionUrl());
+ hsqlDatasource.setUser(getDatabaseUser(aliasService));
+ hsqlDatasource.setPassword(getDatabasePassword(aliasService));
+ return hsqlDatasource;
+ }
+}
diff --git
a/gateway-server/src/main/java/org/apache/knox/gateway/database/JDBCUtils.java
b/gateway-server/src/main/java/org/apache/knox/gateway/database/JDBCUtils.java
new file mode 100644
index 000000000..014f8144f
--- /dev/null
+++
b/gateway-server/src/main/java/org/apache/knox/gateway/database/JDBCUtils.java
@@ -0,0 +1,54 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.knox.gateway.database;
+
+import org.apache.commons.io.IOUtils;
+
+import javax.sql.DataSource;
+import java.io.InputStream;
+import java.sql.Connection;
+import java.sql.DatabaseMetaData;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.Locale;
+
+import static java.nio.charset.StandardCharsets.UTF_8;
+
+public class JDBCUtils {
+
+ public static boolean tableExists(String tableName, DataSource dataSource)
throws SQLException {
+ boolean exists;
+ try (Connection connection = dataSource.getConnection()) {
+ final DatabaseMetaData dbMetadata = connection.getMetaData();
+ final String tableNameToCheck =
dbMetadata.storesUpperCaseIdentifiers() ? tableName :
tableName.toLowerCase(Locale.ROOT);
+ try (ResultSet tables =
dbMetadata.getTables(connection.getCatalog(), null, tableNameToCheck, null)) {
+ exists = tables.next();
+ }
+ }
+ return exists;
+ }
+
+ public static void createTableFromSQL(String createSqlFileName, DataSource
dataSource, ClassLoader classLoader) throws Exception {
+ try (InputStream is =
classLoader.getResourceAsStream(createSqlFileName);
+ Connection connection = dataSource.getConnection();Statement
createTableStatement = connection.createStatement()) {
+ String createTableSql = IOUtils.toString(is, UTF_8);
+ createTableStatement.execute(createTableSql);
+ }
+ }
+}
diff --git
a/gateway-server/src/main/java/org/apache/knox/gateway/database/MariaDBDataSource.java
b/gateway-server/src/main/java/org/apache/knox/gateway/database/MariaDBDataSource.java
new file mode 100644
index 000000000..1b011a16e
--- /dev/null
+++
b/gateway-server/src/main/java/org/apache/knox/gateway/database/MariaDBDataSource.java
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.knox.gateway.database;
+
+import org.apache.knox.gateway.config.GatewayConfig;
+import org.apache.knox.gateway.services.security.AliasService;
+import org.apache.knox.gateway.services.security.AliasServiceException;
+import org.mariadb.jdbc.MariaDbDataSource;
+
+import javax.sql.DataSource;
+import java.sql.SQLException;
+
+public class MariaDBDataSource extends AbstractDataSource {
+
+ @Override
+ public DataSource createDataSource(GatewayConfig gatewayConfig,
AliasService aliasService) throws AliasServiceException, SQLException {
+ if (gatewayConfig.getDatabaseConnectionUrl() != null) {
+ return new
MariaDbDataSource(gatewayConfig.getDatabaseConnectionUrl());
+ } else {
+ throw new IllegalArgumentException("MariaDB Java Datasource
requires a connection string!");
+ }
+ }
+}
diff --git
a/gateway-server/src/main/java/org/apache/knox/gateway/database/MysqlDataSource.java
b/gateway-server/src/main/java/org/apache/knox/gateway/database/MysqlDataSource.java
new file mode 100644
index 000000000..c56b135b9
--- /dev/null
+++
b/gateway-server/src/main/java/org/apache/knox/gateway/database/MysqlDataSource.java
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.knox.gateway.database;
+
+import com.mysql.cj.conf.PropertyDefinitions;
+import org.apache.knox.gateway.config.GatewayConfig;
+import org.apache.knox.gateway.services.security.AliasService;
+import org.apache.knox.gateway.services.security.AliasServiceException;
+
+import javax.sql.DataSource;
+import java.sql.SQLException;
+
+public class MysqlDataSource extends AbstractDataSource {
+
+ @Override
+ public DataSource createDataSource(GatewayConfig gatewayConfig,
AliasService aliasService) throws AliasServiceException, SQLException {
+ com.mysql.cj.jdbc.MysqlDataSource dataSource = new
com.mysql.cj.jdbc.MysqlDataSource();
+ if (gatewayConfig.getDatabaseConnectionUrl() != null) {
+ dataSource.setUrl(gatewayConfig.getDatabaseConnectionUrl());
+ } else {
+ dataSource.setDatabaseName(gatewayConfig.getDatabaseName());
+ dataSource.setServerName(gatewayConfig.getDatabaseHost());
+ dataSource.setPortNumber(gatewayConfig.getDatabasePort());
+ dataSource.setUser(getDatabaseUser(aliasService));
+ dataSource.setPassword(getDatabasePassword(aliasService));
+ configureSsl(gatewayConfig, aliasService, dataSource);
+ }
+ return dataSource;
+ }
+
+ private void configureSsl(GatewayConfig gatewayConfig, AliasService
aliasService, com.mysql.cj.jdbc.MysqlDataSource dataSource) throws
AliasServiceException, SQLException {
+ if (gatewayConfig.isDatabaseSslEnabled()) {
+ dataSource.setUseSSL(true);
+ if (gatewayConfig.verifyDatabaseSslServerCertificate()) {
+
dataSource.setSslMode(PropertyDefinitions.SslMode.VERIFY_CA.name());
+ dataSource.setVerifyServerCertificate(true);
+ dataSource.setTrustCertificateKeyStoreType("JKS");
+ dataSource.setTrustCertificateKeyStoreUrl("file:" +
gatewayConfig.getDatabaseSslTruststoreFileName());
+
dataSource.setTrustCertificateKeyStorePassword(getDatabaseAlias(aliasService,
DATABASE_TRUSTSTORE_PASSWORD_ALIAS_NAME));
+ } else {
+ dataSource.setVerifyServerCertificate(false);
+ }
+ }
+ }
+}
diff --git
a/gateway-server/src/main/java/org/apache/knox/gateway/database/OracleDataSource.java
b/gateway-server/src/main/java/org/apache/knox/gateway/database/OracleDataSource.java
new file mode 100644
index 000000000..6a039c9b8
--- /dev/null
+++
b/gateway-server/src/main/java/org/apache/knox/gateway/database/OracleDataSource.java
@@ -0,0 +1,56 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.knox.gateway.database;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.knox.gateway.config.GatewayConfig;
+import org.apache.knox.gateway.services.security.AliasService;
+import org.apache.knox.gateway.services.security.AliasServiceException;
+
+import javax.sql.DataSource;
+import java.sql.SQLException;
+
+public class OracleDataSource extends AbstractDataSource {
+
+ private static final String THIN_DRIVER = "thin";
+
+ @Override
+ public DataSource createDataSource(GatewayConfig gatewayConfig,
AliasService aliasService) throws AliasServiceException, SQLException {
+ final oracle.jdbc.pool.OracleDataSource oracleDataSource = new
oracle.jdbc.pool.OracleDataSource();
+ final String dbUser = getDatabaseUser(aliasService);
+ final String dbPassword = getDatabasePassword(aliasService);
+
+ if (gatewayConfig.getDatabaseConnectionUrl() != null) {
+ oracleDataSource.setURL(gatewayConfig.getDatabaseConnectionUrl());
+ if (StringUtils.isNotBlank(dbUser)) {
+ oracleDataSource.setUser(dbUser);
+ }
+ if (StringUtils.isNotBlank(dbPassword)) {
+ oracleDataSource.setPassword(dbPassword);
+ }
+ } else {
+ oracleDataSource.setDriverType(THIN_DRIVER);
+ oracleDataSource.setServiceName(gatewayConfig.getDatabaseName());
+ oracleDataSource.setServerName(gatewayConfig.getDatabaseHost());
+ oracleDataSource.setPortNumber(gatewayConfig.getDatabasePort());
+ oracleDataSource.setUser(dbUser);
+ oracleDataSource.setPassword(dbPassword);
+ }
+ return oracleDataSource;
+ }
+}
diff --git
a/gateway-server/src/main/java/org/apache/knox/gateway/database/PostgresDataSource.java
b/gateway-server/src/main/java/org/apache/knox/gateway/database/PostgresDataSource.java
new file mode 100644
index 000000000..834436adf
--- /dev/null
+++
b/gateway-server/src/main/java/org/apache/knox/gateway/database/PostgresDataSource.java
@@ -0,0 +1,74 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.knox.gateway.database;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.knox.gateway.config.GatewayConfig;
+import org.apache.knox.gateway.services.security.AliasService;
+import org.apache.knox.gateway.services.security.AliasServiceException;
+import org.postgresql.ds.PGSimpleDataSource;
+import org.postgresql.jdbc.SslMode;
+import org.postgresql.ssl.NonValidatingFactory;
+
+import javax.sql.DataSource;
+import java.sql.SQLException;
+
+public class PostgresDataSource extends AbstractDataSource {
+
+ @Override
+ public DataSource createDataSource(GatewayConfig gatewayConfig,
AliasService aliasService) throws AliasServiceException, SQLException {
+ final PGSimpleDataSource postgresDataSource = new PGSimpleDataSource();
+ final String dbUser = getDatabaseUser(aliasService);
+ final String dbPassword = getDatabasePassword(aliasService);
+ if (gatewayConfig.getDatabaseConnectionUrl() != null) {
+
postgresDataSource.setUrl(gatewayConfig.getDatabaseConnectionUrl());
+
+ // avoid nullifying already configured user/password properties in
case they
+ // were already set in the given JDBC URL but not saved as aliases
+ if (StringUtils.isNotBlank(dbUser)) {
+ postgresDataSource.setUser(dbUser);
+ }
+ if (StringUtils.isNotBlank(dbPassword)) {
+ postgresDataSource.setPassword(dbPassword);
+ }
+ } else {
+
postgresDataSource.setDatabaseName(gatewayConfig.getDatabaseName());
+ postgresDataSource.setServerNames(new
String[]{gatewayConfig.getDatabaseHost()});
+ postgresDataSource.setPortNumbers(new
int[]{gatewayConfig.getDatabasePort()});
+ postgresDataSource.setUser(dbUser);
+ postgresDataSource.setPassword(dbPassword);
+ }
+
+ configureSsl(gatewayConfig, aliasService, postgresDataSource);
+
+ return postgresDataSource;
+ }
+
+ private void configureSsl(GatewayConfig gatewayConfig, AliasService
aliasService, PGSimpleDataSource postgresDataSource) throws
AliasServiceException {
+ if (gatewayConfig.isDatabaseSslEnabled()) {
+ postgresDataSource.setSsl(true);
+ postgresDataSource.setSslMode(SslMode.VERIFY_FULL.value);
+ if (gatewayConfig.verifyDatabaseSslServerCertificate()) {
+
postgresDataSource.setSslRootCert(gatewayConfig.getDatabaseSslTruststoreFileName());
+
postgresDataSource.setSslPassword(getDatabaseAlias(aliasService,
DATABASE_TRUSTSTORE_PASSWORD_ALIAS_NAME));
+ } else {
+
postgresDataSource.setSslfactory(NonValidatingFactory.class.getCanonicalName());
+ }
+ }
+ }
+}
diff --git
a/gateway-server/src/main/java/org/apache/knox/gateway/services/token/impl/DerbyDBTokenStateService.java
b/gateway-server/src/main/java/org/apache/knox/gateway/services/token/impl/DerbyDBTokenStateService.java
index 48525fa86..d333eb571 100644
---
a/gateway-server/src/main/java/org/apache/knox/gateway/services/token/impl/DerbyDBTokenStateService.java
+++
b/gateway-server/src/main/java/org/apache/knox/gateway/services/token/impl/DerbyDBTokenStateService.java
@@ -19,9 +19,9 @@ package org.apache.knox.gateway.services.token.impl;
import static
org.apache.knox.gateway.config.impl.GatewayConfigImpl.GATEWAY_DATABASE_NAME;
import static
org.apache.knox.gateway.config.impl.GatewayConfigImpl.GATEWAY_DATABASE_TYPE;
import static
org.apache.knox.gateway.services.security.AliasService.NO_CLUSTER_NAME;
-import static
org.apache.knox.gateway.util.JDBCUtils.DATABASE_PASSWORD_ALIAS_NAME;
-import static org.apache.knox.gateway.util.JDBCUtils.DATABASE_USER_ALIAS_NAME;
-import static org.apache.knox.gateway.util.JDBCUtils.DERBY_DB_TYPE;
+import static
org.apache.knox.gateway.database.AbstractDataSource.DATABASE_PASSWORD_ALIAS_NAME;
+import static
org.apache.knox.gateway.database.AbstractDataSource.DATABASE_USER_ALIAS_NAME;
+import static org.apache.knox.gateway.database.DatabaseType.DERBY;
import java.nio.file.Files;
import java.nio.file.Path;
@@ -54,7 +54,7 @@ public class DerbyDBTokenStateService extends
JDBCTokenStateService {
try {
derbyDatabaseFolder = Paths.get(config.getGatewaySecurityDir(), DB_NAME);
startDerby();
- ((Configuration) config).set(GATEWAY_DATABASE_TYPE, DERBY_DB_TYPE);
+ ((Configuration) config).set(GATEWAY_DATABASE_TYPE, DERBY.type());
((Configuration) config).set(GATEWAY_DATABASE_NAME,
derbyDatabaseFolder.toString());
getAliasService().addAliasForCluster(NO_CLUSTER_NAME,
DATABASE_USER_ALIAS_NAME, getDatabaseUserName());
getAliasService().addAliasForCluster(NO_CLUSTER_NAME,
DATABASE_PASSWORD_ALIAS_NAME, getDatabasePassword());
diff --git
a/gateway-server/src/main/java/org/apache/knox/gateway/services/token/impl/JDBCTokenStateService.java
b/gateway-server/src/main/java/org/apache/knox/gateway/services/token/impl/JDBCTokenStateService.java
index b9d0f2cfb..cd08c531b 100644
---
a/gateway-server/src/main/java/org/apache/knox/gateway/services/token/impl/JDBCTokenStateService.java
+++
b/gateway-server/src/main/java/org/apache/knox/gateway/services/token/impl/JDBCTokenStateService.java
@@ -17,19 +17,9 @@
*/
package org.apache.knox.gateway.services.token.impl;
-import java.sql.SQLException;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Map;
-import java.util.Set;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.atomic.AtomicBoolean;
-import java.util.concurrent.locks.Lock;
-import java.util.concurrent.locks.ReentrantLock;
-import java.util.stream.Collectors;
-
import org.apache.commons.lang3.StringUtils;
import org.apache.knox.gateway.config.GatewayConfig;
+import org.apache.knox.gateway.database.DataSourceFactory;
import org.apache.knox.gateway.services.ServiceLifecycleException;
import org.apache.knox.gateway.services.security.AliasService;
import org.apache.knox.gateway.services.security.token.KnoxToken;
@@ -37,10 +27,20 @@ import
org.apache.knox.gateway.services.security.token.TokenMetadata;
import org.apache.knox.gateway.services.security.token.TokenMigrationTarget;
import
org.apache.knox.gateway.services.security.token.TokenStateServiceException;
import org.apache.knox.gateway.services.security.token.UnknownTokenException;
-import org.apache.knox.gateway.util.JDBCUtils;
import org.apache.knox.gateway.util.TokenMigrationTool;
import org.apache.knox.gateway.util.Tokens;
+import java.sql.SQLException;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
+import java.util.stream.Collectors;
+
public class JDBCTokenStateService extends AbstractPersistentTokenStateService
implements TokenMigrationTarget {
private AliasService aliasService; // connection username/pw and passcode
HMAC secret are stored here
private TokenStateDatabase tokenDatabase;
@@ -72,7 +72,7 @@ public class JDBCTokenStateService extends
AbstractPersistentTokenStateService i
throw new ServiceLifecycleException("The required AliasService
reference has not been set.");
}
try {
- this.tokenDatabase = new
TokenStateDatabase(JDBCUtils.getDataSource(config, aliasService));
+ this.tokenDatabase = new
TokenStateDatabase(DataSourceFactory.getDataSource(config, aliasService),
config.getDatabaseType());
initialized.set(true);
} catch (Exception e) {
throw new ServiceLifecycleException("Error while initiating
JDBCTokenStateService: " + e, e);
diff --git
a/gateway-server/src/main/java/org/apache/knox/gateway/services/token/impl/TokenStateDatabase.java
b/gateway-server/src/main/java/org/apache/knox/gateway/services/token/impl/TokenStateDatabase.java
index 901b8d3f0..dbc89d695 100644
---
a/gateway-server/src/main/java/org/apache/knox/gateway/services/token/impl/TokenStateDatabase.java
+++
b/gateway-server/src/main/java/org/apache/knox/gateway/services/token/impl/TokenStateDatabase.java
@@ -17,8 +17,13 @@
*/
package org.apache.knox.gateway.services.token.impl;
-import static java.nio.charset.StandardCharsets.UTF_8;
+import org.apache.commons.codec.binary.Base64;
+import org.apache.knox.gateway.database.DatabaseType;
+import org.apache.knox.gateway.database.JDBCUtils;
+import org.apache.knox.gateway.services.security.token.KnoxToken;
+import org.apache.knox.gateway.services.security.token.TokenMetadata;
+import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
@@ -29,16 +34,10 @@ import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
-import javax.sql.DataSource;
-import org.apache.commons.codec.binary.Base64;
-import org.apache.knox.gateway.services.security.token.KnoxToken;
-import org.apache.knox.gateway.services.security.token.TokenMetadata;
-import org.apache.knox.gateway.util.JDBCUtils;
+import static java.nio.charset.StandardCharsets.UTF_8;
public class TokenStateDatabase {
- private static final String TOKENS_TABLE_CREATE_SQL_FILE_NAME =
"createKnoxTokenDatabaseTable.sql";
- private static final String TOKEN_METADATA_TABLE_CREATE_SQL_FILE_NAME =
"createKnoxTokenMetadataDatabaseTable.sql";
static final String TOKENS_TABLE_NAME = "KNOX_TOKENS";
static final String TOKEN_METADATA_TABLE_NAME = "KNOX_TOKEN_METADATA";
private static final String ADD_TOKEN_SQL = "INSERT INTO " +
TOKENS_TABLE_NAME + "(token_id, issue_time, expiration, max_lifetime) VALUES(?,
?, ?, ?)";
@@ -61,15 +60,16 @@ public class TokenStateDatabase {
private final DataSource dataSource;
- TokenStateDatabase(DataSource dataSource) throws Exception {
+ TokenStateDatabase(DataSource dataSource, String dbType) throws Exception {
this.dataSource = dataSource;
- createTableIfNotExists(TOKENS_TABLE_NAME,
TOKENS_TABLE_CREATE_SQL_FILE_NAME);
- createTableIfNotExists(TOKEN_METADATA_TABLE_NAME,
TOKEN_METADATA_TABLE_CREATE_SQL_FILE_NAME);
+ DatabaseType databaseType = DatabaseType.fromString(dbType);
+ createTableIfNotExists(TOKENS_TABLE_NAME, databaseType.tokensTableSql());
+ createTableIfNotExists(TOKEN_METADATA_TABLE_NAME,
databaseType.metadataTableSql());
}
private void createTableIfNotExists(String tableName, String
createSqlFileName) throws Exception {
- if (!JDBCUtils.isTableExists(tableName, dataSource)) {
- JDBCUtils.createTable(createSqlFileName, dataSource,
TokenStateDatabase.class.getClassLoader());
+ if (!JDBCUtils.tableExists(tableName, dataSource)) {
+ JDBCUtils.createTableFromSQL(createSqlFileName, dataSource,
TokenStateDatabase.class.getClassLoader());
}
}
diff --git
a/gateway-server/src/main/java/org/apache/knox/gateway/topology/monitor/RemoteConfigurationMonitorServiceFactory.java
b/gateway-server/src/main/java/org/apache/knox/gateway/topology/monitor/RemoteConfigurationMonitorServiceFactory.java
index 88992095f..75c71e0c8 100644
---
a/gateway-server/src/main/java/org/apache/knox/gateway/topology/monitor/RemoteConfigurationMonitorServiceFactory.java
+++
b/gateway-server/src/main/java/org/apache/knox/gateway/topology/monitor/RemoteConfigurationMonitorServiceFactory.java
@@ -16,14 +16,9 @@
*/
package org.apache.knox.gateway.topology.monitor;
-import java.io.File;
-import java.sql.SQLException;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Map;
-
import org.apache.commons.lang3.StringUtils;
import org.apache.knox.gateway.config.GatewayConfig;
+import org.apache.knox.gateway.database.DataSourceFactory;
import org.apache.knox.gateway.services.GatewayServices;
import org.apache.knox.gateway.services.ServiceLifecycleException;
import org.apache.knox.gateway.services.ServiceType;
@@ -33,7 +28,12 @@ import
org.apache.knox.gateway.services.security.AliasServiceException;
import
org.apache.knox.gateway.topology.monitor.db.DbRemoteConfigurationMonitorService;
import org.apache.knox.gateway.topology.monitor.db.LocalDirectory;
import org.apache.knox.gateway.topology.monitor.db.RemoteConfigDatabase;
-import org.apache.knox.gateway.util.JDBCUtils;
+
+import java.io.File;
+import java.sql.SQLException;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Map;
public class RemoteConfigurationMonitorServiceFactory extends
AbstractServiceFactory {
@@ -59,7 +59,7 @@ public class RemoteConfigurationMonitorServiceFactory extends
AbstractServiceFac
private DbRemoteConfigurationMonitorService
createDbBasedMonitor(GatewayConfig config, AliasService aliasService) throws
ServiceLifecycleException {
try {
- RemoteConfigDatabase db = new
RemoteConfigDatabase(JDBCUtils.getDataSource(config, aliasService));
+ RemoteConfigDatabase db = new
RemoteConfigDatabase(DataSourceFactory.getDataSource(config, aliasService),
config.getDatabaseType());
LocalDirectory descriptorDir = new LocalDirectory(new
File(config.getGatewayDescriptorsDir()));
LocalDirectory providerDir = new LocalDirectory(new
File(config.getGatewayProvidersConfigDir()));
return new DbRemoteConfigurationMonitorService(
diff --git
a/gateway-server/src/main/java/org/apache/knox/gateway/topology/monitor/db/RemoteConfigDatabase.java
b/gateway-server/src/main/java/org/apache/knox/gateway/topology/monitor/db/RemoteConfigDatabase.java
index 51e9d5db4..91f1e2a65 100644
---
a/gateway-server/src/main/java/org/apache/knox/gateway/topology/monitor/db/RemoteConfigDatabase.java
+++
b/gateway-server/src/main/java/org/apache/knox/gateway/topology/monitor/db/RemoteConfigDatabase.java
@@ -26,32 +26,32 @@ import java.util.ArrayList;
import java.util.List;
import javax.sql.DataSource;
-import org.apache.knox.gateway.util.JDBCUtils;
+import org.apache.knox.gateway.database.DatabaseType;
+import org.apache.knox.gateway.database.JDBCUtils;
public class RemoteConfigDatabase {
- private static final String KNOX_PROVIDERS_TABLE_CREATE_SQL_FILE_NAME =
"createKnoxProvidersTable.sql";
- private static final String KNOX_DESCRIPTORS_TABLE_CREATE_SQL_FILE_NAME =
"createKnoxDescriptorsTable.sql";
private static final String KNOX_PROVIDERS_TABLE_NAME = "KNOX_PROVIDERS";
private static final String KNOX_DESCRIPTORS_TABLE_NAME = "KNOX_DESCRIPTORS";
private final DataSource dataSource;
- public RemoteConfigDatabase(DataSource dataSource) {
+ public RemoteConfigDatabase(DataSource dataSource, String dbType) {
this.dataSource = dataSource;
- ensureTablesExist();
+ DatabaseType databaseType = DatabaseType.fromString(dbType);
+ ensureTablesExist(databaseType);
}
- private void ensureTablesExist() {
+ private void ensureTablesExist(DatabaseType databaseType ) {
try {
- createTableIfNotExists(KNOX_PROVIDERS_TABLE_NAME,
KNOX_PROVIDERS_TABLE_CREATE_SQL_FILE_NAME);
- createTableIfNotExists(KNOX_DESCRIPTORS_TABLE_NAME,
KNOX_DESCRIPTORS_TABLE_CREATE_SQL_FILE_NAME);
+ createTableIfNotExists(KNOX_PROVIDERS_TABLE_NAME,
databaseType.providersTableSql());
+ createTableIfNotExists(KNOX_DESCRIPTORS_TABLE_NAME,
databaseType.descriptorsTableSql());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private void createTableIfNotExists(String tableName, String
createSqlFileName) throws Exception {
- if (!JDBCUtils.isTableExists(tableName, dataSource)) {
- JDBCUtils.createTable(createSqlFileName, dataSource,
this.getClass().getClassLoader());
+ if (!JDBCUtils.tableExists(tableName, dataSource)) {
+ JDBCUtils.createTableFromSQL(createSqlFileName, dataSource,
this.getClass().getClassLoader());
}
}
diff --git
a/gateway-server/src/main/java/org/apache/knox/gateway/util/JDBCUtils.java
b/gateway-server/src/main/java/org/apache/knox/gateway/util/JDBCUtils.java
deleted file mode 100644
index efb2c3ea2..000000000
--- a/gateway-server/src/main/java/org/apache/knox/gateway/util/JDBCUtils.java
+++ /dev/null
@@ -1,207 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.knox.gateway.util;
-
-import static java.nio.charset.StandardCharsets.UTF_8;
-
-import java.io.InputStream;
-import java.sql.Connection;
-import java.sql.DatabaseMetaData;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.sql.Statement;
-import java.util.Locale;
-
-import javax.sql.DataSource;
-
-import org.apache.commons.io.IOUtils;
-import org.apache.commons.lang3.StringUtils;
-import org.apache.derby.jdbc.EmbeddedDataSource;
-import org.apache.knox.gateway.config.GatewayConfig;
-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;
-
-import com.mysql.cj.conf.PropertyDefinitions;
-import com.mysql.cj.jdbc.MysqlDataSource;
-
-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";
- public static final String DATABASE_PASSWORD_ALIAS_NAME =
"gateway_database_password";
- public static final String DATABASE_TRUSTSTORE_PASSWORD_ALIAS_NAME =
"gateway_database_ssl_truststore_password";
-
- public static DataSource getDataSource(GatewayConfig gatewayConfig,
AliasService aliasService) throws AliasServiceException, SQLException {
- if (POSTGRESQL_DB_TYPE.equalsIgnoreCase(gatewayConfig.getDatabaseType())) {
- return createPostgresDataSource(gatewayConfig, aliasService);
- } else if
(DERBY_DB_TYPE.equalsIgnoreCase(gatewayConfig.getDatabaseType())) {
- return createDerbyDatasource(gatewayConfig, aliasService);
- } else if (HSQL.equalsIgnoreCase(gatewayConfig.getDatabaseType())) {
- 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());
- }
-
- private static DataSource createPostgresDataSource(GatewayConfig
gatewayConfig, AliasService aliasService) throws AliasServiceException {
- final PGSimpleDataSource postgresDataSource = new PGSimpleDataSource();
- final String dbUser = getDatabaseUser(aliasService);
- final String dbPassword = getDatabasePassword(aliasService);
- if (gatewayConfig.getDatabaseConnectionUrl() != null) {
- postgresDataSource.setUrl(gatewayConfig.getDatabaseConnectionUrl());
-
- // avoid nullifying already configured user/password properties in case
they
- // were already set in the given JDBC URL but not saved as aliases
- if (StringUtils.isNotBlank(dbUser)) {
- postgresDataSource.setUser(dbUser);
- }
- if (StringUtils.isNotBlank(dbPassword)) {
- postgresDataSource.setPassword(dbPassword);
- }
- } else {
- postgresDataSource.setDatabaseName(gatewayConfig.getDatabaseName());
- postgresDataSource.setServerNames(new String[] {
gatewayConfig.getDatabaseHost() });
- postgresDataSource.setPortNumbers(new int[] {
gatewayConfig.getDatabasePort() });
- postgresDataSource.setUser(dbUser);
- postgresDataSource.setPassword(dbPassword);
- }
-
- configurePostgreSQLSsl(gatewayConfig, aliasService, postgresDataSource);
-
- return postgresDataSource;
- }
-
- private static void configurePostgreSQLSsl(GatewayConfig gatewayConfig,
AliasService aliasService, PGSimpleDataSource postgresDataSource) throws
AliasServiceException {
- if (gatewayConfig.isDatabaseSslEnabled()) {
- postgresDataSource.setSsl(true);
- postgresDataSource.setSslMode(SslMode.VERIFY_FULL.value);
- if (gatewayConfig.verifyDatabaseSslServerCertificate()) {
-
postgresDataSource.setSslRootCert(gatewayConfig.getDatabaseSslTruststoreFileName());
- postgresDataSource.setSslPassword(getDatabaseAlias(aliasService,
DATABASE_TRUSTSTORE_PASSWORD_ALIAS_NAME));
- } else {
-
postgresDataSource.setSslfactory(NonValidatingFactory.class.getCanonicalName());
- }
- }
- }
-
- private static DataSource createDerbyDatasource(GatewayConfig gatewayConfig,
AliasService aliasService) throws AliasServiceException {
- final EmbeddedDataSource embeddedDataSource = new EmbeddedDataSource();
- embeddedDataSource.setDatabaseName(gatewayConfig.getDatabaseName());
- embeddedDataSource.setUser(getDatabaseUser(aliasService));
- embeddedDataSource.setPassword(getDatabasePassword(aliasService));
- return embeddedDataSource;
- }
-
-
- private static DataSource createHsqlDatasource(GatewayConfig gatewayConfig,
AliasService aliasService) throws AliasServiceException {
- JDBCDataSource hsqlDatasource = new JDBCDataSource();
- hsqlDatasource.setUrl(gatewayConfig.getDatabaseConnectionUrl());
- hsqlDatasource.setUser(getDatabaseUser(aliasService));
- hsqlDatasource.setPassword(getDatabasePassword(aliasService));
- return hsqlDatasource;
- }
-
- private static DataSource createMySqlDataSource(GatewayConfig gatewayConfig,
AliasService aliasService) throws AliasServiceException, SQLException {
- MysqlDataSource dataSource = new MysqlDataSource();
- if (gatewayConfig.getDatabaseConnectionUrl() != null) {
- dataSource.setUrl(gatewayConfig.getDatabaseConnectionUrl());
- } else {
- dataSource.setDatabaseName(gatewayConfig.getDatabaseName());
- dataSource.setServerName(gatewayConfig.getDatabaseHost());
- dataSource.setPortNumber(gatewayConfig.getDatabasePort());
- dataSource.setUser(getDatabaseUser(aliasService));
- dataSource.setPassword(getDatabasePassword(aliasService));
- configureMysqlSsl(gatewayConfig, aliasService, dataSource);
- }
- return dataSource;
- }
-
- private static void configureMysqlSsl(GatewayConfig gatewayConfig,
AliasService aliasService, MysqlDataSource dataSource) throws
AliasServiceException, SQLException {
- if (gatewayConfig.isDatabaseSslEnabled()) {
- dataSource.setUseSSL(true);
- if (gatewayConfig.verifyDatabaseSslServerCertificate()) {
- dataSource.setSslMode(PropertyDefinitions.SslMode.VERIFY_CA.name());
- dataSource.setVerifyServerCertificate(true);
- dataSource.setTrustCertificateKeyStoreType("JKS");
- dataSource.setTrustCertificateKeyStoreUrl("file:"+
gatewayConfig.getDatabaseSslTruststoreFileName());
-
dataSource.setTrustCertificateKeyStorePassword(getDatabaseAlias(aliasService,
DATABASE_TRUSTSTORE_PASSWORD_ALIAS_NAME));
- } else {
- dataSource.setVerifyServerCertificate(false);
- }
- }
- }
-
- 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);
- }
-
- private static String getDatabasePassword(AliasService aliasService) throws
AliasServiceException {
- return getDatabaseAlias(aliasService, DATABASE_PASSWORD_ALIAS_NAME);
- }
-
- private static String getDatabaseAlias(AliasService aliasService, String
aliasName) throws AliasServiceException {
- final char[] value =
aliasService.getPasswordFromAliasForGateway(aliasName);
- return value == null ? null : new String(value);
- }
-
- public static boolean isTableExists(String tableName, DataSource dataSource)
throws SQLException {
- boolean exists = false;
- try (Connection connection = dataSource.getConnection()) {
- final DatabaseMetaData dbMetadata = connection.getMetaData();
- final String tableNameToCheck = dbMetadata.storesUpperCaseIdentifiers()
? tableName : tableName.toLowerCase(Locale.ROOT);
- try (ResultSet tables = dbMetadata.getTables(connection.getCatalog(),
null, tableNameToCheck, null)) {
- exists = tables.next();
- }
- }
- return exists;
- }
-
- public static void createTable(String createSqlFileName, DataSource
dataSource, ClassLoader classLoader) throws Exception {
- final InputStream is = classLoader.getResourceAsStream(createSqlFileName);
- String createTableSql = IOUtils.toString(is, UTF_8);
- if (isDerbyDatasource(dataSource)) {
- createTableSql = createTableSql.replaceAll("IF NOT EXISTS ", "");
- }
- try (Connection connection = dataSource.getConnection(); Statement
createTableStatment = connection.createStatement();) {
- createTableStatment.execute(createTableSql);
- }
- }
-
- private static boolean isDerbyDatasource(DataSource dataSource) {
- return dataSource.getClass().getName().contains("derby");
- }
-}
diff --git a/gateway-server/src/main/resources/createKnoxDescriptorsTable.sql
b/gateway-server/src/main/resources/createKnoxDescriptorsTable.sql
index 614c7a139..61589d2f0 100644
--- a/gateway-server/src/main/resources/createKnoxDescriptorsTable.sql
+++ b/gateway-server/src/main/resources/createKnoxDescriptorsTable.sql
@@ -13,10 +13,10 @@
-- License for the specific language governing permissions and limitations
under
-- the License.
-CREATE TABLE IF NOT EXISTS KNOX_DESCRIPTORS ( -- IF NOT EXISTS syntax is not
supported by Derby
+CREATE TABLE IF NOT EXISTS KNOX_DESCRIPTORS (
name varchar(256) NOT NULL,
content TEXT NOT NULL,
last_modified_time TIMESTAMP NOT NULL,
deleted boolean DEFAULT false NOT NULL,
PRIMARY KEY (name)
-)
\ No newline at end of file
+)
diff --git a/gateway-server/src/main/resources/createKnoxProvidersTable.sql
b/gateway-server/src/main/resources/createKnoxDescriptorsTableDerby.sql
similarity index 91%
copy from gateway-server/src/main/resources/createKnoxProvidersTable.sql
copy to gateway-server/src/main/resources/createKnoxDescriptorsTableDerby.sql
index a4eda9dd9..6414a138b 100644
--- a/gateway-server/src/main/resources/createKnoxProvidersTable.sql
+++ b/gateway-server/src/main/resources/createKnoxDescriptorsTableDerby.sql
@@ -13,10 +13,10 @@
-- License for the specific language governing permissions and limitations
under
-- the License.
-CREATE TABLE IF NOT EXISTS KNOX_PROVIDERS ( -- IF NOT EXISTS syntax is not
supported by Derby
+CREATE TABLE KNOX_DESCRIPTORS (
name varchar(256) NOT NULL,
content TEXT NOT NULL,
last_modified_time TIMESTAMP NOT NULL,
deleted boolean DEFAULT false NOT NULL,
PRIMARY KEY (name)
-)
\ No newline at end of file
+)
diff --git a/gateway-server/src/main/resources/createKnoxProvidersTable.sql
b/gateway-server/src/main/resources/createKnoxDescriptorsTableOracle.sql
similarity index 81%
copy from gateway-server/src/main/resources/createKnoxProvidersTable.sql
copy to gateway-server/src/main/resources/createKnoxDescriptorsTableOracle.sql
index a4eda9dd9..70d9f4e02 100644
--- a/gateway-server/src/main/resources/createKnoxProvidersTable.sql
+++ b/gateway-server/src/main/resources/createKnoxDescriptorsTableOracle.sql
@@ -13,10 +13,10 @@
-- License for the specific language governing permissions and limitations
under
-- the License.
-CREATE TABLE IF NOT EXISTS KNOX_PROVIDERS ( -- IF NOT EXISTS syntax is not
supported by Derby
- name varchar(256) NOT NULL,
- content TEXT NOT NULL,
+CREATE TABLE KNOX_DESCRIPTORS (
+ name varchar2(256) NOT NULL,
+ content CLOB NOT NULL,
last_modified_time TIMESTAMP NOT NULL,
- deleted boolean DEFAULT false NOT NULL,
+ deleted NUMBER(1) DEFAULT false NOT NULL,
PRIMARY KEY (name)
-)
\ No newline at end of file
+)
diff --git a/gateway-server/src/main/resources/createKnoxProvidersTable.sql
b/gateway-server/src/main/resources/createKnoxProvidersTable.sql
index a4eda9dd9..5dfdd0d96 100644
--- a/gateway-server/src/main/resources/createKnoxProvidersTable.sql
+++ b/gateway-server/src/main/resources/createKnoxProvidersTable.sql
@@ -13,10 +13,10 @@
-- License for the specific language governing permissions and limitations
under
-- the License.
-CREATE TABLE IF NOT EXISTS KNOX_PROVIDERS ( -- IF NOT EXISTS syntax is not
supported by Derby
+CREATE TABLE IF NOT EXISTS KNOX_PROVIDERS (
name varchar(256) NOT NULL,
content TEXT NOT NULL,
last_modified_time TIMESTAMP NOT NULL,
deleted boolean DEFAULT false NOT NULL,
PRIMARY KEY (name)
-)
\ No newline at end of file
+)
diff --git a/gateway-server/src/main/resources/createKnoxProvidersTable.sql
b/gateway-server/src/main/resources/createKnoxProvidersTableDerby.sql
similarity index 91%
copy from gateway-server/src/main/resources/createKnoxProvidersTable.sql
copy to gateway-server/src/main/resources/createKnoxProvidersTableDerby.sql
index a4eda9dd9..426431859 100644
--- a/gateway-server/src/main/resources/createKnoxProvidersTable.sql
+++ b/gateway-server/src/main/resources/createKnoxProvidersTableDerby.sql
@@ -13,10 +13,10 @@
-- License for the specific language governing permissions and limitations
under
-- the License.
-CREATE TABLE IF NOT EXISTS KNOX_PROVIDERS ( -- IF NOT EXISTS syntax is not
supported by Derby
+CREATE TABLE KNOX_PROVIDERS (
name varchar(256) NOT NULL,
content TEXT NOT NULL,
last_modified_time TIMESTAMP NOT NULL,
deleted boolean DEFAULT false NOT NULL,
PRIMARY KEY (name)
-)
\ No newline at end of file
+)
diff --git a/gateway-server/src/main/resources/createKnoxProvidersTable.sql
b/gateway-server/src/main/resources/createKnoxProvidersTableOracle.sql
similarity index 81%
copy from gateway-server/src/main/resources/createKnoxProvidersTable.sql
copy to gateway-server/src/main/resources/createKnoxProvidersTableOracle.sql
index a4eda9dd9..b46221a49 100644
--- a/gateway-server/src/main/resources/createKnoxProvidersTable.sql
+++ b/gateway-server/src/main/resources/createKnoxProvidersTableOracle.sql
@@ -13,10 +13,10 @@
-- License for the specific language governing permissions and limitations
under
-- the License.
-CREATE TABLE IF NOT EXISTS KNOX_PROVIDERS ( -- IF NOT EXISTS syntax is not
supported by Derby
- name varchar(256) NOT NULL,
- content TEXT NOT NULL,
+CREATE TABLE KNOX_PROVIDERS (
+ name varchar2(256) NOT NULL,
+ content CLOB NOT NULL,
last_modified_time TIMESTAMP NOT NULL,
- deleted boolean DEFAULT false NOT NULL,
+ deleted NUMBER(1) DEFAULT false NOT NULL,
PRIMARY KEY (name)
-)
\ No newline at end of file
+)
diff --git a/gateway-server/src/main/resources/createKnoxTokenDatabaseTable.sql
b/gateway-server/src/main/resources/createKnoxTokenDatabaseTable.sql
index ff0aba7e6..7df06520d 100644
--- a/gateway-server/src/main/resources/createKnoxTokenDatabaseTable.sql
+++ b/gateway-server/src/main/resources/createKnoxTokenDatabaseTable.sql
@@ -13,10 +13,10 @@
-- License for the specific language governing permissions and limitations
under
-- the License.
-CREATE TABLE IF NOT EXISTS KNOX_TOKENS ( -- IF NOT EXISTS syntax is not
supported by Derby
+CREATE TABLE IF NOT EXISTS KNOX_TOKENS (
token_id varchar(128) NOT NULL,
issue_time bigint NOT NULL,
expiration bigint NOT NULL,
max_lifetime bigint NOT NULL,
PRIMARY KEY (token_id)
-)
\ No newline at end of file
+)
diff --git a/gateway-server/src/main/resources/createKnoxTokenDatabaseTable.sql
b/gateway-server/src/main/resources/createKnoxTokenDatabaseTableDerby.sql
similarity index 91%
copy from gateway-server/src/main/resources/createKnoxTokenDatabaseTable.sql
copy to gateway-server/src/main/resources/createKnoxTokenDatabaseTableDerby.sql
index ff0aba7e6..06e960e6f 100644
--- a/gateway-server/src/main/resources/createKnoxTokenDatabaseTable.sql
+++ b/gateway-server/src/main/resources/createKnoxTokenDatabaseTableDerby.sql
@@ -13,10 +13,10 @@
-- License for the specific language governing permissions and limitations
under
-- the License.
-CREATE TABLE IF NOT EXISTS KNOX_TOKENS ( -- IF NOT EXISTS syntax is not
supported by Derby
+CREATE TABLE KNOX_TOKENS (
token_id varchar(128) NOT NULL,
issue_time bigint NOT NULL,
expiration bigint NOT NULL,
max_lifetime bigint NOT NULL,
PRIMARY KEY (token_id)
-)
\ No newline at end of file
+)
diff --git a/gateway-server/src/main/resources/createKnoxTokenDatabaseTable.sql
b/gateway-server/src/main/resources/createKnoxTokenDatabaseTableOracle.sql
similarity index 78%
copy from gateway-server/src/main/resources/createKnoxTokenDatabaseTable.sql
copy to gateway-server/src/main/resources/createKnoxTokenDatabaseTableOracle.sql
index ff0aba7e6..529ea2569 100644
--- a/gateway-server/src/main/resources/createKnoxTokenDatabaseTable.sql
+++ b/gateway-server/src/main/resources/createKnoxTokenDatabaseTableOracle.sql
@@ -13,10 +13,10 @@
-- License for the specific language governing permissions and limitations
under
-- the License.
-CREATE TABLE IF NOT EXISTS KNOX_TOKENS ( -- IF NOT EXISTS syntax is not
supported by Derby
- token_id varchar(128) NOT NULL,
- issue_time bigint NOT NULL,
- expiration bigint NOT NULL,
- max_lifetime bigint NOT NULL,
+CREATE TABLE KNOX_TOKENS (
+ token_id varchar2(128) NOT NULL,
+ issue_time number NOT NULL,
+ expiration number NOT NULL,
+ max_lifetime number NOT NULL,
PRIMARY KEY (token_id)
-)
\ No newline at end of file
+)
diff --git
a/gateway-server/src/main/resources/createKnoxTokenMetadataDatabaseTable.sql
b/gateway-server/src/main/resources/createKnoxTokenMetadataDatabaseTable.sql
index 5c69dc8cb..a578d83d9 100644
--- a/gateway-server/src/main/resources/createKnoxTokenMetadataDatabaseTable.sql
+++ b/gateway-server/src/main/resources/createKnoxTokenMetadataDatabaseTable.sql
@@ -13,10 +13,10 @@
-- License for the specific language governing permissions and limitations
under
-- the License.
-CREATE TABLE IF NOT EXISTS KNOX_TOKEN_METADATA ( -- IF NOT EXISTS syntax is
not supported by Derby
+CREATE TABLE IF NOT EXISTS KNOX_TOKEN_METADATA (
token_id varchar(128) NOT NULL,
md_name varchar(32) NOT NULL,
md_value varchar(256) NOT NULL,
PRIMARY KEY (token_id, md_name),
CONSTRAINT fk_token_id FOREIGN KEY(token_id) REFERENCES
KNOX_TOKENS(token_id) ON DELETE CASCADE
-)
\ No newline at end of file
+)
diff --git
a/gateway-server/src/main/resources/createKnoxTokenMetadataDatabaseTable.sql
b/gateway-server/src/main/resources/createKnoxTokenMetadataDatabaseTableDerby.sql
similarity index 91%
copy from
gateway-server/src/main/resources/createKnoxTokenMetadataDatabaseTable.sql
copy to
gateway-server/src/main/resources/createKnoxTokenMetadataDatabaseTableDerby.sql
index 5c69dc8cb..489a64f29 100644
--- a/gateway-server/src/main/resources/createKnoxTokenMetadataDatabaseTable.sql
+++
b/gateway-server/src/main/resources/createKnoxTokenMetadataDatabaseTableDerby.sql
@@ -13,10 +13,10 @@
-- License for the specific language governing permissions and limitations
under
-- the License.
-CREATE TABLE IF NOT EXISTS KNOX_TOKEN_METADATA ( -- IF NOT EXISTS syntax is
not supported by Derby
+CREATE TABLE KNOX_TOKEN_METADATA (
token_id varchar(128) NOT NULL,
md_name varchar(32) NOT NULL,
md_value varchar(256) NOT NULL,
PRIMARY KEY (token_id, md_name),
CONSTRAINT fk_token_id FOREIGN KEY(token_id) REFERENCES
KNOX_TOKENS(token_id) ON DELETE CASCADE
-)
\ No newline at end of file
+)
diff --git
a/gateway-server/src/main/resources/createKnoxTokenMetadataDatabaseTable.sql
b/gateway-server/src/main/resources/createKnoxTokenMetadataDatabaseTableOracle.sql
similarity index 82%
copy from
gateway-server/src/main/resources/createKnoxTokenMetadataDatabaseTable.sql
copy to
gateway-server/src/main/resources/createKnoxTokenMetadataDatabaseTableOracle.sql
index 5c69dc8cb..0d32b1a89 100644
--- a/gateway-server/src/main/resources/createKnoxTokenMetadataDatabaseTable.sql
+++
b/gateway-server/src/main/resources/createKnoxTokenMetadataDatabaseTableOracle.sql
@@ -13,10 +13,10 @@
-- License for the specific language governing permissions and limitations
under
-- the License.
-CREATE TABLE IF NOT EXISTS KNOX_TOKEN_METADATA ( -- IF NOT EXISTS syntax is
not supported by Derby
- token_id varchar(128) NOT NULL,
- md_name varchar(32) NOT NULL,
- md_value varchar(256) NOT NULL,
+CREATE TABLE KNOX_TOKEN_METADATA (
+ token_id varchar2(128) NOT NULL,
+ md_name varchar2(32) NOT NULL,
+ md_value varchar2(256) NOT NULL,
PRIMARY KEY (token_id, md_name),
CONSTRAINT fk_token_id FOREIGN KEY(token_id) REFERENCES
KNOX_TOKENS(token_id) ON DELETE CASCADE
-)
\ No newline at end of file
+)
diff --git
a/gateway-server/src/test/java/org/apache/knox/gateway/util/JDBCUtilsTest.java
b/gateway-server/src/test/java/org/apache/knox/gateway/database/DataSourceFactoryTest.java
similarity index 61%
rename from
gateway-server/src/test/java/org/apache/knox/gateway/util/JDBCUtilsTest.java
rename to
gateway-server/src/test/java/org/apache/knox/gateway/database/DataSourceFactoryTest.java
index 730827ef5..efaae22e0 100644
---
a/gateway-server/src/test/java/org/apache/knox/gateway/util/JDBCUtilsTest.java
+++
b/gateway-server/src/test/java/org/apache/knox/gateway/database/DataSourceFactoryTest.java
@@ -15,15 +15,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.knox.gateway.util;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertTrue;
-
+package org.apache.knox.gateway.database;
import com.mysql.cj.jdbc.MysqlDataSource;
+import oracle.jdbc.pool.OracleDataSource;
import org.apache.derby.jdbc.EmbeddedDataSource;
import org.apache.knox.gateway.config.GatewayConfig;
import org.apache.knox.gateway.services.security.AliasService;
@@ -34,16 +29,21 @@ import org.mariadb.jdbc.MariaDbDataSource;
import org.postgresql.ds.PGSimpleDataSource;
import org.postgresql.ssl.NonValidatingFactory;
-public class JDBCUtilsTest {
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+public class DataSourceFactoryTest {
@Test
public void shouldReturnPostgresDataSource() throws Exception {
final GatewayConfig gatewayConfig =
EasyMock.createNiceMock(GatewayConfig.class);
-
EasyMock.expect(gatewayConfig.getDatabaseType()).andReturn(JDBCUtils.POSTGRESQL_DB_TYPE).anyTimes();
+
EasyMock.expect(gatewayConfig.getDatabaseType()).andReturn(DatabaseType.POSTGRESQL.type()).anyTimes();
final AliasService aliasService =
EasyMock.createNiceMock(AliasService.class);
EasyMock.expect(aliasService.getPasswordFromAliasForGateway(EasyMock.anyString())).andReturn(null).anyTimes();
EasyMock.replay(gatewayConfig, aliasService);
- assertTrue(JDBCUtils.getDataSource(gatewayConfig, aliasService) instanceof
PGSimpleDataSource);
+ assertTrue(DataSourceFactory.getDataSource(gatewayConfig, aliasService)
instanceof PGSimpleDataSource);
}
@Test
@@ -52,7 +52,7 @@ public class JDBCUtilsTest {
final AliasService aliasService =
EasyMock.createNiceMock(AliasService.class);
setBasicPostgresExpectations(gatewayConfig, aliasService);
EasyMock.replay(gatewayConfig, aliasService);
- final PGSimpleDataSource dataSource = (PGSimpleDataSource)
JDBCUtils.getDataSource(gatewayConfig, aliasService);
+ final PGSimpleDataSource dataSource = (PGSimpleDataSource)
DataSourceFactory.getDataSource(gatewayConfig, aliasService);
assertEquals("localhost", dataSource.getServerNames()[0]);
assertEquals(5432, dataSource.getPortNumbers()[0]);
assertEquals("sampleDatabase", dataSource.getDatabaseName());
@@ -62,12 +62,12 @@ public class JDBCUtilsTest {
}
private void setBasicPostgresExpectations(GatewayConfig gatewayConfig,
AliasService aliasService) throws AliasServiceException {
-
EasyMock.expect(gatewayConfig.getDatabaseType()).andReturn(JDBCUtils.POSTGRESQL_DB_TYPE).anyTimes();
+
EasyMock.expect(gatewayConfig.getDatabaseType()).andReturn(DatabaseType.POSTGRESQL.type()).anyTimes();
EasyMock.expect(gatewayConfig.getDatabaseHost()).andReturn("localhost").anyTimes();
EasyMock.expect(gatewayConfig.getDatabasePort()).andReturn(5432).anyTimes();
EasyMock.expect(gatewayConfig.getDatabaseName()).andReturn("sampleDatabase");
-
EasyMock.expect(aliasService.getPasswordFromAliasForGateway(JDBCUtils.DATABASE_USER_ALIAS_NAME)).andReturn("user".toCharArray()).anyTimes();
-
EasyMock.expect(aliasService.getPasswordFromAliasForGateway(JDBCUtils.DATABASE_PASSWORD_ALIAS_NAME)).andReturn("password".toCharArray()).anyTimes();
+
EasyMock.expect(aliasService.getPasswordFromAliasForGateway(AbstractDataSource.DATABASE_USER_ALIAS_NAME)).andReturn("user".toCharArray()).anyTimes();
+
EasyMock.expect(aliasService.getPasswordFromAliasForGateway(AbstractDataSource.DATABASE_PASSWORD_ALIAS_NAME)).andReturn("password".toCharArray()).anyTimes();
}
@Test
@@ -81,7 +81,7 @@ public class JDBCUtilsTest {
EasyMock.expect(gatewayConfig.verifyDatabaseSslServerCertificate()).andReturn(false).anyTimes();
EasyMock.replay(gatewayConfig, aliasService);
- final PGSimpleDataSource dataSource = (PGSimpleDataSource)
JDBCUtils.getDataSource(gatewayConfig, aliasService);
+ final PGSimpleDataSource dataSource = (PGSimpleDataSource)
DataSourceFactory.getDataSource(gatewayConfig, aliasService);
assertTrue(dataSource.isSsl());
assertNull(dataSource.getSslRootCert());
assertEquals(dataSource.getSslfactory(),
NonValidatingFactory.class.getCanonicalName());
@@ -98,10 +98,10 @@ public class JDBCUtilsTest {
EasyMock.expect(gatewayConfig.isDatabaseSslEnabled()).andReturn(true).anyTimes();
EasyMock.expect(gatewayConfig.verifyDatabaseSslServerCertificate()).andReturn(true).anyTimes();
EasyMock.expect(gatewayConfig.getDatabaseSslTruststoreFileName()).andReturn("/sample/file/path").anyTimes();
-
EasyMock.expect(aliasService.getPasswordFromAliasForGateway(JDBCUtils.DATABASE_TRUSTSTORE_PASSWORD_ALIAS_NAME)).andReturn("password".toCharArray()).anyTimes();
+
EasyMock.expect(aliasService.getPasswordFromAliasForGateway(AbstractDataSource.DATABASE_TRUSTSTORE_PASSWORD_ALIAS_NAME)).andReturn("password".toCharArray()).anyTimes();
EasyMock.replay(gatewayConfig, aliasService);
- final PGSimpleDataSource dataSource = (PGSimpleDataSource)
JDBCUtils.getDataSource(gatewayConfig, aliasService);
+ final PGSimpleDataSource dataSource = (PGSimpleDataSource)
DataSourceFactory.getDataSource(gatewayConfig, aliasService);
assertTrue(dataSource.isSsl());
assertEquals(dataSource.getSslRootCert(), "/sample/file/path");
EasyMock.verify(gatewayConfig, aliasService);
@@ -112,12 +112,12 @@ public class JDBCUtilsTest {
final String connectionUrl =
"jdbc:postgresql://postgresql_host:1234/testDb?user=smolnar&password=secret&ssl=true&sslmode=verify-ca&sslrootcert=/var/lib/knox/gateway/conf/postgresql/root.crt";
final GatewayConfig gatewayConfig =
EasyMock.createNiceMock(GatewayConfig.class);
final AliasService aliasService =
EasyMock.createNiceMock(AliasService.class);
-
EasyMock.expect(aliasService.getPasswordFromAliasForGateway(JDBCUtils.DATABASE_USER_ALIAS_NAME)).andReturn(null).anyTimes();
-
EasyMock.expect(aliasService.getPasswordFromAliasForGateway(JDBCUtils.DATABASE_PASSWORD_ALIAS_NAME)).andReturn(null).anyTimes();
-
EasyMock.expect(gatewayConfig.getDatabaseType()).andReturn(JDBCUtils.POSTGRESQL_DB_TYPE).anyTimes();
+
EasyMock.expect(aliasService.getPasswordFromAliasForGateway(AbstractDataSource.DATABASE_USER_ALIAS_NAME)).andReturn(null).anyTimes();
+
EasyMock.expect(aliasService.getPasswordFromAliasForGateway(AbstractDataSource.DATABASE_PASSWORD_ALIAS_NAME)).andReturn(null).anyTimes();
+
EasyMock.expect(gatewayConfig.getDatabaseType()).andReturn(DatabaseType.POSTGRESQL.type()).anyTimes();
EasyMock.expect(gatewayConfig.getDatabaseConnectionUrl()).andReturn(connectionUrl).anyTimes();
EasyMock.replay(gatewayConfig, aliasService);
- final PGSimpleDataSource dataSource = (PGSimpleDataSource)
JDBCUtils.getDataSource(gatewayConfig, aliasService);
+ final PGSimpleDataSource dataSource = (PGSimpleDataSource)
DataSourceFactory.getDataSource(gatewayConfig, aliasService);
assertEquals("postgresql_host", dataSource.getServerNames()[0]);
assertEquals(1234, dataSource.getPortNumbers()[0]);
assertEquals("testDb", dataSource.getDatabaseName());
@@ -131,23 +131,23 @@ public class JDBCUtilsTest {
@Test
public void shouldReturnDerbyDataSource() throws Exception {
final GatewayConfig gatewayConfig =
EasyMock.createNiceMock(GatewayConfig.class);
-
EasyMock.expect(gatewayConfig.getDatabaseType()).andReturn(JDBCUtils.DERBY_DB_TYPE).anyTimes();
+
EasyMock.expect(gatewayConfig.getDatabaseType()).andReturn(DatabaseType.DERBY.type()).anyTimes();
final AliasService aliasService =
EasyMock.createNiceMock(AliasService.class);
EasyMock.expect(aliasService.getPasswordFromAliasForGateway(EasyMock.anyString())).andReturn(null).anyTimes();
EasyMock.replay(gatewayConfig, aliasService);
- assertTrue(JDBCUtils.getDataSource(gatewayConfig, aliasService) instanceof
EmbeddedDataSource);
+ assertTrue(DataSourceFactory.getDataSource(gatewayConfig, aliasService)
instanceof EmbeddedDataSource);
}
@Test
public void derbyDataSourceShouldHaveProperConnectionProperties() throws
Exception {
final GatewayConfig gatewayConfig =
EasyMock.createNiceMock(GatewayConfig.class);
-
EasyMock.expect(gatewayConfig.getDatabaseType()).andReturn(JDBCUtils.DERBY_DB_TYPE).anyTimes();
+
EasyMock.expect(gatewayConfig.getDatabaseType()).andReturn(DatabaseType.DERBY.type()).anyTimes();
EasyMock.expect(gatewayConfig.getDatabaseName()).andReturn("sampleDatabase");
final AliasService aliasService =
EasyMock.createNiceMock(AliasService.class);
-
EasyMock.expect(aliasService.getPasswordFromAliasForGateway(JDBCUtils.DATABASE_USER_ALIAS_NAME)).andReturn("user".toCharArray()).anyTimes();
-
EasyMock.expect(aliasService.getPasswordFromAliasForGateway(JDBCUtils.DATABASE_PASSWORD_ALIAS_NAME)).andReturn("password".toCharArray()).anyTimes();
+
EasyMock.expect(aliasService.getPasswordFromAliasForGateway(AbstractDataSource.DATABASE_USER_ALIAS_NAME)).andReturn("user".toCharArray()).anyTimes();
+
EasyMock.expect(aliasService.getPasswordFromAliasForGateway(AbstractDataSource.DATABASE_PASSWORD_ALIAS_NAME)).andReturn("password".toCharArray()).anyTimes();
EasyMock.replay(gatewayConfig, aliasService);
- final EmbeddedDataSource dataSource = (EmbeddedDataSource)
JDBCUtils.getDataSource(gatewayConfig, aliasService);
+ final EmbeddedDataSource dataSource = (EmbeddedDataSource)
DataSourceFactory.getDataSource(gatewayConfig, aliasService);
assertEquals("sampleDatabase", dataSource.getDatabaseName());
assertEquals("user", dataSource.getUser());
assertEquals("password", dataSource.getPassword());
@@ -156,25 +156,25 @@ public class JDBCUtilsTest {
@Test
public void shouldReturnMySqlDataSource() throws Exception {
final GatewayConfig gatewayConfig =
EasyMock.createNiceMock(GatewayConfig.class);
-
EasyMock.expect(gatewayConfig.getDatabaseType()).andReturn(JDBCUtils.MYSQL_DB_TYPE).anyTimes();
+
EasyMock.expect(gatewayConfig.getDatabaseType()).andReturn(DatabaseType.MYSQL.type()).anyTimes();
final AliasService aliasService =
EasyMock.createNiceMock(AliasService.class);
EasyMock.expect(aliasService.getPasswordFromAliasForGateway(EasyMock.anyString())).andReturn(null).anyTimes();
EasyMock.replay(gatewayConfig, aliasService);
- assertTrue(JDBCUtils.getDataSource(gatewayConfig, aliasService) instanceof
MysqlDataSource);
+ assertTrue(DataSourceFactory.getDataSource(gatewayConfig, aliasService)
instanceof MysqlDataSource);
}
@Test
public void testMysqlDataSourceShouldHaveProperConnectionProperties() throws
Exception {
GatewayConfig gatewayConfig = EasyMock.createNiceMock(GatewayConfig.class);
AliasService aliasService = EasyMock.createNiceMock(AliasService.class);
-
EasyMock.expect(gatewayConfig.getDatabaseType()).andReturn(JDBCUtils.MYSQL_DB_TYPE).anyTimes();
+
EasyMock.expect(gatewayConfig.getDatabaseType()).andReturn(DatabaseType.MYSQL.type()).anyTimes();
EasyMock.expect(gatewayConfig.getDatabaseHost()).andReturn("localhost").anyTimes();
EasyMock.expect(gatewayConfig.getDatabasePort()).andReturn(5432).anyTimes();
EasyMock.expect(gatewayConfig.getDatabaseName()).andReturn("sampleDatabase");
-
EasyMock.expect(aliasService.getPasswordFromAliasForGateway(JDBCUtils.DATABASE_USER_ALIAS_NAME)).andReturn("user".toCharArray()).anyTimes();
-
EasyMock.expect(aliasService.getPasswordFromAliasForGateway(JDBCUtils.DATABASE_PASSWORD_ALIAS_NAME)).andReturn("password".toCharArray()).anyTimes();
+
EasyMock.expect(aliasService.getPasswordFromAliasForGateway(AbstractDataSource.DATABASE_USER_ALIAS_NAME)).andReturn("user".toCharArray()).anyTimes();
+
EasyMock.expect(aliasService.getPasswordFromAliasForGateway(AbstractDataSource.DATABASE_PASSWORD_ALIAS_NAME)).andReturn("password".toCharArray()).anyTimes();
EasyMock.replay(gatewayConfig, aliasService);
- MysqlDataSource dataSource = (MysqlDataSource)
JDBCUtils.getDataSource(gatewayConfig, aliasService);
+ MysqlDataSource dataSource = (MysqlDataSource)
DataSourceFactory.getDataSource(gatewayConfig, aliasService);
assertEquals("localhost", dataSource.getServerName());
assertEquals(5432, dataSource.getPortNumber());
assertEquals("sampleDatabase", dataSource.getDatabaseName());
@@ -188,10 +188,10 @@ public class JDBCUtilsTest {
public void testGetMySqlDatasourceFromJdbcConnectionUrl() throws Exception {
String connectionUrl =
"jdbc:mysql://mysql_host:1234/testDb?user=user&password=secret&ssl=true&sslmode=verify-ca&sslrootcert=/var/lib/knox/gateway/conf/postgresql/root.crt";
GatewayConfig gatewayConfig = EasyMock.createNiceMock(GatewayConfig.class);
-
EasyMock.expect(gatewayConfig.getDatabaseType()).andReturn(JDBCUtils.MYSQL_DB_TYPE).anyTimes();
+
EasyMock.expect(gatewayConfig.getDatabaseType()).andReturn(DatabaseType.MYSQL.type()).anyTimes();
EasyMock.expect(gatewayConfig.getDatabaseConnectionUrl()).andReturn(connectionUrl).anyTimes();
EasyMock.replay(gatewayConfig);
- MysqlDataSource dataSource = (MysqlDataSource)
JDBCUtils.getDataSource(gatewayConfig, null);
+ MysqlDataSource dataSource = (MysqlDataSource)
DataSourceFactory.getDataSource(gatewayConfig, null);
assertEquals(connectionUrl, dataSource.getUrl());
EasyMock.verify(gatewayConfig);
}
@@ -199,10 +199,10 @@ public class JDBCUtilsTest {
@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.getDatabaseType()).andReturn(DatabaseType.MARIADB.type()).anyTimes();
EasyMock.expect(gatewayConfig.getDatabaseConnectionUrl()).andReturn("jdbc:mariadb://localhost:1234").anyTimes();
EasyMock.replay(gatewayConfig);
- assertTrue(JDBCUtils.getDataSource(gatewayConfig, null) instanceof
MariaDbDataSource);
+ assertTrue(DataSourceFactory.getDataSource(gatewayConfig, null) instanceof
MariaDbDataSource);
}
@Test
@@ -220,12 +220,12 @@ public class JDBCUtilsTest {
boolean error = false;
try {
final GatewayConfig gatewayConfig =
EasyMock.createNiceMock(GatewayConfig.class);
-
EasyMock.expect(gatewayConfig.getDatabaseType()).andReturn(JDBCUtils.MARIA_DB_TYPE).anyTimes();
+
EasyMock.expect(gatewayConfig.getDatabaseType()).andReturn(DatabaseType.MARIADB.type()).anyTimes();
if (connectionUrl != null) {
EasyMock.expect(gatewayConfig.getDatabaseConnectionUrl()).andReturn(connectionUrl).anyTimes();
}
EasyMock.replay(gatewayConfig);
- JDBCUtils.getDataSource(gatewayConfig, null);
+ DataSourceFactory.getDataSource(gatewayConfig, null);
} catch (Exception e) {
error = true;
assertEquals(expectedError, e.getMessage());
@@ -233,4 +233,54 @@ public class JDBCUtilsTest {
assertTrue(error);
}
+ @Test
+ public void shouldReturnOracleDataSource() throws Exception {
+ final GatewayConfig gatewayConfig =
EasyMock.createNiceMock(GatewayConfig.class);
+
EasyMock.expect(gatewayConfig.getDatabaseType()).andReturn(DatabaseType.ORACLE.type()).anyTimes();
+ final AliasService aliasService =
EasyMock.createNiceMock(AliasService.class);
+
EasyMock.expect(aliasService.getPasswordFromAliasForGateway(EasyMock.anyString())).andReturn(null).anyTimes();
+ EasyMock.replay(gatewayConfig, aliasService);
+ assertTrue(DataSourceFactory.getDataSource(gatewayConfig, aliasService)
instanceof OracleDataSource);
+ }
+
+ @Test
+ public void oracleDataSourceShouldHaveProperConnectionProperties() throws
Exception {
+ final GatewayConfig gatewayConfig =
EasyMock.createNiceMock(GatewayConfig.class);
+ final AliasService aliasService =
EasyMock.createNiceMock(AliasService.class);
+ setBasicOracleExpectations(gatewayConfig, aliasService);
+ EasyMock.replay(gatewayConfig, aliasService);
+ final OracleDataSource dataSource = (OracleDataSource)
DataSourceFactory.getDataSource(gatewayConfig, aliasService);
+ assertEquals("localhost", dataSource.getServerName());
+ assertEquals(1521, dataSource.getPortNumber());
+ assertEquals("sampleDatabase", dataSource.getServiceName());
+ assertEquals("user", dataSource.getUser());
+ }
+
+ @Test
+ public void testGetOracleDatasourceFromJdbcConnectionUrl() throws Exception {
+ final String connectionUrl =
"jdbc:oracle:thin:testuser/testpw@oracle_host:1521/TESTDB";
+ final GatewayConfig gatewayConfig =
EasyMock.createNiceMock(GatewayConfig.class);
+ final AliasService aliasService =
EasyMock.createNiceMock(AliasService.class);
+
EasyMock.expect(aliasService.getPasswordFromAliasForGateway(AbstractDataSource.DATABASE_USER_ALIAS_NAME)).andReturn(null).anyTimes();
+
EasyMock.expect(aliasService.getPasswordFromAliasForGateway(AbstractDataSource.DATABASE_PASSWORD_ALIAS_NAME)).andReturn(null).anyTimes();
+
EasyMock.expect(gatewayConfig.getDatabaseType()).andReturn(DatabaseType.ORACLE.type()).anyTimes();
+
EasyMock.expect(gatewayConfig.getDatabaseConnectionUrl()).andReturn(connectionUrl).anyTimes();
+ EasyMock.replay(gatewayConfig, aliasService);
+ final OracleDataSource dataSource = (OracleDataSource)
DataSourceFactory.getDataSource(gatewayConfig, aliasService);
+ assertNull(dataSource.getUser());
+ assertEquals(0, dataSource.getPortNumber());
+ assertNull(dataSource.getServerName());
+ assertNull(dataSource.getServiceName());
+ assertEquals("jdbc:oracle:thin:testuser/testpw@oracle_host:1521/TESTDB",
dataSource.getURL());
+ EasyMock.verify(gatewayConfig);
+ }
+
+ private void setBasicOracleExpectations(GatewayConfig gatewayConfig,
AliasService aliasService) throws AliasServiceException {
+
EasyMock.expect(gatewayConfig.getDatabaseType()).andReturn(DatabaseType.ORACLE.type()).anyTimes();
+
EasyMock.expect(gatewayConfig.getDatabaseHost()).andReturn("localhost").anyTimes();
+
EasyMock.expect(gatewayConfig.getDatabasePort()).andReturn(1521).anyTimes();
+
EasyMock.expect(gatewayConfig.getDatabaseName()).andReturn("sampleDatabase");
+
EasyMock.expect(aliasService.getPasswordFromAliasForGateway(AbstractDataSource.DATABASE_USER_ALIAS_NAME)).andReturn("user".toCharArray()).anyTimes();
+
EasyMock.expect(aliasService.getPasswordFromAliasForGateway(AbstractDataSource.DATABASE_PASSWORD_ALIAS_NAME)).andReturn("password".toCharArray()).anyTimes();
+ }
}
diff --git
a/gateway-server/src/test/java/org/apache/knox/gateway/services/factory/ServiceFactoryTest.java
b/gateway-server/src/test/java/org/apache/knox/gateway/services/factory/ServiceFactoryTest.java
index 6c1fa6894..e75c7613a 100644
---
a/gateway-server/src/test/java/org/apache/knox/gateway/services/factory/ServiceFactoryTest.java
+++
b/gateway-server/src/test/java/org/apache/knox/gateway/services/factory/ServiceFactoryTest.java
@@ -17,26 +17,11 @@
*/
package org.apache.knox.gateway.services.factory;
-import static
org.apache.knox.gateway.services.security.AliasService.NO_CLUSTER_NAME;
-import static org.easymock.EasyMock.anyString;
-import static org.easymock.EasyMock.expect;
-import static org.easymock.EasyMock.replay;
-import static org.hamcrest.CoreMatchers.isA;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertTrue;
-
-import java.io.File;
-import java.io.IOException;
-import java.lang.reflect.Field;
-import java.nio.file.Paths;
-import java.util.HashMap;
-import java.util.Locale;
-import java.util.Map;
-
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.apache.knox.gateway.config.impl.GatewayConfigImpl;
+import org.apache.knox.gateway.database.AbstractDataSource;
+import org.apache.knox.gateway.database.DatabaseType;
import org.apache.knox.gateway.services.GatewayServices;
import org.apache.knox.gateway.services.Service;
import org.apache.knox.gateway.services.ServiceLifecycleException;
@@ -48,13 +33,29 @@ import
org.apache.knox.gateway.services.security.AliasServiceException;
import org.apache.knox.gateway.services.security.KeystoreService;
import org.apache.knox.gateway.services.security.MasterService;
import org.apache.knox.gateway.services.token.impl.DerbyDBTokenStateService;
-import org.apache.knox.gateway.util.JDBCUtils;
import org.apache.knox.test.TestUtils;
import org.easymock.EasyMock;
import org.junit.After;
import org.junit.Rule;
import org.junit.rules.ExpectedException;
+import java.io.File;
+import java.io.IOException;
+import java.lang.reflect.Field;
+import java.nio.file.Paths;
+import java.util.HashMap;
+import java.util.Locale;
+import java.util.Map;
+
+import static
org.apache.knox.gateway.services.security.AliasService.NO_CLUSTER_NAME;
+import static org.easymock.EasyMock.anyString;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
+import static org.hamcrest.CoreMatchers.isA;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
class ServiceFactoryTest {
@SuppressWarnings("deprecation")
@@ -81,15 +82,15 @@ class ServiceFactoryTest {
final AliasService aliasService =
EasyMock.createNiceMock(AliasService.class);
if (expectDbCredentialLookup) {
try {
- aliasService.addAliasForCluster(NO_CLUSTER_NAME,
JDBCUtils.DATABASE_USER_ALIAS_NAME,
DerbyDBTokenStateService.DEFAULT_TOKEN_DB_USER_NAME);
+ aliasService.addAliasForCluster(NO_CLUSTER_NAME,
AbstractDataSource.DATABASE_USER_ALIAS_NAME,
DerbyDBTokenStateService.DEFAULT_TOKEN_DB_USER_NAME);
EasyMock.expectLastCall().anyTimes();
- aliasService.addAliasForCluster(NO_CLUSTER_NAME,
JDBCUtils.DATABASE_PASSWORD_ALIAS_NAME, masterSecret);
+ aliasService.addAliasForCluster(NO_CLUSTER_NAME,
AbstractDataSource.DATABASE_PASSWORD_ALIAS_NAME, masterSecret);
EasyMock.expectLastCall().anyTimes();
-
expect(aliasService.getPasswordFromAliasForGateway(JDBCUtils.DATABASE_USER_ALIAS_NAME)).andReturn(DerbyDBTokenStateService.DEFAULT_TOKEN_DB_USER_NAME.toCharArray()).anyTimes();
-
expect(aliasService.getPasswordFromAliasForGateway(JDBCUtils.DATABASE_PASSWORD_ALIAS_NAME)).andReturn(masterSecret.toCharArray()).anyTimes();
+
expect(aliasService.getPasswordFromAliasForGateway(AbstractDataSource.DATABASE_USER_ALIAS_NAME)).andReturn(DerbyDBTokenStateService.DEFAULT_TOKEN_DB_USER_NAME.toCharArray()).anyTimes();
+
expect(aliasService.getPasswordFromAliasForGateway(AbstractDataSource.DATABASE_PASSWORD_ALIAS_NAME)).andReturn(masterSecret.toCharArray()).anyTimes();
// prepare GatewayConfig
-
expect(gatewayConfig.getDatabaseType()).andReturn(JDBCUtils.DERBY_DB_TYPE).anyTimes();
+
expect(gatewayConfig.getDatabaseType()).andReturn(DatabaseType.DERBY.type()).anyTimes();
tempDbFolder = TestUtils.createTempDir(this.getClass().getName());
expect(gatewayConfig.getGatewaySecurityDir()).andReturn(tempDbFolder.getAbsolutePath()).anyTimes();
expect(gatewayConfig.getDatabaseName()).andReturn(Paths.get(tempDbFolder.getAbsolutePath(),
DerbyDBTokenStateService.DB_NAME).toString()).anyTimes();
diff --git
a/gateway-server/src/test/java/org/apache/knox/gateway/services/token/impl/JDBCTokenStateServiceTest.java
b/gateway-server/src/test/java/org/apache/knox/gateway/services/token/impl/JDBCTokenStateServiceTest.java
index efa7b71c9..b42ebe8f1 100644
---
a/gateway-server/src/test/java/org/apache/knox/gateway/services/token/impl/JDBCTokenStateServiceTest.java
+++
b/gateway-server/src/test/java/org/apache/knox/gateway/services/token/impl/JDBCTokenStateServiceTest.java
@@ -17,11 +17,23 @@
*/
package org.apache.knox.gateway.services.token.impl;
-import static java.nio.charset.StandardCharsets.UTF_8;
-import static org.apache.knox.gateway.util.JDBCUtils.HSQL;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
+import org.apache.commons.codec.binary.Base64;
+import org.apache.commons.codec.digest.HmacAlgorithms;
+import org.apache.commons.lang3.reflect.FieldUtils;
+import org.apache.knox.gateway.config.GatewayConfig;
+import org.apache.knox.gateway.database.AbstractDataSource;
+import org.apache.knox.gateway.database.DatabaseType;
+import org.apache.knox.gateway.services.security.AliasService;
+import org.apache.knox.gateway.services.security.token.KnoxToken;
+import org.apache.knox.gateway.services.security.token.TokenMetadata;
+import org.apache.knox.gateway.services.security.token.UnknownTokenException;
+import org.apache.knox.gateway.services.security.token.impl.TokenMAC;
+import org.easymock.EasyMock;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.ClassRule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
import java.sql.Connection;
import java.sql.DriverManager;
@@ -38,22 +50,10 @@ import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
-import org.apache.commons.codec.binary.Base64;
-import org.apache.commons.codec.digest.HmacAlgorithms;
-import org.apache.commons.lang3.reflect.FieldUtils;
-import org.apache.knox.gateway.config.GatewayConfig;
-import org.apache.knox.gateway.services.security.AliasService;
-import org.apache.knox.gateway.services.security.token.KnoxToken;
-import org.apache.knox.gateway.services.security.token.TokenMetadata;
-import org.apache.knox.gateway.services.security.token.UnknownTokenException;
-import org.apache.knox.gateway.services.security.token.impl.TokenMAC;
-import org.apache.knox.gateway.util.JDBCUtils;
-import org.easymock.EasyMock;
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
-import org.junit.ClassRule;
-import org.junit.Test;
-import org.junit.rules.TemporaryFolder;
+import static java.nio.charset.StandardCharsets.UTF_8;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
public class JDBCTokenStateServiceTest {
@@ -74,12 +74,12 @@ public class JDBCTokenStateServiceTest {
@BeforeClass
public static void setUp() throws Exception {
final GatewayConfig gatewayConfig =
EasyMock.createNiceMock(GatewayConfig.class);
-
EasyMock.expect(gatewayConfig.getDatabaseType()).andReturn(HSQL).anyTimes();
+
EasyMock.expect(gatewayConfig.getDatabaseType()).andReturn(DatabaseType.HSQL.type()).anyTimes();
EasyMock.expect(gatewayConfig.getDatabaseConnectionUrl()).andReturn(CONNECTION_URL).anyTimes();
EasyMock.expect(gatewayConfig.getDatabaseName()).andReturn(DB_NAME).anyTimes();
final AliasService aliasService =
EasyMock.createNiceMock(AliasService.class);
-
EasyMock.expect(aliasService.getPasswordFromAliasForGateway(JDBCUtils.DATABASE_USER_ALIAS_NAME)).andReturn(USERNAME.toCharArray()).anyTimes();
-
EasyMock.expect(aliasService.getPasswordFromAliasForGateway(JDBCUtils.DATABASE_PASSWORD_ALIAS_NAME)).andReturn(PASSWORD.toCharArray()).anyTimes();
+
EasyMock.expect(aliasService.getPasswordFromAliasForGateway(AbstractDataSource.DATABASE_USER_ALIAS_NAME)).andReturn(USERNAME.toCharArray()).anyTimes();
+
EasyMock.expect(aliasService.getPasswordFromAliasForGateway(AbstractDataSource.DATABASE_PASSWORD_ALIAS_NAME)).andReturn(PASSWORD.toCharArray()).anyTimes();
EasyMock.replay(gatewayConfig, aliasService);
jdbcTokenStateService = new JDBCTokenStateService();
jdbcTokenStateService.setAliasService(aliasService);
diff --git
a/gateway-server/src/test/java/org/apache/knox/gateway/topology/monitor/db/RemoteConfigDatabaseTest.java
b/gateway-server/src/test/java/org/apache/knox/gateway/topology/monitor/db/RemoteConfigDatabaseTest.java
index 34c558482..01fa82cdb 100644
---
a/gateway-server/src/test/java/org/apache/knox/gateway/topology/monitor/db/RemoteConfigDatabaseTest.java
+++
b/gateway-server/src/test/java/org/apache/knox/gateway/topology/monitor/db/RemoteConfigDatabaseTest.java
@@ -52,7 +52,7 @@ public class RemoteConfigDatabaseTest {
@Before
public void setUp() throws Exception {
- db = new RemoteConfigDatabase(dataSource);
+ db = new RemoteConfigDatabase(dataSource, "hsql");
}
@After
@@ -195,4 +195,4 @@ public class RemoteConfigDatabaseTest {
assertEquals(0, db.selectProviders().size());
assertEquals(0, db.selectDescriptors().size());
}
-}
\ No newline at end of file
+}
diff --git a/pom.xml b/pom.xml
index 7b5c18c43..21b238991 100644
--- a/pom.xml
+++ b/pom.xml
@@ -267,6 +267,7 @@
<mysql.version>8.0.28</mysql.version>
<mariadb.connector.version>3.3.0</mariadb.connector.version>
<okio.version>3.6.0</okio.version>
+ <oracle.version>23.26.0.0.0</oracle.version>
<protobuf.version>3.25.8</protobuf.version>
<powermock.version>2.0.9</powermock.version>
<purejavacomm.version>0.0.11.1</purejavacomm.version>
@@ -2448,6 +2449,12 @@
<version>${mysql.version}</version>
<scope>provided</scope>
</dependency>
+ <dependency>
+ <groupId>com.oracle.database.jdbc</groupId>
+ <artifactId>ojdbc11</artifactId>
+ <version>${oracle.version}</version>
+ <scope>provided</scope>
+ </dependency>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>