-
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
Logging legacy fallback deprecation warning on login #20493
Merged
kobelb
merged 15 commits into
elastic:security-app-privs
from
kobelb:login-deprecation-log
Jul 6, 2018
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
1db42a4
Logging legacy fallback deprecation on login
kobelb 27211ff
Consolidation the privileges/authorization folder
kobelb a1d65fa
Exposing rudimentary authorization service and fixing authenticate tests
kobelb 3f4a948
Moving authorization services configuration to initAuthorization
kobelb 9723082
Adding "actions" service exposed by the authorization
kobelb 0e63c72
Fixing misspelling
kobelb 65044c5
Merge remote-tracking branch 'upstream/security-app-privs' into login…
kobelb d9732c0
Removing invalid and unused exports
kobelb 8468018
Adding note about only adding privileges
kobelb e7c4ac8
Merge remote-tracking branch 'upstream/security-app-privs' into login…
kobelb 7a5eeb1
Calling it initAuthorizationService
kobelb 2c003b4
Throwing explicit validation error in actions.getSavedObjectAction
kobelb bf79dda
Deep freezing authorization service
kobelb dc2c984
Adding deepFreeze tests
kobelb c8f828e
Checking privileges in one call and cleaning up tests
kobelb 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 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
25 changes: 25 additions & 0 deletions
25
x-pack/plugins/security/server/lib/authorization/__snapshots__/actions.test.js.snap
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,25 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`#getSavedObjectAction() action of "" throws error 1`] = `"action is required and must be a string"`; | ||
|
||
exports[`#getSavedObjectAction() action of {} throws error 1`] = `"action is required and must be a string"`; | ||
|
||
exports[`#getSavedObjectAction() action of 1 throws error 1`] = `"action is required and must be a string"`; | ||
|
||
exports[`#getSavedObjectAction() action of null throws error 1`] = `"action is required and must be a string"`; | ||
|
||
exports[`#getSavedObjectAction() action of true throws error 1`] = `"action is required and must be a string"`; | ||
|
||
exports[`#getSavedObjectAction() action of undefined throws error 1`] = `"action is required and must be a string"`; | ||
|
||
exports[`#getSavedObjectAction() type of "" throws error 1`] = `"type is required and must be a string"`; | ||
|
||
exports[`#getSavedObjectAction() type of {} throws error 1`] = `"type is required and must be a string"`; | ||
|
||
exports[`#getSavedObjectAction() type of 1 throws error 1`] = `"type is required and must be a string"`; | ||
|
||
exports[`#getSavedObjectAction() type of null throws error 1`] = `"type is required and must be a string"`; | ||
|
||
exports[`#getSavedObjectAction() type of true throws error 1`] = `"type is required and must be a string"`; | ||
|
||
exports[`#getSavedObjectAction() type of undefined throws error 1`] = `"type is required and must be a string"`; |
4 changes: 3 additions & 1 deletion
4
x-pack/plugins/security/server/lib/authorization/__snapshots__/check_privileges.test.js.snap
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`throws error if missing version privilege and has login privilege 1`] = `"Multiple versions of Kibana are running against the same Elasticsearch cluster, unable to authorize user."`; | ||
exports[`with index privileges throws error if missing version privilege and has login privilege 1`] = `[Error: Multiple versions of Kibana are running against the same Elasticsearch cluster, unable to authorize user.]`; | ||
|
||
exports[`with no index privileges throws error if missing version privilege and has login privilege 1`] = `[Error: Multiple versions of Kibana are running against the same Elasticsearch cluster, unable to authorize user.]`; |
7 changes: 7 additions & 0 deletions
7
x-pack/plugins/security/server/lib/authorization/__snapshots__/init.test.js.snap
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,7 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`deep freezes exposed service 1`] = `"Cannot delete property 'checkPrivilegesWithRequest' of #<Object>"`; | ||
|
||
exports[`deep freezes exposed service 2`] = `"Cannot add property foo, object is not extensible"`; | ||
|
||
exports[`deep freezes exposed service 3`] = `"Cannot assign to read only property 'login' of object '#<Object>'"`; |
26 changes: 26 additions & 0 deletions
26
x-pack/plugins/security/server/lib/authorization/actions.js
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,26 @@ | ||
/* | ||
* 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 { isString } from 'lodash'; | ||
|
||
export function actionsFactory(config) { | ||
const kibanaVersion = config.get('pkg.version'); | ||
|
||
return { | ||
getSavedObjectAction(type, action) { | ||
if (!type || !isString(type)) { | ||
throw new Error('type is required and must be a string'); | ||
} | ||
|
||
if (!action || !isString(action)) { | ||
throw new Error('action is required and must be a string'); | ||
} | ||
|
||
return `action:saved_objects/${type}/${action}`; | ||
}, | ||
login: `action:login`, | ||
version: `version:${kibanaVersion}`, | ||
}; | ||
} |
69 changes: 69 additions & 0 deletions
69
x-pack/plugins/security/server/lib/authorization/actions.test.js
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,69 @@ | ||
/* | ||
* 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 { actionsFactory } from './actions'; | ||
|
||
const createMockConfig = (settings = {}) => { | ||
const mockConfig = { | ||
get: jest.fn() | ||
}; | ||
|
||
mockConfig.get.mockImplementation(key => settings[key]); | ||
|
||
return mockConfig; | ||
}; | ||
|
||
describe('#login', () => { | ||
test('returns action:login', () => { | ||
const mockConfig = createMockConfig(); | ||
|
||
const actions = actionsFactory(mockConfig); | ||
|
||
expect(actions.login).toEqual('action:login'); | ||
}); | ||
}); | ||
|
||
describe('#version', () => { | ||
test(`returns version:\${config.get('pkg.version')}`, () => { | ||
const version = 'mock-version'; | ||
const mockConfig = createMockConfig({ 'pkg.version': version }); | ||
|
||
const actions = actionsFactory(mockConfig); | ||
|
||
expect(actions.version).toEqual(`version:${version}`); | ||
}); | ||
}); | ||
|
||
describe('#getSavedObjectAction()', () => { | ||
test('uses type and action to build action', () => { | ||
const mockConfig = createMockConfig(); | ||
const actions = actionsFactory(mockConfig); | ||
const type = 'saved-object-type'; | ||
const action = 'saved-object-action'; | ||
|
||
const result = actions.getSavedObjectAction(type, action); | ||
|
||
expect(result).toEqual(`action:saved_objects/${type}/${action}`); | ||
}); | ||
|
||
[null, undefined, '', 1, true, {}].forEach(type => { | ||
test(`type of ${JSON.stringify(type)} throws error`, () => { | ||
const mockConfig = createMockConfig(); | ||
const actions = actionsFactory(mockConfig); | ||
|
||
expect(() => actions.getSavedObjectAction(type, 'saved-object-action')).toThrowErrorMatchingSnapshot(); | ||
}); | ||
}); | ||
|
||
[null, undefined, '', 1, true, {}].forEach(action => { | ||
test(`action of ${JSON.stringify(action)} throws error`, () => { | ||
const mockConfig = createMockConfig(); | ||
const actions = actionsFactory(mockConfig); | ||
|
||
expect(() => actions.getSavedObjectAction('saved-object-type', action)).toThrowErrorMatchingSnapshot(); | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
|
@@ -4,103 +4,82 @@ | |
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { getClient } from '../../../../../server/lib/get_client_shield'; | ||
import { uniq } from 'lodash'; | ||
import { ALL_RESOURCE } from '../../../common/constants'; | ||
import { getVersionAction, getLoginAction } from '../privileges'; | ||
|
||
export const CHECK_PRIVILEGES_RESULT = { | ||
UNAUTHORIZED: Symbol(), | ||
AUTHORIZED: Symbol(), | ||
LEGACY: Symbol(), | ||
UNAUTHORIZED: Symbol('Unauthorized'), | ||
AUTHORIZED: Symbol('Authorized'), | ||
LEGACY: Symbol('Legacy'), | ||
}; | ||
|
||
export function checkPrivilegesWithRequestFactory(server) { | ||
const callWithRequest = getClient(server).callWithRequest; | ||
export function checkPrivilegesWithRequestFactory(shieldClient, config, actions) { | ||
const { callWithRequest } = shieldClient; | ||
|
||
const config = server.config(); | ||
const kibanaVersion = config.get('pkg.version'); | ||
const application = config.get('xpack.security.rbac.application'); | ||
const kibanaIndex = config.get('kibana.index'); | ||
|
||
const loginAction = getLoginAction(); | ||
const versionAction = getVersionAction(kibanaVersion); | ||
const hasIncompatibileVersion = (applicationPrivilegesResponse) => { | ||
return !applicationPrivilegesResponse[actions.version] && applicationPrivilegesResponse[actions.login]; | ||
}; | ||
|
||
return function checkPrivilegesWithRequest(request) { | ||
const hasAllApplicationPrivileges = (applicationPrivilegesResponse) => { | ||
return Object.values(applicationPrivilegesResponse).every(val => val === true); | ||
}; | ||
|
||
const checkApplicationPrivileges = async (privileges) => { | ||
const privilegeCheck = await callWithRequest(request, 'shield.hasPrivileges', { | ||
body: { | ||
applications: [{ | ||
application, | ||
resources: [ALL_RESOURCE], | ||
privileges | ||
}] | ||
} | ||
}); | ||
const hasNoApplicationPrivileges = (applicationPrivilegesResponse) => { | ||
return Object.values(applicationPrivilegesResponse).every(val => val === false); | ||
}; | ||
|
||
const hasPrivileges = privilegeCheck.application[application][ALL_RESOURCE]; | ||
const hasLegacyPrivileges = (indexPrivilegesResponse) => { | ||
return Object.values(indexPrivilegesResponse).includes(true); | ||
}; | ||
|
||
// We include the login action in all privileges, so the existence of it and not the version privilege | ||
// lets us know that we're running in an incorrect configuration. Without the login privilege check, we wouldn't | ||
// know whether the user just wasn't authorized for this instance of Kibana in general | ||
if (!hasPrivileges[getVersionAction(kibanaVersion)] && hasPrivileges[getLoginAction()]) { | ||
throw new Error('Multiple versions of Kibana are running against the same Elasticsearch cluster, unable to authorize user.'); | ||
} | ||
const determineResult = (applicationPrivilegesResponse, indexPrivilegesResponse) => { | ||
if (hasAllApplicationPrivileges(applicationPrivilegesResponse)) { | ||
return CHECK_PRIVILEGES_RESULT.AUTHORIZED; | ||
} | ||
|
||
return { | ||
username: privilegeCheck.username, | ||
hasAllRequested: privilegeCheck.has_all_requested, | ||
privileges: hasPrivileges | ||
}; | ||
}; | ||
if (hasNoApplicationPrivileges(applicationPrivilegesResponse) && hasLegacyPrivileges(indexPrivilegesResponse)) { | ||
return CHECK_PRIVILEGES_RESULT.LEGACY; | ||
} | ||
|
||
const hasPrivilegesOnKibanaIndex = async () => { | ||
const privilegeCheck = await callWithRequest(request, 'shield.hasPrivileges', { | ||
return CHECK_PRIVILEGES_RESULT.UNAUTHORIZED; | ||
}; | ||
|
||
return function checkPrivilegesWithRequest(request) { | ||
|
||
return async function checkPrivileges(privileges) { | ||
const allApplicationPrivileges = uniq([actions.version, actions.login, ...privileges]); | ||
const hasPrivilegesResponse = await callWithRequest(request, 'shield.hasPrivileges', { | ||
body: { | ||
applications: [{ | ||
application, | ||
resources: [ALL_RESOURCE], | ||
privileges: allApplicationPrivileges | ||
}], | ||
index: [{ | ||
names: [kibanaIndex], | ||
privileges: ['create', 'delete', 'read', 'view_index_metadata'] | ||
}] | ||
}], | ||
} | ||
}); | ||
|
||
return Object.values(privilegeCheck.index[kibanaIndex]).includes(true); | ||
}; | ||
|
||
return async function checkPrivileges(privileges) { | ||
const allPrivileges = [versionAction, loginAction, ...privileges]; | ||
const applicationPrivilegesCheck = await checkApplicationPrivileges(allPrivileges); | ||
|
||
const username = applicationPrivilegesCheck.username; | ||
|
||
// We don't want to expose the version privilege to consumers, as it's an implementation detail only to detect version mismatch | ||
const missing = Object.keys(applicationPrivilegesCheck.privileges) | ||
.filter(p => !applicationPrivilegesCheck.privileges[p]) | ||
.filter(p => p !== versionAction); | ||
|
||
if (applicationPrivilegesCheck.hasAllRequested) { | ||
return { | ||
result: CHECK_PRIVILEGES_RESULT.AUTHORIZED, | ||
username, | ||
missing, | ||
}; | ||
} | ||
const applicationPrivilegesResponse = hasPrivilegesResponse.application[application][ALL_RESOURCE]; | ||
const indexPrivilegesResponse = hasPrivilegesResponse.index[kibanaIndex]; | ||
|
||
if (!applicationPrivilegesCheck.privileges[loginAction] && await hasPrivilegesOnKibanaIndex()) { | ||
const msg = 'Relying on index privileges is deprecated and will be removed in Kibana 7.0'; | ||
server.log(['warning', 'deprecated', 'security'], msg); | ||
|
||
return { | ||
result: CHECK_PRIVILEGES_RESULT.LEGACY, | ||
username, | ||
missing, | ||
}; | ||
if (hasIncompatibileVersion(applicationPrivilegesResponse)) { | ||
throw new Error('Multiple versions of Kibana are running against the same Elasticsearch cluster, unable to authorize user.'); | ||
} | ||
|
||
return { | ||
result: CHECK_PRIVILEGES_RESULT.UNAUTHORIZED, | ||
username, | ||
missing, | ||
result: determineResult(applicationPrivilegesResponse, indexPrivilegesResponse), | ||
username: hasPrivilegesResponse.username, | ||
|
||
// we only return missing privileges that they're specifically checking for | ||
missing: Object.keys(applicationPrivilegesResponse) | ||
.filter(privilege => privileges.includes(privilege)) | ||
.filter(privilege => !applicationPrivilegesResponse[privilege]) | ||
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. nit: These |
||
}; | ||
}; | ||
}; | ||
|
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.
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.
😎