Skip to content

Commit

Permalink
Merge pull request #148 from hypersign-protocol/master
Browse files Browse the repository at this point in the history
merging from master
  • Loading branch information
Vishwas1 authored Jul 29, 2024
2 parents dae4302 + 96be2d5 commit d7a9d98
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 65 deletions.
101 changes: 55 additions & 46 deletions .deploy/deployment.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
---
apiVersion: apps/v1
kind: Deployment
metadata:
Expand All @@ -16,50 +15,57 @@ spec:
app: entity-api
spec:
containers:
- name: entity-api
image: __GOOGLE_ARTIFACT_URL__/__GOOGLE_PROJECT_ID__/__GOOGLE_ARTIFACT_REPO__/enity-api-service:__LATEST_RELEASE_TAG__
imagePullPolicy: Always
- name: entity-api
image: __GOOGLE_ARTIFACT_URL__/__GOOGLE_PROJECT_ID__/__GOOGLE_ARTIFACT_REPO__/enity-api-service:__LATEST_RELEASE_TAG__
imagePullPolicy: Always
livenessProbe:
httpGet:
path: /api/v1/edv/state
port: 3001
initialDelaySeconds: 120
periodSeconds: 30
failureThreshold: 3

resources:
limits:
memory: "1Gi"
cpu: "1000m"
ports:
- containerPort: __PORT__
env:
- name: PORT
value: "__PORT__"
- name: BASE_DB_PATH
value: __BASE_DB_PATH__
- name: DB_CONFIG
value: __DB_CONFIG__
- name: HID_NETWORK_RPC
value: __HID_NETWORK_RPC__
- name: HID_NETWORK_API
value: __HID_NETWORK_API__
- name: HID_NETWORK_NAMESPACE
value: __HID_NETWORK_NAMESPACE__
- name: EDV_BASE_URL
value: __EDV_BASE_URL__
- name: EDV_CONFIG_DIR
value: __EDV_CONFIG_DIR__
- name: EDV_DID_FILE_PATH
value: __EDV_DID_FILE_PATH__
- name: EDV_KEY_FILE_PATH
value: __EDV_KEY_FILE_PATH__
- name: MNEMONIC
value: __MNEMONIC__
- name: JWT_SECRET
value: __JWT_SECRET__
- name: WHITELISTED_CORS
value: "['https://entity.hypersign.id','https://api.entity.hypersign.id','https://wallet-prajna.hypersign.id']"
volumeMounts:
- name: mongo
mountPath: "/data"
volumes:
resources:
limits:
memory: "1Gi"
cpu: "500m"
ports:
- containerPort: __PORT__
env:
- name: PORT
value: "__PORT__"
- name: BASE_DB_PATH
value: __BASE_DB_PATH__
- name: DB_CONFIG
value: __DB_CONFIG__
- name: HID_NETWORK_RPC
value: __HID_NETWORK_RPC__
- name: HID_NETWORK_API
value: __HID_NETWORK_API__
- name: HID_NETWORK_NAMESPACE
value: __HID_NETWORK_NAMESPACE__
- name: EDV_BASE_URL
value: __EDV_BASE_URL__
- name: EDV_CONFIG_DIR
value: __EDV_CONFIG_DIR__
- name: EDV_DID_FILE_PATH
value: __EDV_DID_FILE_PATH__
- name: EDV_KEY_FILE_PATH
value: __EDV_KEY_FILE_PATH__
- name: MNEMONIC
value: __MNEMONIC__
- name: JWT_SECRET
value: __JWT_SECRET__
- name: WHITELISTED_CORS
value: "['https://entity.hypersign.id','https://api.entity.hypersign.id','https://wallet-prajna.hypersign.id']"
volumeMounts:
- name: mongo
secret:
secretName: mongo
mountPath: "/data"
volumes:
- name: mongo
secret:
secretName: mongo
---
apiVersion: v1
kind: Service
Expand All @@ -74,9 +80,9 @@ spec:
selector:
app: entity-api
ports:
- port: __PORT__
targetPort: __PORT__
protocol: TCP
- port: __PORT__
targetPort: __PORT__
protocol: TCP
# ---
# apiVersion: apps/v1
# kind: Deployment
Expand Down Expand Up @@ -122,3 +128,6 @@ spec:
# - port: 8080
# targetPort: 8080
# protocol: TCP



25 changes: 25 additions & 0 deletions .deploy/hpa.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: entity-api
namespace: hypermine-development
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: entity-api
minReplicas: 1
maxReplicas: 5
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70
- type: Resource
resource:
name: memory
target:
type: Utilization
averageUtilization: 70
8 changes: 7 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
FROM node:16
FROM node:18
RUN npm install -g npm@latest
WORKDIR /usr/src/app
COPY ./package.json .
RUN npx patch-package -y

