Skip to content
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

[7.x] Remove restriction that route must start with /api to use api authorization (#58351) #58409

Merged
merged 2 commits into from
Feb 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,7 @@ import {
import { authorizationMock } from './index.mock';

describe('initAPIAuthorization', () => {
test(`route that doesn't start with "/api/" continues`, async () => {
const mockHTTPSetup = coreMock.createSetup().http;
initAPIAuthorization(
mockHTTPSetup,
authorizationMock.create(),
loggingServiceMock.create().get()
);

const [[postAuthHandler]] = mockHTTPSetup.registerOnPostAuth.mock.calls;

const mockRequest = httpServerMock.createKibanaRequest({ method: 'get', path: '/app/foo' });
const mockResponse = httpServerMock.createResponseFactory();
const mockPostAuthToolkit = httpServiceMock.createOnPostAuthToolkit();

await postAuthHandler(mockRequest, mockResponse, mockPostAuthToolkit);

expect(mockResponse.notFound).not.toHaveBeenCalled();
expect(mockPostAuthToolkit.next).toHaveBeenCalledTimes(1);
});

test(`protected route that starts with "/api/", but "mode.useRbacForRequest()" returns false continues`, async () => {
test(`protected route when "mode.useRbacForRequest()" returns false continues`, async () => {
const mockHTTPSetup = coreMock.createSetup().http;
const mockAuthz = authorizationMock.create();
initAPIAuthorization(mockHTTPSetup, mockAuthz, loggingServiceMock.create().get());
Expand All @@ -44,7 +24,7 @@ describe('initAPIAuthorization', () => {

const mockRequest = httpServerMock.createKibanaRequest({
method: 'get',
path: '/api/foo',
path: '/foo/bar',
routeTags: ['access:foo'],
});
const mockResponse = httpServerMock.createResponseFactory();
Expand All @@ -59,7 +39,7 @@ describe('initAPIAuthorization', () => {
expect(mockAuthz.mode.useRbacForRequest).toHaveBeenCalledWith(mockRequest);
});

test(`unprotected route that starts with "/api/", but "mode.useRbacForRequest()" returns true continues`, async () => {
test(`unprotected route when "mode.useRbacForRequest()" returns true continues`, async () => {
const mockHTTPSetup = coreMock.createSetup().http;
const mockAuthz = authorizationMock.create();
initAPIAuthorization(mockHTTPSetup, mockAuthz, loggingServiceMock.create().get());
Expand All @@ -68,7 +48,7 @@ describe('initAPIAuthorization', () => {

const mockRequest = httpServerMock.createKibanaRequest({
method: 'get',
path: '/api/foo',
path: '/foo/bar',
routeTags: ['not-access:foo'],
});
const mockResponse = httpServerMock.createResponseFactory();
Expand All @@ -83,7 +63,7 @@ describe('initAPIAuthorization', () => {
expect(mockAuthz.mode.useRbacForRequest).toHaveBeenCalledWith(mockRequest);
});

test(`protected route that starts with "/api/", "mode.useRbacForRequest()" returns true and user is authorized continues`, async () => {
test(`protected route when "mode.useRbacForRequest()" returns true and user is authorized continues`, async () => {
const mockHTTPSetup = coreMock.createSetup().http;
const mockAuthz = authorizationMock.create({ version: '1.0.0-zeta1' });
initAPIAuthorization(mockHTTPSetup, mockAuthz, loggingServiceMock.create().get());
Expand All @@ -93,7 +73,7 @@ describe('initAPIAuthorization', () => {
const headers = { authorization: 'foo' };
const mockRequest = httpServerMock.createKibanaRequest({
method: 'get',
path: '/api/foo',
path: '/foo/bar',
headers,
routeTags: ['access:foo'],
});
Expand All @@ -118,7 +98,7 @@ describe('initAPIAuthorization', () => {
expect(mockAuthz.mode.useRbacForRequest).toHaveBeenCalledWith(mockRequest);
});

test(`protected route that starts with "/api/", "mode.useRbacForRequest()" returns true and user isn't authorized responds with a 404`, async () => {
test(`protected route when "mode.useRbacForRequest()" returns true and user isn't authorized responds with a 404`, async () => {
const mockHTTPSetup = coreMock.createSetup().http;
const mockAuthz = authorizationMock.create({ version: '1.0.0-zeta1' });
initAPIAuthorization(mockHTTPSetup, mockAuthz, loggingServiceMock.create().get());
Expand All @@ -128,7 +108,7 @@ describe('initAPIAuthorization', () => {
const headers = { authorization: 'foo' };
const mockRequest = httpServerMock.createKibanaRequest({
method: 'get',
path: '/api/foo',
path: '/foo/bar',
headers,
routeTags: ['access:foo'],
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export function initAPIAuthorization(
logger: Logger
) {
http.registerOnPostAuth(async (request, response, toolkit) => {
// if the api doesn't start with "/api/" or we aren't using RBAC for this request, just continue
if (!request.url.path!.startsWith('/api/') || !mode.useRbacForRequest(request)) {
// if we aren't using RBAC for this request, just continue
if (!mode.useRbacForRequest(request)) {
return toolkit.next();
}

Expand Down