Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[frontend]Show prerequisites info in atomic testing #1858

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@ import { Chip, Grid, List, Paper, Tooltip, Typography } from '@mui/material';
import { makeStyles } from '@mui/styles';
import { useContext, useEffect, useState } from 'react';

import { fetchDocuments } from '../../../../actions/Document';
import { DocumentHelper } from '../../../../actions/helper';
import Empty from '../../../../components/Empty';
import ExpandableMarkdown from '../../../../components/ExpandableMarkdown';
import { useFormatter } from '../../../../components/i18n';
import ItemStatus from '../../../../components/ItemStatus';
import Loader from '../../../../components/Loader';
import PlatformIcon from '../../../../components/PlatformIcon';
import SearchFilter from '../../../../components/SearchFilter';
import { useHelper } from '../../../../store';
import type { AttackPatternSimple, InjectTargetWithResult, KillChainPhaseSimple } from '../../../../utils/api-types';
import { useAppDispatch } from '../../../../utils/hooks';
import useDataLoader from '../../../../utils/hooks/useDataLoader';
import useSearchAnFilter from '../../../../utils/SortingFiltering';
import { isNotEmptyField } from '../../../../utils/utils';
import InjectIcon from '../../common/injects/InjectIcon';
Expand Down Expand Up @@ -42,13 +47,20 @@ const useStyles = makeStyles(() => ({
const AtomicTesting = () => {
// Standard hooks
const classes = useStyles();
const dispatch = useAppDispatch();
const { t, tPick, fldt } = useFormatter();
const [selectedTarget, setSelectedTarget] = useState<InjectTargetWithResult>();
const [currentParentTarget, setCurrentParentTarget] = useState<InjectTargetWithResult>();
const filtering = useSearchAnFilter('', 'name', ['name']);

// Fetching data
const { injectResultOverviewOutput } = useContext<InjectResultOverviewOutputContextType>(InjectResultOverviewOutputContext);
const { documentMap } = useHelper((helper: DocumentHelper) => ({
documentMap: helper.getDocumentsMap(),
}));
useDataLoader(() => {
dispatch(fetchDocuments());
});
useEffect(() => {
setSelectedTarget(selectedTarget || currentParentTarget || injectResultOverviewOutput?.inject_targets[0]);
}, [injectResultOverviewOutput]);
Expand Down Expand Up @@ -79,7 +91,7 @@ const AtomicTesting = () => {
</Typography>
<Paper classes={{ root: classes.paper }} variant="outlined">
<Grid container spacing={3}>
<Grid item xs={12} style={{ paddingTop: 10 }}>
<Grid item xs={8} style={{ paddingTop: 10 }}>
<Typography
variant="h3"
gutterBottom
Expand All @@ -92,6 +104,16 @@ const AtomicTesting = () => {
limit={300}
/>
</Grid>
<Grid item xs={4} style={{ paddingTop: 10 }}>
<Typography
variant="h3"
gutterBottom
style={{ marginTop: 20 }}
>
{t('Status')}
</Typography>
<ItemStatus isInject={true} status={injectResultOverviewOutput.inject_status?.status_name} label={t(injectResultOverviewOutput.inject_status?.status_name ?? 'Unknown')} />
</Grid>
<Grid item xs={4} style={{ paddingTop: 10 }}>
<Typography
variant="h3"
Expand Down Expand Up @@ -142,13 +164,23 @@ const AtomicTesting = () => {
gutterBottom
style={{ marginTop: 20 }}
>
{t('Status')}
{t('Documents')}
</Typography>
<ItemStatus
isInject={true}
status={injectResultOverviewOutput.inject_status?.status_name}
label={t(injectResultOverviewOutput.inject_status?.status_name ?? 'Unknown')}
/>
{
injectResultOverviewOutput.injects_documents !== undefined && injectResultOverviewOutput.injects_documents.length > 0
? injectResultOverviewOutput.injects_documents.map((documentId) => {
const document = documentMap[documentId];
return (
<Typography key={documentId} variant="body1">
{document?.document_name ?? '-'}
</Typography>
);
}) : (
<Typography variant="body1" gutterBottom>
-
</Typography>
)
}
</Grid>
<Grid item xs={4} style={{ paddingTop: 10 }}>
<Typography
Expand Down
Loading