Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/credebl/platform into qa…
Browse files Browse the repository at this point in the history
…-prod-9-2-2024
  • Loading branch information
nishad-ayanworks committed Feb 9, 2024
2 parents 62e3e40 + 9768cc9 commit 4edc788
Show file tree
Hide file tree
Showing 177 changed files with 6,607 additions and 2,446 deletions.
67 changes: 67 additions & 0 deletions Dockerfiles/Dockerfile.utility
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Stage 1: Build the application
FROM node:18-slim as build
RUN npm install -g pnpm

# We don't need the standalone Chromium
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD true

# Install Google Chrome Stable and fonts
# Note: this installs the necessary libs to make the browser work with Puppeteer.
RUN apt-get update && apt-get install gnupg wget -y && \
wget --quiet --output-document=- https://dl-ssl.google.com/linux/linux_signing_key.pub | gpg --dearmor > /etc/apt/trusted.gpg.d/google-archive.gpg && \
sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list' && \
apt-get update && \
apt-get install google-chrome-stable -y --no-install-recommends && \
rm -rf /var/lib/apt/lists/*

# RUN apk update && apk list --all-versions chromium
# Set the working directory
WORKDIR /app

# Copy package.json and package-lock.json
COPY package.json ./

# Install dependencies
RUN pnpm install

# Copy the rest of the application code
COPY . .
RUN cd libs/prisma-service && npx prisma migrate deploy && npx prisma generate

# Build the user service
RUN pnpm run build utility

# Stage 2: Create the final image
FROM node:18-slim

# We don't need the standalone Chromium
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD true

# Install Google Chrome Stable and fonts
# Note: this installs the necessary libs to make the browser work with Puppeteer.
RUN apt-get update && apt-get install gnupg wget -y && \
wget --quiet --output-document=- https://dl-ssl.google.com/linux/linux_signing_key.pub | gpg --dearmor > /etc/apt/trusted.gpg.d/google-archive.gpg && \
sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list' && \
apt-get update && \
apt-get install google-chrome-stable -y --no-install-recommends && \
rm -rf /var/lib/apt/lists/*

# Set the working directory
WORKDIR /app
RUN npm install -g pnpm

# Copy the compiled code from the build stage
COPY --from=build /app/dist/apps/utility/ ./dist/apps/utility/

# Copy the libs folder from the build stage
COPY --from=build /app/libs/ ./libs/
#COPY --from=build /app/package.json ./
COPY --from=build /app/node_modules ./node_modules


# Set the command to run the microservice
CMD ["sh", "-c", "cd libs/prisma-service && npx prisma migrate deploy && npx prisma generate && npx prisma && cd ../.. && node dist/apps/utility/main.js"]

# docker build -t user -f Dockerfiles/Dockerfile.user .
# docker run -d --env-file .env --name user docker.io/library/user
# docker logs -f user
42 changes: 42 additions & 0 deletions Dockerfiles/Dockerfile.webhook
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Stage 1: Build the application
FROM node:18-alpine as build
RUN npm install -g pnpm
# Set the working directory
WORKDIR /app

# Copy package.json and package-lock.json
COPY package.json ./
#COPY package-lock.json ./

# Install dependencies
RUN pnpm i

# Copy the rest of the application code
COPY . .
RUN cd libs/prisma-service && npx prisma migrate deploy && npx prisma generate

# Build the webhook service
RUN pnpm run build webhook

# Stage 2: Create the final image
FROM node:18-alpine
RUN npm install -g pnpm
# Set the working directory
WORKDIR /app

# Copy the compiled code from the build stage
COPY --from=build /app/dist/apps/webhook/ ./dist/apps/webhook/

# Copy the libs folder from the build stage
COPY --from=build /app/libs/ ./libs/
#COPY --from=build /app/package.json ./
COPY --from=build /app/node_modules ./node_modules
# COPY --from=build /app/uploadedFiles ./uploadedFiles


# Set the command to run the microservice
CMD ["sh", "-c", "cd libs/prisma-service && npx prisma migrate deploy && npx prisma generate && cd ../.. && node dist/apps/webhook/main.js"]

# docker build -t webhook -f Dockerfiles/Dockerfile.webhook .
# docker run -d --env-file .env --name webhook docker.io/library/webhook
# docker logs -f webhook
68 changes: 50 additions & 18 deletions apps/agent-provisioning/AFJ/scripts/start_agent.sh
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,27 @@ PROTOCOL=${12}
TENANT=${13}
AFJ_VERSION=${14}
INDY_LEDGER=${15}
INBOUND_ENDPOINT=${16}

