Skip to content

Commit

Permalink
Merge pull request #227 from zilliztech/ssl
Browse files Browse the repository at this point in the history
fix #225 and fix that server doesn't respect new address
  • Loading branch information
shanghaikid authored Jul 21, 2023
2 parents 0309e33 + ae4239a commit 7bcfe42
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 25 deletions.
3 changes: 1 addition & 2 deletions client/src/pages/connect/AuthForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion client/src/utils/Format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
};

Expand Down
13 changes: 5 additions & 8 deletions server/src/middlewares/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -15,27 +15,24 @@ 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.
// Otherwise will cause 401 error.
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`;

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.'
);
}

Expand Down
25 changes: 11 additions & 14 deletions server/src/milvus/milvus.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ 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}`;
}

checkMilvus() {
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');
Expand All @@ -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
Expand All @@ -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);
}
}

Expand Down

0 comments on commit 7bcfe42

Please sign in to comment.