Skip to content

Commit

Permalink
make sure to keep track who is doing the telemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
XavierM committed Nov 22, 2021
1 parent 1de4fc9 commit 392af2f
Showing 1 changed file with 43 additions and 11 deletions.
54 changes: 43 additions & 11 deletions x-pack/plugins/security/public/nav_control/nav_control_service.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* 2.0.
*/

import type { BroadcastChannel as BroadcastChannelType } from 'broadcast-channel';
import { createLeaderElection } from 'broadcast-channel';
import { sortBy } from 'lodash';
import React from 'react';
import ReactDOM from 'react-dom';
Expand Down Expand Up @@ -41,7 +43,11 @@ export interface SecurityNavControlServiceStart {
addUserMenuLinks: (newUserMenuLink: UserMenuLink[]) => void;
}

const USER_IN_SESSION = 'user_in_session';

export class SecurityNavControlService {
private channel?: BroadcastChannelType<{ userInSession: boolean }>;

private securityLicense!: SecurityLicense;
private authc!: AuthenticationServiceSetup;
private logoutUrl!: string;
Expand Down Expand Up @@ -69,6 +75,7 @@ export class SecurityNavControlService {
if (shouldRegisterNavControl) {
this.registerSecurityNavControl(core);
}
this.telemetryOnAuthType(core);
}
);

Expand Down Expand Up @@ -112,17 +119,7 @@ export class SecurityNavControlService {
private registerSecurityNavControl(
core: Pick<CoreStart, 'chrome' | 'http' | 'i18n' | 'injectedMetadata' | 'application'>
) {
const currentUserPromise = this.authc.getCurrentUser().then((authenticatedUser) => {
console.log('### only one time ###');
try {
core.http.post('/internal/security/telemetry/auth_type', {
body: JSON.stringify({ auth_type: authenticatedUser.authentication_type }),
});
// eslint-disable-next-line no-empty
} catch {}

return authenticatedUser;
});
const currentUserPromise = this.authc.getCurrentUser();
core.chrome.navControls.registerRight({
order: 2000,
mount: (el: HTMLElement) => {
Expand Down Expand Up @@ -151,4 +148,39 @@ export class SecurityNavControlService {
private sortUserMenuLinks(userMenuLinks: UserMenuLink[]) {
return sortBy(userMenuLinks, 'order');
}

private async telemetryOnAuthType(core: Pick<CoreStart, 'http'>) {
try {
const { BroadcastChannel } = await import('broadcast-channel');
const authenticatedUser = await this.authc.getCurrentUser();

if (!this.channel) {
const tenant = core.http.basePath.serverBasePath;
this.channel = new BroadcastChannel(`${tenant}/${USER_IN_SESSION}`, {
webWorkerSupport: false,
});
}

const elector = createLeaderElection(this.channel, {
fallbackInterval: 0, // optional configuration for how often will renegotiation for leader occur -> no need of renegotiation
responseTime: 1000, // optional configuration for how long will instances have to respond
});
elector.awaitLeadership().then(() => {
if (elector.isLeader) {
core.http.post('/internal/security/telemetry/auth_type', {
body: JSON.stringify({ auth_type: authenticatedUser.authentication_type }),
});
}
});

setTimeout(async () => {
if (!elector.isLeader) {
// Let's kill the non leader since we do not need to do telemetry if you are not a leader
await elector.die();
}
}, 5000);

// eslint-disable-next-line no-empty
} catch {}
}
}

0 comments on commit 392af2f

Please sign in to comment.