|
| 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 { configDeprecationFactory, applyDeprecations } from '@kbn/config'; |
| 8 | +import { securityConfigDeprecationProvider } from './config_deprecations'; |
| 9 | +import { cloneDeep } from 'lodash'; |
| 10 | + |
| 11 | +const applyConfigDeprecations = (settings: Record<string, any> = {}) => { |
| 12 | + const deprecations = securityConfigDeprecationProvider(configDeprecationFactory); |
| 13 | + const deprecationMessages: string[] = []; |
| 14 | + const migrated = applyDeprecations( |
| 15 | + settings, |
| 16 | + deprecations.map((deprecation) => ({ |
| 17 | + deprecation, |
| 18 | + path: 'xpack.security', |
| 19 | + })), |
| 20 | + (msg) => deprecationMessages.push(msg) |
| 21 | + ); |
| 22 | + return { |
| 23 | + messages: deprecationMessages, |
| 24 | + migrated, |
| 25 | + }; |
| 26 | +}; |
| 27 | + |
| 28 | +describe('Config Deprecations', () => { |
| 29 | + it('does not report deprecations for default configuration', () => { |
| 30 | + const defaultConfig = { xpack: { security: {} } }; |
| 31 | + const { messages, migrated } = applyConfigDeprecations(cloneDeep(defaultConfig)); |
| 32 | + expect(migrated).toEqual(defaultConfig); |
| 33 | + expect(messages).toHaveLength(0); |
| 34 | + }); |
| 35 | + |
| 36 | + it('renames sessionTimeout to session.idleTimeout', () => { |
| 37 | + const config = { |
| 38 | + xpack: { |
| 39 | + security: { |
| 40 | + sessionTimeout: 123, |
| 41 | + }, |
| 42 | + }, |
| 43 | + }; |
| 44 | + const { messages, migrated } = applyConfigDeprecations(cloneDeep(config)); |
| 45 | + expect(migrated.xpack.security.sessionTimeout).not.toBeDefined(); |
| 46 | + expect(migrated.xpack.security.session.idleTimeout).toEqual(123); |
| 47 | + expect(messages).toMatchInlineSnapshot(` |
| 48 | + Array [ |
| 49 | + "\\"xpack.security.sessionTimeout\\" is deprecated and has been replaced by \\"xpack.security.session.idleTimeout\\"", |
| 50 | + ] |
| 51 | + `); |
| 52 | + }); |
| 53 | + |
| 54 | + it(`warns that 'authorization.legacyFallback.enabled' is unused`, () => { |
| 55 | + const config = { |
| 56 | + xpack: { |
| 57 | + security: { |
| 58 | + authorization: { |
| 59 | + legacyFallback: { |
| 60 | + enabled: true, |
| 61 | + }, |
| 62 | + }, |
| 63 | + }, |
| 64 | + }, |
| 65 | + }; |
| 66 | + const { messages } = applyConfigDeprecations(cloneDeep(config)); |
| 67 | + expect(messages).toMatchInlineSnapshot(` |
| 68 | + Array [ |
| 69 | + "xpack.security.authorization.legacyFallback.enabled is deprecated and is no longer used", |
| 70 | + ] |
| 71 | + `); |
| 72 | + }); |
| 73 | + |
| 74 | + it(`warns that 'authc.saml.maxRedirectURLSize is unused`, () => { |
| 75 | + const config = { |
| 76 | + xpack: { |
| 77 | + security: { |
| 78 | + authc: { |
| 79 | + saml: { |
| 80 | + maxRedirectURLSize: 123, |
| 81 | + }, |
| 82 | + }, |
| 83 | + }, |
| 84 | + }, |
| 85 | + }; |
| 86 | + const { messages } = applyConfigDeprecations(cloneDeep(config)); |
| 87 | + expect(messages).toMatchInlineSnapshot(` |
| 88 | + Array [ |
| 89 | + "xpack.security.authc.saml.maxRedirectURLSize is deprecated and is no longer used", |
| 90 | + ] |
| 91 | + `); |
| 92 | + }); |
| 93 | + |
| 94 | + it(`warns that 'xpack.security.authc.providers.saml.<provider-name>.maxRedirectURLSize' is unused`, () => { |
| 95 | + const config = { |
| 96 | + xpack: { |
| 97 | + security: { |
| 98 | + authc: { |
| 99 | + providers: { |
| 100 | + saml: { |
| 101 | + saml1: { |
| 102 | + maxRedirectURLSize: 123, |
| 103 | + }, |
| 104 | + }, |
| 105 | + }, |
| 106 | + }, |
| 107 | + }, |
| 108 | + }, |
| 109 | + }; |
| 110 | + const { messages } = applyConfigDeprecations(cloneDeep(config)); |
| 111 | + expect(messages).toMatchInlineSnapshot(` |
| 112 | + Array [ |
| 113 | + "\`xpack.security.authc.providers.saml.<provider-name>.maxRedirectURLSize\` is deprecated and is no longer used", |
| 114 | + ] |
| 115 | + `); |
| 116 | + }); |
| 117 | + |
| 118 | + it(`warns when 'xpack.security.authc.providers' is an array of strings`, () => { |
| 119 | + const config = { |
| 120 | + xpack: { |
| 121 | + security: { |
| 122 | + authc: { |
| 123 | + providers: ['basic', 'saml'], |
| 124 | + }, |
| 125 | + }, |
| 126 | + }, |
| 127 | + }; |
| 128 | + const { messages, migrated } = applyConfigDeprecations(cloneDeep(config)); |
| 129 | + expect(migrated).toEqual(config); |
| 130 | + expect(messages).toMatchInlineSnapshot(` |
| 131 | + Array [ |
| 132 | + "Defining \`xpack.security.authc.providers\` as an array of provider types is deprecated. Use extended \`object\` format instead.", |
| 133 | + ] |
| 134 | + `); |
| 135 | + }); |
| 136 | + |
| 137 | + it(`warns when both the basic and token providers are enabled`, () => { |
| 138 | + const config = { |
| 139 | + xpack: { |
| 140 | + security: { |
| 141 | + authc: { |
| 142 | + providers: ['basic', 'token'], |
| 143 | + }, |
| 144 | + }, |
| 145 | + }, |
| 146 | + }; |
| 147 | + const { messages, migrated } = applyConfigDeprecations(cloneDeep(config)); |
| 148 | + expect(migrated).toEqual(config); |
| 149 | + expect(messages).toMatchInlineSnapshot(` |
| 150 | + Array [ |
| 151 | + "Defining \`xpack.security.authc.providers\` as an array of provider types is deprecated. Use extended \`object\` format instead.", |
| 152 | + "Enabling both \`basic\` and \`token\` authentication providers in \`xpack.security.authc.providers\` is deprecated. Login page will only use \`token\` provider.", |
| 153 | + ] |
| 154 | + `); |
| 155 | + }); |
| 156 | + |
| 157 | + it('warns when the security plugin is disabled', () => { |
| 158 | + const config = { |
| 159 | + xpack: { |
| 160 | + security: { |
| 161 | + enabled: false, |
| 162 | + }, |
| 163 | + }, |
| 164 | + }; |
| 165 | + const { messages, migrated } = applyConfigDeprecations(cloneDeep(config)); |
| 166 | + expect(migrated).toEqual(config); |
| 167 | + expect(messages).toMatchInlineSnapshot(` |
| 168 | + Array [ |
| 169 | + "Disabling the security plugin (\`xpack.security.enabled\`) will not be supported in the next major version (8.0). To turn off security features, disable them in Elasticsearch instead.", |
| 170 | + ] |
| 171 | + `); |
| 172 | + }); |
| 173 | + |
| 174 | + it('does not warn when the security plugin is enabled', () => { |
| 175 | + const config = { |
| 176 | + xpack: { |
| 177 | + security: { |
| 178 | + enabled: true, |
| 179 | + }, |
| 180 | + }, |
| 181 | + }; |
| 182 | + const { messages, migrated } = applyConfigDeprecations(cloneDeep(config)); |
| 183 | + expect(migrated).toEqual(config); |
| 184 | + expect(messages).toHaveLength(0); |
| 185 | + }); |
| 186 | +}); |
0 commit comments