-
Notifications
You must be signed in to change notification settings - Fork 8.5k
Update Monitoring plugin's Elasticsearch configuration #55119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jportner
merged 7 commits into
elastic:master
from
jportner:issue-55073-update-monitoring-plugin-es-config
Jan 21, 2020
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ee53dac
Fix Monitoring plugin Elasticsearch SSL config
jportner dcdb829
Add missing Elasticsearch config deprecations
jportner f879cda
Merge branch 'master' into issue-55073-update-monitoring-plugin-es-co…
elasticmachine 3503ada
Remove optional chaining
jportner 3075c16
Remove more optional chaining
jportner fafa45f
Merge branch 'master' into issue-55073-update-monitoring-plugin-es-co…
jportner cb8c39b
PR review feedback
jportner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
x-pack/legacy/plugins/monitoring/server/es_client/parse_elasticsearch_config.test.mocks.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License; | ||
| * you may not use this file except in compliance with the Elastic License. | ||
| */ | ||
|
|
||
| export const mockReadFileSync = jest.fn(); | ||
jportner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| jest.mock('fs', () => ({ readFileSync: mockReadFileSync })); | ||
|
|
||
| export const mockReadPkcs12Keystore = jest.fn(); | ||
| export const mockReadPkcs12Truststore = jest.fn(); | ||
| jest.mock('../../../../../../src/core/utils', () => ({ | ||
| readPkcs12Keystore: mockReadPkcs12Keystore, | ||
| readPkcs12Truststore: mockReadPkcs12Truststore, | ||
| })); | ||
181 changes: 181 additions & 0 deletions
181
x-pack/legacy/plugins/monitoring/server/es_client/parse_elasticsearch_config.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License; | ||
| * you may not use this file except in compliance with the Elastic License. | ||
| */ | ||
|
|
||
| import { | ||
| mockReadFileSync, | ||
| mockReadPkcs12Keystore, | ||
| mockReadPkcs12Truststore, | ||
| } from './parse_elasticsearch_config.test.mocks'; | ||
|
|
||
| import { parseElasticsearchConfig } from './parse_elasticsearch_config'; | ||
|
|
||
| const parse = (config: any) => { | ||
| return parseElasticsearchConfig({ | ||
| get: () => config, | ||
| }); | ||
| }; | ||
|
|
||
| describe('reads files', () => { | ||
| beforeEach(() => { | ||
| mockReadFileSync.mockReset(); | ||
| mockReadFileSync.mockImplementation((path: string) => `content-of-${path}`); | ||
| mockReadPkcs12Keystore.mockReset(); | ||
| mockReadPkcs12Keystore.mockImplementation((path: string) => ({ | ||
| key: `content-of-${path}.key`, | ||
| cert: `content-of-${path}.cert`, | ||
| ca: [`content-of-${path}.ca`], | ||
| })); | ||
| mockReadPkcs12Truststore.mockReset(); | ||
| mockReadPkcs12Truststore.mockImplementation((path: string) => [`content-of-${path}`]); | ||
| }); | ||
|
|
||
| it('reads certificate authorities when ssl.keystore.path is specified', () => { | ||
| const configValue = parse({ ssl: { keystore: { path: 'some-path' } } }); | ||
| expect(mockReadPkcs12Keystore).toHaveBeenCalledTimes(1); | ||
| expect(configValue.ssl.certificateAuthorities).toEqual(['content-of-some-path.ca']); | ||
| }); | ||
|
|
||
| it('reads certificate authorities when ssl.truststore.path is specified', () => { | ||
| const configValue = parse({ ssl: { truststore: { path: 'some-path' } } }); | ||
| expect(mockReadPkcs12Truststore).toHaveBeenCalledTimes(1); | ||
| expect(configValue.ssl.certificateAuthorities).toEqual(['content-of-some-path']); | ||
| }); | ||
|
|
||
| it('reads certificate authorities when ssl.certificateAuthorities is specified', () => { | ||
| let configValue = parse({ ssl: { certificateAuthorities: 'some-path' } }); | ||
| expect(mockReadFileSync).toHaveBeenCalledTimes(1); | ||
| expect(configValue.ssl.certificateAuthorities).toEqual(['content-of-some-path']); | ||
|
|
||
| mockReadFileSync.mockClear(); | ||
| configValue = parse({ ssl: { certificateAuthorities: ['some-path'] } }); | ||
| expect(mockReadFileSync).toHaveBeenCalledTimes(1); | ||
| expect(configValue.ssl.certificateAuthorities).toEqual(['content-of-some-path']); | ||
|
|
||
| mockReadFileSync.mockClear(); | ||
| configValue = parse({ ssl: { certificateAuthorities: ['some-path', 'another-path'] } }); | ||
| expect(mockReadFileSync).toHaveBeenCalledTimes(2); | ||
| expect(configValue.ssl.certificateAuthorities).toEqual([ | ||
| 'content-of-some-path', | ||
| 'content-of-another-path', | ||
| ]); | ||
| }); | ||
|
|
||
| it('reads certificate authorities when ssl.keystore.path, ssl.truststore.path, and ssl.certificateAuthorities are specified', () => { | ||
| const configValue = parse({ | ||
| ssl: { | ||
| keystore: { path: 'some-path' }, | ||
| truststore: { path: 'another-path' }, | ||
| certificateAuthorities: 'yet-another-path', | ||
| }, | ||
| }); | ||
| expect(mockReadPkcs12Keystore).toHaveBeenCalledTimes(1); | ||
| expect(mockReadPkcs12Truststore).toHaveBeenCalledTimes(1); | ||
| expect(mockReadFileSync).toHaveBeenCalledTimes(1); | ||
| expect(configValue.ssl.certificateAuthorities).toEqual([ | ||
| 'content-of-some-path.ca', | ||
| 'content-of-another-path', | ||
| 'content-of-yet-another-path', | ||
| ]); | ||
| }); | ||
|
|
||
| it('reads a private key and certificate when ssl.keystore.path is specified', () => { | ||
| const configValue = parse({ ssl: { keystore: { path: 'some-path' } } }); | ||
| expect(mockReadPkcs12Keystore).toHaveBeenCalledTimes(1); | ||
| expect(configValue.ssl.key).toEqual('content-of-some-path.key'); | ||
| expect(configValue.ssl.certificate).toEqual('content-of-some-path.cert'); | ||
| }); | ||
|
|
||
| it('reads a private key when ssl.key is specified', () => { | ||
| const configValue = parse({ ssl: { key: 'some-path' } }); | ||
| expect(mockReadFileSync).toHaveBeenCalledTimes(1); | ||
| expect(configValue.ssl.key).toEqual('content-of-some-path'); | ||
| }); | ||
|
|
||
| it('reads a certificate when ssl.certificate is specified', () => { | ||
| const configValue = parse({ ssl: { certificate: 'some-path' } }); | ||
| expect(mockReadFileSync).toHaveBeenCalledTimes(1); | ||
| expect(configValue.ssl.certificate).toEqual('content-of-some-path'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('throws when config is invalid', () => { | ||
| beforeAll(() => { | ||
| const realFs = jest.requireActual('fs'); | ||
| mockReadFileSync.mockImplementation((path: string) => realFs.readFileSync(path)); | ||
| const utils = jest.requireActual('../../../../../../src/core/utils'); | ||
| mockReadPkcs12Keystore.mockImplementation((path: string, password?: string) => | ||
| utils.readPkcs12Keystore(path, password) | ||
| ); | ||
| mockReadPkcs12Truststore.mockImplementation((path: string, password?: string) => | ||
| utils.readPkcs12Truststore(path, password) | ||
| ); | ||
| }); | ||
|
|
||
| it('throws if key is invalid', () => { | ||
| const value = { ssl: { key: '/invalid/key' } }; | ||
| expect(() => parse(value)).toThrowErrorMatchingInlineSnapshot( | ||
| `"ENOENT: no such file or directory, open '/invalid/key'"` | ||
| ); | ||
| }); | ||
|
|
||
| it('throws if certificate is invalid', () => { | ||
| const value = { ssl: { certificate: '/invalid/cert' } }; | ||
| expect(() => parse(value)).toThrowErrorMatchingInlineSnapshot( | ||
| `"ENOENT: no such file or directory, open '/invalid/cert'"` | ||
| ); | ||
| }); | ||
|
|
||
| it('throws if certificateAuthorities is invalid', () => { | ||
| const value = { ssl: { certificateAuthorities: '/invalid/ca' } }; | ||
| expect(() => parse(value)).toThrowErrorMatchingInlineSnapshot( | ||
| `"ENOENT: no such file or directory, open '/invalid/ca'"` | ||
| ); | ||
| }); | ||
|
|
||
| it('throws if keystore path is invalid', () => { | ||
| const value = { ssl: { keystore: { path: '/invalid/keystore' } } }; | ||
| expect(() => parse(value)).toThrowErrorMatchingInlineSnapshot( | ||
| `"ENOENT: no such file or directory, open '/invalid/keystore'"` | ||
| ); | ||
| }); | ||
|
|
||
| it('throws if keystore does not contain a key', () => { | ||
| mockReadPkcs12Keystore.mockReturnValueOnce({}); | ||
| const value = { ssl: { keystore: { path: 'some-path' } } }; | ||
| expect(() => parse(value)).toThrowErrorMatchingInlineSnapshot( | ||
| `"Did not find key in Elasticsearch keystore."` | ||
| ); | ||
| }); | ||
|
|
||
| it('throws if keystore does not contain a certificate', () => { | ||
| mockReadPkcs12Keystore.mockReturnValueOnce({ key: 'foo' }); | ||
| const value = { ssl: { keystore: { path: 'some-path' } } }; | ||
| expect(() => parse(value)).toThrowErrorMatchingInlineSnapshot( | ||
| `"Did not find certificate in Elasticsearch keystore."` | ||
| ); | ||
| }); | ||
|
|
||
| it('throws if truststore path is invalid', () => { | ||
| const value = { ssl: { keystore: { path: '/invalid/truststore' } } }; | ||
| expect(() => parse(value)).toThrowErrorMatchingInlineSnapshot( | ||
| `"ENOENT: no such file or directory, open '/invalid/truststore'"` | ||
| ); | ||
| }); | ||
|
|
||
| it('throws if key and keystore.path are both specified', () => { | ||
| const value = { ssl: { key: 'foo', keystore: { path: 'bar' } } }; | ||
| expect(() => parse(value)).toThrowErrorMatchingInlineSnapshot( | ||
| `"[config validation of [xpack.monitoring.elasticsearch].ssl]: cannot use [key] when [keystore.path] is specified"` | ||
| ); | ||
| }); | ||
|
|
||
| it('throws if certificate and keystore.path are both specified', () => { | ||
| const value = { ssl: { certificate: 'foo', keystore: { path: 'bar' } } }; | ||
| expect(() => parse(value)).toThrowErrorMatchingInlineSnapshot( | ||
| `"[config validation of [xpack.monitoring.elasticsearch].ssl]: cannot use [certificate] when [keystore.path] is specified"` | ||
| ); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.