Skip to content

Commit

Permalink
Extract function
Browse files Browse the repository at this point in the history
  • Loading branch information
kdelemme committed Nov 7, 2024
1 parent cb992ae commit 34f3cdb
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { SLORepository } from './slo_repository';
import { createTempSummaryDocument } from './summary_transform_generator/helpers/create_temp_summary';
import { TransformManager } from './transform_manager';
import { getTransformQueryComposite } from './utils/get_transform_compite_query';
import { assertExpectedIndicatorSourceIndexPrivileges } from './utils/assert_expected_indicator_source_index_privileges';

export class CreateSLO {
constructor(
Expand All @@ -47,7 +48,7 @@ export class CreateSLO {
validateSLO(slo);

await this.assertSLOInexistant(slo);
await this.assertExpectedIndicatorSourceIndexPrivileges(slo);
await assertExpectedIndicatorSourceIndexPrivileges(slo, this.esClient);

const rollbackOperations = [];
const createPromise = this.repository.create(slo);
Expand Down Expand Up @@ -124,18 +125,6 @@ export class CreateSLO {
throw new SLOIdConflict(`SLO [${slo.id}] already exists`);
}
}

private async assertExpectedIndicatorSourceIndexPrivileges(slo: SLODefinition) {
const privileges = await this.esClient.security.hasPrivileges({
index: [{ names: slo.indicator.params.index, privileges: ['read', 'view_index_metadata'] }],
});
if (!privileges.has_all_requested) {
throw new SecurityException(
`Missing ['read', 'view_index_metadata'] privileges on the source index [${slo.indicator.params.index}]`
);
}
}

async createTempSummaryDocument(slo: SLODefinition) {
return await retryTransientEsErrors(
() =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,25 @@
* 2.0.
*/

import { ElasticsearchClient, IBasePath, Logger, IScopedClusterClient } from '@kbn/core/server';
import { ElasticsearchClient, IBasePath, IScopedClusterClient, Logger } from '@kbn/core/server';
import { resetSLOResponseSchema } from '@kbn/slo-schema';
import {
getSLOPipelineId,
getSLOSummaryPipelineId,
getSLOSummaryTransformId,
getSLOTransformId,
SLO_DESTINATION_INDEX_PATTERN,
SLO_MODEL_VERSION,
SLO_SUMMARY_DESTINATION_INDEX_PATTERN,
SLO_SUMMARY_TEMP_INDEX_NAME,
getSLOPipelineId,
getSLOSummaryPipelineId,
getSLOSummaryTransformId,
getSLOTransformId,
} from '../../common/constants';
import { getSLOPipelineTemplate } from '../assets/ingest_templates/slo_pipeline_template';
import { getSLOSummaryPipelineTemplate } from '../assets/ingest_templates/slo_summary_pipeline_template';
import { retryTransientEsErrors } from '../utils/retry';
import { SLORepository } from './slo_repository';
import { createTempSummaryDocument } from './summary_transform_generator/helpers/create_temp_summary';
import { TransformManager } from './transform_manager';
import { SLODefinition } from '../domain/models';
import { SecurityException } from '../errors';
import { assertExpectedIndicatorSourceIndexPrivileges } from './utils/assert_expected_indicator_source_index_privileges';

export class ResetSLO {
constructor(
Expand All @@ -41,7 +40,7 @@ export class ResetSLO {
public async execute(sloId: string) {
const slo = await this.repository.findById(sloId);

await this.assertExpectedIndicatorSourceIndexPrivileges(slo);
await assertExpectedIndicatorSourceIndexPrivileges(slo, this.esClient);

const summaryTransformId = getSLOSummaryTransformId(slo.id, slo.revision);
await this.summaryTransformManager.stop(summaryTransformId);
Expand Down Expand Up @@ -117,17 +116,6 @@ export class ResetSLO {
return resetSLOResponseSchema.encode(updatedSlo);
}

private async assertExpectedIndicatorSourceIndexPrivileges(slo: SLODefinition) {
const privileges = await this.esClient.security.hasPrivileges({
index: [{ names: slo.indicator.params.index, privileges: ['read', 'view_index_metadata'] }],
});
if (!privileges.has_all_requested) {
throw new SecurityException(
`Missing ['read', 'view_index_metadata'] privileges on the source index [${slo.indicator.params.index}]`
);
}
}

/**
* Deleting all SLI rollup data matching the sloId. All revision will be deleted in case of
* residual documents.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
* 2.0.
*/

import { ElasticsearchClient, IBasePath, Logger, IScopedClusterClient } from '@kbn/core/server';
import { ElasticsearchClient, IBasePath, IScopedClusterClient, Logger } from '@kbn/core/server';
import { UpdateSLOParams, UpdateSLOResponse, updateSLOResponseSchema } from '@kbn/slo-schema';
import { asyncForEach } from '@kbn/std';
import { isEqual, pick } from 'lodash';
import {
SLO_DESTINATION_INDEX_PATTERN,
SLO_SUMMARY_DESTINATION_INDEX_PATTERN,
SLO_SUMMARY_TEMP_INDEX_NAME,
getSLOPipelineId,
getSLOSummaryPipelineId,
getSLOSummaryTransformId,
getSLOTransformId,
SLO_DESTINATION_INDEX_PATTERN,
SLO_SUMMARY_DESTINATION_INDEX_PATTERN,
SLO_SUMMARY_TEMP_INDEX_NAME,
} from '../../common/constants';
import { getSLOPipelineTemplate } from '../assets/ingest_templates/slo_pipeline_template';
import { getSLOSummaryPipelineTemplate } from '../assets/ingest_templates/slo_summary_pipeline_template';
Expand All @@ -27,6 +27,7 @@ import { retryTransientEsErrors } from '../utils/retry';
import { SLORepository } from './slo_repository';
import { createTempSummaryDocument } from './summary_transform_generator/helpers/create_temp_summary';
import { TransformManager } from './transform_manager';
import { assertExpectedIndicatorSourceIndexPrivileges } from './utils/assert_expected_indicator_source_index_privileges';

export class UpdateSLO {
constructor(
Expand Down Expand Up @@ -68,7 +69,7 @@ export class UpdateSLO {

validateSLO(updatedSlo);

await this.assertExpectedIndicatorSourceIndexPrivileges(updatedSlo);
await assertExpectedIndicatorSourceIndexPrivileges(updatedSlo, this.esClient);

const rollbackOperations = [];
await this.repository.update(updatedSlo);
Expand Down Expand Up @@ -203,17 +204,6 @@ export class UpdateSLO {
return this.toResponse(updatedSlo);
}

private async assertExpectedIndicatorSourceIndexPrivileges(slo: SLODefinition) {
const privileges = await this.esClient.security.hasPrivileges({
index: [{ names: slo.indicator.params.index, privileges: ['read', 'view_index_metadata'] }],
});
if (!privileges.has_all_requested) {
throw new SecurityException(
`Missing ['read', 'view_index_metadata'] privileges on the source index [${slo.indicator.params.index}]`
);
}
}

private async deleteOriginalSLO(originalSlo: SLODefinition) {
try {
const originalRollupTransformId = getSLOTransformId(originalSlo.id, originalSlo.revision);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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 { ElasticsearchClient } from '@kbn/core/server';
import { SLODefinition } from '../../domain/models';
import { SecurityException } from '../../errors';

export async function assertExpectedIndicatorSourceIndexPrivileges(
slo: SLODefinition,
esClient: ElasticsearchClient
) {
const privileges = await esClient.security.hasPrivileges({
index: [{ names: slo.indicator.params.index, privileges: ['read', 'view_index_metadata'] }],
});
if (!privileges.has_all_requested) {
throw new SecurityException(
`Missing ['read', 'view_index_metadata'] privileges on the source index [${slo.indicator.params.index}]`
);
}
}

0 comments on commit 34f3cdb

Please sign in to comment.