Skip to content

Commit 758f158

Browse files
authored
Fixes unauthorized error in es datasources (#24624) (#24808)
* Fixes check for security plugin * Cleaned up security check logic. Fixed tests for create_handlers. Added TODOs * Updated comment * Added tests * Updated variable names
1 parent 628e717 commit 758f158

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

x-pack/plugins/canvas/server/lib/__tests__/create_handlers.js

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import expect from 'expect.js';
88
import { createHandlers } from '../create_handlers';
99

1010
let securityMode = 'pass';
11+
let isSecurityAvailable = true;
12+
let isSecurityEnabled = true;
1113
const authError = new Error('auth error');
1214

1315
const mockRequest = {
@@ -27,6 +29,15 @@ const mockServer = {
2729
callWithRequest: (...args) => Promise.resolve(args),
2830
}),
2931
},
32+
// TODO: remove this when we use the method exposed by security https://github.com/elastic/kibana/pull/24616
33+
xpack_main: {
34+
info: {
35+
feature: () => ({
36+
isAvailable: () => isSecurityAvailable,
37+
isEnabled: () => isSecurityEnabled,
38+
}),
39+
},
40+
},
3041
},
3142
config: () => ({
3243
has: () => false,
@@ -42,6 +53,8 @@ describe('server createHandlers', () => {
4253

4354
beforeEach(() => {
4455
securityMode = 'pass';
56+
isSecurityEnabled = true;
57+
isSecurityAvailable = true;
4558
handlers = createHandlers(mockRequest, mockServer);
4659
});
4760

@@ -75,7 +88,7 @@ describe('server createHandlers', () => {
7588
});
7689
});
7790

78-
it('works without security', async () => {
91+
it('works without security plugin in kibana', async () => {
7992
// create server without security plugin
8093
const mockServerClone = {
8194
...mockServer,
@@ -98,5 +111,41 @@ describe('server createHandlers', () => {
98111
expect(endpoint).to.equal('endpoint');
99112
expect(payload).to.equal('payload');
100113
});
114+
115+
it('works without security available', async () => {
116+
// create server with security unavailable (i.e. when user is on a basic license)
117+
isSecurityAvailable = false;
118+
119+
// this shouldn't do anything
120+
securityMode = 'fail';
121+
122+
// make sure the method still works
123+
handlers = createHandlers(mockRequest, mockServer);
124+
const [request, endpoint, payload] = await handlers.elasticsearchClient(
125+
'endpoint',
126+
'payload'
127+
);
128+
expect(request).to.equal(mockRequest);
129+
expect(endpoint).to.equal('endpoint');
130+
expect(payload).to.equal('payload');
131+
});
132+
133+
it('works with security disabled in elasticsearch', async () => {
134+
// create server with security disabled
135+
isSecurityEnabled = false;
136+
137+
// this shouldn't do anything
138+
securityMode = 'fail';
139+
140+
// make sure the method still works
141+
handlers = createHandlers(mockRequest, mockServer);
142+
const [request, endpoint, payload] = await handlers.elasticsearchClient(
143+
'endpoint',
144+
'payload'
145+
);
146+
expect(request).to.equal(mockRequest);
147+
expect(endpoint).to.equal('endpoint');
148+
expect(payload).to.equal('payload');
149+
});
101150
});
102151
});

x-pack/plugins/canvas/server/lib/create_handlers.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
import boom from 'boom';
8+
import { isSecurityEnabled } from './feature_check';
89

910
export const createHandlers = (request, server) => {
1011
const { callWithRequest } = server.plugins.elasticsearch.getCluster('data');
@@ -19,7 +20,8 @@ export const createHandlers = (request, server) => {
1920
httpHeaders: request.headers,
2021
elasticsearchClient: async (...args) => {
2122
// check if the session is valid because continuing to use it
22-
if (server.plugins.security) {
23+
// TODO: replace this when we use the method exposed by security https://github.com/elastic/kibana/pull/24616
24+
if (isSecurityEnabled(server)) {
2325
const authenticationResult = await server.plugins.security.authenticate(request);
2426
if (!authenticationResult.succeeded()) throw boom.unauthorized(authenticationResult.error);
2527
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
export const isSecurityEnabled = server => {
8+
const kibanaSecurity = server.plugins.security;
9+
const esSecurity = server.plugins.xpack_main.info.feature('security');
10+
11+
return kibanaSecurity && esSecurity.isAvailable() && esSecurity.isEnabled();
12+
};

0 commit comments

Comments
 (0)