-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
Omit runtime fields from FLS suggestions #78330
Merged
legrego
merged 6 commits into
elastic:master
from
legrego:security/fls-omit-runtime-fields
Oct 1, 2020
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
946c169
Omit runtime fields from FLS suggestions
legrego 026eea6
reenable test suites
legrego 5d82c6f
Merge branch 'master' into security/fls-omit-runtime-fields
elasticmachine 2f7c6dd
Apply suggestions from code review
legrego 71c28d2
Merge branch 'master' of github.com:elastic/kibana into security/fls-…
legrego c99fd72
adding unit test
legrego 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
58 changes: 58 additions & 0 deletions
58
x-pack/plugins/security/server/routes/indices/get_fields.test.ts
This file contains 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,58 @@ | ||
/* | ||
* 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 { httpServerMock, elasticsearchServiceMock } from '../../../../../../src/core/server/mocks'; | ||
import { kibanaResponseFactory } from '../../../../../../src/core/server'; | ||
|
||
import { routeDefinitionParamsMock } from '../index.mock'; | ||
import { defineGetFieldsRoutes } from './get_fields'; | ||
|
||
const createFieldMapping = (field: string, type: string) => ({ | ||
[field]: { mapping: { [field]: { type } } }, | ||
}); | ||
|
||
const createEmptyFieldMapping = (field: string) => ({ [field]: { mapping: {} } }); | ||
|
||
const mockFieldMappingResponse = { | ||
foo: { | ||
mappings: { | ||
...createFieldMapping('fooField', 'keyword'), | ||
...createFieldMapping('commonField', 'keyword'), | ||
...createEmptyFieldMapping('emptyField'), | ||
}, | ||
}, | ||
bar: { | ||
mappings: { | ||
...createFieldMapping('commonField', 'keyword'), | ||
...createFieldMapping('barField', 'keyword'), | ||
...createFieldMapping('runtimeField', 'runtime'), | ||
}, | ||
}, | ||
}; | ||
|
||
describe('GET /internal/security/fields/{query}', () => { | ||
it('returns a list of deduplicated fields, omitting empty and runtime fields', async () => { | ||
const mockRouteDefinitionParams = routeDefinitionParamsMock.create(); | ||
|
||
const scopedClient = elasticsearchServiceMock.createLegacyScopedClusterClient(); | ||
scopedClient.callAsCurrentUser.mockResolvedValue(mockFieldMappingResponse); | ||
mockRouteDefinitionParams.clusterClient.asScoped.mockReturnValue(scopedClient); | ||
|
||
defineGetFieldsRoutes(mockRouteDefinitionParams); | ||
|
||
const [[, handler]] = mockRouteDefinitionParams.router.get.mock.calls; | ||
|
||
const headers = { authorization: 'foo' }; | ||
const mockRequest = httpServerMock.createKibanaRequest({ | ||
method: 'get', | ||
path: `/internal/security/fields/foo`, | ||
headers, | ||
}); | ||
const response = await handler({} as any, mockRequest, kibanaResponseFactory); | ||
expect(response.status).toBe(200); | ||
expect(response.payload).toEqual(['fooField', 'commonField', 'barField']); | ||
}); | ||
}); |
This file contains 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 |
---|---|---|
|
@@ -8,6 +8,20 @@ import { schema } from '@kbn/config-schema'; | |
import { RouteDefinitionParams } from '../index'; | ||
import { wrapIntoCustomErrorResponse } from '../../errors'; | ||
|
||
interface FieldMappingResponse { | ||
[indexName: string]: { | ||
mappings: { | ||
[fieldName: string]: { | ||
mapping: { | ||
[fieldName: string]: { | ||
type: string; | ||
}; | ||
}; | ||
}; | ||
}; | ||
}; | ||
} | ||
|
||
export function defineGetFieldsRoutes({ router, clusterClient }: RouteDefinitionParams) { | ||
router.get( | ||
{ | ||
|
@@ -23,21 +37,35 @@ export function defineGetFieldsRoutes({ router, clusterClient }: RouteDefinition | |
fields: '*', | ||
allowNoIndices: false, | ||
includeDefaults: true, | ||
})) as Record<string, { mappings: Record<string, unknown> }>; | ||
})) as FieldMappingResponse; | ||
|
||
// The flow is the following (see response format at https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-field-mapping.html): | ||
// 1. Iterate over all matched indices. | ||
// 2. Extract all the field names from the `mappings` field of the particular index. | ||
// 3. Collect and flatten the list of the field names. | ||
// 3. Collect and flatten the list of the field names, omitting any fields without mappings, and any runtime fields | ||
// 4. Use `Set` to get only unique field names. | ||
const fields = Array.from( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question: how do you feel about adding a simple jest test to test this logic? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea, will do! |
||
new Set( | ||
Object.values(indexMappings).flatMap((indexMapping) => { | ||
return Object.keys(indexMapping.mappings).filter((fieldName) => { | ||
const mappingValues = Object.values(indexMapping.mappings[fieldName].mapping); | ||
const hasMapping = mappingValues.length > 0; | ||
|
||
const isRuntimeField = hasMapping && mappingValues[0]?.type === 'runtime'; | ||
|
||
// fields without mappings are internal fields such as `_routing` and `_index`, | ||
// and therefore don't make sense as autocomplete suggestions for FLS. | ||
|
||
// Runtime fields are not securable via FLS. | ||
// Administrators should instead secure access to the fields which derive this information. | ||
return hasMapping && !isRuntimeField; | ||
}); | ||
}) | ||
) | ||
); | ||
|
||
return response.ok({ | ||
body: Array.from( | ||
new Set( | ||
Object.values(indexMappings) | ||
.map((indexMapping) => Object.keys(indexMapping.mappings)) | ||
.flat() | ||
) | ||
), | ||
body: fields, | ||
}); | ||
} catch (error) { | ||
return response.customError(wrapIntoCustomErrorResponse(error)); | ||
|
This file contains 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 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Improved type safety, at least until we can migrate away from the legacy ES client.