-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathrestapi.rs
More file actions
374 lines (297 loc) · 10 KB
/
restapi.rs
File metadata and controls
374 lines (297 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
use crate::utils::DisplayVec;
use goose::goose::{GooseMethod, GooseRequest, GooseUser, TransactionResult};
use reqwest::{Client, RequestBuilder};
use serde_json::Value as JsonValue;
use serde_json::json;
use std::sync::{
Arc,
atomic::{AtomicUsize, Ordering},
};
use urlencoding::encode;
// send_request sends a request to the server
//
// path is the path of the request, e.g. "/api/v2/sbom/count-by-package"
// user is the GooseUser to send the request with
// method is the HTTP method to use, e.g. GooseMethod::Get
// value is the JSON value to send in the request body
// client_method is the reqwest method to use, e.g. Client::get
async fn send_request(
path: String,
user: &mut GooseUser,
method: GooseMethod,
value: JsonValue,
client_method: fn(&Client, String) -> RequestBuilder,
) -> TransactionResult {
let url = user.build_url(&path)?;
let reqwest_request_builder = client_method(&user.client, url);
let goose_request = GooseRequest::builder()
.method(method)
.path(path.as_str())
.set_request_builder(reqwest_request_builder.json(&value))
.build();
let _response = user.request(goose_request).await?;
Ok(())
}
/// Send Sbom labels request using PUT method
pub async fn put_sbom_labels(
sbom_ids: Vec<String>,
counter: Arc<AtomicUsize>,
user: &mut GooseUser,
) -> TransactionResult {
let path = format!(
"/api/v2/sbom/{}/label",
sbom_ids[counter.fetch_add(1, Ordering::Relaxed) % sbom_ids.len()].clone()
);
let json = json!({
"source": "It's a put request",
"foo": "bar",
"space": "with space",
"empty": "",
});
send_request(path, user, GooseMethod::Put, json, Client::put).await
}
/// Send Sbom labels request using PATCH method
pub async fn patch_sbom_labels(
sbom_ids: Vec<String>,
counter: Arc<AtomicUsize>,
user: &mut GooseUser,
) -> TransactionResult {
let path = format!(
"/api/v2/sbom/{}/label",
sbom_ids[counter.fetch_add(1, Ordering::Relaxed) % sbom_ids.len()].clone()
);
let json = json!({
"source": "It's a patch request",
"foo": "bar",
"space": "with space",
"empty": "",
});
send_request(path, user, GooseMethod::Patch, json, Client::patch).await
}
pub async fn get_advisory(id: String, user: &mut GooseUser) -> TransactionResult {
let uri = format!("/api/v2/advisory/{}", encode(&format!("urn:uuid:{}", id)));
let _response = user.get(&uri).await?;
Ok(())
}
pub async fn download_advisory(id: String, user: &mut GooseUser) -> TransactionResult {
let uri = format!(
"/api/v2/advisory/{}/download",
encode(&format!("urn:uuid:{}", id))
);
let _response = user.get(&uri).await?;
Ok(())
}
pub async fn list_sbom_labels(user: &mut GooseUser) -> TransactionResult {
let filter_text = json!([
{ "key": "source"},
]);
let uri = format!(
"/api/v2/sbom-labels?filter_text={}&limit={}",
encode(&filter_text.to_string()),
1000
);
let _response = user.get(&uri).await?;
Ok(())
}
pub async fn count_by_package(purl: String, user: &mut GooseUser) -> TransactionResult {
let filter_text = json!([
{
"purl": purl,
"cpe": null
}
]);
send_request(
"/api/v2/sbom/count-by-package".to_string(),
user,
GooseMethod::Get,
filter_text,
Client::get,
)
.await?;
Ok(())
}
pub async fn list_advisory_labels(user: &mut GooseUser) -> TransactionResult {
let uri = format!(
"/api/v2/advisory-labels?filter_text={}&limit={}",
encode("type"),
1000
);
let _response = user.get(&uri).await?;
Ok(())
}
pub async fn list_advisory(user: &mut GooseUser) -> TransactionResult {
let _response = user.get("/api/v2/advisory").await?;
Ok(())
}
pub async fn list_advisory_paginated(user: &mut GooseUser) -> TransactionResult {
let _response = user.get("/api/v2/advisory?offset=100&limit=10").await?;
Ok(())
}
pub async fn get_advisory_by_doc_id(user: &mut GooseUser) -> TransactionResult {
// just pick one CVE that should be in the dump: CVE-2022-0981
let _response = user
.get("/api/v2/advisory?q=identifier%3dCVE-2022-0981")
.await?;
Ok(())
}
pub async fn search_advisory(user: &mut GooseUser) -> TransactionResult {
// search for whatever value is fine (e.g. 'this-string-is-not-important') to trigger the load
// on the search so decided for 'CVE-2021-' that also represents a potential user search
let _response = user.get("/api/v2/advisory?q=CVE-2021-").await?;
Ok(())
}
pub async fn list_importer(user: &mut GooseUser) -> TransactionResult {
let _response = user.get("/api/v2/importer").await?;
Ok(())
}
pub async fn list_organizations(user: &mut GooseUser) -> TransactionResult {
let _response = user.get("/api/v2/organization").await?;
Ok(())
}
pub async fn list_packages(user: &mut GooseUser) -> TransactionResult {
let _response = user.get("/api/v2/purl").await?;
Ok(())
}
pub async fn list_packages_paginated(user: &mut GooseUser) -> TransactionResult {
let _response = user.get("/api/v2/purl?offset=100&limit=10").await?;
Ok(())
}
pub async fn get_purl_details(purl_id: String, user: &mut GooseUser) -> TransactionResult {
let _response = user.get(&format!("/api/v2/purl/{purl_id}")).await?;
Ok(())
}
pub async fn search_purls(user: &mut GooseUser) -> TransactionResult {
let _response = user.get("/api/v2/purl?q=curl").await?;
Ok(())
}
pub async fn search_exact_purl(user: &mut GooseUser) -> TransactionResult {
let _response = user.get("/api/v2/purl?q=name=curl").await?;
Ok(())
}
pub async fn list_products(user: &mut GooseUser) -> TransactionResult {
let _response = user.get("/api/v2/product").await?;
Ok(())
}
pub async fn list_sboms(user: &mut GooseUser) -> TransactionResult {
let _response = user.get("/api/v2/sbom").await?;
Ok(())
}
pub async fn list_sboms_paginated(user: &mut GooseUser) -> TransactionResult {
let _response = user.get("/api/v2/sbom?offset=100&limit=10").await?;
Ok(())
}
pub async fn list_vulnerabilities(user: &mut GooseUser) -> TransactionResult {
let _response = user.get("/api/v2/vulnerability").await?;
Ok(())
}
pub async fn list_vulnerabilities_paginated(user: &mut GooseUser) -> TransactionResult {
let _response = user
.get("/api/v2/vulnerability?offset=100&limit=10")
.await?;
Ok(())
}
pub async fn get_sbom(sbom_id: String, user: &mut GooseUser) -> TransactionResult {
let _response = user.get(&format!("/api/v2/sbom/{sbom_id}")).await?;
Ok(())
}
pub async fn get_sbom_advisories(sbom_id: String, user: &mut GooseUser) -> TransactionResult {
let _response = user
.get(&format!("/api/v2/sbom/{sbom_id}/advisory"))
.await?;
Ok(())
}
pub async fn get_sbom_packages(sbom_id: String, user: &mut GooseUser) -> TransactionResult {
let _response = user
.get(&format!("/api/v2/sbom/{sbom_id}/packages"))
.await?;
Ok(())
}
pub async fn get_sbom_related(sbom_id: String, user: &mut GooseUser) -> TransactionResult {
let _response = user.get(&format!("/api/v2/sbom/{sbom_id}/related")).await?;
Ok(())
}
pub async fn get_vulnerability(id: String, user: &mut GooseUser) -> TransactionResult {
let _response = user.get(&format!("/api/v2/vulnerability/{id}")).await?;
Ok(())
}
pub async fn sbom_by_package(purl: String, user: &mut GooseUser) -> TransactionResult {
let _response = user
.get(&format!("/api/v2/sbom/by-package?purl={}", encode(&purl)))
.await?;
Ok(())
}
pub async fn get_sbom_license_ids(sbom_id: String, user: &mut GooseUser) -> TransactionResult {
let _response = user
.get(&format!(
"/api/v2/sbom/{}/all-license-ids",
encode(&sbom_id)
))
.await?;
Ok(())
}
pub async fn get_analysis_status(user: &mut GooseUser) -> TransactionResult {
let _response = user.get("/api/v2/analysis/status").await?;
Ok(())
}
pub async fn get_analysis_latest_cpe(user: &mut GooseUser) -> TransactionResult {
let _response = user.get("/api/v2/analysis/latest/component/cpe%3A%2Fa%3Aredhat%3Aopenshift_builds%3A1.3%3A%3Ael9").await?;
Ok(())
}
pub async fn post_vulnerability_analyze(purl: String, user: &mut GooseUser) -> TransactionResult {
let _response = user
.post_json(
"/api/v2/vulnerability/analyze",
&json!({
"purls": [
purl
]
}),
)
.await?;
Ok(())
}
pub async fn search_licenses(user: &mut GooseUser) -> TransactionResult {
let _response = user.get("/api/v2/license?q=ASL&sort=license:desc").await?;
Ok(())
}
pub async fn search_sboms_by_license(user: &mut GooseUser) -> TransactionResult {
let _response = user
.get("/api/v2/sbom?q=license~GPL&sort=name:desc")
.await?;
Ok(())
}
pub async fn search_purls_by_license(user: &mut GooseUser) -> TransactionResult {
let _response = user
.get("/api/v2/purl?q=license~GPLv3+ with exceptions|Apache&sort=name:desc")
.await?;
Ok(())
}
pub async fn get_recommendations(
purls: DisplayVec<String>,
user: &mut GooseUser,
) -> TransactionResult {
let _response = user
.post_json(
"/api/v2/purl/recommend",
&json!({
"purls": purls
}),
)
.await?;
Ok(())
}
/// Delete an SBOM by ID from a pre-populated pool using sequential iteration
/// Sequentially iterates through the pool using an atomic counter
pub async fn delete_sbom_from_pool_sequential(
pool: Vec<String>,
counter: Arc<AtomicUsize>,
user: &mut GooseUser,
) -> TransactionResult {
// Get next index atomically and increment, wrapping around pool size
let index = counter.fetch_add(1, Ordering::Relaxed);
if index < pool.len() {
let sbom_id = &pool[index];
let _response = user.delete(&format!("/api/v2/sbom/{sbom_id}")).await?;
}
Ok(())
}