ADMIN_PORT_FILE="$PWD/apps/agent-provisioning/AFJ/port-file/last-admin-port.txt"
INBOUND_PORT_FILE="$PWD/apps/agent-provisioning/AFJ/port-file/last-inbound-port.txt"
ADMIN_PORT=8001
INBOUND_PORT=9001

increment_port() {
local port="$1"
local lower_limit="$2"
local port="$1"
local lower_limit="$2"

while [ "$port" -le "$lower_limit" ]; do
port=$((port + 1)) # Increment the port using arithmetic expansion
done
while [ "$port" -le "$lower_limit" ]; do
port=$((port + 1)) # Increment the port using arithmetic expansion
done

echo "$port"
echo "$port"
}

# Check if admin port file exists and if not, create and initialize it
if [ ! -e "$ADMIN_PORT_FILE" ]; then
echo "$ADMIN_PORT" > "$ADMIN_PORT_FILE"
echo "$ADMIN_PORT" >"$ADMIN_PORT_FILE"
fi

# Read the last used admin port number from the file
Expand All @@ -47,12 +48,12 @@ echo "Last used admin port: $last_used_admin_port"
last_used_admin_port=$(increment_port "$last_used_admin_port" "$last_used_admin_port")

# Save the updated admin port number back to the file and update the global variable
echo "$last_used_admin_port" > "$ADMIN_PORT_FILE"
echo "$last_used_admin_port" >"$ADMIN_PORT_FILE"
ADMIN_PORT="$last_used_admin_port"

# Check if inbound port file exists and if not, create and initialize it
if [ ! -e "$INBOUND_PORT_FILE" ]; then
echo "$INBOUND_PORT" > "$INBOUND_PORT_FILE"
echo "$INBOUND_PORT" >"$INBOUND_PORT_FILE"
fi

# Read the last used inbound port number from the file
Expand All @@ -63,7 +64,7 @@ echo "Last used inbound port: $last_used_inbound_port"
last_used_inbound_port=$(increment_port "$last_used_inbound_port" "$last_used_inbound_port")

# Save the updated inbound port number back to the file and update the global variable
echo "$last_used_inbound_port" > "$INBOUND_PORT_FILE"
echo "$last_used_inbound_port" >"$INBOUND_PORT_FILE"
INBOUND_PORT="$last_used_inbound_port"

echo "Last used admin port: $ADMIN_PORT"
Expand All @@ -85,7 +86,29 @@ else
mkdir ${PWD}/apps/agent-provisioning/AFJ/agent-config
fi

AGENT_ENDPOINT="${PROTOCOL}://${EXTERNAL_IP}:${INBOUND_PORT}"
if [ -d "${PWD}/apps/agent-provisioning/AFJ/token" ]; then
echo "token directory exists."
else
echo "Error: token directory does not exists."
mkdir ${PWD}/apps/agent-provisioning/AFJ/token
fi

# Define a regular expression pattern for IP address
IP_REGEX="^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$"

# Check if the input is a domain
if echo "$INBOUND_ENDPOINT" | grep -qP "^[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$"; then
echo "INBOUND_ENDPOINT is a domain: $INBOUND_ENDPOINT"
AGENT_ENDPOINT=$INBOUND_ENDPOINT
else
# Check if the input is an IP address
if [[ $INBOUND_ENDPOINT =~ $IP_REGEX ]]; then
echo "INBOUND_ENDPOINT is an IP address: $INBOUND_ENDPOINT"
AGENT_ENDPOINT="${PROTOCOL}://${EXTERNAL_IP}:${INBOUND_PORT}"
else
echo "Invalid input for INBOUND_ENDPOINT: $INBOUND_ENDPOINT"
fi
fi

echo "-----$AGENT_ENDPOINT----"
CONFIG_FILE="${PWD}/apps/agent-provisioning/AFJ/agent-config/${AGENCY}_${CONTAINER_NAME}.json"
Expand All @@ -96,7 +119,7 @@ if [ -f "$CONFIG_FILE" ]; then
rm "$CONFIG_FILE"
fi

