12
12
import io .openbas .database .repository .TeamRepository ;
13
13
import io .openbas .database .repository .UserRepository ;
14
14
import io .openbas .injector_contract .outputs .ContractOutputElement ;
15
- import io .openbas .injector_contract .outputs .ContractOutputUtils ;
16
15
import io .openbas .rest .inject .service .InjectService ;
17
16
import jakarta .annotation .Resource ;
18
17
import jakarta .persistence .EntityNotFoundException ;
19
18
import jakarta .validation .constraints .NotBlank ;
20
19
import java .util .ArrayList ;
21
20
import java .util .List ;
22
- import java .util .Optional ;
23
21
import java .util .Set ;
24
22
import lombok .RequiredArgsConstructor ;
25
23
import lombok .extern .slf4j .Slf4j ;
@@ -87,11 +85,11 @@ public void deleteFinding(@NotNull final String id) {
87
85
// This structrued output is generated based on injectorcontract where we can find the node
88
86
// Outputs and with that the injector generate this structure output--
89
87
90
- public void extractFindingsFromInjectorContract (Inject inject , ObjectNode structuredOutput ) {
88
+ public List <SimpleFinding > extractFindingsFromInjectorContract (
89
+ String injectId , InjectorContract injectorContract , ObjectNode structuredOutput ) {
91
90
// NOTE: do it in every call to callback ? (reflexion on implant mechanism)
92
- List <Finding > findings = new ArrayList <>();
91
+ List <SimpleFinding > findings = new ArrayList <>();
93
92
// Get the contract
94
- InjectorContract injectorContract = inject .getInjectorContract ().orElseThrow ();
95
93
List <ContractOutputElement > contractOutputs =
96
94
getContractOutputs (injectorContract .getConvertedContent (), mapper );
97
95
if (!contractOutputs .isEmpty ()) {
@@ -105,61 +103,59 @@ public void extractFindingsFromInjectorContract(Inject inject, ObjectNode struct
105
103
if (!contractOutput .getType ().validate .apply (jsonNode )) {
106
104
throw new IllegalArgumentException ("Finding not correctly formatted" );
107
105
}
108
- Finding finding = ContractOutputUtils .createFinding (contractOutput );
106
+ SimpleFinding finding = new SimpleFinding ();
107
+ finding .setType (contractOutput .getType ().toString ());
108
+ finding .setField (contractOutput .getField ());
109
+ finding .setLabels (contractOutput .getLabels ());
109
110
finding .setValue (contractOutput .getType ().toFindingValue .apply (jsonNode ));
110
- Finding linkedFinding = linkFindings (contractOutput , jsonNode , finding );
111
- findings .add (linkedFinding );
111
+ linkFindings (contractOutput , jsonNode , finding );
112
+ findings .add (finding );
112
113
}
113
114
}
114
115
} else {
115
116
JsonNode jsonNode = structuredOutput .get (contractOutput .getField ());
116
117
if (!contractOutput .getType ().validate .apply (jsonNode )) {
117
118
throw new IllegalArgumentException ("Finding not correctly formatted" );
118
119
}
119
- Finding finding = ContractOutputUtils .createFinding (contractOutput );
120
+ SimpleFinding finding = new SimpleFinding ();
121
+ finding .setType (contractOutput .getType ().toString ());
122
+ finding .setField (contractOutput .getField ());
123
+ finding .setLabels (contractOutput .getLabels ());
120
124
finding .setValue (contractOutput .getType ().toFindingValue .apply (jsonNode ));
121
- Finding linkedFinding = linkFindings (contractOutput , jsonNode , finding );
122
- findings .add (linkedFinding );
125
+ linkFindings (contractOutput , jsonNode , finding );
126
+ findings .add (finding );
123
127
}
124
128
}
125
129
});
126
130
}
127
- this .createFindings (findings , inject .getId ());
131
+ findings .forEach ((finding ) -> finding .setInjectId (injectId ));
132
+ return findings ;
128
133
}
129
134
130
- private Finding linkFindings (
131
- ContractOutputElement contractOutput , JsonNode jsonNode , Finding finding ) {
135
+ private void linkFindings (
136
+ ContractOutputElement contractOutput , JsonNode jsonNode , SimpleFinding finding ) {
132
137
// Create links with assets
133
138
if (contractOutput .getType ().toFindingAssets != null ) {
134
139
List <String > assetsIds = contractOutput .getType ().toFindingAssets .apply (jsonNode );
135
- List <Optional <Asset >> assets =
136
- assetsIds .stream ().map (this .assetRepository ::findById ).toList ();
137
- if (!assets .isEmpty ()) {
138
- finding .setAssets (assets .stream ().filter (Optional ::isPresent ).map (Optional ::get ).toList ());
139
- }
140
+ finding .setAssets (assetsIds );
140
141
}
141
142
// Create links with teams
142
143
if (contractOutput .getType ().toFindingTeams != null ) {
143
144
List <String > teamsIds = contractOutput .getType ().toFindingTeams .apply (jsonNode );
144
- List <Optional <Team >> teams = teamsIds .stream ().map (this .teamRepository ::findById ).toList ();
145
- if (!teams .isEmpty ()) {
146
- finding .setTeams (teams .stream ().filter (Optional ::isPresent ).map (Optional ::get ).toList ());
147
- }
145
+ finding .setTeams (teamsIds );
148
146
}
149
147
// Create links with users
150
148
if (contractOutput .getType ().toFindingUsers != null ) {
151
149
List <String > usersIds = contractOutput .getType ().toFindingUsers .apply (jsonNode );
152
- List <Optional <User >> users = usersIds .stream ().map (this .userRepository ::findById ).toList ();
153
- if (!users .isEmpty ()) {
154
- finding .setUsers (users .stream ().filter (Optional ::isPresent ).map (Optional ::get ).toList ());
155
- }
150
+ finding .setUsers (usersIds );
156
151
}
157
- return finding ;
158
152
}
159
153
160
154
/** Extracts findings from structured output that was generated using output parsers. */
161
- public void extractFindingsFromOutputParsers (
162
- Inject inject , Agent agent , Set <OutputParser > outputParsers , JsonNode structuredOutput ) {
155
+ public List <SimpleFinding > extractFindingsFromOutputParsers (
156
+ String injectId , String assetId , Set <OutputParser > outputParsers , JsonNode structuredOutput ) {
157
+
158
+ List <SimpleFinding > results = new ArrayList <>();
163
159
164
160
outputParsers .forEach (
165
161
outputParser -> {
@@ -176,15 +172,17 @@ public void extractFindingsFromOutputParsers(
176
172
throw new IllegalArgumentException ("Finding not correctly formatted" );
177
173
}
178
174
// Build and save the finding
179
- findingUtils .buildFinding (
180
- inject ,
181
- agent .getAsset (),
182
- contractOutputElement ,
183
- contractOutputElement .getType ().toFindingValue .apply (jsonNode ));
175
+ results .add (
176
+ findingUtils .buildSimplerFinding (
177
+ injectId ,
178
+ assetId ,
179
+ contractOutputElement ,
180
+ contractOutputElement .getType ().toFindingValue .apply (jsonNode )));
184
181
}
185
182
}
186
183
}
187
184
});
188
185
});
186
+ return results ;
189
187
}
190
188
}
0 commit comments