diff --git a/.env.demo b/.env.demo index c9cd8c829..a29d058b7 100644 --- a/.env.demo +++ b/.env.demo @@ -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/ diff --git a/.env.sample b/.env.sample index cb422b084..0d396f142 100644 --- a/.env.sample +++ b/.env.sample @@ -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 diff --git a/apps/agent-provisioning/AFJ/scripts/docker_start_agent.sh b/apps/agent-provisioning/AFJ/scripts/docker_start_agent.sh index f6d588dac..30f914044 100644 --- a/apps/agent-provisioning/AFJ/scripts/docker_start_agent.sh +++ b/apps/agent-provisioning/AFJ/scripts/docker_start_agent.sh @@ -225,22 +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=$(printf "%s" "$container_logs" \ - | sed -En 's/.*[Kk][Ee][Yy]: *([^ ]*).*/\1/p') - - # Fallback (macOS BSD sed often fails) - if [ -z "$token" ]; then - token=$(printf "%s" "$container_logs" \ - | awk '/[Aa][Pp][Ii][ -]*[Kk][Ee][Yy]/ {print $NF; exit}') - fi - - # Print the extracted token - echo "Token: $token" - + ENDPOINT="${PWD}/endpoints/${AGENCY}_${CONTAINER_NAME}.json" # Check if the file exists @@ -254,11 +239,6 @@ if [ $? -eq 0 ]; then } EOF - cat <${PWD}/token/${AGENCY}_${CONTAINER_NAME}.json - { - "token" : "$token" - } -EOF echo "Agent config created" else echo "===============" diff --git a/apps/agent-provisioning/AFJ/scripts/fargate.sh b/apps/agent-provisioning/AFJ/scripts/fargate.sh index bb8ea9b66..1da3dcad9 100644 --- a/apps/agent-provisioning/AFJ/scripts/fargate.sh +++ b/apps/agent-provisioning/AFJ/scripts/fargate.sh @@ -349,56 +349,6 @@ task_id=$(echo "$service_description" | jq -r ' | .id ') -# to fetch log group of container -log_group=/ecs/$TASKDEFINITION_FAMILY -echo "log_group=$log_group" - -# Get Log Stream Name -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 <${PWD}/agent-provisioning/AFJ/endpoints/${AGENCY}_${CONTAINER_NAME}.json { @@ -406,11 +356,6 @@ done } EOF - cat <${PWD}/agent-provisioning/AFJ/token/${AGENCY}_${CONTAINER_NAME}.json - { - "token" : "$token" - } -EOF echo "Agent config created" else diff --git a/apps/agent-provisioning/AFJ/scripts/start_agent.sh b/apps/agent-provisioning/AFJ/scripts/start_agent.sh index b9629a80e..f71f15427 100755 --- a/apps/agent-provisioning/AFJ/scripts/start_agent.sh +++ b/apps/agent-provisioning/AFJ/scripts/start_agent.sh @@ -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]+$" @@ -235,22 +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=$(printf "%s" "$container_logs" \ - | sed -En 's/.*[Kk][Ee][Yy]: *([^ ]*).*/\1/p') - - # Fallback (macOS BSD sed often fails) - if [ -z "$token" ]; then - token=$(printf "%s" "$container_logs" \ - | awk '/[Aa][Pp][Ii][ -]*[Kk][Ee][Yy]/ {print $NF; exit}') - fi - - # 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 @@ -264,11 +242,6 @@ if [ $? -eq 0 ]; then } EOF - cat <${PWD}/token/${AGENCY}_${CONTAINER_NAME}.json - { - "token" : "$token" - } -EOF echo "Agent config created" else echo "===============" diff --git a/apps/agent-provisioning/AFJ/scripts/start_agent_ecs.sh b/apps/agent-provisioning/AFJ/scripts/start_agent_ecs.sh index 3bedb6f36..1e40ab429 100644 --- a/apps/agent-provisioning/AFJ/scripts/start_agent_ecs.sh +++ b/apps/agent-provisioning/AFJ/scripts/start_agent_ecs.sh @@ -335,56 +335,6 @@ task_id=$(echo "$service_description" | jq -r ' | .id ') -# to fetch log group of container -log_group=/ecs/$TASKDEFINITION_FAMILY -echo "log_group=$log_group" - -# Get Log Stream Name -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 <${PWD}/agent-provisioning/AFJ/endpoints/${AGENCY}_${CONTAINER_NAME}.json @@ -393,12 +343,6 @@ done } EOF - cat <${PWD}/agent-provisioning/AFJ/token/${AGENCY}_${CONTAINER_NAME}.json - { - "token" : "$token" - } -EOF - echo "Agent config created" else echo "===============" diff --git a/apps/agent-provisioning/src/agent-provisioning.service.ts b/apps/agent-provisioning/src/agent-provisioning.service.ts index 2f2cc5b2e..3fe825e47 100644 --- a/apps/agent-provisioning/src/agent-provisioning.service.ts +++ b/apps/agent-provisioning/src/agent-provisioning.service.ts @@ -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() @@ -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 }); }); }); diff --git a/apps/agent-service/src/agent-service.service.ts b/apps/agent-service/src/agent-service.service.ts index fb41a1801..949c46e01 100644 --- a/apps/agent-service/src/agent-service.service.ts +++ b/apps/agent-service/src/agent-service.service.ts @@ -17,7 +17,6 @@ import { } from '@nestjs/common'; import { ClientProxy, RpcException } from '@nestjs/microservices'; import * as dotenv from 'dotenv'; -import * as fs from 'fs'; import { map } from 'rxjs/operators'; dotenv.config(); import { @@ -528,10 +527,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(), @@ -560,20 +557,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 }); } diff --git a/libs/common/src/common.service.ts b/libs/common/src/common.service.ts index 6f59b24b3..bb670bff8 100644 --- a/libs/common/src/common.service.ts +++ b/libs/common/src/common.service.ts @@ -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() @@ -310,6 +311,7 @@ export class CommonService { async getBaseAgentToken(agentEndPoint: string, apiKey: string): Promise { 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',