Skip to content

Commit

Permalink
Improved role management error handling for partially authorized users (
Browse files Browse the repository at this point in the history
#96468)

* Role management: Gracefully handle underprivileged users

* Removed redundant condition
  • Loading branch information
thomheymann authored Apr 8, 2021
1 parent 869fd93 commit 4af344a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -480,23 +480,25 @@ describe('<EditRolePage />', () => {
});
});

it('can render if features are not available', async () => {
const { http } = coreMock.createStart();
http.get.mockImplementation(async (path: any) => {
if (path === '/api/features') {
const error = { response: { status: 404 } };
throw error;
}
it('registers fatal error if features endpoint fails unexpectedly', async () => {
const error = { response: { status: 500 } };
const getFeatures = jest.fn().mockRejectedValue(error);
const props = getProps({ action: 'edit' });
const wrapper = mountWithIntl(<EditRolePage {...props} getFeatures={getFeatures} />);

if (path === '/api/spaces/space') {
return buildSpaces();
}
});
await waitForRender(wrapper);
expect(props.fatalErrors.add).toHaveBeenLastCalledWith(error);
expect(wrapper.find(SpaceAwarePrivilegeSection)).toHaveLength(0);
});

const wrapper = mountWithIntl(<EditRolePage {...{ ...getProps({ action: 'edit' }), http }} />);
it('can render if features call is not allowed', async () => {
const error = { response: { status: 403 } };
const getFeatures = jest.fn().mockRejectedValue(error);
const props = getProps({ action: 'edit' });
const wrapper = mountWithIntl(<EditRolePage {...props} getFeatures={getFeatures} />);

await waitForRender(wrapper);

expect(props.fatalErrors.add).not.toHaveBeenCalled();
expect(wrapper.find(SpaceAwarePrivilegeSection)).toHaveLength(1);
expect(wrapper.find('[data-test-subj="userCannotManageSpacesCallout"]')).toHaveLength(0);
expectSaveFormButtons(wrapper);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,13 +256,12 @@ function useFeatures(
// possible that a user with `manage_security` will attempt to visit the role management page without the
// correct Kibana privileges. If that's the case, then they receive a partial view of the role, and the UI does
// not allow them to make changes to that role's kibana privileges. When this user visits the edit role page,
// this API endpoint will throw a 404, which causes view to fail completely. So we instead attempt to detect the
// 404 here, and respond in a way that still allows the UI to render itself.
const unauthorizedForFeatures = err.response?.status === 404;
// this API endpoint will throw a 403, which causes view to fail completely. So we instead attempt to detect the
// 403 here, and respond in a way that still allows the UI to render itself.
const unauthorizedForFeatures = err.response?.status === 403;
if (unauthorizedForFeatures) {
return [] as KibanaFeature[];
}

fatalErrors.add(err);
})
.then((retrievedFeatures) => {
Expand Down Expand Up @@ -296,7 +295,6 @@ export const EditRolePage: FunctionComponent<Props> = ({
// We should keep the same mutable instance of Validator for every re-render since we'll
// eventually enable validation after the first time user tries to save a role.
const { current: validator } = useRef(new RoleValidator({ shouldValidate: false }));

const [formError, setFormError] = useState<RoleValidationResult | null>(null);
const runAsUsers = useRunAsUsers(userAPIClient, fatalErrors);
const indexPatternsTitles = useIndexPatternsTitles(indexPatterns, fatalErrors, notifications);
Expand Down

0 comments on commit 4af344a

Please sign in to comment.