Skip to content

Commit 64538b8

Browse files
authored
Merge pull request #8 from Tencent/dev/aleronwang
feat: add TLS/SSL support for HTTP and gRPC clients
2 parents 222cea2 + a9936eb commit 64538b8

28 files changed

Lines changed: 5488 additions & 2428 deletions

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
# Changelog
2+
## v2.6.1
3+
* refactor: Refactoring stub code, extract business logic from HttpStub and GrpcStub to separate service classes for better maintainability.
4+
* feat: Add TLS/SSL support for both HTTP and RPC clients. Support custom CA certificates and insecure skip verify mode for HTTPS connections.
5+
26
## v2.6.0
37
* feat: Sparse vectors support disk indexing. Add a field called `diskSwapEnabled` to control whether it is enabled or not.
48
* fix: modify hybrid search result question.

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ You can use **Apache Maven** or **Gradle**/**Grails** to download the SDK.
2424
<dependency>
2525
<groupId>com.tencent.tcvectordb</groupId>
2626
<artifactId>vectordatabase-sdk-java</artifactId>
27-
<version>2.6.0</version>
27+
<version>2.6.1</version>
2828
</dependency>
2929
```
3030

3131
- Gradle/Grails
3232

3333
```gradle
34-
compile 'com.tencent.tcvectordb:vectordatabase-sdk-java:2.6.0'
34+
compile 'com.tencent.tcvectordb:vectordatabase-sdk-java:2.6.1'
3535
```
3636

3737
### Examples

tcvectordb/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ You can use **Apache Maven** or **Gradle**/**Grails** to download the SDK.
2424
<dependency>
2525
<groupId>com.tencent.tcvectordb</groupId>
2626
<artifactId>vectordatabase-sdk-java</artifactId>
27-
<version>2.6.0</version>
27+
<version>2.6.1</version>
2828
</dependency>
2929
```
3030

3131
- Gradle/Grails
3232

3333
```gradle
34-
compile 'com.tencent.tcvectordb:vectordatabase-sdk-java:2.6.0'
34+
compile 'com.tencent.tcvectordb:vectordatabase-sdk-java:2.6.1'
3535
```
3636

3737
### Examples

tcvectordb/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>com.tencent.tcvectordb</groupId>
66
<artifactId>vectordatabase-sdk-java</artifactId>
7-
<version>2.6.0</version>
7+
<version>2.6.1</version>
88
<packaging>jar</packaging>
99

1010
<name>vectordatabase-sdk-java</name>

tcvectordb/src/main/java/com/tencent/tcvectordb/examples/VectorDBTLSExampleReal.java

Lines changed: 454 additions & 0 deletions
Large diffs are not rendered by default.

tcvectordb/src/main/java/com/tencent/tcvectordb/model/param/database/ConnectParam.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,33 @@
2525

2626
/**
2727
* Parameters for client connection.
28+
* <p>
29+
* This class provides configuration options for connecting to VectorDB server,
30+
* including connection settings, timeout settings, and TLS/SSL configuration.
31+
* </p>
32+
*
33+
* <h3>Basic Usage:</h3>
34+
* <pre>{@code
35+
* ConnectParam connectParam = ConnectParam.newBuilder()
36+
* .withUrl("http://localhost:8080")
37+
* .withUsername("root")
38+
* .withKey("your-api-key")
39+
* .build();
40+
* }</pre>
41+
*
42+
* <h3>HTTPS with TLS Configuration:</h3>
43+
* <pre>{@code
44+
* TLSConfig tlsConfig = TLSConfig.newBuilder()
45+
* .withCACert("/path/to/ca-cert.pem")
46+
* .build();
47+
*
48+
* ConnectParam connectParam = ConnectParam.newBuilder()
49+
* .withUrl("https://your-vectordb-server.com")
50+
* .withUsername("root")
51+
* .withKey("your-api-key")
52+
* .withTLSConfig(tlsConfig)
53+
* .build();
54+
* }</pre>
2855
*/
2956
public class ConnectParam {
3057
private final String url;
@@ -48,6 +75,11 @@ public class ConnectParam {
4875
*/
4976
private final int keepAliveDuration;
5077

78+
/**
79+
* TLS/SSL configuration for secure connections.
80+
*/
81+
private final TLSConfig tlsConfig;
82+
5183
private ConnectParam(Builder builder) {
5284
this.url = builder.url;
5385
this.username = builder.username;
@@ -57,6 +89,7 @@ private ConnectParam(Builder builder) {
5789
this.maxIdleConnections = builder.maxIdleConnections;
5890
this.keepAliveDuration = builder.keepAliveDuration;
5991
this.connectionPoolSize = builder.connectionPoolSize;
92+
this.tlsConfig = builder.tlsConfig;
6093
}
6194

6295
public String getUrl() {
@@ -91,6 +124,15 @@ public int getKeepAliveDuration() {
91124
return keepAliveDuration;
92125
}
93126

127+
/**
128+
* Gets the TLS configuration.
129+
*
130+
* @return TLS configuration, or null if not set
131+
*/
132+
public TLSConfig getTLSConfig() {
133+
return tlsConfig;
134+
}
135+
94136
public static Builder newBuilder() {
95137
return new Builder();
96138
}
@@ -119,6 +161,11 @@ public static class Builder {
119161
*/
120162
private int keepAliveDuration = 5 * 60;
121163

164+
/**
165+
* TLS configuration
166+
*/
167+
private TLSConfig tlsConfig;
168+
122169
private Builder() {
123170
}
124171

@@ -184,6 +231,22 @@ public Builder withKeepAliveDuration(int keepAliveDuration) {
184231
return this;
185232
}
186233

234+
/**
235+
* Sets the TLS configuration for secure connections.
236+
* <p>
237+
* Use this to configure CA certificates, skip certificate verification,
238+
* or set custom SNI server names for HTTPS connections.
239+
* </p>
240+
*
241+
* @param tlsConfig TLS configuration
242+
* @return this builder
243+
* @see TLSConfig
244+
*/
245+
public Builder withTLSConfig(TLSConfig tlsConfig) {
246+
this.tlsConfig = tlsConfig;
247+
return this;
248+
}
249+
187250

188251
public ConnectParam build() throws ParamException {
189252
if (StringUtils.isEmpty(this.url)) {
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
/*
2+
* Copyright (C) 2023 Tencent Cloud.
3+
* Permission is hereby granted, free of charge, to any person obtaining a copy of
4+
* this software and associated documentation files (the vectordb-sdk-java), to
5+
* deal in the Software without restriction, including without limitation the
6+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
* copies of the Software, and to permit persons to whom the Software is furnished
8+
* to do so, subject to the following conditions:
9+
*
10+
* The above copyright notice and this permission notice shall be included in all
11+
* copies or substantial portions of the Software.
12+
*
13+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
14+
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
15+
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
16+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
18+
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19+
*/
20+
21+
package com.tencent.tcvectordb.model.param.database;
22+
23+
/**
24+
* TLS configuration for HTTPClient and RPCClient.
25+
* <p>
26+
* This class provides configuration options for TLS/SSL connections to VectorDB server.
27+
* </p>
28+
*
29+
* <h3>Usage Examples:</h3>
30+
*
31+
* <p><b>1. Using CA certificate file path:</b></p>
32+
* <pre>{@code
33+
* TLSConfig tlsConfig = TLSConfig.newBuilder()
34+
* .withCACert("/path/to/ca-cert.pem")
35+
* .build();
36+
* }</pre>
37+
*
38+
* <p><b>2. Using CA certificate content directly:</b></p>
39+
* <pre>{@code
40+
* String caCertContent = "-----BEGIN CERTIFICATE-----\n...certificate content...\n-----END CERTIFICATE-----";
41+
* TLSConfig tlsConfig = TLSConfig.newBuilder()
42+
* .withCACert(caCertContent)
43+
* .build();
44+
* }</pre>
45+
*
46+
* <p><b>3. Skip certificate verification (for testing only):</b></p>
47+
* <pre>{@code
48+
* TLSConfig tlsConfig = TLSConfig.newBuilder()
49+
* .withInsecureSkipVerify(true)
50+
* .build();
51+
* }</pre>
52+
*/
53+
public class TLSConfig {
54+
55+
/**
56+
* Default server name for SNI when connecting to IP addresses.
57+
*/
58+
public static final String DEFAULT_SERVER_NAME = "vdb.tencentcloud.com";
59+
60+
/**
61+
* CA certificate content or file path for HTTPS connections.
62+
* <p>
63+
* If the value is a valid file path, the certificate will be read from the file.
64+
* Otherwise, it will be treated as the certificate content directly (PEM format).
65+
* </p>
66+
*/
67+
private final String caCert;
68+
69+
/**
70+
* Whether to skip TLS certificate verification.
71+
* <p>
72+
* WARNING: This should only be used for testing environments.
73+
* Never use this in production as it makes the connection vulnerable to MITM attacks.
74+
* </p>
75+
*/
76+
private final boolean insecureSkipVerify;
77+
78+
/**
79+
* Server name for SNI (Server Name Indication).
80+
* <p>
81+
* This is automatically set to {@link #DEFAULT_SERVER_NAME} when connecting to an IP address.
82+
* </p>
83+
*/
84+
private final String serverName;
85+
86+
private TLSConfig(Builder builder) {
87+
this.caCert = builder.caCert;
88+
this.insecureSkipVerify = builder.insecureSkipVerify;
89+
this.serverName = builder.serverName;
90+
}
91+
92+
/**
93+
* Gets the CA certificate content or file path.
94+
*
95+
* @return CA certificate content or file path, or null if not set
96+
*/
97+
public String getCACert() {
98+
return caCert;
99+
}
100+
101+
/**
102+
* Returns whether to skip TLS certificate verification.
103+
*
104+
* @return true if certificate verification should be skipped, false otherwise
105+
*/
106+
public boolean isInsecureSkipVerify() {
107+
return insecureSkipVerify;
108+
}
109+
110+
/**
111+
* Gets the server name for SNI.
112+
*
113+
* @return server name for SNI
114+
*/
115+
public String getServerName() {
116+
return serverName;
117+
}
118+
119+
/**
120+
* Creates a new builder for TLSConfig.
121+
*
122+
* @return a new Builder instance
123+
*/
124+
public static Builder newBuilder() {
125+
return new Builder();
126+
}
127+
128+
/**
129+
* Builder class for TLSConfig.
130+
*/
131+
public static class Builder {
132+
private String caCert;
133+
private boolean insecureSkipVerify = false;
134+
private String serverName = DEFAULT_SERVER_NAME;
135+
136+
private Builder() {
137+
}
138+
139+
/**
140+
* Sets the CA certificate content or file path.
141+
* <p>
142+
* The certificate can be provided in two ways:
143+
* <ul>
144+
* <li>As a file path pointing to a PEM-encoded certificate file</li>
145+
* <li>As the certificate content directly in PEM format</li>
146+
* </ul>
147+
* </p>
148+
*
149+
* @param caCert CA certificate content or file path
150+
* @return this builder
151+
*/
152+
public Builder withCACert(String caCert) {
153+
this.caCert = caCert;
154+
return this;
155+
}
156+
157+
/**
158+
* Sets whether to skip TLS certificate verification.
159+
* <p>
160+
* WARNING: Only use this for testing environments. Never use in production.
161+
* </p>
162+
*
163+
* @param insecureSkipVerify true to skip certificate verification
164+
* @return this builder
165+
*/
166+
public Builder withInsecureSkipVerify(boolean insecureSkipVerify) {
167+
this.insecureSkipVerify = insecureSkipVerify;
168+
return this;
169+
}
170+
171+
/**
172+
* Sets the server name for SNI.
173+
* <p>
174+
* By default, this is set to {@link TLSConfig#DEFAULT_SERVER_NAME}.
175+
* </p>
176+
*
177+
* @param serverName server name for SNI
178+
* @return this builder
179+
*/
180+
public Builder withServerName(String serverName) {
181+
if (serverName != null && !serverName.isEmpty()) {
182+
this.serverName = serverName;
183+
}
184+
return this;
185+
}
186+
187+
/**
188+
* Builds the TLSConfig instance.
189+
*
190+
* @return a new TLSConfig instance
191+
*/
192+
public TLSConfig build() {
193+
return new TLSConfig(this);
194+
}
195+
}
196+
197+
@Override
198+
public String toString() {
199+
return "TLSConfig{" +
200+
"caCert='" + (caCert != null ? "[SET]" : "[NOT SET]") + '\'' +
201+
", insecureSkipVerify=" + insecureSkipVerify +
202+
", serverName='" + serverName + '\'' +
203+
'}';
204+
}
205+
}

0 commit comments

Comments
 (0)