Skip to content

Commit 8a5fb88

Browse files
committed
Merge branch 'dev' of github.com:chat2db/Chat2DB into dev
2 parents db57c6f + 9269314 commit 8a5fb88

7 files changed

Lines changed: 647 additions & 15 deletions

File tree

chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ncx/ConverterController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public DataResult<UploadVO> dbpUploadFile(@RequestParam("file") MultipartFile fi
5757
// 验证文件后缀
5858
String fileExtension = FileUtils.getFileExtension(Objects.requireNonNull(file.getOriginalFilename()));
5959
if (!fileExtension.equalsIgnoreCase(FileUtils.ConfigFile.DBP.name())) {
60-
return DataResult.error("1", "上传的文件必须是ncx文件!");
60+
return DataResult.error("1", "上传的文件必须是dbp文件!");
6161
}
6262
File temp = new File(ConfigUtils.CONFIG_BASE_PATH + File.separator + UUID.randomUUID() + ".tmp");
6363
file.transferTo(temp);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* DBeaver - Universal Database Manager
3+
* Copyright (C) 2010-2023 DBeaver Corp and others
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package ai.chat2db.server.web.api.controller.ncx.dbeaver;
19+
20+
/**
21+
* Value encryptor
22+
*/
23+
public interface DBSValueEncryptor {
24+
25+
byte[] encryptValue(byte[] value);
26+
27+
byte[] decryptValue(byte[] value);
28+
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* DBeaver - Universal Database Manager
3+
* Copyright (C) 2010-2023 DBeaver Corp and others
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package ai.chat2db.server.web.api.controller.ncx.dbeaver;
18+
19+
import org.apache.poi.util.IOUtils;
20+
21+
import javax.crypto.Cipher;
22+
import javax.crypto.CipherInputStream;
23+
import javax.crypto.CipherOutputStream;
24+
import javax.crypto.SecretKey;
25+
import javax.crypto.spec.IvParameterSpec;
26+
import javax.crypto.spec.SecretKeySpec;
27+
import java.io.ByteArrayInputStream;
28+
import java.io.ByteArrayOutputStream;
29+
import java.io.InputStream;
30+
import java.nio.charset.StandardCharsets;
31+
import java.util.Arrays;
32+
33+
/**
34+
* Default value encryptor.
35+
*
36+
* Uses Eclipse secure preferences to read/write secrets.
37+
*/
38+
public class DefaultValueEncryptor implements DBSValueEncryptor {
39+
40+
public static final String CIPHER_NAME = "AES/CBC/PKCS5Padding";
41+
public static final String KEY_ALGORITHM = "AES";
42+
43+
private static final byte[] LOCAL_KEY_CACHE = new byte[] { -70, -69, 74, -97, 119, 74, -72, 83, -55, 108, 45, 101, 61, -2, 84, 74 };
44+
45+
private final SecretKey secretKey;
46+
private final Cipher cipher;
47+
48+
public DefaultValueEncryptor(SecretKey secretKey) {
49+
this.secretKey = secretKey;
50+
try {
51+
this.cipher = Cipher.getInstance(CIPHER_NAME);
52+
} catch (Exception e) {
53+
throw new IllegalStateException("Internal error during encrypted init", e);
54+
}
55+
}
56+
57+
/**
58+
* 通过 DBeaver 源码查看到默认的 SecretKey
59+
**/
60+
public static SecretKey getLocalSecretKey() {
61+
return new SecretKeySpec(LOCAL_KEY_CACHE, DefaultValueEncryptor.KEY_ALGORITHM);
62+
}
63+
64+
public static SecretKey makeSecretKeyFromPassword(String password) {
65+
byte[] bytes = password.getBytes(StandardCharsets.UTF_8);
66+
byte[] passBytes = Arrays.copyOf(bytes, 16);
67+
return new SecretKeySpec(passBytes, KEY_ALGORITHM);
68+
}
69+
70+
@Override
71+
public byte[] encryptValue(byte[] value) {
72+
try {
73+
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
74+
byte[] iv = cipher.getIV();
75+
76+
ByteArrayOutputStream resultBuffer = new ByteArrayOutputStream();
77+
try (CipherOutputStream cipherOut = new CipherOutputStream(resultBuffer, cipher)) {
78+
resultBuffer.write(iv);
79+
cipherOut.write(value);
80+
}
81+
return resultBuffer.toByteArray();
82+
} catch (Exception e) {
83+
throw new RuntimeException("Error encrypting value", e);
84+
}
85+
}
86+
87+
@Override
88+
public byte[] decryptValue(byte[] value) {
89+
try (InputStream byteStream = new ByteArrayInputStream(value)) {
90+
byte[] fileIv = new byte[16];
91+
byteStream.read(fileIv);
92+
cipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(fileIv));
93+
94+
try (CipherInputStream cipherIn = new CipherInputStream(byteStream, cipher)) {
95+
ByteArrayOutputStream resultBuffer = new ByteArrayOutputStream();
96+
IOUtils.copy(cipherIn, resultBuffer);
97+
return resultBuffer.toByteArray();
98+
}
99+
100+
} catch (Exception e) {
101+
throw new RuntimeException("Error decrypting value", e);
102+
}
103+
}
104+
105+
}

chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ncx/enums/DataBaseType.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,31 +37,31 @@ public enum DataBaseType {
3737
/**
3838
* Mariadb
3939
**/
40-
Mariadb("jdbc:mariadb://%s:%s"),
40+
MARIADB("jdbc:mariadb://%s:%s"),
4141
/**
4242
* DM
4343
**/
4444
DM("jdbc:dm://%s:%s"),
4545
/**
46-
* KINGBASE8
46+
* KINGBASE
4747
**/
48-
KINGBASE8("jdbc:kingbase8://%s:%s"),
48+
KINGBASE("jdbc:kingbase8://%s:%s"),
4949
/**
5050
* Presto
5151
**/
52-
Presto("jdbc:presto://%s:%s"),
52+
PRESTO("jdbc:presto://%s:%s"),
5353
/**
5454
* OceanBase
5555
**/
56-
OceanBase("jdbc:oceanbase://%s:%s"),
56+
OCEANBASE("jdbc:oceanbase://%s:%s"),
5757
/**
5858
* Hive
5959
**/
60-
Hive("jdbc:hive2://%s:%s"),
60+
HIVE("jdbc:hive2://%s:%s"),
6161
/**
6262
* ClickHouse
6363
**/
64-
ClickHouse("jdbc:clickhouse://%s:%s");
64+
CLICKHOUSE("jdbc:clickhouse://%s:%s");
6565

6666
private String urlString;
6767

@@ -76,7 +76,8 @@ public void setUrlString(String urlString) {
7676
public static DataBaseType matchType(String value) {
7777
if (StringUtils.isNotEmpty(value)) {
7878
for (DataBaseType dataBase : DataBaseType.values()) {
79-
if (dataBase.name().equals(value.toUpperCase())) {
79+
//kingbase -> kingbase8
80+
if (value.toUpperCase().contains(dataBase.name())) {
8081
return dataBase;
8182
}
8283
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* DBeaver - Universal Database Manager
3+
* Copyright (C) 2010-2023 DBeaver Corp and others
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package ai.chat2db.server.web.api.controller.ncx.enums;
19+
20+
/**
21+
* Import/Export constants
22+
*/
23+
public class ExportConstants {
24+
25+
public static final String ARCHIVE_FILE_EXT = ".dbp"; //NON-NLS-1
26+
public static final String CONFIG_FILE = ".dbeaver"; //NON-NLS-1
27+
public static final String CONFIG_DATASOURCE_FILE = "data-sources.json"; //NON-NLS-1
28+
public static final String CONFIG_CREDENTIALS_FILE = "credentials-config.json"; //NON-NLS-1
29+
public static final String DIR_PROJECTS = "projects"; //NON-NLS-1
30+
public static final String DIR_DRIVERS = "drivers"; //NON-NLS-1
31+
public static final String DIR_CONNECTIONS = "connections"; //NON-NLS-1
32+
public static final String DIR_CONFIGURATION = "configuration"; //NON-NLS-1
33+
public static final String GENERIC = "generic"; //NON-NLS-1
34+
35+
public static final String META_FILENAME = "meta.xml"; //NON-NLS-1
36+
37+
public static final String TAG_ARCHIVE = "archive"; //NON-NLS-1
38+
public static final String TAG_SOURCE = "source";
39+
public static final String TAG_PROJECTS = "projects"; //NON-NLS-1
40+
public static final String TAG_PROJECT = "project"; //NON-NLS-1
41+
public static final String TAG_RESOURCE = "resource"; //NON-NLS-1
42+
public static final String TAG_ATTRIBUTE = "attribute"; //NON-NLS-1
43+
public static final String TAG_LIBRARIES = "libraries"; //NON-NLS-1
44+
45+
public static final String ATTR_VERSION = "version"; //NON-NLS-1
46+
public static final String ATTR_HOST = "host";
47+
public static final String ATTR_ADDRESS = "address";
48+
public static final String ATTR_TIME = "time";
49+
public static final String ATTR_QUALIFIER = "qualifier"; //NON-NLS-1
50+
public static final String ATTR_NAME = "name"; //NON-NLS-1
51+
public static final String ATTR_VALUE = "value"; //NON-NLS-1
52+
public static final String ATTR_DIRECTORY = "directory"; //NON-NLS-1
53+
public static final String ATTR_DESCRIPTION = "description"; //NON-NLS-1
54+
public static final String ATTR_CHARSET = "charset"; //NON-NLS-1
55+
public static final String ATTR_PATH = "path"; //NON-NLS-1
56+
public static final String ATTR_FILE = "file"; //NON-NLS-1
57+
58+
}

0 commit comments

Comments
 (0)