Skip to content

Commit

Permalink
Change core ES usage collection
Browse files Browse the repository at this point in the history
  • Loading branch information
jportner committed Aug 31, 2021
1 parent 0ed956f commit bc1842a
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const createStartContractMock = () => {
keystoreConfigured: false,
truststoreConfigured: false,
},
username: 'none',
principal: 'other',
},
http: {
basePathConfigured: false,
Expand Down
44 changes: 31 additions & 13 deletions src/core/server/core_usage_data/core_usage_data_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ describe('CoreUsageDataService', () => {
"logQueries": false,
"numberOfHostsConfigured": 1,
"pingTimeoutMs": 30000,
"principal": "other",
"requestHeadersWhitelistConfigured": false,
"requestTimeoutMs": 30000,
"shardTimeoutMs": 30000,
Expand All @@ -250,7 +251,6 @@ describe('CoreUsageDataService', () => {
"truststoreConfigured": false,
"verificationMode": "full",
},
"username": "none",
},
"http": Object {
"basePathConfigured": false,
Expand Down Expand Up @@ -365,39 +365,57 @@ describe('CoreUsageDataService', () => {
`);
});

describe('elasticsearch.username', () => {
async function doTest({ username, expected }: { username: string; expected: string }) {
describe('elasticsearch.principal', () => {
async function doTest({
username,
serviceAccountToken,
expectedPrincipal,
}: {
username?: string;
serviceAccountToken?: string;
expectedPrincipal: string;
}) {
const defaultMockImplementation = getConfigServiceAtPathMockImplementation();
configService.atPath.mockImplementation((path) => {
if (path === 'elasticsearch') {
return new BehaviorSubject(RawElasticsearchConfig.schema.validate({ username }));
return new BehaviorSubject(
RawElasticsearchConfig.schema.validate({ username, serviceAccountToken })
);
}
return defaultMockImplementation(path);
});
const { getCoreUsageData } = setup();
return expect(getCoreUsageData()).resolves.toEqual(
expect.objectContaining({
config: expect.objectContaining({
elasticsearch: expect.objectContaining({ username: expected }),
elasticsearch: expect.objectContaining({ principal: expectedPrincipal }),
}),
})
);
}

it('returns expected usage data for "elastic"', async () => {
return doTest({ username: 'elastic', expected: 'elastic' });
it('returns expected usage data for elastic.username "elastic"', async () => {
return doTest({ username: 'elastic', expectedPrincipal: 'elastic_user' });
});

it('returns expected usage data for "kibana"', async () => {
return doTest({ username: 'kibana', expected: 'kibana' });
it('returns expected usage data for elastic.username "kibana"', async () => {
return doTest({ username: 'kibana', expectedPrincipal: 'kibana_user' });
});

it('returns expected usage data for "kibana_system"', async () => {
return doTest({ username: 'kibana_system', expected: 'kibana_system' });
it('returns expected usage data for elastic.username "kibana_system"', async () => {
return doTest({ username: 'kibana_system', expectedPrincipal: 'kibana_system_user' });
});

it('returns expected usage data for anything else', async () => {
return doTest({ username: 'anything else', expected: 'other' });
it('returns expected usage data for elastic.username anything else', async () => {
return doTest({ username: 'anything else', expectedPrincipal: 'other_user' });
});

it('returns expected usage data for elastic.serviceAccountToken', async () => {
// Note: elastic.username and elastic.serviceAccountToken are mutually exclusive
return doTest({
serviceAccountToken: 'any',
expectedPrincipal: 'kibana_service_account',
});
});
});
});
Expand Down
13 changes: 8 additions & 5 deletions src/core/server/core_usage_data/core_usage_data_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ export class CoreUsageDataService implements CoreService<CoreUsageDataSetup, Cor
truststoreConfigured: isConfigured.record(es.ssl.truststore),
keystoreConfigured: isConfigured.record(es.ssl.keystore),
},
username: getEsUsernameUsage(es.username),
principal: getEsPrincipalUsage(es),
},
http: {
basePathConfigured: isConfigured.string(http.basePath),
Expand Down Expand Up @@ -515,18 +515,21 @@ export class CoreUsageDataService implements CoreService<CoreUsageDataSetup, Cor
}
}

function getEsUsernameUsage(username: string | undefined) {
let value: CoreConfigUsageData['elasticsearch']['username'] = 'none';
function getEsPrincipalUsage({ username, serviceAccountToken }: ElasticsearchConfigType) {
let value: CoreConfigUsageData['elasticsearch']['principal'] = 'other';
if (isConfigured.string(username)) {
switch (username) {
case 'elastic': // deprecated
case 'kibana': // deprecated
case 'kibana_system':
value = username;
value = `${username}_user` as const;
break;
default:
value = 'other';
value = 'other_user';
}
} else if (serviceAccountToken) {
// cannot be used with elasticsearch.username
value = 'kibana_service_account';
}
return value;
}
8 changes: 7 additions & 1 deletion src/core/server/core_usage_data/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,13 @@ export interface CoreConfigUsageData {
};
apiVersion: string;
healthCheckDelayMs: number;
username: 'none' | 'elastic' | 'kibana' | 'kibana_system' | 'other';
principal:
| 'elastic_user'
| 'kibana_user'
| 'kibana_system_user'
| 'other_user'
| 'kibana_service_account'
| 'other';
};

http: {
Expand Down
2 changes: 1 addition & 1 deletion src/core/server/server.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ export interface CoreConfigUsageData {
};
apiVersion: string;
healthCheckDelayMs: number;
username: 'none' | 'elastic' | 'kibana' | 'kibana_system' | 'other';
principal: 'elastic_user' | 'kibana_user' | 'kibana_system_user' | 'other_user' | 'kibana_service_account' | 'other';
};
// (undocumented)
http: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,12 @@ export function getCoreUsageCollector(
'The interval in miliseconds between health check requests Kibana sends to the Elasticsearch.',
},
},
username: {
principal: {
type: 'keyword',
_meta: { description: 'Indicates what elasticsearch username is configured, if any.' },
_meta: {
description:
'Indicates what elasticsearch user or service account is configured, if any.',
},
},
},

Expand Down
4 changes: 2 additions & 2 deletions src/plugins/telemetry/schema/oss_plugins.json
Original file line number Diff line number Diff line change
Expand Up @@ -5958,10 +5958,10 @@
"description": "The interval in miliseconds between health check requests Kibana sends to the Elasticsearch."
}
},
"username": {
"principal": {
"type": "keyword",
"_meta": {
"description": "Indicates what elasticsearch username is configured, if any."
"description": "Indicates what elasticsearch user or service account is configured, if any."
}
}
}
Expand Down

0 comments on commit bc1842a

Please sign in to comment.