提交 b31bdc70 编写于 作者: S Serge Rider 提交者: GitHub

Merge pull request #10124 from dbeaver/mysql-credentials#9983

#9983 Checkbox to override credentials stored in system

Former-commit-id: 5d20717e
......@@ -27,6 +27,7 @@ import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.jkiss.dbeaver.ext.mysql.tasks.MySQLNativeCredentialsSettings;
import org.jkiss.dbeaver.ext.mysql.ui.internal.MySQLUIMessages;
import org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration;
import org.jkiss.dbeaver.tasks.ui.nativetool.AbstractNativeToolWizard;
......@@ -47,11 +48,11 @@ abstract class MySQLWizardPageSettings<WIZARD extends AbstractNativeToolWizard>
{
final DBPConnectionConfiguration connectionInfo = wizard.getSettings().getDataSourceContainer().getActualConnectionConfiguration();
if (connectionInfo != null) {
Group securityGroup = UIUtils.createControlGroup(parent, MySQLUIMessages.tools_db_export_wizard_page_settings_security_group, 2, GridData.HORIZONTAL_ALIGN_BEGINNING, 0);
Group securityGroup = UIUtils.createControlGroup(parent, MySQLUIMessages.tools_db_export_wizard_page_settings_security_group, 3, GridData.HORIZONTAL_ALIGN_BEGINNING, 0);
Label infoLabel = new Label(securityGroup, SWT.NONE);
infoLabel.setText(NLS.bind(MySQLUIMessages.tools_db_export_wizard_page_settings_security_label_info, connectionInfo.getUserName()));
GridData gd = new GridData(GridData.FILL_HORIZONTAL);
gd.horizontalSpan = 2;
gd.horizontalSpan = 3;
infoLabel.setLayoutData(gd);
Button authButton = new Button(securityGroup, SWT.PUSH);
authButton.setText(MySQLUIMessages.tools_db_export_wizard_page_settings_security_button_auth);
......@@ -78,6 +79,19 @@ abstract class MySQLWizardPageSettings<WIZARD extends AbstractNativeToolWizard>
wizard.getSettings().setToolUserPassword(null);
}
});
if (wizard.getSettings() instanceof MySQLNativeCredentialsSettings) {
MySQLNativeCredentialsSettings settings = (MySQLNativeCredentialsSettings) wizard.getSettings();
Button overrideCredentials = UIUtils.createCheckbox(securityGroup, MySQLUIMessages.tools_db_export_wizard_page_settings_security_checkbox_override_host_credentials, settings.isOverrideCredentials());
overrideCredentials.setToolTipText(MySQLUIMessages.tools_db_export_wizard_page_settings_security_checkbox_override_host_credentials_tip);
overrideCredentials.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
settings.setOverrideCredentials(overrideCredentials.getSelection());
}
});
}
}
}
......
......@@ -143,5 +143,7 @@ public class MySQLUIMessages extends NLS {
public static String tools_db_export_wizard_page_settings_security_label_info;
public static String tools_db_export_wizard_page_settings_security_button_auth;
public static String tools_db_export_wizard_page_settings_security_button_reset;
public static String tools_db_export_wizard_page_settings_security_checkbox_override_host_credentials;
public static String tools_db_export_wizard_page_settings_security_checkbox_override_host_credentials_tip;
public static String tools_db_export_wizard_page_settings_auth_title;
}
......@@ -108,7 +108,9 @@ tools_script_execute_wizard_page_settings_set_script_execution_settings=Set scri
tools_script_execute_wizard_page_settings_checkbox_disable_foreign_key_checks = Disable foreign key checks
tools_script_execute_wizard_page_settings_checkbox_disable_foreign_key_checks_tooltip = Disables foreign key checks while performing restore
tools_db_export_wizard_page_settings_security_group=Security
tools_db_export_wizard_page_settings_security_label_info=Override user credentials ({0}) for external tools.\nIf not set, external tools like ''mysqldump'' will likely use credentials stored in the system.
tools_db_export_wizard_page_settings_security_label_info=Override user credentials ({0}) for external tools.\nExternal tools like ''mysqldump'' may require different set of permissions.
tools_db_export_wizard_page_settings_security_button_auth=Authenticate
tools_db_export_wizard_page_settings_security_button_reset=Reset to default
tools_db_export_wizard_page_settings_security_checkbox_override_host_credentials=Override host credentials
tools_db_export_wizard_page_settings_security_checkbox_override_host_credentials_tip=If checked, host credentials (on this machine, if any) will be overridden with credentials set in connection configuration.
tools_db_export_wizard_page_settings_auth_title=Authentication
......@@ -104,7 +104,9 @@ tools_script_execute_wizard_page_settings_set_script_execution_settings=\u041D\u
tools_script_execute_wizard_page_settings_checkbox_disable_foreign_key_checks = \u041E\u0442\u043A\u043B\u044E\u0447\u0438\u0442\u044C \u043F\u0440\u043E\u0432\u0435\u0440\u043A\u0443 \u0432\u043D\u0435\u0448\u043D\u0438\u0439 \u043A\u043B\u044E\u0447\u0435\u0439
tools_script_execute_wizard_page_settings_checkbox_disable_foreign_key_checks_tooltip = \u041E\u0442\u043A\u043B\u044E\u0447\u0430\u0435\u0442 \u043F\u0440\u043E\u0432\u0435\u0440\u043A\u0443 \u0432\u043D\u0435\u0448\u043D\u0438\u0439 \u043A\u043B\u044E\u0447\u0435\u0439 \u043D\u0430 \u0432\u0440\u0435\u043C\u044F \u0432\u044B\u043F\u043E\u043B\u043D\u0435\u043D\u0438\u044F \u0438\u043C\u043F\u043E\u0440\u0442\u0430
tools_db_export_wizard_page_settings_security_group=\u0411\u0435\u0437\u043E\u043F\u0430\u0441\u043D\u043E\u0441\u0442\u044C
tools_db_export_wizard_page_settings_security_label_info=\u0423\u043A\u0430\u0437\u0430\u0442\u044C \u0434\u0440\u0443\u0433\u0438\u0435 \u0443\u0447\u0451\u0442\u043D\u044B\u0435 \u0434\u0430\u043D\u043D\u044B\u0435 \u0432\u043C\u0435\u0441\u0442\u043E \u0443\u0441\u0442\u0430\u043D\u043E\u0432\u043B\u0435\u043D\u043D\u044B\u0445 ({0}).\n\u0421\u0442\u043E\u0440\u043E\u043D\u043D\u0438\u0435 \u0438\u043D\u0441\u0442\u0440\u0443\u043C\u0435\u043D\u0442\u044B \u043F\u043E \u0442\u0438\u043F\u0443 ''mysqldump'' \u043C\u043E\u0433\u0443\u0442 \u0442\u0440\u0435\u0431\u043E\u0432\u0430\u0442\u044C \u0438\u043D\u044B\u0435 \u0443\u0447\u0451\u0442\u043D\u044B\u0435 \u0434\u0430\u043D\u043D\u044B\u0435, \u0438 \u0435\u0441\u043B\u0438 \u043E\u043D\u0438 \u043D\u0435 \u0437\u0430\u0434\u0430\u043D\u044B, \u0442\u043E \u0431\u0443\u0434\u0443\u0442 \u0438\u0441\u043F\u043E\u043B\u044C\u0437\u043E\u0432\u0430\u0442\u044C\u0441\u044F \u0443\u0447\u0451\u0442\u043D\u044B\u0435 \u0434\u0430\u043D\u043D\u044B\u0435 \u0438\u0437 \u0441\u0438\u0441\u0442\u0435\u043C\u044B.
tools_db_export_wizard_page_settings_security_label_info=\u0423\u043A\u0430\u0437\u0430\u0442\u044C \u0434\u0440\u0443\u0433\u0438\u0435 \u0443\u0447\u0451\u0442\u043D\u044B\u0435 \u0434\u0430\u043D\u043D\u044B\u0435 \u0432\u043C\u0435\u0441\u0442\u043E \u0443\u0441\u0442\u0430\u043D\u043E\u0432\u043B\u0435\u043D\u043D\u044B\u0445 ({0}).\n\u0421\u0442\u043E\u0440\u043E\u043D\u043D\u0438\u0435 \u0438\u043D\u0441\u0442\u0440\u0443\u043C\u0435\u043D\u0442\u044B \u043F\u043E \u0442\u0438\u043F\u0443 ''mysqldump'' \u043C\u043E\u0433\u0443\u0442 \u0442\u0440\u0435\u0431\u043E\u0432\u0430\u0442\u044C \u0438\u043D\u043E\u0439 \u043D\u0430\u0431\u043E\u0440 \u043F\u0440\u0430\u0432.
tools_db_export_wizard_page_settings_security_button_auth=\u0410\u0432\u0442\u043E\u0440\u0438\u0437\u043E\u0432\u0430\u0442\u044C\u0441\u044F
tools_db_export_wizard_page_settings_security_button_reset=\u0421\u0431\u0440\u043E\u0441\u0438\u0442\u044C \u043F\u043E \u0443\u043C\u043E\u043B\u0447\u0430\u043D\u0438\u044E
tools_db_export_wizard_page_settings_security_checkbox_override_host_credentials=\u041F\u0435\u0440\u0435\u043E\u043F\u0440\u0435\u0434\u0435\u043B\u0438\u0442\u044C \u0443\u0447\u0451\u0442\u043D\u044B\u0435 \u0434\u0430\u043D\u043D\u044B\u0435 \u0441\u0438\u0441\u0442\u0435\u043C\u044B
tools_db_export_wizard_page_settings_security_checkbox_override_host_credentials_tip=\u0415\u0441\u043B\u0438 \u043E\u0442\u043C\u0435\u0447\u0435\u043D\u043E, \u0442\u043E \u043B\u043E\u043A\u0430\u043B\u044C\u043D\u044B\u0435 \u0443\u0447\u0451\u0442\u043D\u044B\u0435 \u0434\u0430\u043D\u043D\u044B\u0435 \u0431\u0443\u0434\u0443\u0442 \u043F\u0435\u0440\u0435\u043E\u043F\u0440\u0435\u0434\u0435\u043B\u0435\u043D\u044B \u0443\u0447\u0451\u0442\u043D\u044B\u043C\u0438 \u0434\u0430\u043D\u043D\u044B\u043C\u0438 \u0438\u0437 \u043A\u043E\u043D\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0438 \u043F\u043E\u0434\u043A\u043B\u044E\u0447\u0435\u043D\u0438\u044F.
tools_db_export_wizard_page_settings_auth_title=\u0410\u0432\u0442\u043E\u0440\u0438\u0437\u0430\u0446\u0438\u044F
......@@ -206,6 +206,7 @@ public class MySQLConstants {
public static final String TYPE_YEAR = "year";
public static final String BIN_FOLDER = "bin";
public static final String ENV_VAR_MYSQL_PWD = "MYSQL_PWD";
public static final String EXTRA_INFO_VIRTUAL_GENERATED = "VIRTUAL GENERATED";
}
......@@ -54,7 +54,7 @@ public class MySQLDatabaseExportHandler extends MySQLNativeToolHandler<MySQLExpo
@Override
protected List<String> getCommandLine(MySQLExportSettings settings, MySQLDatabaseExportInfo arg) throws IOException {
List<String> cmd = getMySQLToolCommandLine(this, settings, arg);
List<String> cmd = super.getCommandLine(settings, arg);
if (!CommonUtils.isEmpty(arg.getTables())) {
cmd.add(arg.getDatabase().getName());
for (MySQLTableBase table : arg.getTables()) {
......
......@@ -37,7 +37,7 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class MySQLExportSettings extends AbstractImportExportSettings<DBSObject> {
public class MySQLExportSettings extends AbstractImportExportSettings<DBSObject> implements MySQLNativeCredentialsSettings {
private static final Log log = Log.getLog(MySQLExportSettings.class);
......@@ -58,6 +58,7 @@ public class MySQLExportSettings extends AbstractImportExportSettings<DBSObject>
private boolean binariesInHex;
private boolean noData;
private boolean showViews;
private boolean overrideCredentials;
public List<MySQLDatabaseExportInfo> exportObjects = new ArrayList<>();
......@@ -157,6 +158,17 @@ public class MySQLExportSettings extends AbstractImportExportSettings<DBSObject>
return exportObjects;
}
@Override
public boolean isOverrideCredentials() {
return overrideCredentials;
}
@Override
public void setOverrideCredentials(boolean value) {
this.overrideCredentials = value;
}
@Override
public void fillExportObjectsFromInput() {
Map<MySQLCatalog, List<MySQLTableBase>> objMap = new LinkedHashMap<>();
for (DBSObject object : getDatabaseObjects()) {
......@@ -200,6 +212,7 @@ public class MySQLExportSettings extends AbstractImportExportSettings<DBSObject>
binariesInHex = CommonUtils.getBoolean(store.getString("MySQL.export.binariesInHex"), false);
noData = CommonUtils.getBoolean(store.getString("MySQL.export.noData"), false);
showViews = CommonUtils.getBoolean(store.getString("MySQL.export.showViews"), false);
overrideCredentials = CommonUtils.getBoolean(store.getString(MySQLNativeCredentialsSettings.PREFERENCE_NAME), false);
if (CommonUtils.isEmpty(getExtraCommandArgs())) {
// Backward compatibility
setExtraCommandArgs(store.getString("MySQL.export.extraArgs"));
......@@ -269,6 +282,7 @@ public class MySQLExportSettings extends AbstractImportExportSettings<DBSObject>
store.setValue("MySQL.export.binariesInHex", binariesInHex);
store.setValue("MySQL.export.noData", noData);
store.setValue("MySQL.export.showViews", showViews);
store.setValue(MySQLNativeCredentialsSettings.PREFERENCE_NAME, overrideCredentials);
if (store instanceof DBPPreferenceMap && !CommonUtils.isEmpty(exportObjects)) {
// Save input objects to task properties
......
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2020 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.ext.mysql.tasks;
public interface MySQLNativeCredentialsSettings {
String PREFERENCE_NAME = "MySQL.overrideNativeCredentials";
boolean isOverrideCredentials();
void setOverrideCredentials(boolean value);
}
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2020 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.ext.mysql.tasks;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.ext.mysql.MySQLConstants;
import org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.runtime.VoidProgressMonitor;
import org.jkiss.dbeaver.model.struct.DBSObject;
import org.jkiss.dbeaver.model.task.DBTTask;
import org.jkiss.dbeaver.runtime.DBWorkbench;
import org.jkiss.dbeaver.tasks.nativetool.AbstractNativeToolHandler;
import org.jkiss.dbeaver.tasks.nativetool.AbstractNativeToolSettings;
......@@ -18,9 +39,36 @@ import java.util.List;
public abstract class MySQLNativeToolHandler<SETTINGS extends AbstractNativeToolSettings<BASE_OBJECT>, BASE_OBJECT extends DBSObject, PROCESS_ARG>
extends AbstractNativeToolHandler<SETTINGS, BASE_OBJECT, PROCESS_ARG> {
protected List<String> getMySQLToolCommandLine(AbstractNativeToolHandler<SETTINGS, BASE_OBJECT, PROCESS_ARG> handler, SETTINGS settings, PROCESS_ARG arg) throws IOException {
private File config;
@Override
protected boolean doExecute(DBRProgressMonitor monitor, DBTTask task, SETTINGS settings, Log log) throws DBException, InterruptedException {
try {
return super.doExecute(monitor, task, settings, log);
} finally {
if (config != null && !config.delete()) {
log.debug("Failed to delete configuration file");
}
}
}
@Override
protected void setupProcessParameters(SETTINGS settings, PROCESS_ARG arg, ProcessBuilder process) {
if (!isOverrideCredentials(settings)) {
String toolUserPassword = settings.getToolUserPassword();
if (CommonUtils.isEmpty(settings.getToolUserName())) {
toolUserPassword = settings.getDataSourceContainer().getActualConnectionConfiguration().getUserPassword();
}
process.environment().put(MySQLConstants.ENV_VAR_MYSQL_PWD, toolUserPassword);
}
}
@Override
protected List<String> getCommandLine(SETTINGS settings, PROCESS_ARG arg) throws IOException {
List<String> cmd = new ArrayList<>();
handler.fillProcessParameters(settings, arg, cmd);
fillProcessParameters(settings, arg, cmd);
String toolUserName = settings.getToolUserName();
String toolUserPassword = settings.getToolUserPassword();
......@@ -35,13 +83,12 @@ public abstract class MySQLNativeToolHandler<SETTINGS extends AbstractNativeTool
toolUserPassword = settings.getDataSourceContainer().getActualConnectionConfiguration().getUserPassword();
}
/*
* Let's assume that if username is not set, then
* native tool must search for credentials on its own.
*/
if (!CommonUtils.isEmpty(toolUserName)) {
String credentialsFile = createCredentialsFile(toolUserName, toolUserPassword);
cmd.add(1, "--defaults-file=" + credentialsFile);
if (isOverrideCredentials(settings)) {
config = createCredentialsFile(toolUserName, toolUserPassword);
cmd.add(1, "--defaults-file=" + config.getAbsolutePath());
} else {
cmd.add("-u");
cmd.add(toolUserName);
}
DBPConnectionConfiguration connectionInfo = settings.getDataSourceContainer().getActualConnectionConfiguration();
......@@ -53,22 +100,24 @@ public abstract class MySQLNativeToolHandler<SETTINGS extends AbstractNativeTool
return cmd;
}
/*
* Native tools like mysqldump doesn't read password set in
* MYSQL_PWD (also it's deprecated since MySQL 8.0) and prefer
* to grab credentials from my.cnf (that is located somewhere in the system),
* so we'll generate our own my.cnf with required credentials (#5350)
*/
private static String createCredentialsFile(String username, String password) throws IOException {
private static File createCredentialsFile(String username, String password) throws IOException {
File dir = DBWorkbench.getPlatform().getTempFolder(new VoidProgressMonitor(), "mysql-native-handler"); //$NON-NLS-1$
File cnf = new File(dir, ".my.cnf"); //$NON-NLS-1$
cnf.deleteOnExit();
try (Writer writer = new FileWriter(cnf)) {
writer.write("[client]"); //$NON-NLS-1$
writer.write("\nuser=" + (CommonUtils.isEmpty(username) ? "" : username)); //$NON-NLS-1$
writer.write("\npassword=" + (CommonUtils.isEmpty(password) ? "" : password)); //$NON-NLS-1$
writer.write("\nuser=" + CommonUtils.notEmpty(username)); //$NON-NLS-1$
writer.write("\npassword=" + CommonUtils.notEmpty(password)); //$NON-NLS-1$
}
return cnf.getPath();
return cnf;
}
private boolean isOverrideCredentials(SETTINGS settings) {
if (settings instanceof MySQLNativeCredentialsSettings) {
return ((MySQLNativeCredentialsSettings) settings).isOverrideCredentials();
}
return false;
}
}
......@@ -39,7 +39,7 @@ public class MySQLScriptExecuteHandler extends MySQLNativeToolHandler<MySQLScrip
@Override
protected List<String> getCommandLine(MySQLScriptExecuteSettings settings, MySQLCatalog arg) throws IOException {
List<String> cmd = getMySQLToolCommandLine(this, settings, arg);
List<String> cmd = super.getCommandLine(settings, arg);
if (settings.isVerbose()) {
cmd.add("-v");
}
......
......@@ -26,7 +26,7 @@ import org.jkiss.dbeaver.model.runtime.DBRRunnableContext;
import org.jkiss.dbeaver.tasks.nativetool.AbstractScriptExecuteSettings;
import org.jkiss.utils.CommonUtils;
public class MySQLScriptExecuteSettings extends AbstractScriptExecuteSettings<MySQLCatalog> {
public class MySQLScriptExecuteSettings extends AbstractScriptExecuteSettings<MySQLCatalog> implements MySQLNativeCredentialsSettings {
private static final String PREFERENCE_PREFIX = "MySQL.script.";
private static final String PREFERENCE_IS_IMPORT = PREFERENCE_PREFIX + ".import";
private static final String PREFERENCE_LOG_LEVEL = PREFERENCE_PREFIX + "logLevel";
......@@ -46,6 +46,8 @@ public class MySQLScriptExecuteSettings extends AbstractScriptExecuteSettings<My
private boolean isForeignKeyCheckDisabled;
private boolean overrideCredentials;
public LogLevel getLogLevel() {
return logLevel;
}
......@@ -82,6 +84,16 @@ public class MySQLScriptExecuteSettings extends AbstractScriptExecuteSettings<My
return isForeignKeyCheckDisabled;
}
@Override
public boolean isOverrideCredentials() {
return overrideCredentials;
}
@Override
public void setOverrideCredentials(boolean value) {
this.overrideCredentials = value;
}
@Override
public MySQLServerHome findNativeClientHome(String clientHomeId) {
return MySQLDataSourceProvider.getServerHome(clientHomeId);
......@@ -95,6 +107,7 @@ public class MySQLScriptExecuteSettings extends AbstractScriptExecuteSettings<My
logLevel = CommonUtils.valueOf(LogLevel.class, preferenceStore.getString(PREFERENCE_LOG_LEVEL), LogLevel.Normal);
noBeep = CommonUtils.toBoolean(preferenceStore.getString(PREFERENCE_NO_BEEP));
isForeignKeyCheckDisabled = CommonUtils.toBoolean(preferenceStore.getString(PREFERENCE_DISABLE_FOREIGN_KEY));
overrideCredentials = CommonUtils.toBoolean(preferenceStore.getString(MySQLNativeCredentialsSettings.PREFERENCE_NAME));
}
@Override
......@@ -105,5 +118,6 @@ public class MySQLScriptExecuteSettings extends AbstractScriptExecuteSettings<My
preferenceStore.setValue(PREFERENCE_LOG_LEVEL, logLevel.name());
preferenceStore.setValue(PREFERENCE_NO_BEEP, noBeep);
preferenceStore.setValue(PREFERENCE_DISABLE_FOREIGN_KEY, isForeignKeyCheckDisabled);
preferenceStore.setValue(MySQLNativeCredentialsSettings.PREFERENCE_NAME, overrideCredentials);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册