Skip to content

Commit bde2895

Browse files
authored
Additional validation for elasticsearch username (#48247)
* Additional validation for elasticsearch username If "elastic" user is set in config: * In dev mode, throws an error * In prod mode, logs a deprecation warning * Fix user for functional tests * Revert last two commits Revert "Fix user for functional tests" and "Fix user for plugin functional tests in Jenkinsfile" * Change elasticsearch creds for test server Now uses "kibana" user instead of "elastic" user * Fix plugin API functional tests * Fix PKI API integration test * Change log messages, now conditional on `dist: false` not `dev: true`
1 parent 34c99cb commit bde2895

File tree

10 files changed

+54
-17
lines changed

10 files changed

+54
-17
lines changed

src/core/server/elasticsearch/__snapshots__/elasticsearch_config.test.ts.snap

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/core/server/elasticsearch/elasticsearch_config.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,11 @@ test('#ssl.certificateAuthorities accepts both string and array of strings', ()
107107
);
108108
expect(configValue.ssl.certificateAuthorities).toEqual(['some-path', 'another-path']);
109109
});
110+
111+
test('#username throws if equal to "elastic", only while running from source', () => {
112+
const obj = {
113+
username: 'elastic',
114+
};
115+
expect(() => config.schema.validate(obj, { dist: false })).toThrowErrorMatchingSnapshot();
116+
expect(() => config.schema.validate(obj, { dist: true })).not.toThrow();
117+
});

src/core/server/elasticsearch/elasticsearch_config.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import { schema, TypeOf } from '@kbn/config-schema';
2121
import { Duration } from 'moment';
22+
import { Logger } from '../logging';
2223

2324
const hostURISchema = schema.uri({ scheme: ['http', 'https'] });
2425

