Skip to content

Commit

Permalink
Merge pull request #396 from credebl/dev-to-main-afj-auth
Browse files Browse the repository at this point in the history
refactor: AFJ Authentication
  • Loading branch information
KulkarniShashank authored Dec 29, 2023
2 parents 9298bd2 + 1ba7334 commit 298c083
Show file tree
Hide file tree
Showing 113 changed files with 3,483 additions and 2,264 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ libs/prisma-service/prisma/data/credebl-master-table.json
uploadedFles/exports
uploadedFles/import
uploadedFles/export
nats-server.conf
nats-server.conf
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
# Stage 1: Build the application
FROM node:18-alpine as build
RUN npm install -g pnpm
# RUN npm install -g pnpm
# Install AWS CLI
# RUN apk update
# RUN apk add openssh-client
# RUN apk update
# RUN apk add aws-cli
RUN npm install -g pnpm --ignore-scripts \
&& apk update \
&& apk add openssh-client \
&& apk add aws-cli

# Set the working directory
WORKDIR /app
Expand All @@ -23,24 +32,35 @@ RUN pnpm run build agent-provisioning

# Stage 2: Create the final image
FROM node:18-alpine as prod
# Install AWS CLI
# RUN apk update
# RUN apk add openssh-client
# RUN apk update
# RUN apk add aws-cli
RUN npm install -g pnpm --ignore-scripts \
&& apk update \
&& apk add openssh-client \
&& apk add aws-cli

WORKDIR /app
RUN npm install -g pnpm

RUN mkdir -p ./agent-provisioning/AFJ/endpoints
RUN mkdir -p ./agent-provisioning/AFJ/agent-config
RUN mkdir -p ./agent-provisioning/AFJ/port-file
RUN mkdir -p ./agent-provisioning/AFJ/token

# Copy the compiled code
COPY --from=build /app/dist/apps/agent-provisioning/ ./dist/apps/agent-provisioning/
COPY --from=build /app/node_modules ./node_modules
COPY --from=build /app/apps/agent-provisioning/AFJ/scripts ./agent-provisioning/AFJ/scripts
COPY --from=build /app/apps/agent-provisioning/AFJ/port-files ./agent-provisioning/AFJ/port-file
COPY --from=build /app/apps/agent-provisioning/AFJ/port-file ./agent-provisioning/AFJ/port-file

# Set permissions
RUN chmod +x /app/agent-provisioning/AFJ/scripts/start_agent.sh
RUN chmod +x /app/agent-provisioning/AFJ/scripts/start_agent_ecs.sh
RUN chmod 777 /app/agent-provisioning/AFJ/endpoints
RUN chmod 777 /app/agent-provisioning/AFJ/agent-config

RUN chmod 777 /app/agent-provisioning/AFJ/token
# Copy the libs folder
COPY libs/ ./libs/

Expand Down
49 changes: 42 additions & 7 deletions apps/agent-provisioning/AFJ/scripts/start_agent.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/bin/bash

START_TIME=$(date +%s)

AGENCY=$1
Expand Down Expand Up @@ -76,12 +78,25 @@ else
mkdir ${PWD}/apps/agent-provisioning/AFJ/endpoints
fi

docker build . -t $AFJ_VERSION -f apps/agent-provisioning/AFJ/afj-controller/Dockerfile
if [ -d "${PWD}/apps/agent-provisioning/AFJ/agent-config" ]; then
echo "Endpoints directory exists."
else
echo "Error: Endpoints directory does not exists."
mkdir ${PWD}/apps/agent-provisioning/AFJ/agent-config
fi

AGENT_ENDPOINT="${PROTOCOL}://${EXTERNAL_IP}:${INBOUND_PORT}"

echo "-----$AGENT_ENDPOINT----"
cat <<EOF >>${PWD}/apps/agent-provisioning/AFJ/agent-config/${AGENCY}_${CONTAINER_NAME}.json
CONFIG_FILE="${PWD}/apps/agent-provisioning/AFJ/agent-config/${AGENCY}_${CONTAINER_NAME}.json"

# Check if the file exists
if [ -f "$CONFIG_FILE" ]; then
# If it exists, remove the file
rm "$CONFIG_FILE"
fi

