Skip to content

Commit

Permalink
- adds API that checks for existence of log events for given dataset
Browse files Browse the repository at this point in the history
- cleans up some routes
  • Loading branch information
ogupte committed May 22, 2023
1 parent f7d0b44 commit e7b8d0d
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,36 +42,34 @@ export function InstallElasticAgent() {

const { data: installShipperSetup, status: installShipperSetupStatus } =
useFetcher((callApi) => {
if (CurrentStep !== InstallElasticAgent) {
return;
}

return callApi(
'POST /internal/observability_onboarding/custom_logs/install_shipper_setup',
{
params: {
body: {
name: wizardState.datasetName,
state: {
datasetName: wizardState.datasetName,
namespace: wizardState.namespace,
customConfigurations: wizardState.customConfigurations,
logFilePaths: wizardState.logFilePaths,
if (CurrentStep === InstallElasticAgent) {
return callApi(
'POST /internal/observability_onboarding/custom_logs/install_shipper_setup',
{
params: {
body: {
name: wizardState.datasetName,
state: {
datasetName: wizardState.datasetName,
namespace: wizardState.namespace,
customConfigurations: wizardState.customConfigurations,
logFilePaths: wizardState.logFilePaths,
},
},
},
},
}
);
}
);
}
}, []);

const { data: yamlConfig = '', status: yamlConfigStatus } = useFetcher(
(callApi) => {
if (installShipperSetup?.apiKeyId) {
if (CurrentStep === InstallElasticAgent && installShipperSetup) {
return callApi(
'GET /api/observability_onboarding/elastic_agent/config',
{
headers: {
authorization: `ApiKey ${installShipperSetup?.apiKeyEncoded}`,
authorization: `ApiKey ${installShipperSetup.apiKeyEncoded}`,
},
}
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { KibanaRequest } from '@kbn/core-http-server';
import { HTTPAuthorizationHeader } from '@kbn/security-plugin/server';

export const getAuthenticationAPIKey = (request: KibanaRequest) => {
const authorizationHeader = HTTPAuthorizationHeader.parseFromRequest(request);
if (authorizationHeader && authorizationHeader.credentials) {
const apiKey = Buffer.from(authorizationHeader.credentials, 'base64')
.toString()
.split(':');
return {
apiKeyId: apiKey[0],
apiKey: apiKey[1],
};
}
throw new Error('Authorization header is missing');
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import * as t from 'io-ts';
import Boom from '@hapi/boom';
import type { Client } from '@elastic/elasticsearch';
import { createObservabilityOnboardingServerRoute } from '../create_observability_onboarding_server_route';
import { getESHosts } from './get_es_hosts';
Expand All @@ -19,6 +20,7 @@ import {
} from '../../saved_objects/observability_onboarding_status';
import { getObservabilityOnboardingState } from './get_observability_onboarding_state';
import { findLatestObservabilityOnboardingState } from './find_latest_observability_onboarding_state';
import { getAuthenticationAPIKey } from '../../lib/get_authentication_api_key';

const createApiKeyRoute = createObservabilityOnboardingServerRoute({
endpoint:
Expand Down Expand Up @@ -102,25 +104,10 @@ const stepProgressUpdateRoute = createObservabilityOnboardingServerRoute({
path: { name },
query: { status },
},
request: {
headers: { authorization },
},
request,
core,
} = resources;
if (
!(
typeof authorization === 'string' && authorization.startsWith('ApiKey ')
)
) {
return {
message:
'Unable to report setup progress without ApiKey in authorization header.',
};
}
const apiKeyEncoded = authorization.split(' ')[1];
const [apiKeyId] = Buffer.from(apiKeyEncoded, 'base64')
.toString('utf8')
.split(':');
const { apiKeyId } = getAuthenticationAPIKey(request);
const coreStart = await core.start();
const savedObjectsClient =
coreStart.savedObjects.createInternalRepository();
Expand Down Expand Up @@ -156,7 +143,7 @@ const stepProgressUpdateRoute = createObservabilityOnboardingServerRoute({
});

const getStateRoute = createObservabilityOnboardingServerRoute({
endpoint: 'GET /api/observability_onboarding/custom_logs/state',
endpoint: 'GET /internal/observability_onboarding/custom_logs/state',
options: { tags: [] },
params: t.type({
query: t.type({
Expand Down Expand Up @@ -185,7 +172,7 @@ const getStateRoute = createObservabilityOnboardingServerRoute({
});

const getLatestStateRoute = createObservabilityOnboardingServerRoute({
endpoint: 'GET /api/observability_onboarding/custom_logs/state/latest',
endpoint: 'GET /internal/observability_onboarding/custom_logs/state/latest',
options: { tags: [] },
async handler(resources): Promise<{
savedObservabilityOnboardingState: SavedObservabilityOnboardingState | null;
Expand All @@ -201,8 +188,48 @@ const getLatestStateRoute = createObservabilityOnboardingServerRoute({
},
});

const cleanStateRoute = createObservabilityOnboardingServerRoute({
endpoint: 'GET /api/observability_onboarding/custom_logs/clean_state',
const customLogsExistsRoute = createObservabilityOnboardingServerRoute({
endpoint: 'GET /internal/observability_onboarding/custom_logs/exists',
options: { tags: [] },
params: t.type({
query: t.type({
dataset: t.string,
namespace: t.string,
}),
}),
async handler(resources): Promise<{ exists: boolean }> {
const {
core,
request,
params: {
query: { dataset, namespace },
},
} = resources;
const coreStart = await core.start();
const esClient =
coreStart.elasticsearch.client.asScoped(request).asCurrentUser;
try {
const { hits } = await esClient.search({
index: `logs-${dataset}-${namespace}`,
terminate_after: 1,
});
const total = hits.total as { value: number };
return { exists: total.value > 0 };
} catch (error) {
if (error.statusCode === 404) {
return { exists: false };
}
throw Boom.boomify(error, {
statusCode: error.statusCode,
message: error.message,
data: error.body,
});
}
},
});

const deleteStatesRoute = createObservabilityOnboardingServerRoute({
endpoint: 'DELETE /internal/observability_onboarding/custom_logs/states',
options: { tags: [] },
async handler(resources): Promise<object> {
const { core } = resources;
Expand All @@ -225,5 +252,6 @@ export const customLogsRouteRepository = {
...stepProgressUpdateRoute,
...getStateRoute,
...getLatestStateRoute,
...cleanStateRoute,
...customLogsExistsRoute,
...deleteStatesRoute,
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,12 @@
*/

import type { Client } from '@elastic/elasticsearch';
import { KibanaRequest } from '@kbn/core-http-server';
import { HTTPAuthorizationHeader } from '@kbn/security-plugin/server';
import { getAuthenticationAPIKey } from '../../lib/get_authentication_api_key';
import { createObservabilityOnboardingServerRoute } from '../create_observability_onboarding_server_route';
import { findLatestObservabilityOnboardingState } from '../custom_logs/find_latest_observability_onboarding_state';
import { getESHosts } from '../custom_logs/get_es_hosts';
import { generateYml } from './generate_yml';

const getAuthenticationAPIKey = (request: KibanaRequest) => {
const authorizationHeader = HTTPAuthorizationHeader.parseFromRequest(request);
if (authorizationHeader && authorizationHeader.credentials) {
const apiKey = Buffer.from(authorizationHeader.credentials, 'base64')
.toString()
.split(':');
return {
apiKeyId: apiKey[0],
apiKey: apiKey[1],
};
}
throw new Error('Authorization header is missing');
};

const generateConfig = createObservabilityOnboardingServerRoute({
endpoint: 'GET /api/observability_onboarding/elastic_agent/config',
options: { tags: [] },
Expand Down

0 comments on commit e7b8d0d

Please sign in to comment.