Browse Source

Merge pull request #227 from zilliztech/ssl

fix #225 and fix that server doesn't respect new address
ryjiang 2 years ago
parent
commit
7bcfe42b41

+ 1 - 2
client/src/pages/connect/AuthForm.tsx

@@ -9,7 +9,6 @@ import { ITextfieldConfig } from '../../components/customInput/Types';
 import { useFormValidation } from '../../hooks/Form';
 import { formatForm } from '../../utils/Form';
 import { MilvusHttp } from '../../http/Milvus';
-import { formatAddress } from '../../utils/Format';
 import { useNavigate } from 'react-router-dom';
 import { rootContext } from '../../context/Root';
 import { authContext } from '../../context/Auth';
@@ -183,7 +182,7 @@ export const AuthForm = (props: any) => {
 
   const handleConnect = async (event: React.FormEvent) => {
     event.preventDefault();
-    const address = formatAddress(form.address);
+    const address = form.address;
     const data = { ...form, address };
     await MilvusHttp.connect(data);
 

+ 1 - 1
client/src/utils/Format.ts

@@ -144,7 +144,7 @@ export const getCreateFieldType = (config: Field): CreateFieldType => {
 // Trim the address
 export const formatAddress = (address: string): string => {
   // remove http or https prefix from address
-  const ip = address.replace(/(http|https):\/\//, '');
+  const ip = address.replace(/(http):\/\//, '');
   return ip.includes(':') ? ip : `${ip}:${DEFAULT_MILVUS_PORT}`;
 };
 

+ 5 - 8
server/src/middlewares/index.ts

@@ -1,4 +1,4 @@
-import { Request, Response, NextFunction, Errback } from 'express';
+import { Request, Response, NextFunction } from 'express';
 import morgan from 'morgan';
 import chalk from 'chalk';
 import { MilvusService } from '../milvus/milvus.service';
@@ -15,9 +15,8 @@ export const ReqHeaderMiddleware = (
   const insightCache = req.app.get(INSIGHT_CACHE);
   // all ape requests need set milvus address in header.
   // server will set activeaddress in milvus service.
-  const milvusAddress = MilvusService.formatAddress(
-    (req.headers[MILVUS_ADDRESS] as string) || ''
-  );
+  const milvusAddress = (req.headers[MILVUS_ADDRESS] as string) || '';
+
   // console.log('------ Request headers -------', req.headers);
   //  only api request has MILVUS_ADDRESS.
   //  When client run in express, we dont need static files like: xx.js run this logic.
@@ -25,9 +24,7 @@ export const ReqHeaderMiddleware = (
   if (milvusAddress && insightCache.has(milvusAddress)) {
     MilvusService.activeAddress = milvusAddress;
     // insight cache will update expire time when use insightCache.get
-    MilvusService.activeMilvusClient = insightCache.get(
-      MilvusService.formatAddress(milvusAddress)
-    );
+    MilvusService.activeMilvusClient = insightCache.get(milvusAddress);
   }
 
   const CONNECT_URL = `/api/v1/milvus/connect`;
@@ -35,7 +32,7 @@ export const ReqHeaderMiddleware = (
   if (req.url !== CONNECT_URL && !MilvusService.activeMilvusClient) {
     throw HttpErrors(
       HTTP_STATUS_CODE.FORBIDDEN,
-      'Can not find your connection, please connect Milvus again'
+      'Can not find your connection, please check your connection settings.'
     );
   }
 

+ 11 - 14
server/src/milvus/milvus.service.ts

@@ -23,8 +23,8 @@ export class MilvusService {
   }
 
   static formatAddress(address: string) {
-    // remove http or https prefix from address
-    const ip = address.replace(/(http|https):\/\//, '');
+    // remove http prefix from address
+    const ip = address.replace(/(http):\/\//, '');
     return ip.includes(':') ? ip : `${ip}:${DEFAULT_MILVUS_PORT}`;
   }
 
@@ -32,7 +32,7 @@ export class MilvusService {
     if (!MilvusService.activeMilvusClient) {
       throw HttpErrors(
         HTTP_STATUS_CODE.FORBIDDEN,
-        'Can not find your connection, please connect Milvus again'
+        'Can not find your connection, please check your connection settings.'
       );
 
       // throw new Error('Please connect milvus first');
@@ -50,20 +50,17 @@ export class MilvusService {
     const { address, username, password } = data;
     // grpc only need address without http
     const milvusAddress = MilvusService.formatAddress(address);
-    const hasAuth = username !== undefined && password !== undefined;
 
     try {
-      const milvusClient: MilvusClient = hasAuth
-        ? new MilvusClient({
-            address: milvusAddress,
-            username,
-            password,
-          })
-        : new MilvusClient({ address });
+      const milvusClient: MilvusClient = new MilvusClient({
+        address: milvusAddress,
+        username,
+        password,
+      });
 
       // don't break attu
       await milvusClient.connectPromise.catch(error => {
-        throw HttpErrors(HTTP_STATUS_CODE.BAD_REQUEST, error);
+        throw HttpErrors(HTTP_STATUS_CODE.FORBIDDEN, error);
       });
 
       // check healthy
@@ -78,8 +75,8 @@ export class MilvusService {
       }
     } catch (error) {
       // if milvus is not working, delete connection.
-      cache.del(address);
-      throw HttpErrors(HTTP_STATUS_CODE.BAD_REQUEST, error);
+      cache.dump();
+      throw HttpErrors(HTTP_STATUS_CODE.FORBIDDEN, error);
     }
   }