cat <<EOF >>${CONFIG_FILE}
{
"label": "${AGENCY}_${CONTAINER_NAME}",
"walletId": "$WALLET_NAME",
Expand Down Expand Up @@ -117,7 +132,15 @@ cat <<EOF >>${PWD}/apps/agent-provisioning/AFJ/agent-config/${AGENCY}_${CONTAINE
EOF

FILE_NAME="docker-compose_${AGENCY}_${CONTAINER_NAME}.yaml"
cat <<EOF >>${PWD}/apps/agent-provisioning/AFJ/${FILE_NAME}

DOCKER_COMPOSE="${PWD}/apps/agent-provisioning/AFJ/${FILE_NAME}"

# Check if the file exists
if [ -f "$DOCKER_COMPOSE" ]; then
# If it exists, remove the file
rm "$DOCKER_COMPOSE"
fi
cat <<EOF >>${DOCKER_COMPOSE}
version: '3'
services:
Expand Down Expand Up @@ -152,7 +175,7 @@ if [ $? -eq 0 ]; then
echo "container-name::::::${CONTAINER_NAME}"
echo "file-name::::::$FILE_NAME"

docker-compose -f $FILE_NAME --project-name ${AGENCY}_${CONTAINER_NAME} up -d
docker compose -f $FILE_NAME up -d
if [ $? -eq 0 ]; then

n=0
Expand All @@ -177,10 +200,22 @@ if [ $? -eq 0 ]; then
done

echo "Creating agent config"
cat <<EOF >>${PWD}/endpoints/${AGENCY}_${CONTAINER_NAME}.json
ENDPOINT="${PWD}/endpoints/${AGENCY}_${CONTAINER_NAME}.json"

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

cat <<EOF >>${PWD}/token/${AGENCY}_${CONTAINER_NAME}.json
{
"CONTROLLER_ENDPOINT":"${EXTERNAL_IP}:${ADMIN_PORT}",
"AGENT_ENDPOINT" : "${INTERNAL_IP}:${ADMIN_PORT}"
"token" : "$token"
}
EOF
echo "Agent config created"
Expand Down
28 changes: 23 additions & 5 deletions apps/agent-provisioning/AFJ/scripts/start_agent_ecs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,22 @@ S3_BUCKET_ARN=${18}
CLUSTER_NAME=${19}
TESKDEFINITION_FAMILY=${20}

SERVICE_NAME="${AGENCY}-${CONTAINER_NAME}-service"
DESIRED_COUNT=1

generate_random_string() {
echo "$(date +%s%N | sha256sum | base64 | head -c 12)"
}

# Call the function to generate a random string
random_string=$(generate_random_string)

# Print the generated random string
echo "Random String: $random_string"

SERVICE_NAME="${AGENCY}-${CONTAINER_NAME}-service-${random_string}"
EXTERNAL_IP=$(echo "$2" | tr -d '[:space:]')
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_FILE="$PWD/agent-provisioning/AFJ/port-file/last-admin-port.txt"
INBOUND_PORT_FILE="$PWD/agent-provisioning/AFJ/port-file/last-inbound-port.txt"
ADMIN_PORT=8001
INBOUND_PORT=9001

Expand Down Expand Up @@ -80,7 +91,7 @@ echo "AGENT SPIN-UP STARTED"

AGENT_ENDPOINT="${PROTOCOL}://${EXTERNAL_IP}:${INBOUND_PORT}"

cat <<EOF >>/app/agent-provisioning/AFJ/agent-config/${AGENCY}_${CONTAINER_NAME}.json
cat <<EOF >/app/agent-provisioning/AFJ/agent-config/${AGENCY}_${CONTAINER_NAME}.json
{
"label": "${AGENCY}_${CONTAINER_NAME}",
"walletId": "$WALLET_NAME",
Expand Down Expand Up @@ -233,12 +244,19 @@ if [ $? -eq 0 ]; then
done

echo "Creating agent config"
cat <<EOF >>${PWD}/agent-provisioning/AFJ/endpoints/${AGENCY}_${CONTAINER_NAME}.json
cat <<EOF >${PWD}/agent-provisioning/AFJ/endpoints/${AGENCY}_${CONTAINER_NAME}.json
{
"CONTROLLER_ENDPOINT":"${EXTERNAL_IP}:${ADMIN_PORT}",
"AGENT_ENDPOINT" : "${INTERNAL_IP}:${ADMIN_PORT}"
}
EOF

cat <<EOF >${PWD}/agent-provisioning/AFJ/token/${AGENCY}_${CONTAINER_NAME}.json
{
"token" : ""
}
EOF

echo "Agent config created"
else
echo "==============="
Expand Down
15 changes: 9 additions & 6 deletions apps/agent-provisioning/src/agent-provisioning.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,24 @@ export class AgentProvisioningService {
try {

const { containerName, externalIp, orgId, seed, walletName, walletPassword, walletStorageHost, walletStoragePassword, walletStoragePort, walletStorageUser, webhookEndpoint, agentType, protocol, afjVersion, tenant, indyLedger } = 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} ${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) => {
this.logger.log(`shell script output: ${stdout}`);
if (stderr) {
this.logger.log(`shell script error: ${stderr}`);
}
const agentEndPoint: string = await fs.readFileSync(`${process.env.PWD}${process.env.AFJ_AGENT_ENDPOINT_PATH}${orgId}_${containerName}.json`, 'utf8');
resolve(agentEndPoint);

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

resolve({
agentEndPoint: JSON.parse(agentEndPoint).CONTROLLER_ENDPOINT,
agentToken: JSON.parse(agentToken).token
});
});
});
return spinUpResponse;
Expand Down
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;
apiKey?:string;
}

export interface IAgentSpinUp {
Expand Down
Loading

0 comments on commit 298c083

Please sign in to comment.