Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .env.demo
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,9 @@ SCHEMA_FILE_SERVER_TOKEN=
GEO_LOCATION_MASTER_DATA_IMPORT_SCRIPT=/prisma/scripts/geo_location_data_import.sh
UPDATE_CLIENT_CREDENTIAL_SCRIPT=/prisma/scripts/update_client_credential_data.sh
# Note: the below 3 variables are only in case of starting services using docker
AFJ_AGENT_TOKEN_PATH=/agent-provisioning/AFJ/token/
AFJ_AGENT_SPIN_UP=/agent-provisioning/AFJ/scripts/docker_start_agent.sh
AFJ_AGENT_ENDPOINT_PATH=/agent-provisioning/AFJ/endpoints/
# Uncomment bellow three lines and comment the above to start services locally without using docker, using pnpm
# AFJ_AGENT_TOKEN_PATH=/apps/agent-provisioning/AFJ/token/
# AFJ_AGENT_SPIN_UP=/apps/agent-provisioning/AFJ/scripts/start_agent.sh
# AFJ_AGENT_ENDPOINT_PATH=/apps/agent-provisioning/AFJ/endpoints/

Expand Down
2 changes: 0 additions & 2 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,6 @@ SCHEMA_NKEY_SEED= xxxxxxxxxxxxx // Please provide Nkeys secret for schema servic
UTILITIES_NKEY_SEED= xxxxxxxxxxxxx // Please provide Nkeys secret for utilities service
GEOLOCATION_NKEY_SEED= xxxxxxxxxxx // Please provide Nkeys secret for geo-location service

AFJ_AGENT_TOKEN_PATH=/apps/agent-provisioning/AFJ/token/

# This was inserted by prisma init:
# Environment variables declared in this file are automatically made available to Prisma.
# See the documentation for more detail: https://pris.ly/d/prisma-schema#accessing-environment-variables-from-the-schema
Expand Down
15 changes: 1 addition & 14 deletions apps/agent-provisioning/AFJ/scripts/docker_start_agent.sh
Original file line number Diff line number Diff line change
Expand Up @@ -225,15 +225,7 @@ 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 Key: ([^ ]+).*/\1/p')

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


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

# Check if the file exists
Expand All @@ -247,11 +239,6 @@ if [ $? -eq 0 ]; then
}
EOF

cat <<EOF >${PWD}/token/${AGENCY}_${CONTAINER_NAME}.json
{
"token" : "$token"
}
EOF
echo "Agent config created"
else
echo "==============="
Expand Down
42 changes: 0 additions & 42 deletions apps/agent-provisioning/AFJ/scripts/fargate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -358,46 +358,9 @@ log_stream=ecs/$CONTAINER_NAME/$task_id

echo "logstrem=$log_stream"

# Check if the token folder exists, and create it if it doesn't
token_folder="$PWD/agent-provisioning/AFJ/token"
if [ ! -d "$token_folder" ]; then
mkdir -p "$token_folder"
fi

# Set maximum retry attempts
RETRIES=3

# Loop to attempt retrieving token from logs
for attempt in $(seq 1 $RETRIES); do
echo "Attempt $attempt: Checking service logs for token..."

# Fetch logs and grep for API token
token=$(aws logs get-log-events \
--log-group-name "$log_group" \
--log-stream-name "$log_stream" \
--region $AWS_PUBLIC_REGION \
--query 'events[*].message' \
--output text \
| tr -d '\033' \
| grep 'API Key:' \
| sed -E 's/.*API Key:[[:space:]]*([a-zA-Z0-9._:-]*).*/\1/' \
| head -n 1
)
# echo "token=$token"
if [ -n "$token" ]; then
echo "Token found: $token"
# Write token to a file
echo "{\"token\": \"$token\"}" > "$PWD/agent-provisioning/AFJ/token/${AGENCY}_${CONTAINER_NAME}.json"
break # Exit loop if token is found
else
echo "Token not found in logs. Retrying..."
if [ $attempt -eq $RETRIES ]; then
echo "Reached maximum retry attempts. Token not found."
fi
fi
# Add a delay of 10 seconds between retries
sleep 10
done

