mirror of
https://github.com/dadosfera/maestro.git
synced 2026-11-17 17:19:08 +00:00
Review (maestro #510): the guard inferred 'editable' from the absence of run history. It now reads GET /pipeline/{id} (DB only) and short-circuits on job.input.connector === 'cdc'; batch keeps the pipeline_run check. Adds src/utils/cdc.ts as the single isCdc* helper. Co-Authored-By: WOZCODE <contact@withwoz.com>
91 lines
3.0 KiB
TypeScript
91 lines
3.0 KiB
TypeScript
import {
|
|
BadRequestException,
|
|
CanActivate,
|
|
ExecutionContext,
|
|
Inject,
|
|
Injectable,
|
|
OnModuleInit,
|
|
} from '@nestjs/common';
|
|
import { ClientGrpc } from '@nestjs/microservices';
|
|
import { map, Observable } from 'rxjs';
|
|
import { PackTheMetadata } from 'src/utils/PackTheMetadata';
|
|
import {
|
|
ReadService,
|
|
ProtoServices,
|
|
} from '@dadosfera/protospack-v2/dist/lib/PipelineV2';
|
|
import { PipelinesClientConfiguration } from 'src/modules/pipelinesV2/pipelines-client';
|
|
import { PlatformApiService } from 'src/modules/platform-api/platform-api.service';
|
|
import DadosferaLogger from '@dadosfera/dadosfera-logs';
|
|
import { isCdcPipeline } from 'src/utils/cdc';
|
|
|
|
@Injectable()
|
|
export class PipelineExecutionGuard implements CanActivate {
|
|
logger: DadosferaLogger;
|
|
|
|
constructor(
|
|
@Inject(DadosferaLogger)
|
|
dadosferaLogger: DadosferaLogger,
|
|
private readonly platformApiService: PlatformApiService,
|
|
) {
|
|
this.logger = dadosferaLogger.logger;
|
|
}
|
|
|
|
async canActivate(context: ExecutionContext): Promise<boolean> {
|
|
try {
|
|
this.logger.info(
|
|
'PipelineExecutionGuard: Checking if pipeline can be executed...',
|
|
);
|
|
const request = context.switchToHttp().getRequest();
|
|
const pipelineId = request.params.pipelineId;
|
|
const user = request.user;
|
|
const idRegex = /[^0-9a-zA-Z_$]+/g;
|
|
const convertedId = pipelineId.replace(idRegex, '_');
|
|
|
|
// Decide by the pipeline's explicit type (platform job
|
|
// `input.connector`), never by the absence of run history.
|
|
const pipeline = await this.platformApiService.proxy(
|
|
'GET',
|
|
`/pipeline/${convertedId}`,
|
|
user,
|
|
);
|
|
|
|
if (isCdcPipeline(pipeline)) {
|
|
// CDC pipelines have no batch runs; the platform-api gates connector
|
|
// edits itself (require_pipeline_running on add/remove tables).
|
|
this.logger.info('PipelineExecutionGuard: CDC pipeline, no run to block on');
|
|
return true;
|
|
}
|
|
|
|
const status = await this.platformApiService.proxy(
|
|
'GET',
|
|
`/pipeline/${convertedId}/pipeline_run`,
|
|
user,
|
|
);
|
|
|
|
const currentStatus = status?.[status.length - 1];
|
|
|
|
this.logger.info('Pipeline current status response:' + JSON.stringify(currentStatus));
|
|
|
|
// Batch pipeline that never ran yet: nothing can be executing.
|
|
if (!currentStatus?.last_status) {
|
|
return true;
|
|
}
|
|
|
|
if (currentStatus.last_status.toLowerCase() === 'running') {
|
|
this.logger.error('Pipeline is running, cannot update input now');
|
|
throw new BadRequestException('Pipeline is running, cannot update input now');
|
|
}
|
|
|
|
return true;
|
|
} catch (error) {
|
|
// Preserve the deliberate is-running rejection; only wrap genuine
|
|
// status-check failures (fail closed on those for a destructive gate).
|
|
if (error instanceof BadRequestException) {
|
|
throw error;
|
|
}
|
|
this.logger.error('Error in PipelineExecutionGuard: ' + error.message);
|
|
throw new BadRequestException('Error checking pipeline status: ' + error.message);
|
|
}
|
|
}
|
|
}
|