Skip to content

Commit

Permalink
Show connection failures on startup (#510)
Browse files Browse the repository at this point in the history
* Add always visible logs on core connection errors

* Also print dns / internet connection errors

* Add Aikido to log

* Update library/agent/Agent.ts

Co-authored-by: Hans Ott <hansott@hotmail.be>

* Extract checkForReportingAPIError

* Simplify

* Remove some logs again

* Minimize changes even more

---------

Co-authored-by: Hans Ott <hansott@hotmail.be>
  • Loading branch information
timokoessler and hansott authored Jan 27, 2025
1 parent 25931e8 commit 38e877c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
1 change: 0 additions & 1 deletion library/agent/Agent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,6 @@ t.test("it logs when failed to report event", async () => {
t.same(logger.getMessages(), [
"Starting agent...",
"Found token, reporting enabled!",
"Failed to start agent",
"Heartbeat...",
"Failed to do heartbeat",
"Failed to report attack",
Expand Down
28 changes: 21 additions & 7 deletions library/agent/Agent.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* eslint-disable max-lines-per-function */
/* eslint-disable max-lines-per-function, no-console */
import { hostname, platform, release } from "os";
import { convertRequestBodyToString } from "../helpers/convertRequestBodyToString";
import { getAgentVersion } from "../helpers/getAgentVersion";
Expand Down Expand Up @@ -113,12 +113,27 @@ export class Agent {
this.timeoutInMS
);

this.checkForReportingAPIError(result);
this.updateServiceConfig(result);

await this.updateBlockedLists();
}
}

checkForReportingAPIError(result: ReportingAPIResponse) {
if (!result.success) {
if (result.error === "invalid_token") {
console.error(
"Aikido: We were unable to connect to the Aikido platform. Please verify that your token is correct."
);
} else {
console.error(
`Aikido: Failed to connect to the Aikido platform: ${result.error}`
);
}
}
}

onErrorThrownByInterceptor({
error,
module,
Expand Down Expand Up @@ -193,7 +208,7 @@ export class Agent {
this.attackLogger.log(attack);

if (this.token) {
this.api.report(this.token, attack, this.timeoutInMS).catch(() => {
this.api.report(this.token, attack, this.timeoutInMS).catch((err) => {
this.logger.log("Failed to report attack");
});
}
Expand All @@ -203,7 +218,7 @@ export class Agent {
* Sends a heartbeat via the API to the server (only when not in serverless mode)
*/
private heartbeat(timeoutInMS = this.timeoutInMS) {
this.sendHeartbeat(timeoutInMS).catch(() => {
this.sendHeartbeat(timeoutInMS).catch((err) => {
this.logger.log("Failed to do heartbeat");
});
}
Expand Down Expand Up @@ -354,7 +369,7 @@ export class Agent {
this.serviceConfig.updateBlockedIPAddresses(blockedIPAddresses);
this.serviceConfig.updateBlockedUserAgents(blockedUserAgents);
} catch (error: any) {
this.logger.log(`Failed to update blocked lists: ${error.message}`);
console.error(`Aikido: Failed to update blocked lists: ${error.message}`);
}
}

Expand Down Expand Up @@ -432,7 +447,6 @@ export class Agent {
this.logger.log("No token provided, disabling reporting.");

if (!this.block && !isAikidoCI()) {
// eslint-disable-next-line no-console
console.log(
"AIKIDO: Running in monitoring only mode without reporting to Aikido Cloud. Set AIKIDO_BLOCK=true to enable blocking."
);
Expand All @@ -448,8 +462,8 @@ export class Agent {
this.startHeartbeats();
this.startPollingForConfigChanges();
})
.catch(() => {
this.logger.log("Failed to start agent");
.catch((err) => {
console.error(`Aikido: Failed to start agent: ${err.message}`);
});
}

Expand Down
5 changes: 5 additions & 0 deletions library/agent/api/fetchBlockedLists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ export async function fetchBlockedLists(token: Token): Promise<{
});

if (statusCode !== 200) {
if (statusCode === 401) {
throw new Error(
`Unable to access the Aikido platform, please check your token.`
);
}
throw new Error(`Failed to fetch blocked lists: ${statusCode}`);
}

Expand Down

0 comments on commit 38e877c

Please sign in to comment.