Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dbeaver/dbeaver#12430 Refactor tunnels secrets save/load #22523

Open
wants to merge 3 commits into
base: devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@

public static final String PROP_HOST = "host";
public static final String PROP_PORT = "port";
public static final String PROP_USER = "user";
public static final String PROP_PASSWORD = "password";
public static final String PROP_PROPERTIES = "properties";

@NotNull
private String id;
Expand Down Expand Up @@ -225,44 +228,32 @@
this.secureProperties.putAll(secureProperties);
}

public Map<String, Object> saveToMap() {
return saveToMap(false);
}

public Map<String, Object> saveToSecret() {
return saveToMap(true);
}

private Map<String, Object> saveToMap(boolean ignoreSecureProperties) {
public Map<String, Object> saveSecretsToMap() {

Check warning on line 231 in plugins/org.jkiss.dbeaver.model/src/org/jkiss/dbeaver/model/net/DBWHandlerConfiguration.java

View check run for this annotation

Jenkins-CI-integration / CheckStyle Report

plugins/org.jkiss.dbeaver.model/src/org/jkiss/dbeaver/model/net/DBWHandlerConfiguration.java#L231

Missing a Javadoc comment.
Map<String, Object> handlerProps = new LinkedHashMap<>();
if (!isSavePassword() && ignoreSecureProperties) {
return handlerProps;
}
if (!CommonUtils.isEmpty(userName)) {
handlerProps.put("user", userName);
handlerProps.put(PROP_USER, userName);
}
if (!CommonUtils.isEmpty(password)) {
handlerProps.put("password", password);
handlerProps.put(PROP_PASSWORD, password);
}
if (!CommonUtils.isEmpty(secureProperties)) {
handlerProps.put("properties", secureProperties);
handlerProps.put(PROP_PROPERTIES, secureProperties);
}
return handlerProps;
}

void loadFromMap(Map<String, Object> handlerMap) {
userName = JSONUtils.getString(handlerMap, "user");
password = JSONUtils.getString(handlerMap, "password");
void loadSecretsFromMap(Map<String, Object> handlerMap) {
userName = JSONUtils.getString(handlerMap, PROP_USER);
password = JSONUtils.getString(handlerMap, PROP_PASSWORD);
secureProperties.clear();
secureProperties.putAll(JSONUtils.deserializeStringMap(handlerMap, "properties"));
secureProperties.putAll(JSONUtils.deserializeStringMap(handlerMap, PROP_PROPERTIES));
}

@Override
public boolean equals(Object obj) {
if (!(obj instanceof DBWHandlerConfiguration)) {
if (!(obj instanceof DBWHandlerConfiguration source)) {
return false;
}
DBWHandlerConfiguration source = (DBWHandlerConfiguration) obj;
return
CommonUtils.equalObjects(this.id, source.id) &&
CommonUtils.equalObjects(this.dataSource, source.dataSource) &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public void persistSecrets(DBSSecretController secretController) throws DBExcept

List<Map<String, Object>> handlersConfigs = new ArrayList<>();
for (DBWHandlerConfiguration cfg : configurations) {
Map<String, Object> hcProps = cfg.saveToMap();
Map<String, Object> hcProps = cfg.saveSecretsToMap();
if (!hcProps.isEmpty()) {
hcProps.put("id", cfg.getId());
handlersConfigs.add(hcProps);
Expand Down Expand Up @@ -156,16 +156,15 @@ public void resolveSecrets(DBSSecretController secretController) throws DBExcept
String configId = JSONUtils.getString(hc, "id");
DBWHandlerConfiguration configuration = getConfiguration(configId);
if (configuration != null) {
configuration.loadFromMap(hc);
configuration.loadSecretsFromMap(hc);
}
}
}

private void loadFromLegacySecret(DBSSecretController secretController) throws DBException {
if (!(secretController instanceof DBSSecretBrowser) || getProject() == null) {
if (!(secretController instanceof DBSSecretBrowser secretBrowser) || getProject() == null) {
return;
}
DBSSecretBrowser secretBrowser = (DBSSecretBrowser) secretController;
for (DBWHandlerConfiguration cfg : configurations) {
String prefix = "projects/" + getProject().getId() + "/network/" + cfg.getId() + "/profile/" + getProfileId();
Map<String, String> secureProps = new LinkedHashMap<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2024 DBeaver Corp and others
*
* Licensed 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.jkiss.dbeaver.model.net;

/**
* Network tunnel controller
*/
public interface DBWTunnelController {

Object acquireTunnelSession(String remoteHost);

Check warning on line 24 in plugins/org.jkiss.dbeaver.model/src/org/jkiss/dbeaver/model/net/DBWTunnelController.java

View check run for this annotation

Jenkins-CI-integration / CheckStyle Report

plugins/org.jkiss.dbeaver.model/src/org/jkiss/dbeaver/model/net/DBWTunnelController.java#L24

Missing a Javadoc comment.

void releaseTunnelSession(Object session);

Check warning on line 26 in plugins/org.jkiss.dbeaver.model/src/org/jkiss/dbeaver/model/net/DBWTunnelController.java

View check run for this annotation

Jenkins-CI-integration / CheckStyle Report

plugins/org.jkiss.dbeaver.model/src/org/jkiss/dbeaver/model/net/DBWTunnelController.java#L26

Missing a Javadoc comment.

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2024 DBeaver Corp and others
*
* Licensed 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.jkiss.dbeaver.model.net.ssh;

import org.jkiss.code.NotNull;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.model.exec.DBCException;
import org.jkiss.dbeaver.model.net.ssh.config.SSHHostConfiguration;
import org.jkiss.dbeaver.model.net.ssh.config.SSHPortForwardConfiguration;

import java.util.List;

/**
* SSH tunnel implementation
*/
public interface SSHTunnelHost {

@NotNull

Check warning on line 32 in plugins/org.jkiss.dbeaver.net.ssh/src/org/jkiss/dbeaver/model/net/ssh/SSHTunnelHost.java

View check run for this annotation

Jenkins-CI-integration / CheckStyle Report

plugins/org.jkiss.dbeaver.net.ssh/src/org/jkiss/dbeaver/model/net/ssh/SSHTunnelHost.java#L32

Missing a Javadoc comment.
SSHHostConfiguration getRemoteHost();

@NotNull

Check warning on line 35 in plugins/org.jkiss.dbeaver.net.ssh/src/org/jkiss/dbeaver/model/net/ssh/SSHTunnelHost.java

View check run for this annotation

Jenkins-CI-integration / CheckStyle Report

plugins/org.jkiss.dbeaver.net.ssh/src/org/jkiss/dbeaver/model/net/ssh/SSHTunnelHost.java#L35

Missing a Javadoc comment.
List<SSHTunnelPortForward> getPortForwards();

@NotNull

Check warning on line 38 in plugins/org.jkiss.dbeaver.net.ssh/src/org/jkiss/dbeaver/model/net/ssh/SSHTunnelHost.java

View check run for this annotation

Jenkins-CI-integration / CheckStyle Report

plugins/org.jkiss.dbeaver.net.ssh/src/org/jkiss/dbeaver/model/net/ssh/SSHTunnelHost.java#L38

Missing a Javadoc comment.
SSHTunnelPortForward acquirePortForward(SSHPortForwardConfiguration config) throws DBCException;

void releasePortForward(@NotNull SSHTunnelPortForward portForward);

Check warning on line 41 in plugins/org.jkiss.dbeaver.net.ssh/src/org/jkiss/dbeaver/model/net/ssh/SSHTunnelHost.java

View check run for this annotation

Jenkins-CI-integration / CheckStyle Report

plugins/org.jkiss.dbeaver.net.ssh/src/org/jkiss/dbeaver/model/net/ssh/SSHTunnelHost.java#L41

Missing a Javadoc comment.

@Nullable

Check warning on line 43 in plugins/org.jkiss.dbeaver.net.ssh/src/org/jkiss/dbeaver/model/net/ssh/SSHTunnelHost.java

View check run for this annotation

Jenkins-CI-integration / CheckStyle Report

plugins/org.jkiss.dbeaver.net.ssh/src/org/jkiss/dbeaver/model/net/ssh/SSHTunnelHost.java#L43

Missing a Javadoc comment.
SSHTunnelHost getNextJump();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2024 DBeaver Corp and others
*
* Licensed 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.jkiss.dbeaver.model.net.ssh;

import org.jkiss.dbeaver.model.net.ssh.config.SSHPortForwardConfiguration;

/**
* SSH tunnel implementation
*/
public interface SSHTunnelPortForward {

SSHTunnelSession<?> getSession();

Check warning on line 26 in plugins/org.jkiss.dbeaver.net.ssh/src/org/jkiss/dbeaver/model/net/ssh/SSHTunnelPortForward.java

View check run for this annotation

Jenkins-CI-integration / CheckStyle Report

plugins/org.jkiss.dbeaver.net.ssh/src/org/jkiss/dbeaver/model/net/ssh/SSHTunnelPortForward.java#L26

Missing a Javadoc comment.

SSHPortForwardConfiguration getConfiguration();

Check warning on line 28 in plugins/org.jkiss.dbeaver.net.ssh/src/org/jkiss/dbeaver/model/net/ssh/SSHTunnelPortForward.java

View check run for this annotation

Jenkins-CI-integration / CheckStyle Report

plugins/org.jkiss.dbeaver.net.ssh/src/org/jkiss/dbeaver/model/net/ssh/SSHTunnelPortForward.java#L28

Missing a Javadoc comment.

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2024 DBeaver Corp and others
*
* Licensed 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.jkiss.dbeaver.model.net.ssh;

/**
* SSH tunnel session
*/
public interface SSHTunnelSession<IMPL extends SSHImplementation> {

IMPL getImplementation();

Check warning on line 24 in plugins/org.jkiss.dbeaver.net.ssh/src/org/jkiss/dbeaver/model/net/ssh/SSHTunnelSession.java

View check run for this annotation

Jenkins-CI-integration / CheckStyle Report

plugins/org.jkiss.dbeaver.net.ssh/src/org/jkiss/dbeaver/model/net/ssh/SSHTunnelSession.java#L24

Missing a Javadoc comment.

SSHTunnelHost getFirstHost();

Check warning on line 26 in plugins/org.jkiss.dbeaver.net.ssh/src/org/jkiss/dbeaver/model/net/ssh/SSHTunnelSession.java

View check run for this annotation

Jenkins-CI-integration / CheckStyle Report

plugins/org.jkiss.dbeaver.net.ssh/src/org/jkiss/dbeaver/model/net/ssh/SSHTunnelSession.java#L26

Missing a Javadoc comment.

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2024 DBeaver Corp and others
*
* Licensed 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.jkiss.dbeaver.model.net.ssh.registry;

import org.jkiss.dbeaver.model.net.ssh.SSHTunnelSession;
import org.jkiss.dbeaver.model.net.ssh.config.SSHHostConfiguration;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SSHSessionController {
private static final SSHSessionController instance = new SSHSessionController();

public synchronized static SSHSessionController getInstance() {

Check warning on line 29 in plugins/org.jkiss.dbeaver.net.ssh/src/org/jkiss/dbeaver/model/net/ssh/registry/SSHSessionController.java

View check run for this annotation

Jenkins-CI-integration / CheckStyle Report

plugins/org.jkiss.dbeaver.net.ssh/src/org/jkiss/dbeaver/model/net/ssh/registry/SSHSessionController.java#L29

static modifier out of order with the JLS suggestions.
return instance;
}

private final Map<String, List<SSHTunnelSession<?>>> hostTunnels = new HashMap<>();

private SSHSessionController() {
}

public SSHTunnelSession<?> acquireSession(List<SSHHostConfiguration> hosts) {

Check warning on line 38 in plugins/org.jkiss.dbeaver.net.ssh/src/org/jkiss/dbeaver/model/net/ssh/registry/SSHSessionController.java

View check run for this annotation

Jenkins-CI-integration / CheckStyle Report

plugins/org.jkiss.dbeaver.net.ssh/src/org/jkiss/dbeaver/model/net/ssh/registry/SSHSessionController.java#L38

Missing a Javadoc comment.
return null;
}

public void releaseSession(SSHTunnelSession<?> session) {

Check warning on line 42 in plugins/org.jkiss.dbeaver.net.ssh/src/org/jkiss/dbeaver/model/net/ssh/registry/SSHSessionController.java

View check run for this annotation

Jenkins-CI-integration / CheckStyle Report

plugins/org.jkiss.dbeaver.net.ssh/src/org/jkiss/dbeaver/model/net/ssh/registry/SSHSessionController.java#L42

Missing a Javadoc comment.

}

public void shutdown() {

Check warning on line 46 in plugins/org.jkiss.dbeaver.net.ssh/src/org/jkiss/dbeaver/model/net/ssh/registry/SSHSessionController.java

View check run for this annotation

Jenkins-CI-integration / CheckStyle Report

plugins/org.jkiss.dbeaver.net.ssh/src/org/jkiss/dbeaver/model/net/ssh/registry/SSHSessionController.java#L46

Missing a Javadoc comment.

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2059,10 +2059,12 @@ private String saveToSecret() {
// Handlers. If config profile is set then props are saved there
List<Map<String, Object>> handlersConfigs = new ArrayList<>();
for (DBWHandlerConfiguration hc : connectionInfo.getHandlers()) {
Map<String, Object> handlerProps = hc.saveToSecret();
if (!handlerProps.isEmpty()) {
handlerProps.put(RegistryConstants.ATTR_ID, hc.getId());
handlersConfigs.add(handlerProps);
if (hc.isSavePassword()) {
Map<String, Object> handlerProps = hc.saveSecretsToMap();
if (!handlerProps.isEmpty()) {
handlerProps.put(RegistryConstants.ATTR_ID, hc.getId());
handlersConfigs.add(handlerProps);
}
}
}
if (!handlersConfigs.isEmpty()) {
Expand Down