|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | + |
| 7 | +import { |
| 8 | + mockReadFileSync, |
| 9 | + mockReadPkcs12Keystore, |
| 10 | + mockReadPkcs12Truststore, |
| 11 | +} from './parse_elasticsearch_config.test.mocks'; |
| 12 | + |
| 13 | +import { parseElasticsearchConfig } from './parse_elasticsearch_config'; |
| 14 | + |
| 15 | +const parse = (config: any) => { |
| 16 | + return parseElasticsearchConfig({ |
| 17 | + get: () => config, |
| 18 | + }); |
| 19 | +}; |
| 20 | + |
| 21 | +describe('reads files', () => { |
| 22 | + beforeEach(() => { |
| 23 | + mockReadFileSync.mockReset(); |
| 24 | + mockReadFileSync.mockImplementation((path: string) => `content-of-${path}`); |
| 25 | + mockReadPkcs12Keystore.mockReset(); |
| 26 | + mockReadPkcs12Keystore.mockImplementation((path: string) => ({ |
| 27 | + key: `content-of-${path}.key`, |
| 28 | + cert: `content-of-${path}.cert`, |
| 29 | + ca: [`content-of-${path}.ca`], |
| 30 | + })); |
| 31 | + mockReadPkcs12Truststore.mockReset(); |
| 32 | + mockReadPkcs12Truststore.mockImplementation((path: string) => [`content-of-${path}`]); |
| 33 | + }); |
| 34 | + |
| 35 | + it('reads certificate authorities when ssl.keystore.path is specified', () => { |
| 36 | + const configValue = parse({ ssl: { keystore: { path: 'some-path' } } }); |
| 37 | + expect(mockReadPkcs12Keystore).toHaveBeenCalledTimes(1); |
| 38 | + expect(configValue.ssl.certificateAuthorities).toEqual(['content-of-some-path.ca']); |
| 39 | + }); |
| 40 | + |
| 41 | + it('reads certificate authorities when ssl.truststore.path is specified', () => { |
| 42 | + const configValue = parse({ ssl: { truststore: { path: 'some-path' } } }); |
| 43 | + expect(mockReadPkcs12Truststore).toHaveBeenCalledTimes(1); |
| 44 | + expect(configValue.ssl.certificateAuthorities).toEqual(['content-of-some-path']); |
| 45 | + }); |
| 46 | + |
| 47 | + it('reads certificate authorities when ssl.certificateAuthorities is specified', () => { |
| 48 | + let configValue = parse({ ssl: { certificateAuthorities: 'some-path' } }); |
| 49 | + expect(mockReadFileSync).toHaveBeenCalledTimes(1); |
| 50 | + expect(configValue.ssl.certificateAuthorities).toEqual(['content-of-some-path']); |
| 51 | + |
| 52 | + mockReadFileSync.mockClear(); |
| 53 | + configValue = parse({ ssl: { certificateAuthorities: ['some-path'] } }); |
| 54 | + expect(mockReadFileSync).toHaveBeenCalledTimes(1); |
| 55 | + expect(configValue.ssl.certificateAuthorities).toEqual(['content-of-some-path']); |
| 56 | + |
| 57 | + mockReadFileSync.mockClear(); |
| 58 | + configValue = parse({ ssl: { certificateAuthorities: ['some-path', 'another-path'] } }); |
| 59 | + expect(mockReadFileSync).toHaveBeenCalledTimes(2); |
| 60 | + expect(configValue.ssl.certificateAuthorities).toEqual([ |
| 61 | + 'content-of-some-path', |
| 62 | + 'content-of-another-path', |
| 63 | + ]); |
| 64 | + }); |
| 65 | + |
| 66 | + it('reads certificate authorities when ssl.keystore.path, ssl.truststore.path, and ssl.certificateAuthorities are specified', () => { |
| 67 | + const configValue = parse({ |
| 68 | + ssl: { |
| 69 | + keystore: { path: 'some-path' }, |
| 70 | + truststore: { path: 'another-path' }, |
| 71 | + certificateAuthorities: 'yet-another-path', |
| 72 | + }, |
| 73 | + }); |
| 74 | + expect(mockReadPkcs12Keystore).toHaveBeenCalledTimes(1); |
| 75 | + expect(mockReadPkcs12Truststore).toHaveBeenCalledTimes(1); |
| 76 | + expect(mockReadFileSync).toHaveBeenCalledTimes(1); |
| 77 | + expect(configValue.ssl.certificateAuthorities).toEqual([ |
| 78 | + 'content-of-some-path.ca', |
| 79 | + 'content-of-another-path', |
| 80 | + 'content-of-yet-another-path', |
| 81 | + ]); |
| 82 | + }); |
| 83 | + |
| 84 | + it('reads a private key and certificate when ssl.keystore.path is specified', () => { |
| 85 | + const configValue = parse({ ssl: { keystore: { path: 'some-path' } } }); |
| 86 | + expect(mockReadPkcs12Keystore).toHaveBeenCalledTimes(1); |
| 87 | + expect(configValue.ssl.key).toEqual('content-of-some-path.key'); |
| 88 | + expect(configValue.ssl.certificate).toEqual('content-of-some-path.cert'); |
| 89 | + }); |
| 90 | + |
| 91 | + it('reads a private key when ssl.key is specified', () => { |
| 92 | + const configValue = parse({ ssl: { key: 'some-path' } }); |
| 93 | + expect(mockReadFileSync).toHaveBeenCalledTimes(1); |
| 94 | + expect(configValue.ssl.key).toEqual('content-of-some-path'); |
| 95 | + }); |
| 96 | + |
| 97 | + it('reads a certificate when ssl.certificate is specified', () => { |
| 98 | + const configValue = parse({ ssl: { certificate: 'some-path' } }); |
| 99 | + expect(mockReadFileSync).toHaveBeenCalledTimes(1); |
| 100 | + expect(configValue.ssl.certificate).toEqual('content-of-some-path'); |
| 101 | + }); |
| 102 | +}); |
| 103 | + |
| 104 | +describe('throws when config is invalid', () => { |
| 105 | + beforeAll(() => { |
| 106 | + const realFs = jest.requireActual('fs'); |
| 107 | + mockReadFileSync.mockImplementation((path: string) => realFs.readFileSync(path)); |
| 108 | + const utils = jest.requireActual('../../../../../../src/core/utils'); |
| 109 | + mockReadPkcs12Keystore.mockImplementation((path: string, password?: string) => |
| 110 | + utils.readPkcs12Keystore(path, password) |
| 111 | + ); |
| 112 | + mockReadPkcs12Truststore.mockImplementation((path: string, password?: string) => |
| 113 | + utils.readPkcs12Truststore(path, password) |
| 114 | + ); |
| 115 | + }); |
| 116 | + |
| 117 | + it('throws if key is invalid', () => { |
| 118 | + const value = { ssl: { key: '/invalid/key' } }; |
| 119 | + expect(() => parse(value)).toThrowErrorMatchingInlineSnapshot( |
| 120 | + `"ENOENT: no such file or directory, open '/invalid/key'"` |
| 121 | + ); |
| 122 | + }); |
| 123 | + |
| 124 | + it('throws if certificate is invalid', () => { |
| 125 | + const value = { ssl: { certificate: '/invalid/cert' } }; |
| 126 | + expect(() => parse(value)).toThrowErrorMatchingInlineSnapshot( |
| 127 | + `"ENOENT: no such file or directory, open '/invalid/cert'"` |
| 128 | + ); |
| 129 | + }); |
| 130 | + |
| 131 | + it('throws if certificateAuthorities is invalid', () => { |
| 132 | + const value = { ssl: { certificateAuthorities: '/invalid/ca' } }; |
| 133 | + expect(() => parse(value)).toThrowErrorMatchingInlineSnapshot( |
| 134 | + `"ENOENT: no such file or directory, open '/invalid/ca'"` |
| 135 | + ); |
| 136 | + }); |
| 137 | + |
| 138 | + it('throws if keystore path is invalid', () => { |
| 139 | + const value = { ssl: { keystore: { path: '/invalid/keystore' } } }; |
| 140 | + expect(() => parse(value)).toThrowErrorMatchingInlineSnapshot( |
| 141 | + `"ENOENT: no such file or directory, open '/invalid/keystore'"` |
| 142 | + ); |
| 143 | + }); |
| 144 | + |
| 145 | + it('throws if keystore does not contain a key', () => { |
| 146 | + mockReadPkcs12Keystore.mockReturnValueOnce({}); |
| 147 | + const value = { ssl: { keystore: { path: 'some-path' } } }; |
| 148 | + expect(() => parse(value)).toThrowErrorMatchingInlineSnapshot( |
| 149 | + `"Did not find key in Elasticsearch keystore."` |
| 150 | + ); |
| 151 | + }); |
| 152 | + |
| 153 | + it('throws if keystore does not contain a certificate', () => { |
| 154 | + mockReadPkcs12Keystore.mockReturnValueOnce({ key: 'foo' }); |
| 155 | + const value = { ssl: { keystore: { path: 'some-path' } } }; |
| 156 | + expect(() => parse(value)).toThrowErrorMatchingInlineSnapshot( |
| 157 | + `"Did not find certificate in Elasticsearch keystore."` |
| 158 | + ); |
| 159 | + }); |
| 160 | + |
| 161 | + it('throws if truststore path is invalid', () => { |
| 162 | + const value = { ssl: { keystore: { path: '/invalid/truststore' } } }; |
| 163 | + expect(() => parse(value)).toThrowErrorMatchingInlineSnapshot( |
| 164 | + `"ENOENT: no such file or directory, open '/invalid/truststore'"` |
| 165 | + ); |
| 166 | + }); |
| 167 | + |
| 168 | + it('throws if key and keystore.path are both specified', () => { |
| 169 | + const value = { ssl: { key: 'foo', keystore: { path: 'bar' } } }; |
| 170 | + expect(() => parse(value)).toThrowErrorMatchingInlineSnapshot( |
| 171 | + `"[config validation of [xpack.monitoring.elasticsearch].ssl]: cannot use [key] when [keystore.path] is specified"` |
| 172 | + ); |
| 173 | + }); |
| 174 | + |
| 175 | + it('throws if certificate and keystore.path are both specified', () => { |
| 176 | + const value = { ssl: { certificate: 'foo', keystore: { path: 'bar' } } }; |
| 177 | + expect(() => parse(value)).toThrowErrorMatchingInlineSnapshot( |
| 178 | + `"[config validation of [xpack.monitoring.elasticsearch].ssl]: cannot use [certificate] when [keystore.path] is specified"` |
| 179 | + ); |
| 180 | + }); |
| 181 | +}); |
0 commit comments