From f3ed0bd163b3f7df40edc759eabee01ecfcf0da3 Mon Sep 17 00:00:00 2001 From: MarineLeM Date: Wed, 13 Nov 2024 16:53:08 +0100 Subject: [PATCH] [backend] throw error when asset is inactive (#1769) --- .../io/openbas/rest/inject/InjectApi.java | 15 +-- .../scheduler/jobs/InjectsExecutionJob.java | 103 ++++++++---------- .../execution/ExecutionExecutorService.java | 2 + .../InjectExpectationUtils.java | 10 -- .../io/openbas/database/model/Execution.java | 9 +- .../database/model/ExecutionStatus.java | 11 +- .../database/model/ExecutionTraceStatus.java | 13 +++ .../database/model/InjectStatusExecution.java | 22 ++-- 8 files changed, 89 insertions(+), 96 deletions(-) create mode 100644 openbas-model/src/main/java/io/openbas/database/model/ExecutionTraceStatus.java diff --git a/openbas-api/src/main/java/io/openbas/rest/inject/InjectApi.java b/openbas-api/src/main/java/io/openbas/rest/inject/InjectApi.java index d836c42e76..e46165e648 100644 --- a/openbas-api/src/main/java/io/openbas/rest/inject/InjectApi.java +++ b/openbas-api/src/main/java/io/openbas/rest/inject/InjectApi.java @@ -114,7 +114,7 @@ public Inject injectExecutionCallback( Inject inject = injectRepository.findById(injectId).orElseThrow(ElementNotFoundException::new); InjectStatus injectStatus = inject.getStatus().orElseThrow(ElementNotFoundException::new); - ExecutionStatus executionStatus = ExecutionStatus.valueOf(input.getStatus()); + ExecutionTraceStatus executionStatus = ExecutionTraceStatus.valueOf(input.getStatus()); InjectStatusExecution execution = new InjectStatusExecution(); Instant trackingEndDate = now(); execution.setTime(trackingEndDate); @@ -122,7 +122,7 @@ public Inject injectExecutionCallback( execution.setMessage(input.getMessage()); execution.setIdentifiers(input.getIdentifiers()); injectStatus.getTraces().add(execution); - if (executionStatus.equals(ExecutionStatus.SUCCESS)) { + if (executionStatus.equals(ExecutionTraceStatus.SUCCESS)) { injectStatus.setTrackingTotalSuccess(injectStatus.getTrackingTotalSuccess() + 1); } else { injectStatus.setTrackingTotalError(injectStatus.getTrackingTotalSuccess() + 1); @@ -139,13 +139,14 @@ public Inject injectExecutionCallback( long errorCounter = 0; long maybePreventedCounter = 0; for (InjectStatusExecution injectStatusExecution : injectStatus.getTraces()) { - ExecutionStatus status = injectStatusExecution.getStatus(); - if (status == ExecutionStatus.SUCCESS || status == ExecutionStatus.WARNING) { + ExecutionTraceStatus status = injectStatusExecution.getStatus(); + if (status == ExecutionTraceStatus.SUCCESS || status == ExecutionTraceStatus.WARNING) { successCounter++; - } else if (status == ExecutionStatus.ERROR || status == ExecutionStatus.COMMAND_NOT_FOUND) { + } else if (status == ExecutionTraceStatus.ERROR + || status == ExecutionTraceStatus.COMMAND_NOT_FOUND) { errorCounter++; - } else if (status == ExecutionStatus.MAYBE_PREVENTED - || status == ExecutionStatus.COMMAND_CANNOT_BE_EXECUTED) { + } else if (status == ExecutionTraceStatus.MAYBE_PREVENTED + || status == ExecutionTraceStatus.COMMAND_CANNOT_BE_EXECUTED) { maybePreventedCounter++; } } diff --git a/openbas-api/src/main/java/io/openbas/scheduler/jobs/InjectsExecutionJob.java b/openbas-api/src/main/java/io/openbas/scheduler/jobs/InjectsExecutionJob.java index 241ff3ec22..97ae32838a 100644 --- a/openbas-api/src/main/java/io/openbas/scheduler/jobs/InjectsExecutionJob.java +++ b/openbas-api/src/main/java/io/openbas/scheduler/jobs/InjectsExecutionJob.java @@ -11,6 +11,7 @@ import io.openbas.execution.ExecutionExecutorService; import io.openbas.helper.InjectHelper; import io.openbas.service.AtomicTestingService; +import jakarta.annotation.Nullable; import jakarta.annotation.Resource; import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; @@ -213,30 +214,11 @@ private void setInjectStatusAndExecuteInject(ExecutableInject executableInject, injectorContract -> { if (!inject.isReady()) { // Status - if (inject.getStatus().isEmpty()) { - InjectStatus status = new InjectStatus(); - status - .getTraces() - .add( - InjectStatusExecution.traceError( - "The inject is not ready to be executed (missing mandatory fields)")); - status.setName(ExecutionStatus.ERROR); - status.setTrackingSentDate(Instant.now()); - status.setInject(inject); - status.setCommandsLines(atomicTestingService.getCommandsLinesFromInject(inject)); - injectStatusRepository.save(status); - } else { - InjectStatus status = inject.getStatus().get(); - status - .getTraces() - .add( - InjectStatusExecution.traceError( - "The inject is not ready to be executed (missing mandatory fields)")); - status.setName(ExecutionStatus.ERROR); - status.setTrackingSentDate(Instant.now()); - status.setCommandsLines(atomicTestingService.getCommandsLinesFromInject(inject)); - injectStatusRepository.save(status); - } + initializeInjectStatus( + inject, + ExecutionStatus.ERROR, + InjectStatusExecution.traceError( + "The inject is not ready to be executed (missing mandatory fields)")); return; } @@ -248,27 +230,24 @@ private void setInjectStatusAndExecuteInject(ExecutableInject executableInject, // Executor logics ExecutableInject newExecutableInject = executableInject; if (Boolean.TRUE.equals(injectorContract.getNeedsExecutor())) { + // Status + InjectStatus statusSaved = + initializeInjectStatus(inject, ExecutionStatus.EXECUTING, null); try { - // Status - if (inject.getStatus().isEmpty()) { - InjectStatus status = new InjectStatus(); - status.setName(ExecutionStatus.EXECUTING); - status.setTrackingSentDate(Instant.now()); - status.setInject(inject); - status.setCommandsLines( - atomicTestingService.getCommandsLinesFromInject(inject)); - injectStatusRepository.save(status); - } else { - InjectStatus status = inject.getStatus().get(); - status.setName(ExecutionStatus.EXECUTING); - status.setTrackingSentDate(Instant.now()); - status.setCommandsLines( - atomicTestingService.getCommandsLinesFromInject(inject)); - injectStatusRepository.save(status); - } newExecutableInject = this.executionExecutorService.launchExecutorContext(executableInject, inject); - } catch (InterruptedException e) { + } catch (Exception e) { + ExecutionTraceStatus traceStatus = + e.getMessage().startsWith("Asset error") + ? ExecutionTraceStatus.ASSET_INACTIVE + : ExecutionTraceStatus.ERROR; + + statusSaved + .getTraces() + .add(InjectStatusExecution.traceError(traceStatus, e.getMessage())); + + statusSaved.setName(ExecutionStatus.ERROR); + injectStatusRepository.save(statusSaved); throw new RuntimeException(e); } } @@ -278,26 +257,32 @@ private void setInjectStatusAndExecuteInject(ExecutableInject executableInject, executeInternal(newExecutableInject); } }, - () -> setInjectStatusWhenNoInjectorContractExists(inject)); + () -> + initializeInjectStatus( + inject, + ExecutionStatus.ERROR, + InjectStatusExecution.traceError("Inject does not have a contract"))); } - private void setInjectStatusWhenNoInjectorContractExists(Inject inject) { - if (inject.getStatus().isEmpty()) { - InjectStatus status = new InjectStatus(); - status.getTraces().add(InjectStatusExecution.traceError("Inject does not have a contract")); - status.setName(ExecutionStatus.ERROR); - status.setTrackingSentDate(Instant.now()); - status.setInject(inject); - status.setCommandsLines(atomicTestingService.getCommandsLinesFromInject(inject)); - injectStatusRepository.save(status); - } else { - InjectStatus status = inject.getStatus().get(); - status.getTraces().add(InjectStatusExecution.traceError("Inject does not have a contract")); - status.setName(ExecutionStatus.ERROR); - status.setTrackingSentDate(Instant.now()); - status.setCommandsLines(atomicTestingService.getCommandsLinesFromInject(inject)); - injectStatusRepository.save(status); + private InjectStatus initializeInjectStatus( + Inject inject, ExecutionStatus status, @Nullable InjectStatusExecution trace) { + InjectStatus injectStatus = + inject + .getStatus() + .orElseGet( + () -> { + InjectStatus newStatus = new InjectStatus(); + newStatus.setInject(inject); + return newStatus; + }); + + if (trace != null) { + injectStatus.getTraces().add(trace); } + injectStatus.setName(status); + injectStatus.setTrackingSentDate(Instant.now()); + injectStatus.setCommandsLines(atomicTestingService.getCommandsLinesFromInject(inject)); + return injectStatusRepository.save(injectStatus); } /** diff --git a/openbas-framework/src/main/java/io/openbas/execution/ExecutionExecutorService.java b/openbas-framework/src/main/java/io/openbas/execution/ExecutionExecutorService.java index 28f5d6ffd3..a71d6b5d72 100644 --- a/openbas-framework/src/main/java/io/openbas/execution/ExecutionExecutorService.java +++ b/openbas-framework/src/main/java/io/openbas/execution/ExecutionExecutorService.java @@ -90,6 +90,8 @@ private void launchExecutorContextForAsset(Inject inject, Asset asset) { Executor executor = asset.getExecutor(); if (executor == null) { log.log(Level.SEVERE, "Cannot find the executor for the asset " + asset.getName()); + } else if (!asset.getActive()) { + throw new RuntimeException("Asset error: " + asset.getName() + " is inactive"); } else { switch (executor.getType()) { case "openbas_caldera" -> { diff --git a/openbas-framework/src/main/java/io/openbas/inject_expectation/InjectExpectationUtils.java b/openbas-framework/src/main/java/io/openbas/inject_expectation/InjectExpectationUtils.java index eaa97252d2..f01fa62558 100644 --- a/openbas-framework/src/main/java/io/openbas/inject_expectation/InjectExpectationUtils.java +++ b/openbas-framework/src/main/java/io/openbas/inject_expectation/InjectExpectationUtils.java @@ -1,10 +1,7 @@ package io.openbas.inject_expectation; -import static io.openbas.database.model.InjectStatusExecution.EXECUTION_TYPE_COMMAND; - import io.openbas.database.model.InjectExpectation; import io.openbas.database.model.InjectExpectationResult; -import io.openbas.database.model.InjectStatusExecution; import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotNull; import java.time.Instant; @@ -42,11 +39,4 @@ public static void computeResult( expectation.getResults().add(expectationResult); } } - - public static Optional getCommandLine(@NotNull final InjectExpectation expectation) { - return expectation.getInject().getStatus().orElseThrow().getTraces().stream() - .filter(trace -> trace.getCategory().equals(EXECUTION_TYPE_COMMAND)) - .findFirst() - .map(InjectStatusExecution::getMessage); - } } diff --git a/openbas-model/src/main/java/io/openbas/database/model/Execution.java b/openbas-model/src/main/java/io/openbas/database/model/Execution.java index d5c465a070..bdd38a0e31 100644 --- a/openbas-model/src/main/java/io/openbas/database/model/Execution.java +++ b/openbas-model/src/main/java/io/openbas/database/model/Execution.java @@ -61,8 +61,8 @@ public void stop() { } public void addTrace(InjectStatusExecution context) { - ExecutionStatus status = context.getStatus(); - if (status.equals(ExecutionStatus.SUCCESS) || status.equals(ExecutionStatus.INFO)) { + ExecutionTraceStatus status = context.getStatus(); + if (status.equals(ExecutionTraceStatus.SUCCESS) || status.equals(ExecutionTraceStatus.INFO)) { LOGGER.log(Level.INFO, context.getMessage()); } else { LOGGER.log(Level.SEVERE, context.getMessage()); @@ -77,9 +77,10 @@ public int getExecutionTime() { public ExecutionStatus getStatus() { boolean hasSuccess = - traces.stream().anyMatch(context -> context.getStatus().equals(ExecutionStatus.SUCCESS)); + traces.stream() + .anyMatch(context -> context.getStatus().equals(ExecutionTraceStatus.SUCCESS)); boolean hasError = - traces.stream().anyMatch(context -> context.getStatus().equals(ExecutionStatus.ERROR)); + traces.stream().anyMatch(context -> context.getStatus().equals(ExecutionTraceStatus.ERROR)); if (!hasSuccess && !hasError) { return ExecutionStatus.PENDING; } else if (hasSuccess && hasError) { diff --git a/openbas-model/src/main/java/io/openbas/database/model/ExecutionStatus.java b/openbas-model/src/main/java/io/openbas/database/model/ExecutionStatus.java index 04dbfaf8f1..e2c3d36bc5 100644 --- a/openbas-model/src/main/java/io/openbas/database/model/ExecutionStatus.java +++ b/openbas-model/src/main/java/io/openbas/database/model/ExecutionStatus.java @@ -1,17 +1,14 @@ package io.openbas.database.model; public enum ExecutionStatus { + SUCCESS, + ERROR, + MAYBE_PREVENTED, + DRAFT, - INFO, QUEUING, EXECUTING, PENDING, PARTIAL, - ERROR, - WARNING, - COMMAND_NOT_FOUND, - COMMAND_CANNOT_BE_EXECUTED, MAYBE_PARTIAL_PREVENTED, - MAYBE_PREVENTED, - SUCCESS } diff --git a/openbas-model/src/main/java/io/openbas/database/model/ExecutionTraceStatus.java b/openbas-model/src/main/java/io/openbas/database/model/ExecutionTraceStatus.java new file mode 100644 index 0000000000..390c1843c6 --- /dev/null +++ b/openbas-model/src/main/java/io/openbas/database/model/ExecutionTraceStatus.java @@ -0,0 +1,13 @@ +package io.openbas.database.model; + +public enum ExecutionTraceStatus { + SUCCESS, + ERROR, + MAYBE_PREVENTED, + + INFO, + COMMAND_NOT_FOUND, + COMMAND_CANNOT_BE_EXECUTED, + WARNING, + ASSET_INACTIVE, +} diff --git a/openbas-model/src/main/java/io/openbas/database/model/InjectStatusExecution.java b/openbas-model/src/main/java/io/openbas/database/model/InjectStatusExecution.java index 9a5b509410..5ae13329aa 100644 --- a/openbas-model/src/main/java/io/openbas/database/model/InjectStatusExecution.java +++ b/openbas-model/src/main/java/io/openbas/database/model/InjectStatusExecution.java @@ -28,7 +28,7 @@ public class InjectStatusExecution { private String category = "standard"; // standard / command_line / ?? @JsonProperty("execution_status") - private ExecutionStatus status; + private ExecutionTraceStatus status; @JsonProperty("execution_context_identifiers") private List identifiers = new ArrayList<>(); @@ -38,7 +38,7 @@ public InjectStatusExecution() { } public InjectStatusExecution( - ExecutionStatus status, List identifiers, String message, String category) { + ExecutionTraceStatus status, List identifiers, String message, String category) { this.status = status; this.identifiers = identifiers; this.message = message; @@ -48,36 +48,40 @@ public InjectStatusExecution( public static InjectStatusExecution traceInfo(String message) { return new InjectStatusExecution( - ExecutionStatus.INFO, List.of(), message, EXECUTION_TYPE_STANDARD); + ExecutionTraceStatus.INFO, List.of(), message, EXECUTION_TYPE_STANDARD); } public static InjectStatusExecution traceInfo(String category, String message) { - return new InjectStatusExecution(ExecutionStatus.INFO, List.of(), message, category); + return new InjectStatusExecution(ExecutionTraceStatus.INFO, List.of(), message, category); } public static InjectStatusExecution traceInfo(String message, List identifiers) { return new InjectStatusExecution( - ExecutionStatus.INFO, identifiers, message, EXECUTION_TYPE_STANDARD); + ExecutionTraceStatus.INFO, identifiers, message, EXECUTION_TYPE_STANDARD); } public static InjectStatusExecution traceSuccess(String message) { return new InjectStatusExecution( - ExecutionStatus.SUCCESS, List.of(), message, EXECUTION_TYPE_STANDARD); + ExecutionTraceStatus.SUCCESS, List.of(), message, EXECUTION_TYPE_STANDARD); } public static InjectStatusExecution traceSuccess(String message, List userIds) { return new InjectStatusExecution( - ExecutionStatus.SUCCESS, userIds, message, EXECUTION_TYPE_STANDARD); + ExecutionTraceStatus.SUCCESS, userIds, message, EXECUTION_TYPE_STANDARD); } public static InjectStatusExecution traceError(String message) { return new InjectStatusExecution( - ExecutionStatus.ERROR, List.of(), message, EXECUTION_TYPE_STANDARD); + ExecutionTraceStatus.ERROR, List.of(), message, EXECUTION_TYPE_STANDARD); + } + + public static InjectStatusExecution traceError(ExecutionTraceStatus status, String message) { + return new InjectStatusExecution(status, List.of(), message, EXECUTION_TYPE_STANDARD); } public static InjectStatusExecution traceMaybePrevented(String message) { return new InjectStatusExecution( - ExecutionStatus.MAYBE_PREVENTED, List.of(), message, EXECUTION_TYPE_STANDARD); + ExecutionTraceStatus.MAYBE_PREVENTED, List.of(), message, EXECUTION_TYPE_STANDARD); } @Override