Skip to content

Commit 5cdf8e6

Browse files
authored
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
1 parent eb4c2c7 commit 5cdf8e6

34 files changed

+947
-351
lines changed

gateway-server/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,11 @@
435435
<artifactId>mysql-connector-java</artifactId>
436436
<scope>provided</scope>
437437
</dependency>
438+
<dependency>
439+
<groupId>com.oracle.database.jdbc</groupId>
440+
<artifactId>ojdbc11</artifactId>
441+
<scope>provided</scope>
442+
</dependency>
438443
<dependency>
439444
<groupId>org.mariadb.jdbc</groupId>
440445
<artifactId>mariadb-java-client</artifactId>
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.apache.knox.gateway.database;
19+
20+
import org.apache.knox.gateway.config.GatewayConfig;
21+
import org.apache.knox.gateway.services.security.AliasService;
22+
import org.apache.knox.gateway.services.security.AliasServiceException;
23+
24+
import javax.sql.DataSource;
25+
import java.sql.SQLException;
26+
27+
public abstract class AbstractDataSource {
28+
29+
public static final String TOKENS_TABLE_CREATE_SQL_FILE_NAME = "createKnoxTokenDatabaseTable.sql";
30+
public static final String TOKEN_METADATA_TABLE_CREATE_SQL_FILE_NAME = "createKnoxTokenMetadataDatabaseTable.sql";
31+
public static final String ORACLE_TOKENS_TABLE_CREATE_SQL_FILE_NAME = "createKnoxTokenDatabaseTableOracle.sql";
32+
public static final String ORACLE_TOKEN_METADATA_TABLE_CREATE_SQL_FILE_NAME = "createKnoxTokenMetadataDatabaseTableOracle.sql";
33+
public static final String DERBY_TOKENS_TABLE_CREATE_SQL_FILE_NAME = "createKnoxTokenDatabaseTableDerby.sql";
34+
public static final String DERBY_TOKEN_METADATA_TABLE_CREATE_SQL_FILE_NAME = "createKnoxTokenMetadataDatabaseTableDerby.sql";
35+
36+
public static final String KNOX_PROVIDERS_TABLE_CREATE_SQL_FILE_NAME = "createKnoxProvidersTable.sql";
37+
public static final String KNOX_DESCRIPTORS_TABLE_CREATE_SQL_FILE_NAME = "createKnoxDescriptorsTable.sql";
38+
public static final String ORACLE_KNOX_PROVIDERS_TABLE_CREATE_SQL_FILE_NAME = "createKnoxProvidersTableOracle.sql";
39+
public static final String ORACLE_KNOX_DESCRIPTORS_TABLE_CREATE_SQL_FILE_NAME = "createKnoxDescriptorsTableOracle.sql";
40+
public static final String DERBY_KNOX_PROVIDERS_TABLE_CREATE_SQL_FILE_NAME = "createKnoxProvidersTableDerby.sql";
41+
public static final String DERBY_KNOX_DESCRIPTORS_TABLE_CREATE_SQL_FILE_NAME = "createKnoxDescriptorsTableDerby.sql";
42+
43+
public static final String DATABASE_USER_ALIAS_NAME = "gateway_database_user";
44+
public static final String DATABASE_PASSWORD_ALIAS_NAME = "gateway_database_password";
45+
public static final String DATABASE_TRUSTSTORE_PASSWORD_ALIAS_NAME = "gateway_database_ssl_truststore_password";
46+
47+
public abstract DataSource createDataSource(GatewayConfig gatewayConfig, AliasService aliasService) throws AliasServiceException, SQLException;
48+
49+
protected String getDatabaseUser(AliasService aliasService) throws AliasServiceException {
50+
return getDatabaseAlias(aliasService, DATABASE_USER_ALIAS_NAME);
51+
}
52+
53+
protected String getDatabasePassword(AliasService aliasService) throws AliasServiceException {
54+
return getDatabaseAlias(aliasService, DATABASE_PASSWORD_ALIAS_NAME);
55+
}
56+
57+
protected String getDatabaseAlias(AliasService aliasService, String aliasName) throws AliasServiceException {
58+
final char[] value = aliasService.getPasswordFromAliasForGateway(aliasName);
59+
return value == null ? null : new String(value);
60+
}
61+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.apache.knox.gateway.database;
19+
20+
import org.apache.knox.gateway.config.GatewayConfig;
21+
import org.apache.knox.gateway.services.security.AliasService;
22+
import org.apache.knox.gateway.services.security.AliasServiceException;
23+
24+
import javax.sql.DataSource;
25+
import java.sql.SQLException;
26+
27+
public class DataSourceFactory {
28+
29+
public static DataSource getDataSource(GatewayConfig gatewayConfig, AliasService aliasService) throws AliasServiceException, SQLException {
30+
DatabaseType dbType = DatabaseType.fromString(gatewayConfig.getDatabaseType());
31+
AbstractDataSource dsFactory;
32+
33+
switch (dbType) {
34+
case POSTGRESQL -> dsFactory = new PostgresDataSource();
35+
case MYSQL -> dsFactory = new MysqlDataSource();
36+
case MARIADB -> dsFactory = new MariaDBDataSource();
37+
case DERBY -> dsFactory = new DerbyDataSource();
38+
case HSQL -> dsFactory = new HsqlDataSource();
39+
case ORACLE -> dsFactory = new OracleDataSource();
40+
default -> throw new IllegalArgumentException("Invalid database type: " + gatewayConfig.getDatabaseType());
41+
}
42+
43+
return dsFactory.createDataSource(gatewayConfig, aliasService);
44+
}
45+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.apache.knox.gateway.database;
19+
20+
public enum DatabaseType {
21+
POSTGRESQL("postgresql",
22+
AbstractDataSource.TOKENS_TABLE_CREATE_SQL_FILE_NAME,
23+
AbstractDataSource.TOKEN_METADATA_TABLE_CREATE_SQL_FILE_NAME,
24+
AbstractDataSource.KNOX_PROVIDERS_TABLE_CREATE_SQL_FILE_NAME,
25+
AbstractDataSource.KNOX_DESCRIPTORS_TABLE_CREATE_SQL_FILE_NAME
26+
),
27+
MYSQL("mysql",
28+
AbstractDataSource.TOKENS_TABLE_CREATE_SQL_FILE_NAME,
29+
AbstractDataSource.TOKEN_METADATA_TABLE_CREATE_SQL_FILE_NAME,
30+
AbstractDataSource.KNOX_PROVIDERS_TABLE_CREATE_SQL_FILE_NAME,
31+
AbstractDataSource.KNOX_DESCRIPTORS_TABLE_CREATE_SQL_FILE_NAME
32+
),
33+
MARIADB("mariadb",
34+
AbstractDataSource.TOKENS_TABLE_CREATE_SQL_FILE_NAME,
35+
AbstractDataSource.TOKEN_METADATA_TABLE_CREATE_SQL_FILE_NAME,
36+
AbstractDataSource.KNOX_PROVIDERS_TABLE_CREATE_SQL_FILE_NAME,
37+
AbstractDataSource.KNOX_DESCRIPTORS_TABLE_CREATE_SQL_FILE_NAME
38+
),
39+
HSQL("hsql",
40+
AbstractDataSource.TOKENS_TABLE_CREATE_SQL_FILE_NAME,
41+
AbstractDataSource.TOKEN_METADATA_TABLE_CREATE_SQL_FILE_NAME,
42+
AbstractDataSource.KNOX_PROVIDERS_TABLE_CREATE_SQL_FILE_NAME,
43+
AbstractDataSource.KNOX_DESCRIPTORS_TABLE_CREATE_SQL_FILE_NAME
44+
),
45+
DERBY("derbydb",
46+
AbstractDataSource.DERBY_TOKENS_TABLE_CREATE_SQL_FILE_NAME,
47+
AbstractDataSource.DERBY_TOKEN_METADATA_TABLE_CREATE_SQL_FILE_NAME,
48+
AbstractDataSource.DERBY_KNOX_PROVIDERS_TABLE_CREATE_SQL_FILE_NAME,
49+
AbstractDataSource.DERBY_KNOX_DESCRIPTORS_TABLE_CREATE_SQL_FILE_NAME
50+
),
51+
ORACLE("oracle",
52+
AbstractDataSource.ORACLE_TOKENS_TABLE_CREATE_SQL_FILE_NAME,
53+
AbstractDataSource.ORACLE_TOKEN_METADATA_TABLE_CREATE_SQL_FILE_NAME,
54+
AbstractDataSource.ORACLE_KNOX_PROVIDERS_TABLE_CREATE_SQL_FILE_NAME,
55+
AbstractDataSource.ORACLE_KNOX_DESCRIPTORS_TABLE_CREATE_SQL_FILE_NAME
56+
);
57+
58+
private final String type;
59+
private final String tokensTableSql;
60+
private final String metadataTableSql;
61+
private final String providersTableSql;
62+
private final String descriptorsTableSql;
63+
64+
DatabaseType(String type, String tokensTableSql, String metadataTableSql, String providersTableSql, String descriptorsTableSql) {
65+
this.type = type;
66+
this.tokensTableSql = tokensTableSql;
67+
this.metadataTableSql = metadataTableSql;
68+
this.providersTableSql = providersTableSql;
69+
this.descriptorsTableSql = descriptorsTableSql;
70+
}
71+
72+
public String type() {
73+
return type;
74+
}
75+
76+
public String tokensTableSql() {
77+
return tokensTableSql;
78+
}
79+
80+
public String metadataTableSql() {
81+
return metadataTableSql;
82+
}
83+
84+
public String providersTableSql() {
85+
return providersTableSql;
86+
}
87+
88+
public String descriptorsTableSql() {
89+
return descriptorsTableSql;
90+
}
91+
92+
public static DatabaseType fromString(String dbType) {
93+
for (DatabaseType dt : values()) {
94+
if (dt.type.equalsIgnoreCase(dbType)) {
95+
return dt;
96+
}
97+
}
98+
throw new IllegalArgumentException("Invalid database type: " + dbType);
99+
}
100+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.apache.knox.gateway.database;
19+
20+
import org.apache.derby.jdbc.EmbeddedDataSource;
21+
import org.apache.knox.gateway.config.GatewayConfig;
22+
import org.apache.knox.gateway.services.security.AliasService;
23+
import org.apache.knox.gateway.services.security.AliasServiceException;
24+
25+
import javax.sql.DataSource;
26+
import java.sql.SQLException;
27+
28+
public class DerbyDataSource extends AbstractDataSource {
29+
30+
@Override
31+
public DataSource createDataSource(GatewayConfig gatewayConfig, AliasService aliasService) throws AliasServiceException, SQLException {
32+
final EmbeddedDataSource embeddedDataSource = new EmbeddedDataSource();
33+
embeddedDataSource.setDatabaseName(gatewayConfig.getDatabaseName());
34+
embeddedDataSource.setUser(getDatabaseUser(aliasService));
35+
embeddedDataSource.setPassword(getDatabasePassword(aliasService));
36+
return embeddedDataSource;
37+
}
38+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.apache.knox.gateway.database;
19+
20+
import org.apache.knox.gateway.config.GatewayConfig;
21+
import org.apache.knox.gateway.services.security.AliasService;
22+
import org.apache.knox.gateway.services.security.AliasServiceException;
23+
import org.hsqldb.jdbc.JDBCDataSource;
24+
25+
import javax.sql.DataSource;
26+
import java.sql.SQLException;
27+
28+
public class HsqlDataSource extends AbstractDataSource {
29+
30+
@Override
31+
public DataSource createDataSource(GatewayConfig gatewayConfig, AliasService aliasService) throws AliasServiceException, SQLException {
32+
JDBCDataSource hsqlDatasource = new JDBCDataSource();
33+
hsqlDatasource.setUrl(gatewayConfig.getDatabaseConnectionUrl());
34+
hsqlDatasource.setUser(getDatabaseUser(aliasService));
35+
hsqlDatasource.setPassword(getDatabasePassword(aliasService));
36+
return hsqlDatasource;
37+
}
38+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.apache.knox.gateway.database;
19+
20+
import org.apache.commons.io.IOUtils;
21+
22+
import javax.sql.DataSource;
23+
import java.io.InputStream;
24+
import java.sql.Connection;
25+
import java.sql.DatabaseMetaData;
26+
import java.sql.ResultSet;
27+
import java.sql.SQLException;
28+
import java.sql.Statement;
29+
import java.util.Locale;
30+
31+
import static java.nio.charset.StandardCharsets.UTF_8;
32+
33+
public class JDBCUtils {
34+
35+
public static boolean tableExists(String tableName, DataSource dataSource) throws SQLException {
36+
boolean exists;
37+
try (Connection connection = dataSource.getConnection()) {
38+
final DatabaseMetaData dbMetadata = connection.getMetaData();
39+
final String tableNameToCheck = dbMetadata.storesUpperCaseIdentifiers() ? tableName : tableName.toLowerCase(Locale.ROOT);
40+
try (ResultSet tables = dbMetadata.getTables(connection.getCatalog(), null, tableNameToCheck, null)) {
41+
exists = tables.next();
42+
}
43+
}
44+
return exists;
45+
}
46+
47+
public static void createTableFromSQL(String createSqlFileName, DataSource dataSource, ClassLoader classLoader) throws Exception {
48+
try (InputStream is = classLoader.getResourceAsStream(createSqlFileName);
49+
Connection connection = dataSource.getConnection();Statement createTableStatement = connection.createStatement()) {
50+
String createTableSql = IOUtils.toString(is, UTF_8);
51+
createTableStatement.execute(createTableSql);
52+
}
53+
}
54+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.apache.knox.gateway.database;
19+
20+
import org.apache.knox.gateway.config.GatewayConfig;
21+
import org.apache.knox.gateway.services.security.AliasService;
22+
import org.apache.knox.gateway.services.security.AliasServiceException;
23+
import org.mariadb.jdbc.MariaDbDataSource;
24+
25+
import javax.sql.DataSource;
26+
import java.sql.SQLException;
27+
28+
public class MariaDBDataSource extends AbstractDataSource {
29+
30+
@Override
31+
public DataSource createDataSource(GatewayConfig gatewayConfig, AliasService aliasService) throws AliasServiceException, SQLException {
32+
if (gatewayConfig.getDatabaseConnectionUrl() != null) {
33+
return new MariaDbDataSource(gatewayConfig.getDatabaseConnectionUrl());
34+
} else {
35+
throw new IllegalArgumentException("MariaDB Java Datasource requires a connection string!");
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)