Skip to content

Commit

Permalink
add logs to debug
Browse files Browse the repository at this point in the history
  • Loading branch information
rolljee committed Dec 13, 2024
1 parent 9eeacb1 commit 5547114
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
2 changes: 0 additions & 2 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,6 @@ export abstract class Kommand extends Command {

process.exit(this.exitCode);
}

return this.exitCode;
}

async beforeConnect() {
Expand Down
27 changes: 18 additions & 9 deletions src/support/kuzzle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@ export class KuzzleSDK {
this.sdk.on("networkError", (error: any) => logger.logKo(error.message));

logger.logInfo(
`Connecting to ${this.protocol}${this.ssl ? "s" : ""}://${this.host}:${
this.port
`Connecting to ${this.protocol}${this.ssl ? "s" : ""}://${this.host}:${this.port
} ...`
);

Expand Down Expand Up @@ -155,31 +154,42 @@ export class KuzzleSDK {
*/
public async impersonate(userKuid: string, callback: { (): Promise<void> }) {
const currentToken = this.sdk.jwt;

let apiKey: any;

try {
console.log('Starting impersonation process...');
apiKey = await this.security.createApiKey(
userKuid,
"Kourou impersonation token",
{ expiresIn: "2h", refresh: false } as any
);

console.log(`Impersonating user ${userKuid}...`, apiKey);

this.sdk.jwt = apiKey._source.token;

console.log('Executing callback...');
await callback();
console.log('Callback completed successfully');
} catch (error) {
console.error('Error during impersonation:', error);
throw error;
} finally {
console.log("Restoring previous token...");
console.log("Starting cleanup in finally block...");
this.sdk.jwt = currentToken;

if (apiKey?._id) {
await this.security.deleteApiKey(userKuid, apiKey._id);
try {
await this.security.deleteApiKey(userKuid, apiKey._id);
console.log('API key cleanup successful');
} catch (cleanupError) {
console.error('Error cleaning up API key:', cleanupError);
}
}
console.log("Cleanup completed");
}
}


disconnect() {
this.sdk.disconnect();

Expand Down Expand Up @@ -217,9 +227,8 @@ export class KuzzleSDK {
}

// Construct the URL
const url = `${this.ssl ? "https" : "http"}://${this.host}:${
this.port
}/_query`;
const url = `${this.ssl ? "https" : "http"}://${this.host}:${this.port
}/_query`;

// Construct the request
const body = JSON.stringify(request);
Expand Down

0 comments on commit 5547114

Please sign in to comment.