|
27 | 27 | import io.milvus.orm.iterator.QueryIterator; |
28 | 28 | import io.milvus.orm.iterator.SearchIterator; |
29 | 29 | import io.milvus.orm.iterator.SearchIteratorV2; |
30 | | -import io.milvus.v2.exception.ErrorCode; |
31 | | -import io.milvus.v2.exception.MilvusClientException; |
32 | 30 | import io.milvus.v2.service.cdc.CDCService; |
33 | 31 | import io.milvus.v2.service.cdc.request.UpdateReplicateConfigurationReq; |
34 | 32 | import io.milvus.v2.service.cdc.response.UpdateReplicateConfigurationResp; |
|
71 | 69 |
|
72 | 70 | import java.util.List; |
73 | 71 | import java.util.concurrent.TimeUnit; |
74 | | -import java.net.InetAddress; |
75 | | -import java.net.UnknownHostException; |
76 | | -import java.net.Socket; |
77 | | -import java.io.IOException; |
78 | | -import java.net.InetSocketAddress; |
79 | | -import javax.net.ssl.*; |
80 | | -import java.io.FileInputStream; |
81 | | -import java.io.InputStream; |
82 | | -import java.security.KeyStore; |
83 | | -import java.security.PrivateKey; |
84 | | -import java.security.SecureRandom; |
85 | | -import java.security.cert.CertificateFactory; |
86 | | -import java.security.cert.X509Certificate; |
87 | 72 |
|
88 | 73 | public class MilvusClientV2 { |
89 | 74 | private static final Logger logger = LoggerFactory.getLogger(MilvusClientV2.class); |
@@ -140,9 +125,9 @@ private void initServices(String dbName) { |
140 | 125 | private void connect(ConnectConfig connectConfig) { |
141 | 126 | this.connectConfig = connectConfig; |
142 | 127 | if (connectConfig.isEnablePrecheck()) { |
143 | | - validateHostname(connectConfig); |
144 | | - validatePort(connectConfig); |
145 | | - validateCert(connectConfig); |
| 128 | + clientUtils.validateHostname(connectConfig); |
| 129 | + clientUtils.validatePort(connectConfig); |
| 130 | + clientUtils.validateCert(connectConfig); |
146 | 131 | } |
147 | 132 | try { |
148 | 133 | if (this.channel != null) { |
@@ -235,120 +220,6 @@ public String currentUsedDatabase() { |
235 | 220 | return dbName; |
236 | 221 | } |
237 | 222 |
|
238 | | - /** |
239 | | - * Validates that the hostname can be resolved before attempting connection. |
240 | | - * This provides early failure with clear error messages for DNS issues. |
241 | | - * |
242 | | - * @param connectConfig Connection configuration containing the host to validate |
243 | | - * @throws MilvusClientException if hostname cannot be resolved |
244 | | - */ |
245 | | - public void validateHostname(ConnectConfig connectConfig) { |
246 | | - String host = connectConfig.getHost(); |
247 | | - |
248 | | - if (StringUtils.isEmpty(host)) { |
249 | | - throw new MilvusClientException(ErrorCode.INVALID_PARAMS, |
250 | | - "Hostname cannot be null or empty"); |
251 | | - } |
252 | | - |
253 | | - try { |
254 | | - // Attempt DNS resolution |
255 | | - InetAddress.getByName(host); |
256 | | - logger.debug("Successfully resolved hostname: {}", host); |
257 | | - } catch (UnknownHostException e) { |
258 | | - String message = String.format( |
259 | | - "Failed to resolve hostname '%s'. Please verify the hostname is correct and DNS is configured properly.", |
260 | | - host |
261 | | - ); |
262 | | - logger.error(message, e); |
263 | | - throw new MilvusClientException(ErrorCode.RPC_ERROR, message); |
264 | | - } |
265 | | - } |
266 | | - |
267 | | - /** |
268 | | - * Validates port number and tests connectivity. |
269 | | - * |
270 | | - * @param connectConfig Connection configuration containing the port to validate |
271 | | - * @throws MilvusClientException if port is invalid or unreachable |
272 | | - */ |
273 | | - public void validatePort(ConnectConfig connectConfig) { |
274 | | - int port = connectConfig.getPort(); |
275 | | - String host = connectConfig.getHost(); |
276 | | - |
277 | | - // Check valid range |
278 | | - if (port < 1 || port > 65535) { |
279 | | - String message = String.format( |
280 | | - "Invalid port number '%d'. Port must be between 1 and 65535.", |
281 | | - port |
282 | | - ); |
283 | | - logger.error(message); |
284 | | - throw new MilvusClientException(ErrorCode.INVALID_PARAMS, message); |
285 | | - } |
286 | | - |
287 | | - // Test if port is reachable |
288 | | - try (Socket socket = new Socket()) { |
289 | | - socket.connect(new InetSocketAddress(host, port), (int) connectConfig.getConnectTimeoutMs()); |
290 | | - logger.debug("Successfully validated port: {}", port); |
291 | | - } catch (IOException e) { |
292 | | - String message = String.format( |
293 | | - "Cannot connect to '%s:%d'. Please verify the port number is correct and server is running.", |
294 | | - host, port |
295 | | - ); |
296 | | - logger.error(message, e); |
297 | | - throw new MilvusClientException(ErrorCode.RPC_ERROR, message); |
298 | | - } |
299 | | - } |
300 | | - |
301 | | - /** |
302 | | - * Validates SSL connection with certificates. |
303 | | - * |
304 | | - * @param connectConfig Connection configuration |
305 | | - * @throws MilvusClientException if SSL connection fails |
306 | | - */ |
307 | | - public void validateCert(ConnectConfig connectConfig) { |
308 | | - if (!connectConfig.isSecure()) { |
309 | | - return; |
310 | | - } |
311 | | - |
312 | | - try { |
313 | | - SSLContext sslContext = SSLContext.getInstance("TLS"); |
314 | | - TrustManagerFactory tmf = null; |
315 | | - |
316 | | - // Load server certificate (CA cert) |
317 | | - if (connectConfig.getServerPemPath() != null && !connectConfig.getServerPemPath().isEmpty()) { |
318 | | - try (InputStream certStream = new FileInputStream(connectConfig.getServerPemPath())) { |
319 | | - CertificateFactory cf = CertificateFactory.getInstance("X.509"); |
320 | | - X509Certificate caCert = (X509Certificate) cf.generateCertificate(certStream); |
321 | | - |
322 | | - KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); |
323 | | - trustStore.load(null, null); |
324 | | - trustStore.setCertificateEntry("ca-cert", caCert); |
325 | | - |
326 | | - tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); |
327 | | - tmf.init(trustStore); |
328 | | - } |
329 | | - } |
330 | | - |
331 | | - // Initialize SSLContext with the server certificate |
332 | | - sslContext.init(null, tmf != null ? tmf.getTrustManagers() : null, new SecureRandom()); |
333 | | - |
334 | | - // Validate connection |
335 | | - SSLSocketFactory socketFactory = sslContext.getSocketFactory(); |
336 | | - try (SSLSocket socket = (SSLSocket) socketFactory.createSocket()) { |
337 | | - socket.connect(new InetSocketAddress(connectConfig.getHost(), connectConfig.getPort()), |
338 | | - (int) connectConfig.getConnectTimeoutMs()); |
339 | | - socket.startHandshake(); |
340 | | - logger.debug("SSL certificate validation passed"); |
341 | | - } |
342 | | - |
343 | | - } catch (SSLException e) { |
344 | | - throw new MilvusClientException(ErrorCode.RPC_ERROR, |
345 | | - "SSL certificate validation failed: " + e.getMessage() + |
346 | | - ". Please verify your certificates are correct."); |
347 | | - } catch (Exception e) { |
348 | | - throw new MilvusClientException(ErrorCode.RPC_ERROR, |
349 | | - "Failed to connect with SSL: " + e.getMessage()); |
350 | | - } |
351 | | - } |
352 | 223 |
|
353 | 224 | ///////////////////////////////////////////////////////////////////////////////////////////// |
354 | 225 | // Database Operations |
|
0 commit comments