Skip to content

Commit

Permalink
[Telemetry] Use staging if it's not a distributable release (elastic#…
Browse files Browse the repository at this point in the history
…63875)

* [Telemetry] Use staging if it's not a distributable release (instead of 'dev' vs. 'prod' approach)

* Rename isProd to isDistributable and useProdKey
  • Loading branch information
afharo committed Apr 20, 2020
1 parent 8798423 commit 4af7b73
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 27 deletions.
8 changes: 4 additions & 4 deletions src/plugins/telemetry/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ export const configSchema = schema.object({
config: schema.string({ defaultValue: getConfigPath() }),
banner: schema.boolean({ defaultValue: true }),
url: schema.conditional(
schema.contextRef('dev'),
schema.literal(true),
schema.contextRef('dist'),
schema.literal(false), // Point to staging if it's not a distributable release
schema.string({
defaultValue: `https://telemetry-staging.elastic.co/xpack/${ENDPOINT_VERSION}/send`,
}),
Expand All @@ -46,8 +46,8 @@ export const configSchema = schema.object({
})
),
optInStatusUrl: schema.conditional(
schema.contextRef('dev'),
schema.literal(true),
schema.contextRef('dist'),
schema.literal(false), // Point to staging if it's not a distributable release
schema.string({
defaultValue: `https://telemetry-staging.elastic.co/opt_in_status/${ENDPOINT_VERSION}/send`,
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ import { encryptTelemetry, getKID } from './encrypt';

describe('getKID', () => {
it(`returns 'kibana_dev' kid for development`, async () => {
const isProd = false;
const kid = getKID(isProd);
const useProdKey = false;
const kid = getKID(useProdKey);
expect(kid).toBe('kibana_dev');
});

it(`returns 'kibana_prod' kid for development`, async () => {
const isProd = true;
const kid = getKID(isProd);
const useProdKey = true;
const kid = getKID(useProdKey);
expect(kid).toBe('kibana');
});
});
Expand All @@ -41,19 +41,19 @@ describe('encryptTelemetry', () => {

it('encrypts payload', async () => {
const payload = { some: 'value' };
await encryptTelemetry(payload, { isProd: true });
await encryptTelemetry(payload, { useProdKey: true });
expect(createRequestEncryptor).toBeCalledWith(telemetryJWKS);
});

it('uses kibana kid on { isProd: true }', async () => {
it('uses kibana kid on { useProdKey: true }', async () => {
const payload = { some: 'value' };
await encryptTelemetry(payload, { isProd: true });
await encryptTelemetry(payload, { useProdKey: true });
expect(mockEncrypt).toBeCalledWith('kibana', payload);
});

it('uses kibana_dev kid on { isProd: false }', async () => {
it('uses kibana_dev kid on { useProdKey: false }', async () => {
const payload = { some: 'value' };
await encryptTelemetry(payload, { isProd: false });
await encryptTelemetry(payload, { useProdKey: false });
expect(mockEncrypt).toBeCalledWith('kibana_dev', payload);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@
import { createRequestEncryptor } from '@elastic/request-crypto';
import { telemetryJWKS } from './telemetry_jwks';

export function getKID(isProd = false): string {
return isProd ? 'kibana' : 'kibana_dev';
export function getKID(useProdKey = false): string {
return useProdKey ? 'kibana' : 'kibana_dev';
}

export async function encryptTelemetry(payload: any, { isProd = false } = {}): Promise<string[]> {
const kid = getKID(isProd);
export async function encryptTelemetry(
payload: any,
{ useProdKey = false } = {}
): Promise<string[]> {
const kid = getKID(useProdKey);
const encryptor = await createRequestEncryptor(telemetryJWKS);
const clusters = [].concat(payload);
return Promise.all(clusters.map((cluster: any) => encryptor.encrypt(kid, cluster)));
Expand Down
10 changes: 4 additions & 6 deletions src/plugins/telemetry_collection_manager/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ export class TelemetryCollectionManagerPlugin
private readonly collections: Array<Collection<any>> = [];
private usageGetterMethodPriority = -1;
private usageCollection?: UsageCollectionSetup;
private readonly isDev: boolean;
private readonly isDistributable: boolean;
private readonly version: string;

constructor(initializerContext: PluginInitializerContext) {
this.logger = initializerContext.logger.get();
this.isDev = initializerContext.env.mode.dev;
this.isDistributable = initializerContext.env.packageInfo.dist;
this.version = initializerContext.env.packageInfo.version;
}

Expand Down Expand Up @@ -158,7 +158,7 @@ export class TelemetryCollectionManagerPlugin
if (config.unencrypted) {
return optInStats;
}
return encryptTelemetry(optInStats, { isProd: !this.isDev });
return encryptTelemetry(optInStats, { useProdKey: this.isDistributable });
}
} catch (err) {
this.logger.debug(`Failed to collect any opt in stats with registered collections.`);
Expand All @@ -176,7 +176,6 @@ export class TelemetryCollectionManagerPlugin
) => {
const context: StatsCollectionContext = {
logger: this.logger.get(collection.title),
isDev: this.isDev,
version: this.version,
...collection.customContext,
};
Expand Down Expand Up @@ -206,7 +205,7 @@ export class TelemetryCollectionManagerPlugin
return usageData;
}

return encryptTelemetry(usageData, { isProd: !this.isDev });
return encryptTelemetry(usageData, { useProdKey: this.isDistributable });
}
} catch (err) {
this.logger.debug(
Expand All @@ -225,7 +224,6 @@ export class TelemetryCollectionManagerPlugin
): Promise<UsageStatsPayload[]> {
const context: StatsCollectionContext = {
logger: this.logger.get(collection.title),
isDev: this.isDev,
version: this.version,
...collection.customContext,
};
Expand Down
1 change: 0 additions & 1 deletion src/plugins/telemetry_collection_manager/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ export interface ESLicense {

export interface StatsCollectionContext {
logger: Logger;
isDev: boolean;
version: string;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ describe('get_all_stats', () => {
},
{
logger: coreMock.createPluginInitializerContext().logger.get('test'),
isDev: true,
version: 'version',
maxBucketSize: 1,
}
Expand All @@ -208,7 +207,6 @@ describe('get_all_stats', () => {
},
{
logger: coreMock.createPluginInitializerContext().logger.get('test'),
isDev: true,
version: 'version',
maxBucketSize: 1,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ const kibana = {

const getContext = () => ({
version: '8675309-snapshot',
isDev: true,
logger: coreMock.createPluginInitializerContext().logger.get('test'),
});

Expand Down

0 comments on commit 4af7b73

Please sign in to comment.