|
| 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 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +import { |
| 9 | + SecurityGetRoleMappingResponse, |
| 10 | + SecurityGetUserResponse, |
| 11 | +} from '@elastic/elasticsearch/lib/api/types'; |
| 12 | +import type { |
| 13 | + CoreSetup, |
| 14 | + DeprecationsDetails, |
| 15 | + DocLinksServiceSetup, |
| 16 | + ElasticsearchClient, |
| 17 | + GetDeprecationsContext, |
| 18 | +} from '@kbn/core/server'; |
| 19 | +import { i18n } from '@kbn/i18n'; |
| 20 | +import type { DeprecationApmDeps } from '.'; |
| 21 | +import { deprecations } from '../lib/deprecations'; |
| 22 | + |
| 23 | +const APM_USER_ROLE_NAME = 'apm_user'; |
| 24 | +const getKibanaPrivilegesDocumentationUrl = (branch: string) => { |
| 25 | + return `https://www.elastic.co/guide/en/kibana/${branch}/kibana-privileges.html`; |
| 26 | +}; |
| 27 | + |
| 28 | +export async function getDeprecationsInfo( |
| 29 | + { esClient }: GetDeprecationsContext, |
| 30 | + core: CoreSetup, |
| 31 | + apmDeps: DeprecationApmDeps |
| 32 | +) { |
| 33 | + const client = esClient.asCurrentUser; |
| 34 | + const { docLinks } = core; |
| 35 | + const { security } = apmDeps; |
| 36 | + |
| 37 | + // Nothing to do if security is disabled |
| 38 | + if (!security?.license.isEnabled()) { |
| 39 | + return []; |
| 40 | + } |
| 41 | + |
| 42 | + const [userDeprecations, roleMappingDeprecations] = await Promise.all([ |
| 43 | + getUsersDeprecations(client, apmDeps, docLinks), |
| 44 | + getRoleMappingsDeprecations(client, apmDeps, docLinks), |
| 45 | + ]); |
| 46 | + |
| 47 | + return [...userDeprecations, ...roleMappingDeprecations]; |
| 48 | +} |
| 49 | + |
| 50 | +async function getUsersDeprecations( |
| 51 | + client: ElasticsearchClient, |
| 52 | + apmDeps: DeprecationApmDeps, |
| 53 | + docLinks: DocLinksServiceSetup |
| 54 | +): Promise<DeprecationsDetails[]> { |
| 55 | + const title = i18n.translate('xpack.apm.deprecations.apmUser.title', { |
| 56 | + defaultMessage: `Check for users assigned the deprecated "{apmUserRoleName}" role`, |
| 57 | + values: { apmUserRoleName: APM_USER_ROLE_NAME }, |
| 58 | + }); |
| 59 | + |
| 60 | + let users: SecurityGetUserResponse; |
| 61 | + try { |
| 62 | + users = await client.security.getUser(); |
| 63 | + } catch (err) { |
| 64 | + const { logger } = apmDeps; |
| 65 | + if (deprecations.getErrorStatusCode(err) === 403) { |
| 66 | + logger.warn( |
| 67 | + 'Failed to retrieve users when checking for deprecations: the "read_security" or "manage_security" cluster privilege is required.' |
| 68 | + ); |
| 69 | + } else { |
| 70 | + logger.error( |
| 71 | + `Failed to retrieve users when checking for deprecations, unexpected error: ${deprecations.getDetailedErrorMessage( |
| 72 | + err |
| 73 | + )}.` |
| 74 | + ); |
| 75 | + } |
| 76 | + return deprecations.deprecationError(title, err, docLinks); |
| 77 | + } |
| 78 | + |
| 79 | + const apmUsers = Object.values(users).flatMap((user) => |
| 80 | + user.roles.find(hasApmUserRole) ? user.username : [] |
| 81 | + ); |
| 82 | + |
| 83 | + if (apmUsers.length === 0) { |
| 84 | + return []; |
| 85 | + } |
| 86 | + |
| 87 | + return [ |
| 88 | + { |
| 89 | + title, |
| 90 | + message: i18n.translate('xpack.apm.deprecations.apmUser.description', { |
| 91 | + defaultMessage: `The "{apmUserRoleName}" role has been deprecated. Remove the "{apmUserRoleName}" role from affected users in this cluster including: {users}`, |
| 92 | + values: { apmUserRoleName: APM_USER_ROLE_NAME, users: apmUsers.join() }, |
| 93 | + }), |
| 94 | + correctiveActions: { |
| 95 | + manualSteps: [ |
| 96 | + i18n.translate('xpack.apm.deprecations.apmUser.manualStepOne', { |
| 97 | + defaultMessage: `Go to Management > Security > Users to find users with the "{apmUserRoleName}" role.`, |
| 98 | + values: { apmUserRoleName: APM_USER_ROLE_NAME }, |
| 99 | + }), |
| 100 | + i18n.translate('xpack.apm.deprecations.apmUser.manualStepTwo', { |
| 101 | + defaultMessage: |
| 102 | + 'Remove the "{apmUserRoleName}" role from all users and add the built-in "viewer" role.', |
| 103 | + values: { apmUserRoleName: APM_USER_ROLE_NAME }, |
| 104 | + }), |
| 105 | + ], |
| 106 | + }, |
| 107 | + level: 'critical', |
| 108 | + deprecationType: 'feature', |
| 109 | + documentationUrl: getKibanaPrivilegesDocumentationUrl(docLinks.version), |
| 110 | + }, |
| 111 | + ]; |
| 112 | +} |
| 113 | + |
| 114 | +async function getRoleMappingsDeprecations( |
| 115 | + client: ElasticsearchClient, |
| 116 | + apmDeps: DeprecationApmDeps, |
| 117 | + docLinks: DocLinksServiceSetup |
| 118 | +): Promise<DeprecationsDetails[]> { |
| 119 | + const title = i18n.translate('xpack.apm.deprecations.apmUserRoleMappings.title', { |
| 120 | + defaultMessage: `Check for role mappings using the deprecated "{apmUserRoleName}" role`, |
| 121 | + values: { apmUserRoleName: APM_USER_ROLE_NAME }, |
| 122 | + }); |
| 123 | + |
| 124 | + let roleMappings: SecurityGetRoleMappingResponse; |
| 125 | + try { |
| 126 | + roleMappings = await client.security.getRoleMapping(); |
| 127 | + } catch (err) { |
| 128 | + const { logger } = apmDeps; |
| 129 | + if (deprecations.getErrorStatusCode(err) === 403) { |
| 130 | + logger.warn( |
| 131 | + 'Failed to retrieve role mappings when checking for deprecations: the "manage_security" cluster privilege is required.' |
| 132 | + ); |
| 133 | + } else { |
| 134 | + logger.error( |
| 135 | + `Failed to retrieve role mappings when checking for deprecations, unexpected error: ${deprecations.getDetailedErrorMessage( |
| 136 | + err |
| 137 | + )}.` |
| 138 | + ); |
| 139 | + } |
| 140 | + return deprecations.deprecationError(title, err, docLinks); |
| 141 | + } |
| 142 | + |
| 143 | + const roleMappingsWithApmUserRole = Object.entries(roleMappings).flatMap(([roleName, role]) => |
| 144 | + role.roles?.find(hasApmUserRole) ? roleName : [] |
| 145 | + ); |
| 146 | + |
| 147 | + if (roleMappingsWithApmUserRole.length === 0) { |
| 148 | + return []; |
| 149 | + } |
| 150 | + |
| 151 | + return [ |
| 152 | + { |
| 153 | + title, |
| 154 | + message: i18n.translate('xpack.apm.deprecations.apmUserRoleMappings.description', { |
| 155 | + defaultMessage: `The "{apmUserRoleName}" role has been deprecated. Remove the "{apmUserRoleName}" role from affected role mappings in this cluster including: {roles}`, |
| 156 | + values: { |
| 157 | + apmUserRoleName: APM_USER_ROLE_NAME, |
| 158 | + roles: roleMappingsWithApmUserRole.join(), |
| 159 | + }, |
| 160 | + }), |
| 161 | + correctiveActions: { |
| 162 | + manualSteps: [ |
| 163 | + i18n.translate('xpack.apm.deprecations.apmUserRoleMappings.manualStepOne', { |
| 164 | + defaultMessage: `Go to Management > Security > Role Mappings to find roles mappings with the "{apmUserRoleName}" role.`, |
| 165 | + values: { apmUserRoleName: APM_USER_ROLE_NAME }, |
| 166 | + }), |
| 167 | + i18n.translate('xpack.apm.deprecations.apmUserRoleMappings.manualStepTwo', { |
| 168 | + defaultMessage: |
| 169 | + 'Remove the "{apmUserRoleName}" role from all role mappings and add the built-in "viewer" role', |
| 170 | + values: { apmUserRoleName: APM_USER_ROLE_NAME }, |
| 171 | + }), |
| 172 | + ], |
| 173 | + }, |
| 174 | + level: 'critical', |
| 175 | + deprecationType: 'feature', |
| 176 | + documentationUrl: getKibanaPrivilegesDocumentationUrl(docLinks.version), |
| 177 | + }, |
| 178 | + ]; |
| 179 | +} |
| 180 | + |
| 181 | +const hasApmUserRole = (role: string) => role === APM_USER_ROLE_NAME; |
0 commit comments