@@ -39,7 +40,23 @@ export const config = {
3940
defaultValue: 'http://localhost:9200',
4041
}),
4142
preserveHost: schema.boolean({ defaultValue: true }),
42-
username: schema.maybe(schema.string()),
43+
username: schema.maybe(
44+
schema.conditional(
45+
schema.contextRef('dist'),
46+
false,
47+
schema.string({
48+
validate: rawConfig => {
49+
if (rawConfig === 'elastic') {
50+
return (
51+
'value of "elastic" is forbidden. This is a superuser account that can obfuscate ' +
52+
'privilege-related issues. You should use the "kibana" user instead.'
53+
);
54+
}
55+
},
56+
}),
57+
schema.string()
58+
)
59+
),
4360
password: schema.maybe(schema.string()),
4461
requestHeadersWhitelist: schema.oneOf([schema.string(), schema.arrayOf(schema.string())], {
4562
defaultValue: ['authorization'],
@@ -166,7 +183,7 @@ export class ElasticsearchConfig {
166183
*/
167184
public readonly customHeaders: ElasticsearchConfigType['customHeaders'];
168185

169-
constructor(rawConfig: ElasticsearchConfigType) {
186+
constructor(rawConfig: ElasticsearchConfigType, log?: Logger) {
170187
this.ignoreVersionMismatch = rawConfig.ignoreVersionMismatch;
171188
this.apiVersion = rawConfig.apiVersion;
172189
this.logQueries = rawConfig.logQueries;
@@ -195,5 +212,14 @@ export class ElasticsearchConfig {
195212
...rawConfig.ssl,
196213
certificateAuthorities,
197214
};
215+
216+
if (this.username === 'elastic' && log !== undefined) {
217+
// logger is optional / not used during tests
218+
// TODO: logger can be removed when issue #40255 is resolved to support deprecations in NP config service
219+
log.warn(
220+
`Setting the elasticsearch username to "elastic" is deprecated. You should use the "kibana" user instead.`,
221+
{ tags: ['deprecation'] }
222+
);
223+
}
198224
}
199225
}

src/core/server/elasticsearch/elasticsearch_service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export class ElasticsearchService implements CoreService<InternalElasticsearchSe
5151
this.log = coreContext.logger.get('elasticsearch-service');
5252
this.config$ = coreContext.configService
5353
.atPath<ElasticsearchConfigType>('elasticsearch')
54-
.pipe(map(rawConfig => new ElasticsearchConfig(rawConfig)));
54+
.pipe(map(rawConfig => new ElasticsearchConfig(rawConfig, coreContext.logger.get('config'))));
5555
}
5656

5757
public async setup(deps: SetupDeps): Promise<InternalElasticsearchServiceSetup> {

src/test_utils/kbn_server.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -270,17 +270,17 @@ export function createTestServers({
270270
// Override provided configs, we know what the elastic user is now
271271
kbnSettings.elasticsearch = {
272272
hosts: [esTestConfig.getUrl()],
273-
username: esTestConfig.getUrlParts().username,
274-
password: esTestConfig.getUrlParts().password,
273+
username: kibanaServerTestUser.username,
274+
password: kibanaServerTestUser.password,
275275
};
276276
}
277277

278278
return {
279279
stop: async () => await es.cleanup(),
280280
es,
281281
hosts: [esTestConfig.getUrl()],
282-
username: esTestConfig.getUrlParts().username,
283-
password: esTestConfig.getUrlParts().password,
282+
username: kibanaServerTestUser.username,
283+
password: kibanaServerTestUser.password,
284284
};
285285
},
286286
startKibana: async () => {

test/common/config.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
import path from 'path';
2121
import { format as formatUrl } from 'url';
22-
import { OPTIMIZE_BUNDLE_DIR, esTestConfig, kbnTestConfig } from '@kbn/test';
22+
import { OPTIMIZE_BUNDLE_DIR, esTestConfig, kbnTestConfig, kibanaServerTestUser } from '@kbn/test';
2323
import { services } from './services';
2424

2525
export default function () {
@@ -53,8 +53,8 @@ export default function () {
5353
'--status.allowAnonymous=true',
5454
'--optimize.enabled=true',
5555
`--elasticsearch.hosts=${formatUrl(servers.elasticsearch)}`,
56-
`--elasticsearch.username=${servers.elasticsearch.username}`,
57-
`--elasticsearch.password=${servers.elasticsearch.password}`,
56+
`--elasticsearch.username=${kibanaServerTestUser.username}`,
57+
`--elasticsearch.password=${kibanaServerTestUser.password}`,
5858
`--kibana.disableWelcomeScreen=true`,
5959
'--telemetry.banner=false',
6060
`--server.maxPayloadBytes=1679958`,

x-pack/test/pki_api_integration/apis/security/pki_auth.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ export default function({ getService }: FtrProviderContext) {
113113
enabled: true,
114114
metadata: {
115115
pki_delegated_by_realm: 'reserved',
116-
pki_delegated_by_user: 'elastic',
116+
pki_delegated_by_user: 'kibana',
117117
pki_dn: 'CN=first_client',
118118
},
119119
authentication_realm: { name: 'pki1', type: 'pki' },
@@ -155,7 +155,7 @@ export default function({ getService }: FtrProviderContext) {
155155
enabled: true,
156156
metadata: {
157157
pki_delegated_by_realm: 'reserved',
158-
pki_delegated_by_user: 'elastic',
158+
pki_delegated_by_user: 'kibana',
159159
pki_dn: 'CN=second_client',
160160
},
161161
authentication_realm: { name: 'pki1', type: 'pki' },

x-pack/test/plugin_api_integration/plugins/task_manager/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export default function TaskTestingAPI(kibana) {
4141

4242
const callCluster = server.plugins.elasticsearch.getCluster('admin').callWithInternalUser;
4343
await callCluster('index', {
44-
index: '.task_manager_test_result',
44+
index: '.kibana_task_manager_test_result',
4545
body: {
4646
type: 'task',
4747
taskId: taskInstance.id,

x-pack/test/plugin_api_integration/test_suites/task_manager/task_manager_integration.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export default function ({ getService }) {
1616
const log = getService('log');
1717
const retry = getService('retry');
1818
const config = getService('config');
19-
const testHistoryIndex = '.task_manager_test_result';
19+
const testHistoryIndex = '.kibana_task_manager_test_result';
2020
const supertest = supertestAsPromised(url.format(config.get('servers.kibana')));
2121

2222
describe('scheduling and running tasks', () => {

x-pack/test/reporting/configs/generate_api.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7-
import { esTestConfig, kbnTestConfig } from '@kbn/test';
7+
import { esTestConfig, kbnTestConfig, kibanaServerTestUser } from '@kbn/test';
88
import { format as formatUrl } from 'url';
99
import { getApiIntegrationConfig } from '../../api_integration/config';
1010
import { getReportingApiConfig } from './api';
@@ -35,8 +35,8 @@ export default async function ({ readConfigFile }) {
3535
`--server.maxPayloadBytes=1679958`,
3636
`--server.port=${kbnTestConfig.getPort()}`,
3737
`--elasticsearch.hosts=${formatUrl(servers.elasticsearch)}`,
38-
`--elasticsearch.password=${servers.elasticsearch.password}`,
39-
`--elasticsearch.username=${servers.elasticsearch.username}`,
38+
`--elasticsearch.username=${kibanaServerTestUser.username}`,
39+
`--elasticsearch.password=${kibanaServerTestUser.password}`,
4040
`--xpack.reporting.csv.enablePanelActionDownload=true`,
4141
`--xpack.reporting.csv.maxSizeBytes=2850`,
4242
`--xpack.reporting.queue.pollInterval=3000`,

0 commit comments

Comments
 (0)