Skip to content

Commit 3ef3ca2

Browse files
resolved: coderabbit comments on PR
Signed-off-by: pranalidhanavade <pranali.dhanavade@ayanworks.com>
1 parent 880ddfd commit 3ef3ca2

File tree

9 files changed

+22
-110
lines changed

9 files changed

+22
-110
lines changed

.env.demo

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,9 @@ SCHEMA_FILE_SERVER_TOKEN=
116116
GEO_LOCATION_MASTER_DATA_IMPORT_SCRIPT=/prisma/scripts/geo_location_data_import.sh
117117
UPDATE_CLIENT_CREDENTIAL_SCRIPT=/prisma/scripts/update_client_credential_data.sh
118118
# Note: the below 3 variables are only in case of starting services using docker
119-
AFJ_AGENT_TOKEN_PATH=/agent-provisioning/AFJ/token/
120119
AFJ_AGENT_SPIN_UP=/agent-provisioning/AFJ/scripts/docker_start_agent.sh
121120
AFJ_AGENT_ENDPOINT_PATH=/agent-provisioning/AFJ/endpoints/
122121
# Uncomment bellow three lines and comment the above to start services locally without using docker, using pnpm
123-
# AFJ_AGENT_TOKEN_PATH=/apps/agent-provisioning/AFJ/token/
124122
# AFJ_AGENT_SPIN_UP=/apps/agent-provisioning/AFJ/scripts/start_agent.sh
125123
# AFJ_AGENT_ENDPOINT_PATH=/apps/agent-provisioning/AFJ/endpoints/
126124

.env.sample

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,6 @@ SCHEMA_NKEY_SEED= xxxxxxxxxxxxx // Please provide Nkeys secret for schema servic
122122
UTILITIES_NKEY_SEED= xxxxxxxxxxxxx // Please provide Nkeys secret for utilities service
123123
GEOLOCATION_NKEY_SEED= xxxxxxxxxxx // Please provide Nkeys secret for geo-location service
124124

125-
AFJ_AGENT_TOKEN_PATH=/apps/agent-provisioning/AFJ/token/
126-
127125
# This was inserted by prisma init:
128126
# Environment variables declared in this file are automatically made available to Prisma.
129127
# See the documentation for more detail: https://pris.ly/d/prisma-schema#accessing-environment-variables-from-the-schema

apps/agent-provisioning/AFJ/scripts/docker_start_agent.sh

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -225,15 +225,7 @@ if [ $? -eq 0 ]; then
225225
done
226226

227227
echo "Creating agent config"
228-
# Capture the logs from the container
229-
container_logs=$(docker logs $(docker ps -q --filter "name=${AGENCY}_${CONTAINER_NAME}"))
230-
231-
# Extract the token from the logs using sed
232-
token=$(echo "$container_logs" | sed -nE 's/.*** API Key: ([^ ]+).*/\1/p')
233-
234-
# Print the extracted token
235-
echo "Token: $token"
236-
228+
237229
ENDPOINT="${PWD}/endpoints/${AGENCY}_${CONTAINER_NAME}.json"
238230

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

250-
cat <<EOF >${PWD}/token/${AGENCY}_${CONTAINER_NAME}.json
251-
{
252-
"token" : "$token"
253-
}
254-
EOF
255242
echo "Agent config created"
256243
else
257244
echo "==============="

apps/agent-provisioning/AFJ/scripts/fargate.sh

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -358,46 +358,9 @@ log_stream=ecs/$CONTAINER_NAME/$task_id
358358

359359
echo "logstrem=$log_stream"
360360

361-
# Check if the token folder exists, and create it if it doesn't
362-
token_folder="$PWD/agent-provisioning/AFJ/token"
363-
if [ ! -d "$token_folder" ]; then
364-
mkdir -p "$token_folder"
365-
fi
366-
367361
# Set maximum retry attempts
368362
RETRIES=3
369363

370-
# Loop to attempt retrieving token from logs
371-
for attempt in $(seq 1 $RETRIES); do
372-
echo "Attempt $attempt: Checking service logs for token..."
373-
374-
# Fetch logs and grep for API token
375-
token=$(aws logs get-log-events \
376-
--log-group-name "$log_group" \
377-
--log-stream-name "$log_stream" \
378-
--region $AWS_PUBLIC_REGION \
379-
--query 'events[*].message' \
380-
--output text \
381-
| tr -d '\033' \
382-
| grep 'API Key:' \
383-
| sed -E 's/.*API Key:[[:space:]]*([a-zA-Z0-9._:-]*).*/\1/' \
384-
| head -n 1
385-
)
386-
# echo "token=$token"
387-
if [ -n "$token" ]; then
388-
echo "Token found: $token"
389-
# Write token to a file
390-
echo "{\"token\": \"$token\"}" > "$PWD/agent-provisioning/AFJ/token/${AGENCY}_${CONTAINER_NAME}.json"
391-
break # Exit loop if token is found
392-
else
393-
echo "Token not found in logs. Retrying..."
394-
if [ $attempt -eq $RETRIES ]; then
395-
echo "Reached maximum retry attempts. Token not found."
396-
fi
397-
fi
398-
# Add a delay of 10 seconds between retries
399-
sleep 10
400-
done
401364

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

409-
cat <<EOF >${PWD}/agent-provisioning/AFJ/token/${AGENCY}_${CONTAINER_NAME}.json
410-
{
411-
"token" : "$token"
412-
}
413-
EOF
414372

415373
echo "Agent config created"
416374
else

