monasca-kibana-plugin/server/mt/routing/routes/pattern_search.js
Arseni Lipinski bfa95367e0 Upgrade monasca-kibana-plugin for Elkstack update
Story: 2006376
Task: 36179

Besides the plugin update, tests should get remade as well as Zuul jobs
due to plugin being built in a different way.

Change-Id: Id5d0bb53d10fa8823ceda2c8e922ea36b9b27501
2020-04-23 18:38:53 +02:00

67 lines
2.3 KiB
JavaScript

/*
* Copyright 2020 FUJITSU LIMITED
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
import Wreck from '@hapi/wreck';
import {SESSION_USER_KEY} from '../../../const';
import {getOpts} from '../_utils';
import getAllowedPatterns from '../../kibana/defaultIndexPattern/_get_allowed_patterns';
import mapUri from '../_map_uri';
export default function (server, method, path) {
return {
method: method,
path: path,
config: {
auth: 'keystone-session',
},
handler: handler
};
async function handler(request, reply) {
const session = request.yar;
let url = mapUri(server, request).split('/');
let project = session.get(SESSION_USER_KEY).project.id;
let logsIndexPref = server.config().get('monasca-kibana-plugin.logsIndexPrefix');
let eventsIndexPref = server.config().get('monasca-kibana-plugin.eventsIndexPrefix');
logsIndexPref = logsIndexPref.replace('<project_id>', project);
eventsIndexPref = eventsIndexPref.replace('<project_id>', project);
server.log(['status', 'info', 'keystone'],
`Allowing only these Index-Prefix ${logsIndexPref}, ${eventsIndexPref}`);
url = url.join('/');
const opts = getOpts(server, request, url, request.payload);
const patterns = getAllowedPatterns(server, project);
try {
const rawResponse = await Wreck.request(request.method, url, opts);
const body = await Wreck.read(rawResponse, opts);
let response = JSON.parse(body.toString());
if (response._shards.total > 0) {
let indexHits = response.aggregations.indices.buckets;
response.aggregations.indices.buckets =
indexHits.filter(elem => patterns.includes(elem.key));
}
return reply.response(response).code(rawResponse.statusCode).passThrough(!!opts.passThrough);
} catch (e) {
throw new Error(e);
}
}
}