Skip to content

Commit

Permalink
add payloadOutput
Browse files Browse the repository at this point in the history
  • Loading branch information
johanah29 committed Nov 15, 2024
1 parent 33b5e76 commit b1e8cfe
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.openbas.database.model.InjectStatusCommandLine;
import io.openbas.rest.injector_contract.output.InjectorContractOutput;
import io.openbas.utils.AtomicTestingUtils.ExpectationResultsByType;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;

import java.time.Instant;
import java.util.ArrayList;
import java.util.List;

import lombok.Builder;
import lombok.Builder.Default;
import lombok.Getter;
Expand Down Expand Up @@ -57,7 +60,7 @@ public class InjectResultOverviewOutput {
@Schema(description = "Full contract")
@JsonProperty("inject_injector_contract")
@NotNull
private InjectorContractSimple injectorContract;
private InjectorContractOutput injectorContract;

@Schema(description = "status")
@JsonProperty("inject_status")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package io.openbas.rest.atomic_testing.form;

import com.fasterxml.jackson.annotation.JsonProperty;
import io.openbas.annotation.Queryable;
import io.openbas.database.model.*;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Setter
@Getter
@Builder
public class PayloadOutput {

@JsonProperty("payload_id")
private String id;

@JsonProperty("payload_type")
private String type;

@JsonProperty("payload_collector_type")
private String collectorType;

@JsonProperty("payload_name")
private String name;

@JsonProperty("payload_description")
private String description;

@JsonProperty("payload_platforms")
private Endpoint.PLATFORM_TYPE[] platforms = new Endpoint.PLATFORM_TYPE[0];

@JsonProperty("payload_attack_patterns")
private List<AttackPatternSimple> attackPatterns = new ArrayList<>();

@JsonProperty("payload_cleanup_executor")
private String cleanupExecutor;

@JsonProperty("payload_cleanup_command")
private String cleanupCommand;

@JsonProperty("payload_arguments")
private List<PayloadArgument> arguments = new ArrayList<>();

@JsonProperty("payload_prerequisites")
private List<PayloadPrerequisite> prerequisites = new ArrayList<>();

@JsonProperty("payload_external_id")
private String externalId;

@JsonProperty("payload_tags")
private Set<String> tags;


}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import io.openbas.database.model.Endpoint;
import io.openbas.database.model.Endpoint.PLATFORM_TYPE;
import io.openbas.rest.atomic_testing.form.PayloadOutput;
import jakarta.validation.constraints.NotBlank;
import java.util.*;
import lombok.Data;
Expand All @@ -25,7 +26,7 @@ public class InjectorContractOutput {
private PLATFORM_TYPE[] platforms;

@JsonProperty("injector_contract_payload_type")
private String payloadType;
private PayloadOutput payload;

@JsonProperty("injector_contract_injector_type")
private String injectorType;
Expand All @@ -41,16 +42,17 @@ public InjectorContractOutput(
Map<String, String> labels,
String content,
PLATFORM_TYPE[] platforms,
String payloadType,
PayloadOutput payload,
String collectorType,
String injectorType,
String[] attackPatterns,
Endpoint.PLATFORM_ARCH arch) {
this.id = id;
this.labels = labels;
this.co
this.content = content;
this.platforms = platforms;
this.payloadType = Optional.ofNullable(collectorType).orElse(payloadType);
this.payload = payload;
this.injectorType = injectorType;

this.attackPatterns =
Expand Down
31 changes: 22 additions & 9 deletions openbas-api/src/main/java/io/openbas/utils/InjectMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
import io.openbas.atomic_testing.TargetType;
import io.openbas.database.model.*;
import io.openbas.rest.atomic_testing.form.*;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

Expand Down Expand Up @@ -36,7 +39,7 @@ public InjectResultOverviewOutput toInjectResultOverviewOutput(Inject inject) {
.type(injectorContract.map(contract -> contract.getInjector().getType()).orElse(null))
.tagIds(inject.getTags().stream().map(Tag::getId).toList())
.documentIds(documentIds)
.injectorContract(toInjectorContractSimple(injectorContract))
.injectorContract(toInjectorContractOutput(injectorContract))
.status(toInjectStatusOutput(inject.getStatus()))
.expectations(toInjectExpectationSimples(inject.getExpectations()))
.killChainPhases(toKillChainPhasesSimples(inject.getKillChainPhases()))
Expand Down Expand Up @@ -67,7 +70,7 @@ public TargetSimple toTargetSimple(Object[] target, TargetType type) {
}

// -- INJECTORCONTRACT to INJECTORCONTRACT SIMPLE --
public InjectorContractSimple toInjectorContractSimple(
public InjectorContractSimple toInjectorContractOutput(
Optional<InjectorContract> injectorContract) {
return injectorContract
.map(
Expand All @@ -77,20 +80,30 @@ public InjectorContractSimple toInjectorContractSimple(
.content(contract.getContent())
.convertedContent(contract.getConvertedContent())
.platforms(contract.getPlatforms())
.payload(toPayloadSimple(Optional.ofNullable(contract.getPayload())))
.payload(toPayloadOutput(Optional.ofNullable(contract.getPayload())))
.labels(contract.getLabels())
.build())
.orElse(null);
}

private PayloadSimple toPayloadSimple(Optional<Payload> payload) {
private PayloadOutput toPayloadOutput(Optional<Payload> payload) {
return payload
.map(
payloadToSimple ->
PayloadSimple.builder()
.id(payloadToSimple.getId())
.type(payloadToSimple.getType())
.collectorType(payloadToSimple.getCollectorType())
payloadToOutput ->
PayloadOutput.builder()
.id(payloadToOutput.getId())
.type(payloadToOutput.getType())
.collectorType(payloadToOutput.getCollectorType())
.name(payloadToOutput.getName())
.description(payloadToOutput.getDescription())
.platforms(payloadToOutput.getPlatforms())
.attackPatterns(toAttackPatternSimples(payloadToOutput.getAttackPatterns()))
.cleanupExecutor(payloadToOutput.getCleanupExecutor())
.cleanupCommand(payloadToOutput.getCleanupCommand())
.arguments(payloadToOutput.getArguments())
.prerequisites(payloadToOutput.getPrerequisites())
.externalId(payloadToOutput.getExternalId())
.tags(payloadToOutput.getTags().stream().map(Tag::getId).collect(Collectors.toSet()))
.build())
.orElse(null);
}
Expand Down
Loading

0 comments on commit b1e8cfe

Please sign in to comment.