Skip to content

Commit

Permalink
[backend] throw error when asset is inactive (#1769)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarineLeM authored Nov 13, 2024
1 parent 5609c26 commit f3ed0bd
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 96 deletions.
15 changes: 8 additions & 7 deletions openbas-api/src/main/java/io/openbas/rest/inject/InjectApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,15 @@ 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);
execution.setStatus(executionStatus);
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);
Expand All @@ -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++;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand All @@ -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);
}
}
Expand All @@ -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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -42,11 +39,4 @@ public static void computeResult(
expectation.getResults().add(expectationResult);
}
}

public static Optional<String> getCommandLine(@NotNull final InjectExpectation expectation) {
return expectation.getInject().getStatus().orElseThrow().getTraces().stream()
.filter(trace -> trace.getCategory().equals(EXECUTION_TYPE_COMMAND))
.findFirst()
.map(InjectStatusExecution::getMessage);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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,
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> identifiers = new ArrayList<>();
Expand All @@ -38,7 +38,7 @@ public InjectStatusExecution() {
}

public InjectStatusExecution(
ExecutionStatus status, List<String> identifiers, String message, String category) {
ExecutionTraceStatus status, List<String> identifiers, String message, String category) {
this.status = status;
this.identifiers = identifiers;
this.message = message;
Expand All @@ -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<String> 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<String> 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
Expand Down

0 comments on commit f3ed0bd

Please sign in to comment.