cat <<EOF >>${CONFIG_FILE}
cat <<EOF >${CONFIG_FILE}
{
"label": "${AGENCY}_${CONTAINER_NAME}",
"walletId": "$WALLET_NAME",
Expand Down Expand Up @@ -140,7 +163,7 @@ if [ -f "$DOCKER_COMPOSE" ]; then
# If it exists, remove the file
rm "$DOCKER_COMPOSE"
fi
cat <<EOF >>${DOCKER_COMPOSE}
cat <<EOF >${DOCKER_COMPOSE}
version: '3'
services:
Expand Down Expand Up @@ -200,20 +223,29 @@ if [ $? -eq 0 ]; then
done

echo "Creating agent config"
# Capture the logs from the container
container_logs=$(docker logs $(docker ps -q --filter "name=${AGENCY}_${CONTAINER_NAME}"))

# Extract the token from the logs using sed
token=$(echo "$container_logs" | sed -nE 's/.*API Toekn: ([^ ]+).*/\1/p')

# Print the extracted token
echo "Token: $token"

ENDPOINT="${PWD}/endpoints/${AGENCY}_${CONTAINER_NAME}.json"

# Check if the file exists
if [ -f "$ENDPOINT" ]; then
# If it exists, remove the file
rm "$ENDPOINT"
# If it exists, remove the file
rm "$ENDPOINT"
fi
cat <<EOF >>${ENDPOINT}
cat <<EOF >${ENDPOINT}
{
"CONTROLLER_ENDPOINT":"${EXTERNAL_IP}:${ADMIN_PORT}"
}
EOF

cat <<EOF >>${PWD}/token/${AGENCY}_${CONTAINER_NAME}.json
cat <<EOF >${PWD}/token/${AGENCY}_${CONTAINER_NAME}.json
{
"token" : "$token"
}
Expand All @@ -228,4 +260,4 @@ else
echo "ERROR : Failed to execute!" && exit 125
fi

echo "Total time elapsed: $(date -ud "@$(($(date +%s) - $START_TIME))" +%T) (HH:MM:SS)"
echo "Total time elapsed: $(date -ud "@$(($(date +%s) - $START_TIME))" +%T) (HH:MM:SS)"
28 changes: 22 additions & 6 deletions apps/agent-provisioning/AFJ/scripts/start_agent_ecs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ PROTOCOL=${12}
TENANT=${13}
AFJ_VERSION=${14}
INDY_LEDGER=${15}
AGENT_HOST=${16}
AWS_ACCOUNT_ID=${17}
S3_BUCKET_ARN=${18}
CLUSTER_NAME=${19}
TESKDEFINITION_FAMILY=${20}
INBOUND_ENDPOINT=${16}
AGENT_HOST=${17}
AWS_ACCOUNT_ID=${18}
S3_BUCKET_ARN=${19}
CLUSTER_NAME=${20}
TESKDEFINITION_FAMILY=${21}

DESIRED_COUNT=1

Expand Down Expand Up @@ -89,7 +90,22 @@ echo "Last used admin port: $ADMIN_PORT"
echo "Last used inbound port: $INBOUND_PORT"
echo "AGENT SPIN-UP STARTED"

AGENT_ENDPOINT="${PROTOCOL}://${EXTERNAL_IP}:${INBOUND_PORT}"
# Define a regular expression pattern for IP address
IP_REGEX="^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$"

# Check if the input is a domain
if echo "$INBOUND_ENDPOINT" | grep -qP "^[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$"; then
echo "INBOUND_ENDPOINT is a domain: $INBOUND_ENDPOINT"
AGENT_ENDPOINT=$INBOUND_ENDPOINT
else
# Check if the input is an IP address
if [[ $INBOUND_ENDPOINT =~ $IP_REGEX ]]; then
echo "INBOUND_ENDPOINT is an IP address: $INBOUND_ENDPOINT"
AGENT_ENDPOINT="${PROTOCOL}://${EXTERNAL_IP}:${INBOUND_PORT}"
else
echo "Invalid input for INBOUND_ENDPOINT: $INBOUND_ENDPOINT"
fi
fi

cat <<EOF >/app/agent-provisioning/AFJ/agent-config/${AGENCY}_${CONTAINER_NAME}.json
{
Expand Down
35 changes: 30 additions & 5 deletions apps/agent-provisioning/src/agent-provisioning.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable, Logger } from '@nestjs/common';
import { Injectable, Logger, NotFoundException } from '@nestjs/common';
import { RpcException } from '@nestjs/microservices';
import { IWalletProvision } from './interface/agent-provisioning.interfaces';
import * as dotenv from 'dotenv';
Expand All @@ -22,10 +22,10 @@ export class AgentProvisioningService {
async walletProvision(payload: IWalletProvision): Promise<object> {
try {

const { containerName, externalIp, orgId, seed, walletName, walletPassword, walletStorageHost, walletStoragePassword, walletStoragePort, walletStorageUser, webhookEndpoint, agentType, protocol, afjVersion, tenant, indyLedger } = payload;
const { containerName, externalIp, orgId, seed, walletName, walletPassword, walletStorageHost, walletStoragePassword, walletStoragePort, walletStorageUser, webhookEndpoint, agentType, protocol, afjVersion, tenant, indyLedger, inboundEndpoint } = payload;
if (agentType === AgentType.AFJ) {
// The wallet provision command is used to invoke a shell script
const walletProvision = `${process.cwd() + process.env.AFJ_AGENT_SPIN_UP} ${orgId} "${externalIp}" "${walletName}" "${walletPassword}" ${seed} ${webhookEndpoint} ${walletStorageHost} ${walletStoragePort} ${walletStorageUser} ${walletStoragePassword} ${containerName} ${protocol} ${tenant} ${afjVersion} ${indyLedger} ${process.env.AGENT_HOST} ${process.env.AWS_ACCOUNT_ID} ${process.env.S3_BUCKET_ARN} ${process.env.CLUSTER_NAME} ${process.env.TESKDEFINITION_FAMILY}`;
const walletProvision = `${process.cwd() + process.env.AFJ_AGENT_SPIN_UP} ${orgId} "${externalIp}" "${walletName}" "${walletPassword}" ${seed} ${webhookEndpoint} ${walletStorageHost} ${walletStoragePort} ${walletStorageUser} ${walletStoragePassword} ${containerName} ${protocol} ${tenant} ${afjVersion} "${indyLedger}" ${inboundEndpoint} ${process.env.AGENT_HOST} ${process.env.AWS_ACCOUNT_ID} ${process.env.S3_BUCKET_ARN} ${process.env.CLUSTER_NAME} ${process.env.TESKDEFINITION_FAMILY}`;
const spinUpResponse: object = new Promise(async (resolve) => {

await exec(walletProvision, async (err, stdout, stderr) => {

Check warning

Code scanning / CodeQL

Shell command built from environment values Medium

This shell command depends on an uncontrolled
absolute path
.
Expand All @@ -34,8 +34,24 @@ export class AgentProvisioningService {
this.logger.log(`shell script error: ${stderr}`);
}

const agentEndPoint = await fs.readFileSync(`${process.cwd()}${process.env.AFJ_AGENT_ENDPOINT_PATH}${orgId}_${containerName}.json`, 'utf8');
const agentToken = await fs.readFileSync(`${process.cwd()}${process.env.AFJ_AGENT_TOKEN_PATH}${orgId}_${containerName}.json`, 'utf8');
const agentEndpointPath = `${process.cwd()}${process.env.AFJ_AGENT_ENDPOINT_PATH}${orgId}_${containerName}.json`;
const agentTokenPath = `${process.cwd()}${process.env.AFJ_AGENT_TOKEN_PATH}${orgId}_${containerName}.json`;

const agentEndPointExists = await this.checkFileExistence(agentEndpointPath);
const agentTokenExists = await this.checkFileExistence(agentTokenPath);

let agentEndPoint;
let agentToken;

if (agentEndPointExists && agentTokenExists) {
this.logger.log('Both files exist');
agentEndPoint = await fs.readFileSync(agentEndpointPath, 'utf8');
agentToken = await fs.readFileSync(agentTokenPath, 'utf8');
// Proceed with accessing the files if needed
} else {
this.logger.log('One or both files do not exist');
throw new NotFoundException(`${agentEndpointPath} or ${agentTokenPath} files do not exist `);
}

resolve({
agentEndPoint: JSON.parse(agentEndPoint).CONTROLLER_ENDPOINT,
Expand All @@ -52,4 +68,13 @@ export class AgentProvisioningService {
throw new RpcException(error);
}
}

async checkFileExistence(filePath: string): Promise<boolean> {
try {
await fs.accessSync(filePath);
return true; // File exists
} catch (error) {
return false; // File does not exist
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface IWalletProvision {
protocol: string;
afjVersion: string;
tenant: boolean;
inboundEndpoint: string;
apiKey?:string;
}

Expand Down
Loading

0 comments on commit 4edc788

Please sign in to comment.