echo "Creating agent config"
cat <<EOF >${PWD}/agent-provisioning/AFJ/endpoints/${AGENCY}_${CONTAINER_NAME}.json
Expand All @@ -406,11 +369,6 @@ done
}
EOF

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

echo "Agent config created"
else
Expand Down
22 changes: 1 addition & 21 deletions apps/agent-provisioning/AFJ/scripts/start_agent.sh
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,6 @@ else
mkdir ${PWD}/apps/agent-provisioning/AFJ/agent-config
fi

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]+$"

Expand Down Expand Up @@ -235,15 +228,7 @@ 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 Key: ([^ ]+).*/\1/p')

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

# This is not actually being read, maybe we can remove this logic for endpoint file
ENDPOINT="${PWD}/endpoints/${AGENCY}_${CONTAINER_NAME}.json"

# Check if the file exists
Expand All @@ -257,11 +242,6 @@ if [ $? -eq 0 ]; then
}
EOF

cat <<EOF >${PWD}/token/${AGENCY}_${CONTAINER_NAME}.json
{
"token" : "$token"
}
EOF
echo "Agent config created"
else
echo "==============="
Expand Down
43 changes: 0 additions & 43 deletions apps/agent-provisioning/AFJ/scripts/start_agent_ecs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -344,47 +344,10 @@ log_stream=ecs/$CONTAINER_NAME/$task_id

echo "logstrem=$log_stream"

# Check if the token folder exists, and create it if it doesn't
token_folder="$PWD/agent-provisioning/AFJ/token"
if [ ! -d "$token_folder" ]; then
mkdir -p "$token_folder"
fi

# Set maximum retry attempts
RETRIES=3

# Loop to attempt retrieving token from logs
for attempt in $(seq 1 $RETRIES); do
echo "Attempt $attempt: Checking service logs for token..."

# Fetch logs and grep for API token
token=$(aws logs get-log-events \
--log-group-name "$log_group" \
--log-stream-name "$log_stream" \
--region $AWS_PUBLIC_REGION \
--query 'events[*].message' \
--output text \
| tr -d '\033' \
| grep 'API Key:' \
| sed -E 's/.*API Key:[[:space:]]*([a-zA-Z0-9._:-]*).*/\1/' \
| head -n 1
)
# echo "token=$token"
if [ -n "$token" ]; then
echo "Token found: $token"
# Write token to a file
echo "{\"token\": \"$token\"}" > "$PWD/agent-provisioning/AFJ/token/${AGENCY}_${CONTAINER_NAME}.json"
break # Exit loop if token is found
else
echo "Token not found in logs. Retrying..."
if [ $attempt -eq $RETRIES ]; then
echo "Reached maximum retry attempts. Token not found."
fi
fi
# Add a delay of 10 seconds between retries
sleep 10
done


echo "Creating agent config"
cat <<EOF >${PWD}/agent-provisioning/AFJ/endpoints/${AGENCY}_${CONTAINER_NAME}.json
Expand All @@ -393,12 +356,6 @@ done
}
EOF

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

echo "Agent config created"
else
echo "==============="
Expand Down
39 changes: 25 additions & 14 deletions apps/agent-provisioning/src/agent-provisioning.service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { Injectable, Logger, NotFoundException } from '@nestjs/common';
import { RpcException } from '@nestjs/microservices';
import { IWalletProvision } from './interface/agent-provisioning.interfaces';
import * as dotenv from 'dotenv';
import { AgentType } from '@credebl/enum/enum';
import * as fs from 'fs';

import { Injectable, Logger, NotFoundException } from '@nestjs/common';

import { AgentType } from '@credebl/enum/enum';
import { IWalletProvision } from './interface/agent-provisioning.interfaces';
import { RpcException } from '@nestjs/microservices';
import { exec } from 'child_process';

dotenv.config();