COPY ./tsconfig.json .

ENV NODE_OPTIONS="--openssl-legacy-provider --max-old-space-size=4096"

RUN npm install
COPY . .
RUN npm run build
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"passport": "^0.6.0",
"passport-jwt": "^4.0.1",
"passport-local": "^1.0.0",
"patch-package": "^8.0.0",
"readline-sync": "^1.4.10",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.2.0",
Expand Down
36 changes: 35 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,25 @@ async function bootstrap() {
mnemonic_EnglishMnemonic,
);

app.setGlobalPrefix('api/v1');
app.setGlobalPrefix('api/v1/');

app.use((req, res, next) => {
Logger.debug({ edv_stats: process.env.EDV_STATUS }, '/api/v1/edv/state');
if (req.path == '/api/v1/edv/state' && process.env.EDV_STATUS !== 'DOWN') {
return res.status(200).json({
status: 200,
isEdvLive: true,
});
}
if (req.path == '/api/v1/edv/state' && process.env.EDV_STATUS == 'DOWN') {
return res.status(502).json({
status: 502,
isEdvLive: false,
});
}
next();
});

if (!existDir(process.env.EDV_CONFIG_DIR)) {
createDir(process.env.EDV_CONFIG_DIR);
}
Expand Down Expand Up @@ -83,8 +101,10 @@ async function bootstrap() {
globalThis.kmsVault = kmsVault;

Logger.log('After keymanager initialization', 'main');
process.env.EDV_STATUS = 'UP';
} catch (e) {
Logger.error('Could not initialize keymanager', 'main');
process.env.EDV_STATUS = 'DOWN';
Logger.error(e);
}

Expand Down Expand Up @@ -126,5 +146,19 @@ async function bootstrap() {
`Server running on http://localhost:${process.env.PORT}`,
'Bootstrap',
);
setInterval(async () => await checkEdv(), 120000);
}

async function checkEdv() {
try {
const resp = await fetch(process.env.EDV_BASE_URL + '/api');

if (resp.status == 200) {
process.env.EDV_STATUS = 'UP';
}
} catch (error) {
process.env.EDV_STATUS = 'DOWN';
}
}

bootstrap();
38 changes: 21 additions & 17 deletions src/utils/middleware/cors.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,33 @@ export class WhitelistSSICorsMiddleware implements NestMiddleware {
'WhitelistSSICorsMiddleware: checking if call is form whitelisted domain starts',
'Middleware',
);
let referer = req.header('Referer');
const origin = req.header('Origin');
// let referer = req.header('Referer');

// Extract the origin
if (referer) {
const referalUrl = new URL(referer);
referer = `${referalUrl.protocol}//${referalUrl.host}`;
}
const origin = req.header('Origin') || referer;
// if (referer) {
// const referalUrl = new URL(referer);
// referer = `${referalUrl.protocol}//${referalUrl.host}`;
// }
const host = req.header('Host');

Logger.debug(
`WhitelistSSICorsMiddleware: request is comming from ${origin}`,
`WhitelistSSICorsMiddleware: request is comming from ${host}`,
'Middleware',
);

let subdomain =
req.subdomains.length > 0 ? req.subdomains.at(-1) : undefined;
const subdomain =
req.subdomains.length > 0 ? req.subdomains.at(-1) : host.split('.')[0];
Logger.debug(`Subdomain ${subdomain} `, 'Middleware');
Logger.debug(`Origin ${origin} `, 'Middleware');
Logger.debug(`Host ${host} `, 'Middleware');

if (!(origin.includes('localhost') || origin.includes('127.0.0.1'))) {
if (!subdomain) {
throw new BadRequestException(['Invalid subdomain']);
}
} else {
subdomain = origin.split('.')[0].split('://')[1];
}
// if (!(origin.includes('localhost') || origin.includes('127.0.0.1'))) {
// if (!subdomain) {
// throw new BadRequestException(['Invalid subdomain']);
// }
// } else {
// subdomain = host.split('.')[0];
// }

if (
req.header('authorization') == undefined ||
Expand All @@ -66,6 +67,9 @@ export class WhitelistSSICorsMiddleware implements NestMiddleware {
throw new UnauthorizedException([e]);
}

if (decoded.grantType != 'access_service_ssi') {
throw new BadRequestException(['Invalid grant type for this service']);
}
type App = {
appId?: string;
kmsId?: string;
Expand Down

0 comments on commit d7a9d98

Please sign in to comment.