Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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}`
);
}
}

Check warning on line 134 in library/agent/Agent.ts

View check run for this annotation

Codecov / codecov/patch

library/agent/Agent.ts#L125-L134

Added lines #L125 - L134 were not covered by tests
}

onErrorThrownByInterceptor({
error,
module,
Expand Down Expand Up @@ -193,7 +208,7 @@
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 @@
* 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 @@
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 @@
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 @@
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
Loading