Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Check for Invalid iKey #1243

Merged
merged 1 commit into from
Nov 15, 2023
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
2 changes: 1 addition & 1 deletion AutoCollection/Statsbeat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ class Statsbeat {
}

private _shutdownStatsbeat() {
this.enable(false);// Disable Statsbeat as is it failed 3 times cosnecutively during initialization, is possible SDK is running in private or restricted network
this.enable(false);// Disable Statsbeat as is it failed 3 times consecutively during initialization, is possible SDK is running in private or restricted network
}

private _getConnectionString(config: Config): string {
Expand Down
5 changes: 5 additions & 0 deletions Library/Sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { FileAccessControl } from "./FileAccessControl";
const legacyThrottleStatusCode = 439; // - Too many requests and refresh cache
const throttleStatusCode = 402; // Monthly Quota Exceeded (new SDK)
const RESPONSE_CODES_INDICATING_REACHED_BREEZE = [200, 206, 402, 408, 429, 439, 500];
const INVALID_IKEY = "Invalid instrumentation key";

class Sender {
private static TAG = "Sender";
Expand Down Expand Up @@ -197,6 +198,10 @@ class Sender {
let endTime = +new Date();
let duration = endTime - startTime;
this._numConsecutiveFailures = 0;
if (responseString.includes(INVALID_IKEY) && res.statusCode === 400) {
Logging.warn("Instrumentation key was invalid, please check the iKey");
this._shutdownStatsbeat();
}
// Handling of Statsbeat instance sending data, should turn it off if is not able to reach ingestion endpoint
if (this._isStatsbeatSender && !this._statsbeatHasReachedIngestionAtLeastOnce) {
if (RESPONSE_CODES_INDICATING_REACHED_BREEZE.includes(res.statusCode)) {
Expand Down
24 changes: 23 additions & 1 deletion Tests/Library/Sender.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,9 +447,23 @@ describe("Library/Sender", () => {
errors: []
};

const invalidIKeyResponse: Contracts.BreezeResponse = {
itemsAccepted: 0,
itemsReceived: 1,
errors: [{
index: 0,
statusCode: 400,
message: "Invalid instrumentation key"
}]
}

let config = new Config("2bb22222-bbbb-1ccc-8ddd-eeeeffff3333");
let statsbeat = new Statsbeat(config);
let statsbeatSender = new Sender(config, null, null, null, statsbeat);
let shutdownCalled = false;
let shutdown = () => {
shutdownCalled = true;
};
let statsbeatSender = new Sender(config, null, null, null, statsbeat, false, shutdown);
let statsbeatError: Error = {name: "Statsbeat", message: "Statsbeat error" };

it("Succesful requests", (done) => {
Expand Down Expand Up @@ -480,6 +494,14 @@ describe("Library/Sender", () => {
});
});

it("Statsbeat should shutdown upon invalid iKey", (done) => {
nockScope = interceptor.reply(400, invalidIKeyResponse);
statsbeatSender.send([testEnvelope], () => {
assert.strictEqual(shutdownCalled, true);
done();
});
});

it("Retry counts", (done) => {
statsbeatSender.setDiskRetryMode(true);
var statsbeatSpy = sandbox.spy(statsbeat, "countRequest");
Expand Down
Loading