apps/agent-provisioning/AFJ/scripts/start_agent.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ if [ $? -eq 0 ]; then
228228
done
229229

230230
echo "Creating agent config"
231-
231+
# This is not actually being read, maybe we can remove this logic for endpoint file
232232
ENDPOINT="${PWD}/endpoints/${AGENCY}_${CONTAINER_NAME}.json"
233233

234234
# Check if the file exists

apps/agent-provisioning/AFJ/scripts/start_agent_ecs.sh

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -344,47 +344,10 @@ log_stream=ecs/$CONTAINER_NAME/$task_id
344344

345345
echo "logstrem=$log_stream"
346346

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

353348
# Set maximum retry attempts
354349
RETRIES=3
355350

356-
# Loop to attempt retrieving token from logs
357-
for attempt in $(seq 1 $RETRIES); do
358-
echo "Attempt $attempt: Checking service logs for token..."
359-
360-
# Fetch logs and grep for API token
361-
token=$(aws logs get-log-events \
362-
--log-group-name "$log_group" \
363-
--log-stream-name "$log_stream" \
364-
--region $AWS_PUBLIC_REGION \
365-
--query 'events[*].message' \
366-
--output text \
367-
| tr -d '\033' \
368-
| grep 'API Key:' \
369-
| sed -E 's/.*API Key:[[:space:]]*([a-zA-Z0-9._:-]*).*/\1/' \
370-
| head -n 1
371-
)
372-
# echo "token=$token"
373-
if [ -n "$token" ]; then
374-
echo "Token found: $token"
375-
# Write token to a file
376-
echo "{\"token\": \"$token\"}" > "$PWD/agent-provisioning/AFJ/token/${AGENCY}_${CONTAINER_NAME}.json"
377-
break # Exit loop if token is found
378-
else
379-
echo "Token not found in logs. Retrying..."
380-
if [ $attempt -eq $RETRIES ]; then
381-
echo "Reached maximum retry attempts. Token not found."
382-
fi
383-
fi
384-
# Add a delay of 10 seconds between retries
385-
sleep 10
386-
done
387-
388351

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

396-
cat <<EOF >${PWD}/agent-provisioning/AFJ/token/${AGENCY}_${CONTAINER_NAME}.json
397-
{
398-
"token" : "$token"
399-
}
400-
EOF
401-
402359
echo "Agent config created"
403360
else
404361
echo "==============="

apps/agent-provisioning/src/agent-provisioning.service.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,24 @@ export class AgentProvisioningService {
6262
// Proceed with accessing the files if needed
6363
} else {
6464
this.logger.log('Agent endpoint file does not exist');
65-
throw new NotFoundException(`${agentEndpointPath} file do not exist `);
65+
throw new NotFoundException(`Agent endpoint file does not exist: ${agentEndpointPath}`);
66+
}
67+
68+
let parsedEndpoint;
69+
try {
70+
parsedEndpoint = JSON.parse(agentEndPoint);
71+
} catch (parseError) {
72+
this.logger.error(`Failed to parse agent endpoint file: ${parseError.message}`);
73+
throw new Error(`Invalid JSON in agent endpoint file: ${agentEndpointPath}`);
74+
}
75+
76+
if (!parsedEndpoint.CONTROLLER_ENDPOINT) {
77+
this.logger.error('CONTROLLER_ENDPOINT key missing in agent endpoint file');
78+
throw new Error(`Missing CONTROLLER_ENDPOINT in: ${agentEndpointPath}`);
6679
}
6780

6881
resolve({
69-
agentEndPoint: JSON.parse(agentEndPoint).CONTROLLER_ENDPOINT
82+
agentEndPoint: parsedEndpoint.CONTROLLER_ENDPOINT
7083
});
7184
});
7285
});

apps/agent-service/src/agent-service.service.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,8 @@ export class AgentServiceService {
526526
socket.emit('did-publish-process-initiated', { clientId: agentSpinupDto.clientSocketId });
527527
socket.emit('invitation-url-creation-started', { clientId: agentSpinupDto.clientSocketId });
528528
}
529-
const agentBaseWalletToken = await this.commonService.getBaseAgentToken(agentDetails.agentEndPoint);
529+
const apiKey = process.env.AGENT_API_KEY;
530+
const agentBaseWalletToken = await this.commonService.getBaseAgentToken(agentDetails.agentEndPoint, apiKey);
530531
if (!agentBaseWalletToken) {
531532
throw new BadRequestException(ResponseMessages.agent.error.baseWalletToken, {
532533
cause: new Error(),

libs/common/src/common.service.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,14 +308,14 @@ export class CommonService {
308308
}
309309
}
310310

311-
async getBaseAgentToken(agentEndPoint: string): Promise<string> {
311+
async getBaseAgentToken(agentEndPoint: string, apiKey: string): Promise<string> {
312312
const normalizedBaseUrl = await this.normalizeUrlWithProtocol(agentEndPoint);
313313
this.logger.log(`Fetching base agent token from ${normalizedBaseUrl}`);
314-
const apikey = process.env.AGENT_API_KEY;
314+
315315
const agentBaseWalletDetils = await this.httpPost(`${normalizedBaseUrl}${CommonConstants.URL_AGENT_TOKEN}`, '', {
316316
headers: {
317317
Accept: 'application/json',
318-
Authorization: apikey
318+
Authorization: apiKey
319319
}
320320
});
321321
if (!agentBaseWalletDetils) {

0 commit comments

Comments
 (0)