提交 076a2f92 编写于 作者: S serge-rider

#6599 Multiple secured credentials support


Former-commit-id: be7a12a3
上级 d58885a1
......@@ -64,7 +64,7 @@ public class ProjectExportWizard extends Wizard implements IExportWizard {
static {
IGNORED_RESOURCES.add(PROJECT_DESC_FILE);
IGNORED_RESOURCES.add(DBPDataSourceRegistry.CREDENTIALS_CONFIG_FILE_NAME);
IGNORED_RESOURCES.add(DBPDataSourceRegistry.CREDENTIALS_CONFIG_FILE_PREFIX);
}
public ProjectExportWizard() {
......
......@@ -41,7 +41,8 @@ public interface DBPDataSourceRegistry extends DBPObject {
String MODERN_CONFIG_FILE_PREFIX = "data-sources"; //$NON-NLS-1$
String MODERN_CONFIG_FILE_EXT = ".json"; //$NON-NLS-1$
String MODERN_CONFIG_FILE_NAME = MODERN_CONFIG_FILE_PREFIX + MODERN_CONFIG_FILE_EXT;
String CREDENTIALS_CONFIG_FILE_NAME = "credentials-config.json"; //$NON-NLS-1$
String CREDENTIALS_CONFIG_FILE_PREFIX = "credentials-config"; //$NON-NLS-1$
String CREDENTIALS_CONFIG_FILE_EXT = ".json"; //$NON-NLS-1$
@NotNull
DBPPlatform getPlatform();
......
......@@ -17,6 +17,7 @@
package org.jkiss.dbeaver.registry;
import org.eclipse.core.resources.IFile;
import org.jkiss.dbeaver.model.app.DBPDataSourceRegistry;
/**
* DataSourceOrigin
......@@ -25,16 +26,31 @@ class DataSourceOrigin
{
private final IFile sourceFile;
private final boolean isDefault;
private final String configSuffix;
public DataSourceOrigin(IFile sourceFile, boolean isDefault) {
this.sourceFile = sourceFile;
this.isDefault = isDefault;
if (isDefault) {
configSuffix = "";
} else {
String configFileName = sourceFile.getName();
configSuffix = configFileName.substring(
DBPDataSourceRegistry.MODERN_CONFIG_FILE_PREFIX.length(),
configFileName.length() - DBPDataSourceRegistry.MODERN_CONFIG_FILE_EXT.length());
}
}
public String getName() {
return sourceFile.getName();
}
public String getConfigSuffix() {
return configSuffix;
}
public boolean isDefault() {
return isDefault;
}
......
......@@ -666,7 +666,7 @@ public class DataSourceRegistry implements DBPDataSourceRegistry {
}
serializer.saveDataSources(
monitor,
origin.isDefault(),
origin,
localDataSources,
configFile);
}
......
......@@ -31,7 +31,7 @@ interface DataSourceSerializer
{
void saveDataSources(
DBRProgressMonitor monitor,
boolean primaryConfig,
DataSourceOrigin origin,
List<DataSourceDescriptor> localDataSources,
IFile configFile)
throws DBException, IOException;
......
......@@ -80,7 +80,7 @@ class DataSourceSerializerLegacy implements DataSourceSerializer
@Override
public void saveDataSources(
DBRProgressMonitor monitor,
boolean primaryConfig,
DataSourceOrigin origin,
List<DataSourceDescriptor> localDataSources,
IFile configFile) throws DBException, IOException
{
......@@ -90,7 +90,7 @@ class DataSourceSerializerLegacy implements DataSourceSerializer
XMLBuilder xml = new XMLBuilder(tempStream, GeneralUtils.UTF8_ENCODING);
xml.setButify(true);
try (XMLBuilder.Element el1 = xml.startElement("data-sources")) {
if (primaryConfig) {
if (origin.isDefault()) {
// Folders (only for default origin)
for (DataSourceFolder folder : registry.getAllFolders()) {
saveFolder(xml, folder);
......@@ -106,7 +106,7 @@ class DataSourceSerializerLegacy implements DataSourceSerializer
}
// Filters
if (primaryConfig) {
if (origin.isDefault()) {
try (XMLBuilder.Element ignored = xml.startElement(RegistryConstants.TAG_FILTERS)) {
for (DBSObjectFilter cf : registry.getSavedFilters()) {
if (!cf.isEmpty()) {
......
......@@ -91,9 +91,10 @@ class DataSourceSerializerModern implements DataSourceSerializer
@Override
public void saveDataSources(
DBRProgressMonitor monitor,
boolean primaryConfig,
DataSourceOrigin origin,
List<DataSourceDescriptor> localDataSources,
IFile configFile) throws DBException, IOException {
IFile configFile) throws DBException, IOException
{
ByteArrayOutputStream dsConfigBuffer = new ByteArrayOutputStream(10000);
try (OutputStreamWriter osw = new OutputStreamWriter(dsConfigBuffer, StandardCharsets.UTF_8)) {
try (JsonWriter jsonWriter = CONFIG_GSON.newJsonWriter(osw)) {
......@@ -101,7 +102,7 @@ class DataSourceSerializerModern implements DataSourceSerializer
jsonWriter.beginObject();
// Save folders
if (primaryConfig) {
if (origin.isDefault()) {
jsonWriter.name("folders");
jsonWriter.beginObject();
// Folders (only for default origin)
......@@ -139,7 +140,7 @@ class DataSourceSerializerModern implements DataSourceSerializer
jsonWriter.endObject();
}
if (primaryConfig) {
if (origin.isDefault()) {
if (!virtualModels.isEmpty()) {
// Save virtual models
jsonWriter.name("virtual-models");
......@@ -240,12 +241,17 @@ class DataSourceSerializerModern implements DataSourceSerializer
throw new IOException("Error saving configuration to a file " + configFile.getFullPath(), e);
}
saveSecureCredentialsFile(monitor.getNestedMonitor(), (IFolder) configFile.getParent());
{
saveSecureCredentialsFile(
monitor.getNestedMonitor(),
(IFolder) configFile.getParent(),
origin);
}
}
private void saveSecureCredentialsFile(IProgressMonitor monitor, IFolder parent) {
private void saveSecureCredentialsFile(IProgressMonitor monitor, IFolder parent, DataSourceOrigin origin) {
IFile credFile = parent.getFile(DBPDataSourceRegistry.CREDENTIALS_CONFIG_FILE_NAME);
IFile credFile = parent.getFile(DBPDataSourceRegistry.CREDENTIALS_CONFIG_FILE_PREFIX + origin.getConfigSuffix() + DBPDataSourceRegistry.CREDENTIALS_CONFIG_FILE_EXT);
try {
// Serialize and encrypt
String jsonString = SECURE_GSON.toJson(secureProperties, Map.class);
......@@ -278,7 +284,7 @@ class DataSourceSerializerModern implements DataSourceSerializer
// Read secured creds file
IFolder mdFolder = registry.getProject().getMetadataFolder(false);
if (mdFolder.exists()) {
IFile credFile = mdFolder.getFile(DBPDataSourceRegistry.CREDENTIALS_CONFIG_FILE_NAME);
IFile credFile = mdFolder.getFile(DBPDataSourceRegistry.CREDENTIALS_CONFIG_FILE_PREFIX + origin.getConfigSuffix() + DBPDataSourceRegistry.CREDENTIALS_CONFIG_FILE_EXT);
if (credFile.exists()) {
ByteArrayOutputStream credBuffer = new ByteArrayOutputStream();
try (InputStream crdStream = credFile.getContents()) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册