-
Notifications
You must be signed in to change notification settings - Fork 32
/
client.rs
660 lines (578 loc) · 25.3 KB
/
client.rs
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
use std::process::exit;
use std::sync::Arc;
use std::time::Duration;
use chrono::{DateTime, Utc};
use color_eyre::eyre::{eyre, Result};
use reqwest::header::HeaderMap;
use reqwest::{Client, Method, Request, StatusCode};
use tokio::time::sleep;
use tower::{Service, ServiceExt};
use tracing::{debug, error, info, Span};
use tracing_indicatif::span_ext::IndicatifSpanExt;
use url::Url;
use crate::cli::models::Token;
use crate::client::builder::RateLimitedService;
use crate::client::headers::GithubHeaders;
use crate::client::models::{Package, PackageVersion};
use crate::client::urls::Urls;
use crate::{Counts, PackageVersions};
#[derive(Debug)]
pub struct PackagesClient {
pub headers: HeaderMap,
pub urls: Urls,
pub fetch_package_service: RateLimitedService,
pub list_packages_service: RateLimitedService,
pub list_package_versions_service: RateLimitedService,
pub delete_package_versions_service: RateLimitedService,
pub token: Token,
}
impl PackagesClient {
pub async fn fetch_packages(
&mut self,
token: &Token,
image_names: &Vec<String>,
counts: Arc<Counts>,
) -> Vec<Package> {
if let Token::Temporal(_) = *token {
// If a repo is assigned the admin role under Package Settings > Manage Actions Access,
// then it can fetch a package's versions directly by name, and delete them. It cannot,
// however, list packages, so for this token type we are limited to fetching packages
// individually, by name
for image_name in image_names {
assert!(!(image_name.contains('!') || image_name.contains('*')), "Restrictions in the Github API prevent us from listing packages when using a $GITHUB_TOKEN token. Because of this, filtering with '!' and '*' are not supported for this token type. Image name {image_name} is therefore not valid.");
}
self.fetch_individual_packages(image_names, counts)
.await
.expect("Failed to fetch packages")
} else {
self.list_packages(self.urls.list_packages_url.clone(), counts)
.await
.expect("Failed to fetch packages")
}
}
async fn fetch_packages_with_pagination(
url: Url,
service: RateLimitedService,
headers: HeaderMap,
counts: Arc<Counts>,
) -> Result<Vec<Package>> {
let mut result = Vec::new();
let mut next_url = Some(url);
while let Some(current_url) = next_url {
debug!("Fetching data from {}", current_url);
// Construct these early, so we do as little work, holding a lock, as possible
let mut request = Request::new(Method::GET, current_url);
*request.headers_mut() = headers.clone();
// Get a lock on the rate limited tower service
// This has mechanisms for keeping us honest wrt. primary and secondary rate limits
let mut handle = service.lock().await;
if (*counts.package_versions.read().await) > (*counts.remaining_requests.read().await) {
error!("Returning without fetching all packages, since the remaining requests are less or equal to the number of package versions already selected");
return Ok(result);
}
// Wait for a green light from the service. This can wait upwards of a minute
// if we've just exceeded the per-minute max requests
let r = handle.ready().await;
// Handle possible error case
let response = match r {
Ok(t) => {
// Initiate the request and drop the handle before awaiting the result
// If we don't drop the handle, our request flow becomes synchronous
let fut = t.call(request);
drop(handle);
match fut.await {
Ok(t) => t,
Err(e) => return Err(eyre!("Request failed: {}", e)),
}
}
Err(e) => {
return Err(eyre!("Service failed to become ready: {}", e));
}
};
let response_headers = GithubHeaders::try_from(response.headers())?;
// Get the string value of the response first, so we can return it in
// a possible error. This will happen if one of our response structs
// are misconfigured, and is pretty helpful
let raw_json = response.text().await?;
let mut items: Vec<Package> = match serde_json::from_str(&raw_json) {
Ok(t) => t,
Err(e) => {
return Err(eyre!(
"Failed to deserialize paginated response: {raw_json}. The error was {e}."
));
}
};
result.append(&mut items);
next_url = if response_headers.x_ratelimit_remaining > 1 {
response_headers.next_link()
} else {
None
};
}
Ok(result)
}
async fn fetch_package_versions_with_pagination<F>(
url: Url,
service: RateLimitedService,
headers: HeaderMap,
counts: Arc<Counts>,
filter_fn: F,
rate_limit_offset: usize,
) -> Result<PackageVersions>
where
F: Fn(Vec<PackageVersion>) -> Result<PackageVersions>,
{
let mut result = PackageVersions::new();
let mut next_url = Some(url);
while let Some(current_url) = next_url {
if (*counts.package_versions.read().await) > (*counts.remaining_requests.read().await) + rate_limit_offset {
info!("Returning without fetching all package versions, since the remaining requests are less or equal to the number of package versions already selected");
return Ok(result);
}
debug!("Fetching data from {}", current_url);
// Construct these early, so we do as little work, holding a lock, as possible
let mut request = Request::new(Method::GET, current_url);
*request.headers_mut() = headers.clone();
// Get a lock on the rate limited tower service
// This has mechanisms for keeping us honest wrt. primary and secondary rate limits
let mut handle = service.lock().await;
// Wait for a green light from the service. This can wait upwards of a minute
// if we've just exceeded the per-minute max requests
let r = handle.ready().await;
// Handle possible error case
let response = match r {
Ok(t) => {
// Initiate the request and drop the handle before awaiting the result
// If we don't drop the handle, our request flow becomes synchronous
let fut = t.call(request);
drop(handle);
match fut.await {
Ok(t) => t,
Err(e) => return Err(eyre!("Request failed: {}", e)),
}
}
Err(e) => {
return Err(eyre!("Service failed to become ready: {}", e));
}
};
let response_headers = GithubHeaders::try_from(response.headers())?;
// Get the string value of the response first, so we can return it in
// a possible error. This will happen if one of our response structs
// are misconfigured, and is pretty helpful
let raw_json = response.text().await?;
let items: Vec<PackageVersion> = match serde_json::from_str(&raw_json) {
Ok(t) => t,
Err(e) => {
return Err(eyre!(
"Failed to deserialize paginated response: {raw_json}. The error was {e}."
));
}
};
let package_versions = filter_fn(items.clone())?;
debug!(
"Filtered out {}/{} package versions",
items.len() - package_versions.len(),
items.len()
);
// Decrement the rate limiter count
*counts.remaining_requests.write().await -= 1;
*counts.package_versions.write().await += package_versions.len();
result.extend(package_versions);
next_url = if response_headers.x_ratelimit_remaining > 1 {
response_headers.next_link()
} else {
None
};
Span::current().pb_set_message(&format!(
"fetched \x1b[33m{}\x1b[0m package versions (\x1b[33m{}\x1b[0m requests remaining in the rate limit)",
result.len(),
*counts.remaining_requests.read().await
));
}
Ok(result)
}
async fn list_packages(&mut self, url: Url, counts: Arc<Counts>) -> Result<Vec<Package>> {
Self::fetch_packages_with_pagination(url, self.list_packages_service.clone(), self.headers.clone(), counts)
.await
}
pub async fn list_package_versions<F>(
&self,
package_name: String,
counts: Arc<Counts>,
filter_fn: F,
rate_limit_offset: usize,
) -> Result<(String, PackageVersions)>
where
F: Fn(Vec<PackageVersion>) -> Result<PackageVersions>,
{
let url = self.urls.list_package_versions_url(&package_name)?;
let package_versions = Self::fetch_package_versions_with_pagination(
url,
self.list_package_versions_service.clone(),
self.headers.clone(),
counts,
filter_fn,
rate_limit_offset,
)
.await?;
info!(
package_name = package_name,
"Selected {} package versions",
package_versions.len()
);
Ok((package_name, package_versions))
}
async fn fetch_individual_package(&self, url: Url, counts: Arc<Counts>) -> Result<Package> {
debug!("Fetching package from {url}");
let mut request = Request::new(Method::GET, url);
*request.headers_mut() = self.headers.clone();
// Get a lock on the tower service which regulates our traffic
let mut handle = self.fetch_package_service.lock().await;
let response = {
// Wait until the service says we're OK to proceed
let r = handle.ready().await;
match r {
Ok(t) => {
// Initiate the request and drop the handle before awaiting the result
// If we don't drop the handle, our request flow becomes synchronous
let fut = t.call(request);
drop(handle);
match fut.await {
Ok(t) => t,
Err(e) => return Err(eyre!("Request failed: {}", e)),
}
}
Err(e) => {
return Err(eyre!("Service failed to become ready: {}", e));
}
}
};
*counts.remaining_requests.write().await -= 1;
GithubHeaders::try_from(response.headers())?;
let raw_json = response.text().await?;
Ok(serde_json::from_str(&raw_json)?)
}
async fn fetch_individual_packages(&self, package_names: &[String], counts: Arc<Counts>) -> Result<Vec<Package>> {
let mut futures = Vec::new();
for package_name in package_names {
let url = self.urls.fetch_package_url(package_name)?;
let fut = self.fetch_individual_package(url, counts.clone());
futures.push(fut);
}
let mut packages = Vec::new();
for fut in futures {
match fut.await {
Ok(package) => {
packages.push(package);
}
Err(e) => return Err(e),
}
}
Ok(packages)
}
/// Delete a package version.
/// Docs for organizations: <https://docs.github.com/en/rest/packages/packages?apiVersion=2022-11-28#delete-package-version-for-an-organization>
/// Docs for users: <https://docs.github.com/en/rest/packages/packages?apiVersion=2022-11-28#delete-a-package-version-for-the-authenticated-user>
pub async fn delete_package_version(
&self,
package_name: String,
package_version: PackageVersion,
dry_run: bool,
) -> std::result::Result<Vec<String>, Vec<String>> {
// Create a vec of all the permutations of package tags stored in this package version
// The vec will look something like ["foo:latest", "foo:production", "foo:2024-10-10T08:00:00"] given
// it had these three tags, and ["foo:untagged"] if it had no tags. This isn't really how things
// work, but is what users will expect to see output.
let names = if package_version.metadata.container.tags.is_empty() {
vec![format!("\x1b[34m{package_name}\x1b[0m:\x1b[33m<untagged>\x1b[0m")]
} else {
package_version
.metadata
.container
.tags
.iter()
.map(|tag| format!("\x1b[34m{package_name}\x1b[0m:\x1b[32m{tag}\x1b[0m"))
.collect()
};
// Output information to the user
if dry_run {
// Sleep a few ms to make logs appear "in order"
// These dry-run logs tend to appear before rate limiting warnings,
// and other logs if they're output right away.
sleep(Duration::from_millis(10)).await;
for name in &names {
info!(
package_version = package_version.id,
"dry-run: Would have deleted {name}"
);
}
return Ok(Vec::new());
}
// Construct URL for this package version
let url = match self.urls.delete_package_version_url(&package_name, &package_version.id) {
Ok(t) => t,
Err(e) => {
error!(
"Failed to create deletion URL for package {} and version {}: {}",
package_name, package_version.id, e
);
return Err(names);
}
};
// Construct initial request
let mut request = Request::new(Method::DELETE, url);
*request.headers_mut() = self.headers.clone();
// Get a lock on the tower service which regulates our traffic
let mut handle = self.delete_package_versions_service.lock().await;
let response = {
// Wait until the service says we're OK to proceed
let r = handle.ready().await;
match r {
Ok(t) => {
// Initiate the request and drop the handle before awaiting the result
// If we don't drop the handle, our request flow becomes synchronous
let fut = t.call(request);
drop(handle);
match fut.await {
Ok(t) => t,
Err(e) => {
error!(
"Failed to delete package version {} with error: {}",
package_version.id, e
);
return Err(names);
}
}
}
Err(e) => {
error!("Service failed to become ready: {}", e);
return Err(names);
}
}
};
match response.status() {
StatusCode::NO_CONTENT => {
for name in &names {
info!(package_version_id = package_version.id, "Deleted {name}");
}
Ok(names)
}
StatusCode::UNPROCESSABLE_ENTITY | StatusCode::BAD_REQUEST => {
error!(
"Failed to delete package version {}: {}",
package_version.id,
response.text().await.unwrap()
);
Err(names)
}
_ => {
error!(
"Failed to delete package version {} with status {}: {}",
package_version.id,
response.status(),
response.text().await.expect("Failed to read text from response")
);
Err(names)
}
}
}
pub async fn fetch_rate_limit(&self) -> Result<(usize, DateTime<Utc>)> {
debug!("Retrieving Github API rate limit");
// Construct initial request
let response = Client::new()
.get("https://api.github.com/rate_limit")
.headers(self.headers.clone())
.send()
.await?;
// Since this is the first call made to the GitHub API, we perform a few extra auth checks here:
// auth check: Make sure we're authorized correctly
if response.status() == StatusCode::UNAUTHORIZED {
eprintln!("Received a 401 response from the GitHub API. Make sure the token is valid, and that it has the correct permissions.");
exit(1);
}
let response_headers = GithubHeaders::try_from(response.headers())?;
// auth check: Make sure we have the correct scopes
match self.token {
Token::Temporal(_) => (),
Token::ClassicPersonalAccess(_) => {
if response_headers.x_oauth_scopes.is_none()
|| !response_headers
.x_oauth_scopes
.clone()
.unwrap()
.contains("delete:packages")
{
eprintln!("The token does not have the scopes needed. Tokens need `delete:packages`. The scopes found were {}.", response_headers.x_oauth_scopes.unwrap_or("none".to_string()));
exit(1);
}
}
}
debug!(
"There are {} requests remaining in the rate limit",
response_headers.x_ratelimit_remaining
);
Ok((
response_headers.x_ratelimit_remaining,
response_headers.x_ratelimit_reset,
))
}
}
#[cfg(test)]
mod tests {
use crate::cli::models::Account;
use crate::client::builder::PackagesClientBuilder;
use reqwest::header::HeaderValue;
use secrecy::Secret;
use super::*;
#[test]
fn github_headers() {
let mut headers = HeaderMap::new();
headers.insert("x-ratelimit-limit", "60".parse().unwrap());
headers.insert("x-ratelimit-remaining", "60".parse().unwrap());
headers.insert("x-ratelimit-reset", "1714483761".parse().unwrap());
headers.insert("x-ratelimit-used", "0".parse().unwrap());
headers.insert("x-oauth-scopes", "read:packages,delete:packages,repo".parse().unwrap());
let parsed_headers = GithubHeaders::try_from(&headers).unwrap();
assert_eq!(parsed_headers.x_ratelimit_reset.timezone(), Utc);
assert_eq!(parsed_headers.x_ratelimit_remaining, 60);
assert!(parsed_headers.x_oauth_scopes.is_some());
}
#[test]
fn link_header() {
let link_headers = [
(
"<https://api.github.com/user/packages?package_type=container&per_page=2&page=2>; rel=\"next\", <https://api.github.com/user/packages?package_type=container&per_page=7&page=7>; rel=\"last\"",
Some(Url::parse("https://api.github.com/user/packages?package_type=container&per_page=2&page=2").unwrap())
),
(
"<https://api.github.com/user/packages?package_type=container&per_page=2&page=3>; rel=\"next\", <https://api.github.com/user/packages?package_type=container&per_page=2&page=2>; rel=\"last\"",
Some(Url::parse("https://api.github.com/user/packages?package_type=container&per_page=2&page=3").unwrap())
),
(
"<<https://api.github.com/user/packages?package_type=container&per_page=2&page=2>; rel=\"last\"",
None
),
];
for (input, expected) in link_headers {
let parsed_links = GithubHeaders::parse_link_header(input);
assert_eq!(parsed_links, expected)
}
}
#[tokio::test]
async fn test_http_headers() {
let test_string = "test".to_string();
let client_builder = PackagesClientBuilder::new()
.set_http_headers(Token::ClassicPersonalAccess(Secret::new(test_string.clone())))
.unwrap();
let set_headers = client_builder.headers.clone().unwrap();
for (header_key, header_value) in [
("x-github-api-version", "2022-11-28"),
("authorization", &format!("Bearer {test_string}")),
("user-agent", "snok/container-retention-policy"),
("accept", "application/vnd.github+json"),
] {
assert_eq!(
set_headers.get(header_key),
Some(&HeaderValue::from_str(header_value).unwrap())
);
}
let client = client_builder
.create_rate_limited_services()
.generate_urls(&Account::User)
.build()
.unwrap();
for (header_key, header_value) in [
("x-github-api-version", "2022-11-28"),
("authorization", &format!("Bearer {test_string}")),
("user-agent", "snok/container-retention-policy"),
("accept", "application/vnd.github+json"),
] {
assert_eq!(
client.headers.get(header_key),
Some(&HeaderValue::from_str(header_value).unwrap())
);
}
}
#[test]
fn personal_urls() {
let urls = Urls::from_account(&Account::User);
assert_eq!(
urls.list_packages_url.as_str(),
"https://api.github.com/user/packages?package_type=container&per_page=100"
);
assert_eq!(
urls.list_package_versions_url("foo").unwrap().as_str(),
"https://api.github.com/user/packages/container/foo/versions?per_page=100"
);
assert_eq!(
urls.delete_package_version_url("foo", &123).unwrap().as_str(),
"https://api.github.com/user/packages/container/foo/versions/123"
);
assert_eq!(
urls.package_version_url("foo", &123).unwrap().as_str(),
"https://github.com/user/packages/container/foo/123"
);
}
#[test]
fn organization_urls() {
let urls = Urls::from_account(&Account::Organization("acme".to_string()));
assert_eq!(
urls.list_packages_url.as_str(),
"https://api.github.com/orgs/acme/packages?package_type=container&per_page=100"
);
assert_eq!(
urls.list_package_versions_url("foo").unwrap().as_str(),
"https://api.github.com/orgs/acme/packages/container/foo/versions?per_page=100"
);
assert_eq!(
urls.delete_package_version_url("foo", &123).unwrap().as_str(),
"https://api.github.com/orgs/acme/packages/container/foo/versions/123"
);
assert_eq!(
urls.package_version_url("foo", &123).unwrap().as_str(),
"https://github.com/orgs/acme/packages/container/foo/123"
);
}
#[test]
fn test_percent_encoding() {
// No special chars
assert_eq!(Urls::percent_encode("example"), "example");
// Special chars
assert_eq!(Urls::percent_encode("a/b"), "a%2Fb".to_string());
assert_eq!(Urls::percent_encode("my_package@1.0"), "my_package%401.0");
// Simple space
assert_eq!(Urls::percent_encode("test test"), "test%20test");
// Other unicode chars
assert_eq!(
Urls::percent_encode("こんにちは"),
"%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF"
);
}
#[test]
fn test_generate_urls() {
let urls = {
let mut builder = PackagesClientBuilder::new();
assert!(builder.urls.is_none());
builder = builder.generate_urls(&Account::User);
builder.urls.unwrap()
};
assert!(urls.list_packages_url.as_str().contains("per_page=100"));
assert!(urls.list_packages_url.as_str().contains("package_type=container"));
assert!(urls.list_packages_url.as_str().contains("api.github.com"));
assert!(urls.packages_api_base.as_str().contains("api.github.com"));
assert!(urls.packages_frontend_base.as_str().contains("https://github.com"));
let urls = {
let mut builder = PackagesClientBuilder::new();
assert!(builder.urls.is_none());
builder = builder.generate_urls(&Account::Organization("foo".to_string()));
builder.urls.unwrap()
};
assert!(urls.list_packages_url.as_str().contains("per_page=100"));
assert!(urls.list_packages_url.as_str().contains("package_type=container"));
assert!(urls.list_packages_url.as_str().contains("api.github.com"));
assert!(urls.packages_api_base.as_str().contains("api.github.com"));
assert!(urls.list_packages_url.as_str().contains("/foo/"));
assert!(urls.packages_api_base.as_str().contains("/foo/"));
assert!(urls.packages_frontend_base.as_str().contains("https://github.com"));
}
}