-
Notifications
You must be signed in to change notification settings - Fork 250
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KNOX-2990 - Using DerbyDatabaseTSS instead of AliasBasedTSS by default
In addition to the new implementation I deprecated the AliasBased, Zookeeper and JournalBased TSS implementations in 2.1.0.
- Loading branch information
Showing
23 changed files
with
645 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
...r/src/main/java/org/apache/knox/gateway/services/token/impl/DerbyDBTokenStateService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
* 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.services.token.impl; | ||
|
||
import static org.apache.commons.text.lookup.StringLookupFactory.KEY_LOCALHOST; | ||
import static org.apache.derby.drda.NetworkServerControl.DEFAULT_PORTNUMBER; | ||
import static org.apache.knox.gateway.config.impl.GatewayConfigImpl.GATEWAY_DATABASE_HOST; | ||
import static org.apache.knox.gateway.config.impl.GatewayConfigImpl.GATEWAY_DATABASE_NAME; | ||
import static org.apache.knox.gateway.config.impl.GatewayConfigImpl.GATEWAY_DATABASE_PORT; | ||
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 java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.Map; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.apache.derby.drda.NetworkServerControl; | ||
import org.apache.knox.gateway.config.GatewayConfig; | ||
import org.apache.knox.gateway.config.impl.GatewayConfigImpl; | ||
import org.apache.knox.gateway.services.ServiceLifecycleException; | ||
import org.apache.knox.gateway.services.security.MasterService; | ||
import org.apache.knox.gateway.util.FileUtils; | ||
|
||
public class DerbyDBTokenStateService extends JDBCTokenStateService { | ||
|
||
private static final String DEFAULT_TOKEN_DB_USER_NAME = "knox"; | ||
private static final String DB_NAME = "tokens"; | ||
|
||
private NetworkServerControl derbyNetworkServerControl; | ||
private MasterService masterService; | ||
|
||
public void setMasterService(MasterService masterService) { | ||
this.masterService = masterService; | ||
} | ||
|
||
@Override | ||
public void init(GatewayConfig config, Map<String, String> options) throws ServiceLifecycleException { | ||
try { | ||
startDerby(); | ||
final Path derbyDatabaseFolder = Paths.get(config.getGatewaySecurityDir(), DB_NAME); | ||
((GatewayConfigImpl) config).set(GATEWAY_DATABASE_TYPE, DERBY_DB_TYPE); | ||
((GatewayConfigImpl) config).set(GATEWAY_DATABASE_HOST, KEY_LOCALHOST); | ||
((GatewayConfigImpl) config).setInt(GATEWAY_DATABASE_PORT, DEFAULT_PORTNUMBER); | ||
((GatewayConfigImpl) 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()); | ||
super.init(config, options); | ||
|
||
// we need the "x" permission too to be able to browse that folder (600 is not enough) | ||
FileUtils.chmod("700", derbyDatabaseFolder.toFile()); | ||
} catch (Exception e) { | ||
throw new ServiceLifecycleException("Error while initiating DerbyDBTokenStateService: " + e, e); | ||
} | ||
} | ||
|
||
private void startDerby() throws Exception { | ||
derbyNetworkServerControl = new NetworkServerControl(getDatabaseUserName(), getDatabasePassword()); | ||
derbyNetworkServerControl.start(null); | ||
TimeUnit.SECONDS.sleep(1); // give a bit of time for the server to start | ||
} | ||
|
||
private String getDatabasePassword() throws Exception { | ||
final char[] dbPasswordAliasValue = getAliasService().getPasswordFromAliasForGateway(DATABASE_PASSWORD_ALIAS_NAME); | ||
return dbPasswordAliasValue != null ? new String(dbPasswordAliasValue) : new String(masterService.getMasterSecret()); | ||
} | ||
|
||
private String getDatabaseUserName() throws Exception { | ||
final char[] dbUserAliasValue = getAliasService().getPasswordFromAliasForGateway(DATABASE_USER_ALIAS_NAME); | ||
return dbUserAliasValue != null ? new String(dbUserAliasValue) : DEFAULT_TOKEN_DB_USER_NAME; | ||
} | ||
|
||
@Override | ||
public void stop() throws ServiceLifecycleException { | ||
try { | ||
if (derbyNetworkServerControl != null) { | ||
derbyNetworkServerControl.shutdown(); | ||
} | ||
} catch (Exception e) { | ||
throw new ServiceLifecycleException("Error while shutting down Derby Database", e); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.