@Injectable()
Expand Down Expand Up @@ -48,27 +51,35 @@ export class AgentProvisioningService {
}

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');
if (agentEndPointExists) {
this.logger.log('Agent endpoint file exists');
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 `);
this.logger.log('Agent endpoint file does not exist');
throw new NotFoundException(`Agent endpoint file does not exist: ${agentEndpointPath}`);
}

let parsedEndpoint;
try {
parsedEndpoint = JSON.parse(agentEndPoint);
} catch (parseError) {
this.logger.error(`Failed to parse agent endpoint file: ${parseError.message}`);
throw new Error(`Invalid JSON in agent endpoint file: ${agentEndpointPath}`);
}

if (!parsedEndpoint.CONTROLLER_ENDPOINT) {
this.logger.error('CONTROLLER_ENDPOINT key missing in agent endpoint file');
throw new Error(`Missing CONTROLLER_ENDPOINT in: ${agentEndpointPath}`);
}

resolve({
agentEndPoint: JSON.parse(agentEndPoint).CONTROLLER_ENDPOINT,
agentToken: JSON.parse(agentToken).token
agentEndPoint: parsedEndpoint.CONTROLLER_ENDPOINT
});
});
});
Expand Down
20 changes: 2 additions & 18 deletions apps/agent-service/src/agent-service.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -526,10 +526,8 @@ export class AgentServiceService {
socket.emit('did-publish-process-initiated', { clientId: agentSpinupDto.clientSocketId });
socket.emit('invitation-url-creation-started', { clientId: agentSpinupDto.clientSocketId });
}
const agentBaseWalletToken = await this.commonService.getBaseAgentToken(
agentDetails.agentEndPoint,
agentDetails?.agentToken
);
const apiKey = process.env.AGENT_API_KEY;
const agentBaseWalletToken = await this.commonService.getBaseAgentToken(agentDetails.agentEndPoint, apiKey);
if (!agentBaseWalletToken) {
throw new BadRequestException(ResponseMessages.agent.error.baseWalletToken, {
cause: new Error(),
Expand Down Expand Up @@ -558,20 +556,6 @@ export class AgentServiceService {
*/
const storeAgentDetails = await this._storeOrgAgentDetails(agentPayload);
if (storeAgentDetails) {
const filePath = `${process.cwd()}${process.env.AFJ_AGENT_TOKEN_PATH}${orgData.id}_${orgData.name
.split(' ')
.join('_')}.json`;
if (agentDetails?.agentToken) {
fs.unlink(filePath, (err) => {
if (err) {
this.logger.error(`Error removing file: ${err.message}`);
throw new InternalServerErrorException(err.message);
} else {
this.logger.log(`File ${filePath} has been removed successfully`);
}
});
}

if (agentSpinupDto.clientSocketId) {
socket.emit('did-publish-process-completed', { clientId: agentSpinupDto.clientSocketId });
}
Expand Down
6 changes: 4 additions & 2 deletions libs/common/src/common.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
/* eslint-disable @typescript-eslint/explicit-function-return-type */
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
import * as CryptoJS from 'crypto-js';
import * as dotenv from 'dotenv';

import { BadRequestException, HttpException, HttpStatus, Injectable, Logger, NotFoundException } from '@nestjs/common';
import { IFormattedResponse, IOptionalParams } from './interfaces/interface';

import { CommonConstants } from './common.constant';
import { HttpService } from '@nestjs/axios';
import * as dotenv from 'dotenv';
import { ResponseMessages } from './response-messages';
import { IFormattedResponse, IOptionalParams } from './interfaces/interface';
import { RpcException } from '@nestjs/microservices';

dotenv.config();

@Injectable()
Expand Down Expand Up @@ -310,6 +311,7 @@ export class CommonService {
async getBaseAgentToken(agentEndPoint: string, apiKey: string): Promise<string> {
const normalizedBaseUrl = await this.normalizeUrlWithProtocol(agentEndPoint);
this.logger.log(`Fetching base agent token from ${normalizedBaseUrl}`);

const agentBaseWalletDetils = await this.httpPost(`${normalizedBaseUrl}${CommonConstants.URL_AGENT_TOKEN}`, '', {
headers: {
Accept: 'application/json',
Expand Down