Skip to content

Commit 38e877c

Browse files
Show connection failures on startup (#510)
* 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>
1 parent 25931e8 commit 38e877c

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

library/agent/Agent.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,6 @@ t.test("it logs when failed to report event", async () => {
666666
t.same(logger.getMessages(), [
667667
"Starting agent...",
668668
"Found token, reporting enabled!",
669-
"Failed to start agent",
670669
"Heartbeat...",
671670
"Failed to do heartbeat",
672671
"Failed to report attack",

library/agent/Agent.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* eslint-disable max-lines-per-function */
1+
/* eslint-disable max-lines-per-function, no-console */
22
import { hostname, platform, release } from "os";
33
import { convertRequestBodyToString } from "../helpers/convertRequestBodyToString";
44
import { getAgentVersion } from "../helpers/getAgentVersion";
@@ -113,12 +113,27 @@ export class Agent {
113113
this.timeoutInMS
114114
);
115115

116+
this.checkForReportingAPIError(result);
116117
this.updateServiceConfig(result);
117118

118119
await this.updateBlockedLists();
119120
}
120121
}
121122

123+
checkForReportingAPIError(result: ReportingAPIResponse) {
124+
if (!result.success) {
125+
if (result.error === "invalid_token") {
126+
console.error(
127+
"Aikido: We were unable to connect to the Aikido platform. Please verify that your token is correct."
128+
);
129+
} else {
130+
console.error(
131+
`Aikido: Failed to connect to the Aikido platform: ${result.error}`
132+
);
133+
}
134+
}
135+
}
136+
122137
onErrorThrownByInterceptor({
123138
error,
124139
module,
@@ -193,7 +208,7 @@ export class Agent {
193208
this.attackLogger.log(attack);
194209

195210
if (this.token) {
196-
this.api.report(this.token, attack, this.timeoutInMS).catch(() => {
211+
this.api.report(this.token, attack, this.timeoutInMS).catch((err) => {
197212
this.logger.log("Failed to report attack");
198213
});
199214
}
@@ -203,7 +218,7 @@ export class Agent {
203218
* Sends a heartbeat via the API to the server (only when not in serverless mode)
204219
*/
205220
private heartbeat(timeoutInMS = this.timeoutInMS) {
206-
this.sendHeartbeat(timeoutInMS).catch(() => {
221+
this.sendHeartbeat(timeoutInMS).catch((err) => {
207222
this.logger.log("Failed to do heartbeat");
208223
});
209224
}
@@ -354,7 +369,7 @@ export class Agent {
354369
this.serviceConfig.updateBlockedIPAddresses(blockedIPAddresses);
355370
this.serviceConfig.updateBlockedUserAgents(blockedUserAgents);
356371
} catch (error: any) {
357-
this.logger.log(`Failed to update blocked lists: ${error.message}`);
372+
console.error(`Aikido: Failed to update blocked lists: ${error.message}`);
358373
}
359374
}
360375

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

434449
if (!this.block && !isAikidoCI()) {
435-
// eslint-disable-next-line no-console
436450
console.log(
437451
"AIKIDO: Running in monitoring only mode without reporting to Aikido Cloud. Set AIKIDO_BLOCK=true to enable blocking."
438452
);
@@ -448,8 +462,8 @@ export class Agent {
448462
this.startHeartbeats();
449463
this.startPollingForConfigChanges();
450464
})
451-
.catch(() => {
452-
this.logger.log("Failed to start agent");
465+
.catch((err) => {
466+
console.error(`Aikido: Failed to start agent: ${err.message}`);
453467
});
454468
}
455469

library/agent/api/fetchBlockedLists.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ export async function fetchBlockedLists(token: Token): Promise<{
2525
});
2626

2727
if (statusCode !== 200) {
28+
if (statusCode === 401) {
29+
throw new Error(
30+
`Unable to access the Aikido platform, please check your token.`
31+
);
32+
}
2833
throw new Error(`Failed to fetch blocked lists: ${statusCode}`);
2934
}
3035

0 commit comments

Comments
 (0)