Skip to content

Commit

Permalink
fix: solved the agent spin-up with apiKey updation on org_agent table (
Browse files Browse the repository at this point in the history
…#417)

Signed-off-by: KulkarniShashank <[email protected]>
  • Loading branch information
KulkarniShashank committed Sep 11, 2024
1 parent 324acc9 commit 97aad96
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 71 deletions.
9 changes: 5 additions & 4 deletions apps/agent-provisioning/AFJ/scripts/start_agent.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ INBOUND_PORT_FILE="$PWD/apps/agent-provisioning/AFJ/port-file/last-inbound-port.
ADMIN_PORT=8001
INBOUND_PORT=9001


increment_port() {
local port="$1"
local lower_limit="$2"
Expand Down Expand Up @@ -96,7 +97,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 +141,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 @@ -216,13 +217,13 @@ if [ $? -eq 0 ]; then
# 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 Down
2 changes: 1 addition & 1 deletion apps/agent-provisioning/src/agent-provisioning.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class AgentProvisioningService {
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) => {
Expand Down
99 changes: 33 additions & 66 deletions apps/connection/src/connection.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class ConnectionService {
*/
async createLegacyConnectionInvitation(payload: IConnection): Promise<ICreateConnectionUrl> {

const {orgId, multiUseInvitation, autoAcceptConnection, alias, label} = payload;
const { orgId, multiUseInvitation, autoAcceptConnection, alias, label } = payload;
try {
const connectionInvitationExist = await this.connectionRepository.getConnectionInvitationByOrgId(orgId);
if (connectionInvitationExist) {
Expand Down Expand Up @@ -68,14 +68,11 @@ export class ConnectionService {
const orgAgentType = await this.connectionRepository.getOrgAgentType(agentDetails?.orgAgentTypeId);
const url = await this.getAgentUrl(orgAgentType, agentEndPoint, agentDetails?.tenantId);

// const apiKey = platformConfig?.sgApiKey;

const apiKey = await this._getOrgAgentApiKey(orgId);
// let apiKey:string = await this.cacheService.get(CommonConstants.CACHE_APIKEY_KEY);
// this.logger.log(`cachedApiKey----getConnections,${apiKey}`);
//if(!apiKey || apiKey === null || apiKey === undefined) {
// apiKey = await this._getOrgAgentApiKey(orgId);
// }
let apiKey: string = await this.cacheService.get(CommonConstants.CACHE_APIKEY_KEY);
this.logger.log(`cachedApiKey----getConnections,${apiKey}`);
if (!apiKey || null === apiKey || undefined === apiKey) {
apiKey = await this._getOrgAgentApiKey(orgId);
}
const createConnectionInvitation = await this._createConnectionInvitation(connectionPayload, url, apiKey);
const invitationObject = createConnectionInvitation?.message?.invitation['@id'];
let shortenedUrl;
Expand Down Expand Up @@ -205,45 +202,15 @@ export class ConnectionService {
theirDid,
theirLabel
};

let url;
if (orgAgentType === OrgAgentType.DEDICATED) {

url = `${agentEndPoint}${CommonConstants.URL_CONN_GET_CONNECTIONS}`;

} else if (orgAgentType === OrgAgentType.SHARED) {

url = `${agentEndPoint}${CommonConstants.URL_SHAGENT_GET_CREATEED_INVITATIONS}`.replace('#', agentDetails.tenantId);
} else {

throw new NotFoundException(ResponseMessages.connection.error.agentUrlNotFound);
}

Object.keys(params).forEach((element: string) => {
const appendParams: string = url.includes('?') ? '&' : '?';

if (params[element] !== undefined) {
url = `${url + appendParams + element}=${params[element]}`;
}
});


// const apiKey = await this._getOrgAgentApiKey(orgId);
let apiKey:string = await this.cacheService.get(CommonConstants.CACHE_APIKEY_KEY);
this.logger.log(`cachedApiKey----getConnections,${apiKey}`);
if (!apiKey || null === apiKey || undefined === apiKey) {
apiKey = await this._getOrgAgentApiKey(orgId);
}
const connectionsDetails = await this._getAllConnections(url, apiKey);
return connectionsDetails?.response;
return connectionResponse;
} catch (error) {

this.logger.error(
`[getConnections] [NATS call]- error in fetch connections details : ${JSON.stringify(error)}`
);

throw new RpcException(error.response ? error.response : error);
}
throw new RpcException(error.response ? error.response : error);
}
}

async _getAllConnections(
Expand Down Expand Up @@ -305,19 +272,19 @@ export class ConnectionService {


// const apiKey = await this._getOrgAgentApiKey(orgId);
let apiKey:string = await this.cacheService.get(CommonConstants.CACHE_APIKEY_KEY);
let apiKey: string = await this.cacheService.get(CommonConstants.CACHE_APIKEY_KEY);
this.logger.log(`cachedApiKey----getConnectionsById,${apiKey}`);
if (!apiKey || null === apiKey || undefined === apiKey) {
apiKey = await this._getOrgAgentApiKey(orgId);
if (!apiKey || null === apiKey || undefined === apiKey) {
apiKey = await this._getOrgAgentApiKey(orgId);
}
const createConnectionInvitation = await this._getConnectionsByConnectionId(url, apiKey);
return createConnectionInvitation?.response;
} catch (error) {
return createConnectionInvitation;


} catch (error) {
this.logger.error(`[getConnectionsById] - error in get connections : ${JSON.stringify(error)}`);

if (error?.response?.error?.reason) {
if (error?.response?.error?.reason) {
throw new RpcException({
message: ResponseMessages.connection.error.connectionNotFound,
statusCode: error?.response?.status,
Expand All @@ -334,25 +301,25 @@ export class ConnectionService {
apiKey: string
): Promise<IConnectionDetailsById> {

//nats call in agent service for fetch connection details
const pattern = { cmd: 'agent-get-connection-details-by-connectionId' };
const payload = { url, apiKey };
return this.connectionServiceProxy
.send<IConnectionDetailsById>(pattern, payload)
.toPromise()
.catch(error => {
this.logger.error(
`[_getConnectionsByConnectionId] [NATS call]- error in fetch connections : ${JSON.stringify(error)}`
);
throw new HttpException(
{
status: error.statusCode,
error: error.error?.message?.error ? error.error?.message?.error : error.error,
message: error.message
}, error.error);
});
//nats call in agent service for fetch connection details
const pattern = { cmd: 'agent-get-connection-details-by-connectionId' };
const payload = { url, apiKey };
return this.connectionServiceProxy
.send<IConnectionDetailsById>(pattern, payload)
.toPromise()
.catch(error => {
this.logger.error(
`[_getConnectionsByConnectionId] [NATS call]- error in fetch connections : ${JSON.stringify(error)}`
);
throw new HttpException(
{
status: error.statusCode,
error: error.error?.message?.error ? error.error?.message?.error : error.error,
message: error.message
}, error.error);
});
}

/**
* Description: Fetch agent url
* @param referenceId
Expand Down

0 comments on commit 97aad96

Please sign in to comment.