Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,16 @@ jest.mock('@console/shared/src/hooks/useUserSettings', () => ({

jest.mock('@console/shared/src/hooks/useUser', () => ({
useUser: jest.fn(() => ({
user: {},
userResource: {},
user: {
username: 'shadowman',
},
userResource: {
metadata: {
annotations: {
'toolchain.dev.openshift.com/sso-user-id': 'sandbox-user-id-123',
},
},
},
userResourceLoaded: true,
userResourceError: null,
username: 'testuser',
Expand All @@ -36,22 +44,15 @@ jest.mock('@console/shared/src/hooks/useUser', () => ({
})),
}));

const mockUserResource = {};

const exampleReturnValue = {
accountMail: undefined,
accountMailDomain: '',
clusterId: undefined,
clusterType: undefined,
consoleVersion: undefined,
organizationId: undefined,
path: undefined,
userResource: mockUserResource,
};

jest.mock('@console/internal/components/utils/k8s-get-hook', () => ({
useK8sGet: () => [mockUserResource, true],
}));

const mockUserSettings = useUserSettings as jest.Mock;

const useResolvedExtensionsMock = useResolvedExtensions as jest.Mock;
Expand Down Expand Up @@ -227,6 +228,7 @@ describe('useTelemetry', () => {
...exampleReturnValue,
clusterType: 'DEVSANDBOX',
consoleVersion: 'x.y.z',
sandboxUserId: 'sandbox-user-id-123',
});
});

Expand Down Expand Up @@ -353,4 +355,51 @@ describe('useTelemetry', () => {
fireTelemetryEvent('test 11');
expect(listener).toHaveBeenCalledTimes(1);
});

it('anonymizes email to only return the domain', () => {
const email = 'shadowman@redhat.com';
const expectedDomain = 'redhat.com';

window.SERVER_FLAGS = {
...originServerFlags,
telemetry: {
ACCOUNT_MAIL: email,
},
};
updateClusterPropertiesFromTests();
const { result } = testHook(() => useTelemetry());
const fireTelemetryEvent = result.current;
fireTelemetryEvent('test 12');
expect(listener).toHaveBeenCalledTimes(1);
expect(listener).toHaveBeenCalledWith('test 12', {
...exampleReturnValue,
accountMailDomain: expectedDomain,
});

// assert PII is not sent
const callArgs = listener.mock.calls[0][1];
expect(callArgs.user).toBeUndefined();
expect(callArgs.userResource).toBeUndefined();
expect(callArgs.accountMail).toBeUndefined();
expect(callArgs.sandboxUserId).toBeUndefined();
});

['invalid-email', 'shadowman@', 'red@hat@redhat.com', '@@', ''].forEach((email) => {
it(`should not extract domains from invalid emails (${email})`, () => {
window.SERVER_FLAGS = {
...originServerFlags,
telemetry: {
ACCOUNT_MAIL: email,
},
};
updateClusterPropertiesFromTests();
const { result } = testHook(() => useTelemetry());
const fireTelemetryEvent = result.current;
fireTelemetryEvent('test 12');
expect(listener).toHaveBeenCalledWith('test 12', {
...exampleReturnValue,
accountMailDomain: '',
});
});
});
});
36 changes: 31 additions & 5 deletions frontend/packages/console-shared/src/hooks/useTelemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ export interface ClusterProperties {
clusterType?: string;
consoleVersion?: string;
organizationId?: string;
accountMail?: string;
accountMailDomain?: string;
}

export type TelemetryEventProperties = {
user?: UserInfo;
userResource?: UserKind;
/** Only sent if in a sandbox cluster */
sandboxUserId?: string;
} & ClusterProperties &
Record<string, any>;

Expand All @@ -36,6 +37,11 @@ export interface TelemetryEvent {

let telemetryEvents: TelemetryEvent[] = [];

const getEmailDomain = (email: string = ''): string => {
const emailParts = email.split('@');
return emailParts.length === 2 ? emailParts[1] : '';
};

export const getClusterProperties = () => {
const clusterProperties: ClusterProperties = {};
clusterProperties.clusterId = window.SERVER_FLAGS.telemetry?.CLUSTER_ID;
Expand All @@ -50,7 +56,7 @@ export const getClusterProperties = () => {
clusterProperties.consoleVersion =
window.SERVER_FLAGS.releaseVersion || window.SERVER_FLAGS.consoleVersion;
clusterProperties.organizationId = window.SERVER_FLAGS.telemetry?.ORGANIZATION_ID;
clusterProperties.accountMail = window.SERVER_FLAGS.telemetry?.ACCOUNT_MAIL;
clusterProperties.accountMailDomain = getEmailDomain(window.SERVER_FLAGS.telemetry?.ACCOUNT_MAIL);
return clusterProperties;
};

Expand All @@ -70,6 +76,20 @@ let clusterProperties = getClusterProperties();

export const updateClusterPropertiesFromTests = () => (clusterProperties = getClusterProperties());

const injectSandboxProperties = (
properties: TelemetryEventProperties,
userResource: UserKind,
): TelemetryEventProperties => {
if (clusterProperties.clusterType === 'DEVSANDBOX') {
return {
...properties,
sandboxUserId:
userResource?.metadata?.annotations?.['toolchain.dev.openshift.com/sso-user-id'],
};
}
return properties;
};

export const useTelemetry = () => {
// TODO use useDynamicPluginInfo() hook to tell whether all dynamic plugins have been processed
// to avoid firing telemetry events multiple times whenever a dynamic plugin loads asynchronously
Expand All @@ -93,7 +113,10 @@ export const useTelemetry = () => {
userResourceIsLoaded
) {
telemetryEvents.forEach(({ eventType, event }) => {
extensions.forEach((e) => e.properties.listener(eventType, { ...event, userResource }));
// Sends the telemetry event to all the listeners
extensions.forEach((e) =>
e.properties.listener(eventType, injectSandboxProperties(event, userResource)),
);
});
telemetryEvents = [];
}
Expand Down Expand Up @@ -124,7 +147,10 @@ export const useTelemetry = () => {
return;
}

extensions.forEach((e) => e.properties.listener(eventType, { ...event, userResource }));
// Sends the telemetry event to all the listeners
extensions.forEach((e) =>
e.properties.listener(eventType, injectSandboxProperties(event, userResource)),
);
},
[extensions, currentUserPreferenceTelemetryValue, userResource, userResourceIsLoaded],
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const eventListener: TelemetryEventListener = async (
switch (eventType) {
case 'identify':
{
const { user, userResource, ...otherProperties }: TelemetryEventProperties = properties;
const { user, sandboxUserId, ...otherProperties }: TelemetryEventProperties = properties;
const clusterId = otherProperties?.clusterId;
const organizationId = otherProperties?.organizationId;
const username = user?.username;
Expand All @@ -67,8 +67,7 @@ export const eventListener: TelemetryEventListener = async (

// anonymize user ID if cluster is not a DEVSANDBOX cluster
if (getClusterProperties().clusterType === 'DEVSANDBOX') {
processedUserId =
userResource?.metadata?.annotations?.['toolchain.dev.openshift.com/sso-user-id'];
processedUserId = sandboxUserId;
} else {
processedUserId = await anonymizeId(userId);
}
Expand Down