Skip to content

Commit

Permalink
add await to context.*
Browse files Browse the repository at this point in the history
  • Loading branch information
yenienserrano committed Nov 8, 2022
1 parent 10e4cb5 commit a5d41f4
Showing 1 changed file with 92 additions and 43 deletions.
135 changes: 92 additions & 43 deletions server/controllers/wazuh-elastic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ export class WazuhElasticCtrl {
*/
async getTemplate(context: RequestHandlerContext, request: KibanaRequest<{ pattern: string }>, response: KibanaResponseFactory) {
try {
const data = await context.core.elasticsearch.client.asInternalUser.cat.templates();
const awaitContext = await context.core;
const data =
await awaitContext.elasticsearch.client.asInternalUser.cat.templates();

const templates = data.body;
if (!templates || typeof templates !== 'string') {
Expand Down Expand Up @@ -144,7 +146,10 @@ export class WazuhElasticCtrl {
*/
async checkPattern(context: RequestHandlerContext, request: KibanaRequest<{ pattern: string }>, response: KibanaResponseFactory) {
try {
const data = await context.core.savedObjects.client.find<SavedObjectsFindResponse<SavedObject>>({ type: 'index-pattern' });
const awaitContext = await context.core;
const data = await awaitContext.savedObjects.client.find<
SavedObjectsFindResponse<SavedObject>
>({ type: 'index-pattern' });

const existsIndexPattern = data.saved_objects.find(
item => item.attributes.title === request.params.pattern
Expand Down Expand Up @@ -239,11 +244,14 @@ export class WazuhElasticCtrl {
);
payload.aggs['2'].terms.field = request.params.field;

const data = await context.core.elasticsearch.client.asCurrentUser.search({
size: 1,
index: request.params.pattern,
body: payload
});
const awaitContext = await context.core;
const data = await awaitContext.elasticsearch.client.asCurrentUser.search(
{
size: 1,
index: request.params.pattern,
body: payload,
},
);

return data.body.hits.total.value === 0 ||
typeof data.body.aggregations['2'].buckets[0] === 'undefined'
Expand Down Expand Up @@ -276,8 +284,9 @@ export class WazuhElasticCtrl {
let results = false,
forbidden = false;
try {
results = await context.core.elasticsearch.client.asCurrentUser.search({
index: item.title
const awaitContext = await context.core;
results = await awaitContext.elasticsearch.client.asCurrentUser.search({
index: item.title,
});
} catch (error) {
forbidden = true;
Expand Down Expand Up @@ -326,10 +335,12 @@ export class WazuhElasticCtrl {
*/
async getCurrentPlatform(context: RequestHandlerContext, request: KibanaRequest<{ user: string }>, response: KibanaResponseFactory) {
try {
const contextWazuh = await context.wazuh;
console.log(contextWazuh);
return response.ok({
body: {
platform: context.wazuh.security.platform
}
platform: contextWazuh.security.platform,
},
});
} catch (error) {
log('wazuh-elastic:getCurrentPlatform', error.message || error);
Expand Down Expand Up @@ -539,7 +550,11 @@ export class WazuhElasticCtrl {
return response.notFound({body:{message: `Visualizations not found for ${request.params.tab}`}});
}
log('wazuh-elastic:createVis', `${tabPrefix}[${tabSufix}] with index pattern ${request.params.pattern}`, 'debug');
const namespace = context.wazuh.plugins.spaces && context.wazuh.plugins.spaces.spacesService && context.wazuh.plugins.spaces.spacesService.getSpaceId(request);
const contextWazuh = await context.wazuh;
const namespace =
contextWazuh.plugins.spaces &&
contextWazuh.plugins.spaces.spacesService &&
contextWazuh.plugins.spaces.spacesService.getSpaceId(request);
const raw = await this.buildVisualizationsRaw(
file,
request.params.pattern,
Expand Down Expand Up @@ -613,10 +628,14 @@ export class WazuhElasticCtrl {
async haveSampleAlerts(context: RequestHandlerContext, request: KibanaRequest, response: KibanaResponseFactory) {
try {
// Check if wazuh sample alerts index exists
const results = await Promise.all(Object.keys(WAZUH_SAMPLE_ALERTS_CATEGORIES_TYPE_ALERTS)
.map((category) => context.core.elasticsearch.client.asCurrentUser.indices.exists({
index: this.buildSampleIndexByCategory(category)
})));
const awaitContext = await context.core;
const results = await Promise.all(
Object.keys(WAZUH_SAMPLE_ALERTS_CATEGORIES_TYPE_ALERTS).map(category =>
awaitContext.elasticsearch.client.asCurrentUser.indices.exists({
index: this.buildSampleIndexByCategory(category),
}),
),
);
return response.ok({
body: { sampleAlertsInstalled: results.some(result => result.body) }
});
Expand All @@ -636,9 +655,11 @@ export class WazuhElasticCtrl {
try {
const sampleAlertsIndex = this.buildSampleIndexByCategory(request.params.category);
// Check if wazuh sample alerts index exists
const existsSampleIndex = await context.core.elasticsearch.client.asCurrentUser.indices.exists({
index: sampleAlertsIndex
});
const awaitContext = await context.core;
const existsSampleIndex =
await awaitContext.elasticsearch.client.asCurrentUser.indices.exists({
index: sampleAlertsIndex,
});
return response.ok({
body: { index: sampleAlertsIndex, exists: existsSampleIndex.body }
})
Expand Down Expand Up @@ -690,7 +711,14 @@ export class WazuhElasticCtrl {
if (!apiHostID) {
return ErrorResponse('No API id provided', 401, 401, response);
};
const responseTokenIsWorking = await context.wazuh.api.client.asCurrentUser.request('GET', `//`, {}, { apiHostID });
const contextWazuh = await context.wazuh;
const responseTokenIsWorking =
await contextWazuh.api.client.asCurrentUser.request(
'GET',
`//`,
{},
{ apiHostID },
);
if (responseTokenIsWorking.status !== 200) {
return ErrorResponse('Token is not valid', 500, 500, response);
};
Expand All @@ -708,9 +736,11 @@ export class WazuhElasticCtrl {
// Index alerts

// Check if wazuh sample alerts index exists
const existsSampleIndex = await context.core.elasticsearch.client.asCurrentUser.indices.exists({
index: sampleAlertsIndex
});
const awaitContext = await context.core;
const existsSampleIndex =
await awaitContext.elasticsearch.client.asCurrentUser.indices.exists({
index: sampleAlertsIndex,
});
if (!existsSampleIndex.body) {
// Create wazuh sample alerts index

Expand All @@ -723,18 +753,18 @@ export class WazuhElasticCtrl {
}
};

await context.core.elasticsearch.client.asCurrentUser.indices.create({
const awaitContext = await context.core;
await awaitContext.elasticsearch.client.asCurrentUser.indices.create({
index: sampleAlertsIndex,
body: configuration
body: configuration,
});
log(
'wazuh-elastic:createSampleAlerts',
`Created ${sampleAlertsIndex} index`,
'debug'
);
}

await context.core.elasticsearch.client.asCurrentUser.bulk({
await awaitContext.elasticsearch.client.asCurrentUser.bulk({
index: sampleAlertsIndex,
body: bulk
});
Expand All @@ -751,9 +781,9 @@ export class WazuhElasticCtrl {
'wazuh-elastic:createSampleAlerts',
`Error adding sample alerts to ${sampleAlertsIndex} index: ${error.message || error}`
);

const [statusCode, errorMessage] = this.getErrorDetails(error);

return ErrorResponse(errorMessage || error, 1000, statusCode, response);
}
}
Expand Down Expand Up @@ -787,18 +817,29 @@ export class WazuhElasticCtrl {
if (!apiHostID) {
return ErrorResponse('No API id provided', 401, 401, response);
};
const responseTokenIsWorking = await context.wazuh.api.client.asCurrentUser.request('GET', `//`, {}, { apiHostID });
const contextWazuh = await context.wazuh;
const responseTokenIsWorking =
await contextWazuh.api.client.asCurrentUser.request(
'GET',
`//`,
{},
{ apiHostID },
);
if (responseTokenIsWorking.status !== 200) {
return ErrorResponse('Token is not valid', 500, 500, response);
};

// Check if Wazuh sample alerts index exists
const existsSampleIndex = await context.core.elasticsearch.client.asCurrentUser.indices.exists({
index: sampleAlertsIndex
});
const contextCore = await context.core;
const existsSampleIndex =
await contextCore.elasticsearch.client.asCurrentUser.indices.exists({
index: sampleAlertsIndex,
});
if (existsSampleIndex.body) {
// Delete Wazuh sample alerts index
await context.core.elasticsearch.client.asCurrentUser.indices.delete({ index: sampleAlertsIndex });
await contextCore.elasticsearch.client.asCurrentUser.indices.delete({
index: sampleAlertsIndex,
});
log(
'wazuh-elastic:deleteSampleAlerts',
`Deleted ${sampleAlertsIndex} index`,
Expand All @@ -823,7 +864,10 @@ export class WazuhElasticCtrl {

async alerts(context: RequestHandlerContext, request: KibanaRequest, response: KibanaResponseFactory) {
try {
const data = await context.core.elasticsearch.client.asCurrentUser.search(request.body);
const awaitContext = await context.core;
const data = await awaitContext.elasticsearch.client.asCurrentUser.search(
request.body,
);
return response.ok({
body: data.body
});
Expand All @@ -838,10 +882,12 @@ export class WazuhElasticCtrl {
try {
const config = getConfiguration();
const statisticsPattern = `${config['cron.prefix'] || 'wazuh'}-${config['cron.statistics.index.name'] || 'statistics'}*`; //TODO: replace by default as constants instead hardcoded ('wazuh' and 'statistics')
const existIndex = await context.core.elasticsearch.client.asCurrentUser.indices.exists({
index: statisticsPattern,
allow_no_indices: false
});
const awaitContext = await context.core;
const existIndex =
await awaitContext.elasticsearch.client.asCurrentUser.indices.exists({
index: statisticsPattern,
allow_no_indices: false,
});
return response.ok({
body: existIndex.body
});
Expand All @@ -856,7 +902,8 @@ export class WazuhElasticCtrl {
try {
const config = getConfiguration();
const monitoringIndexPattern = config['wazuh.monitoring.pattern'] || WAZUH_MONITORING_PATTERN;
const existIndex = await context.core.elasticsearch.client.asCurrentUser.indices.exists({
const awaitContext = await context.core;
const existIndex = await awaitContext.elasticsearch.client.asCurrentUser.indices.exists({
index: monitoringIndexPattern,
allow_no_indices: false
});
Expand All @@ -871,9 +918,11 @@ export class WazuhElasticCtrl {

async usingCredentials(context) {
try {
const data = await context.core.elasticsearch.client.asInternalUser.cluster.getSettings(
{ include_defaults: true }
);
const awaitContext = await context.core;
const data =
await awaitContext.elasticsearch.client.asInternalUser.cluster.getSettings(
{ include_defaults: true },
);
return (((((data || {}).body || {}).defaults || {}).xpack || {}).security || {}).user !== null;
} catch (error) {
return Promise.reject(error);
Expand Down

0 comments on commit a5d41f4

Please sign in to comment.