rattler_upload 0.5.8

A crate to Upload conda packages to various channels.
Documentation
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
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
use std::borrow::Cow;

use fs_err::tokio as fs;
use miette::IntoDiagnostic;
use rattler_conda_types::package::AboutJson;
use rattler_conda_types::utils::url_with_trailing_slash::UrlWithTrailingSlash;
use rattler_conda_types::PackageName;
use reqwest::multipart::Form;
use reqwest::multipart::Part;
use reqwest::Client;
use serde::{Deserialize, Serialize};
use tracing::debug;
use tracing::info;
use url::Url;

use crate::upload::opt::ForceOverwrite;

use super::package::ExtractedPackage;
use crate::tool_configuration::APP_USER_AGENT;

/// Errors that can occur during Anaconda.org operations.
#[derive(Debug, thiserror::Error, miette::Diagnostic)]
pub enum AnacondaError {
    /// A conda token is required but a different authentication type was found.
    #[error(
        "conda token required for anaconda.org, but a different authentication type was found"
    )]
    WrongAuthenticationType,

    /// No API key was provided and none was found in the keychain.
    #[error("no anaconda.org API key provided and none found in keychain")]
    MissingApiKey,

    /// Failed to retrieve authentication from the keychain.
    #[error("failed to retrieve authentication from keychain: {message}")]
    KeychainError {
        /// The error message from the keychain.
        message: String,
    },

    /// The server returned an unexpected status code.
    #[error("unexpected server response (HTTP {status})")]
    UnexpectedStatus {
        /// The HTTP status code.
        status: u16,
    },

    /// Failed to create or update a package on the server.
    #[error("failed to create or update package")]
    PackageMutationFailed(#[source] reqwest::Error),

    /// Failed to create or update a release on the server.
    #[error("failed to create or update release")]
    ReleaseMutationFailed(#[source] reqwest::Error),

    /// Failed to remove a file from the server.
    #[error("failed to remove file")]
    FileRemovalFailed(#[source] reqwest::Error),

    /// The file already exists and --force was not specified.
    #[error("file {0} already exists")]
    #[diagnostic(help("use --force to overwrite"))]
    FileAlreadyExists(String),

    /// Failed to stage a file on the server.
    #[error("failed to stage file (HTTP {status})")]
    StageFailed {
        /// The HTTP status code.
        status: u16,
    },

    /// No channel was selected for upload.
    #[error("no channel selected for upload")]
    #[diagnostic(help("specify at least one channel for upload to anaconda.org"))]
    NoChannel,

    /// The index.json is missing the subdir field.
    #[error("missing subdir in index.json")]
    MissingSubdir,

    /// The package file has no filename.
    #[error("missing filename")]
    MissingFilename,

    /// An error from an underlying operation (I/O, URL parsing, etc.).
    #[error("{0}")]
    Other(miette::Report),
}

impl From<miette::Report> for AnacondaError {
    fn from(report: miette::Report) -> Self {
        AnacondaError::Other(report)
    }
}

pub struct Anaconda {
    client: Client,
    url: UrlWithTrailingSlash,
}

#[derive(Serialize, Deserialize, Debug)]
struct PackageAttrs<'a> {
    package_types: Vec<String>,
    name: Cow<'a, PackageName>,
    #[serde(flatten)]
    about: Cow<'a, AboutJson>,
}

#[derive(Serialize, Deserialize, Debug)]
struct ReleaseCreationArgs<'a> {
    requirements: Vec<String>,
    announce: bool,
    description: Option<String>,
    #[serde(flatten)]
    about: Cow<'a, AboutJson>,
}

#[derive(Serialize, Deserialize, Debug)]
struct FileStageResponse {
    post_url: Url,
    form_data: serde_json::Map<String, serde_json::Value>,
    dist_id: String,
}

impl Anaconda {
    pub fn new(token: String, url: UrlWithTrailingSlash) -> Self {
        let mut default_headers = reqwest::header::HeaderMap::new();

        default_headers.append(
            "Accept",
            "application/json".parse().expect("failed to parse"),
        );
        default_headers.append(
            "Authorization",
            format!("token {token}").parse().expect("failed to parse"),
        );

        default_headers.append(
            "x-binstar-api-version",
            "1.12.2".parse().expect("failed to parse"),
        );

        let client = Client::builder()
            .no_gzip()
            .user_agent(APP_USER_AGENT)
            .default_headers(default_headers)
            .build()
            .expect("failed to create client");

        Self { client, url }
    }
}

impl Anaconda {
    pub async fn create_or_update_package(
        &self,
        owner: &str,
        package: &ExtractedPackage<'_>,
    ) -> Result<(), AnacondaError> {
        let package_name = package.package_name();
        debug!("getting package {}/{}", owner, package_name.as_normalized(),);

        let url = self
            .url
            .join(&format!(
                "package/{}/{}",
                owner,
                package_name.as_normalized(),
            ))
            .into_diagnostic()?;

        let response = self.client.get(url).send().await.into_diagnostic()?;

        let exists = match response.status() {
            reqwest::StatusCode::OK => true,
            reqwest::StatusCode::NOT_FOUND => false,
            status => {
                return Err(AnacondaError::UnexpectedStatus {
                    status: status.as_u16(),
                });
            }
        };

        let url = self
            .url
            .join(&format!(
                "package/{}/{}",
                owner,
                package_name.as_normalized(),
            ))
            .into_diagnostic()?;

        // See inspect_conda_info_dir in anaconda-client
        // https://github.com/Anaconda-Platform/anaconda-client/blob/master/binstar_client/inspect_package/conda.py#L81-L150
        // dumping the entire about.json as public_attrs seems to work fine
        let payload = serde_json::json!({
            "public": true,
            "publish": false,
            "public_attrs": PackageAttrs {
                package_types: vec!["conda".to_string()],
                name: Cow::Borrowed(package_name),
                about: Cow::Borrowed(package.about_json()),
            },
        });

        let req = if exists {
            debug!(
                "updating package {}/{}",
                owner,
                package_name.as_normalized(),
            );
            self.client.patch(url)
        } else {
            debug!(
                "creating package {}/{}",
                owner,
                package_name.as_normalized(),
            );
            self.client.post(url)
        };

        req.json(&payload)
            .send()
            .await
            .map_err(AnacondaError::PackageMutationFailed)?
            .error_for_status()
            .map_err(AnacondaError::PackageMutationFailed)?;

        Ok(())
    }

    pub async fn create_or_update_release(
        &self,
        owner: &str,
        package: &ExtractedPackage<'_>,
    ) -> Result<(), AnacondaError> {
        let package_name = package.package_name();
        let package_version = package.package_version();
        debug!(
            "getting release {}/{}/{}",
            owner,
            package_name.as_normalized(),
            package_version
        );

        let url = self
            .url
            .join(&format!(
                "release/{}/{}/{}",
                owner,
                package_name.as_normalized(),
                package_version,
            ))
            .into_diagnostic()?;

        let response = self.client.get(url).send().await.into_diagnostic()?;

        let exists = match response.status() {
            reqwest::StatusCode::OK => true,
            reqwest::StatusCode::NOT_FOUND => false,
            status => {
                return Err(AnacondaError::UnexpectedStatus {
                    status: status.as_u16(),
                });
            }
        };

        let url = self
            .url
            .join(&format!(
                "release/{}/{}/{}",
                owner,
                package_name.as_normalized(),
                package_version,
            ))
            .into_diagnostic()?;

        let req = if exists {
            debug!(
                "updating release {}/{}/{}",
                owner,
                package_name.as_normalized(),
                package_version
            );
            self.client.patch(url).json(&serde_json::json!({
                "requirements": [],
                "announce": false,
                "description": null,
                "public_attrs": Cow::Borrowed(package.about_json())
            }))
        } else {
            debug!(
                "creating release {}/{}/{}",
                owner,
                package_name.as_normalized(),
                package_version
            );
            self.client.post(url).json(&ReleaseCreationArgs {
                requirements: vec![],
                announce: false,
                description: None,
                about: Cow::Borrowed(package.about_json()),
            })
        };

        req.send()
            .await
            .map_err(AnacondaError::ReleaseMutationFailed)?
            .error_for_status()
            .map_err(AnacondaError::ReleaseMutationFailed)?;

        Ok(())
    }

    pub async fn remove_file(
        &self,
        owner: &str,
        package: &ExtractedPackage<'_>,
    ) -> Result<(), AnacondaError> {
        let package_name = package.package_name();
        let package_version = package.package_version();
        let subdir = package.subdir().ok_or(AnacondaError::MissingSubdir)?;
        let filename = package.filename().ok_or(AnacondaError::MissingFilename)?;

        debug!(
            "removing file {}/{}/{}/{}/{}",
            owner,
            package_name.as_normalized(),
            package_version,
            subdir,
            filename,
        );

        let url = self
            .url
            .join(&format!(
                "dist/{}/{}/{}/{}/{}",
                owner,
                package_name.as_normalized(),
                package_version,
                subdir,
                filename,
            ))
            .into_diagnostic()?;

        self.client
            .delete(url)
            .send()
            .await
            .map_err(AnacondaError::FileRemovalFailed)?
            .error_for_status()
            .map_err(AnacondaError::FileRemovalFailed)?;

        Ok(())
    }

    pub async fn upload_file(
        &self,
        owner: &str,
        channels: &[String],
        force: ForceOverwrite,
        package: &ExtractedPackage<'_>,
    ) -> Result<bool, AnacondaError> {
        if channels.is_empty() {
            return Err(AnacondaError::NoChannel);
        }

        let sha256 = package.sha256().into_diagnostic()?;

        let package_name = package.package_name();
        let version = package.package_version();

        let index_json = &package.index_json();

        let subdir = index_json
            .subdir
            .as_deref()
            .ok_or(AnacondaError::MissingSubdir)?;

        let filename = package.filename().ok_or(AnacondaError::MissingFilename)?;

        debug!(
            "uploading file {}/{}/{}/{}/{}",
            owner,
            package_name.as_normalized(),
            version,
            subdir,
            filename,
        );

        let url = self
            .url
            .join(&format!(
                "stage/{}/{}/{}/{}/{}",
                owner,
                package_name.as_normalized(),
                version,
                subdir,
                filename,
            ))
            .into_diagnostic()?;

        let payload = serde_json::json!({
            "distribution_type": "conda",
            "description": null,
            "attrs": index_json,
            "channels": channels,
            "sha256": sha256,
        });

        let resp = self
            .client
            .post(url)
            .json(&payload)
            .send()
            .await
            .into_diagnostic()?;

        match resp.status() {
            reqwest::StatusCode::OK => (),
            reqwest::StatusCode::CONFLICT => {
                if force.is_enabled() {
                    info!(
                        "file {} already exists, running with --force, removing file and retrying",
                        filename
                    );
                    self.remove_file(owner, package).await?;

                    // We cannot just retry the staging request here, because
                    // Anaconda might have garbage collected the release /
                    // package after the deletion of the file.
                    return Ok(false);
                } else {
                    return Err(AnacondaError::FileAlreadyExists(filename.to_string()));
                }
            }
            status => {
                return Err(AnacondaError::StageFailed {
                    status: status.as_u16(),
                });
            }
        }

        let parsed_response: FileStageResponse = resp.json().await.into_diagnostic()?;

        debug!("Uploading file to S3 Bucket {}", parsed_response.post_url);

        let md5_base64 = package.md5_base64().into_diagnostic()?;
        let file_size = package.file_size().into_diagnostic()?;

        let mut form_data = Form::new();

        for (key, value) in parsed_response.form_data {
            let serde_json::Value::String(value) = value else {
                return Err(miette::miette!("invalid value in form data: {}", value).into());
            };

            form_data = form_data.text(key, value);
        }

        let content = fs::read(package.path()).await.into_diagnostic()?;

        form_data = form_data.text("Content-Length", file_size.to_string());
        form_data = form_data.text("Content-MD5", md5_base64);
        form_data = form_data.part("file", Part::bytes(content));

        reqwest::Client::new()
            .post(parsed_response.post_url)
            .multipart(form_data)
            .header("Accept", "application/json")
            .send()
            .await
            .into_diagnostic()?
            .error_for_status()
            .into_diagnostic()?;

        debug!("Committing file {}", filename);

        let url = self
            .url
            .join(&format!(
                "commit/{}/{}/{}/{}/{}",
                owner,
                package_name.as_normalized(),
                version,
                subdir,
                filename,
            ))
            .into_diagnostic()?;

        self.client
            .post(url)
            .json(&serde_json::json!({
                "dist_id": parsed_response.dist_id,
            }))
            .send()
            .await
            .into_diagnostic()?
            .error_for_status()
            .into_diagnostic()?;

        debug!("File {} uploaded successfully", filename);

        Ok(true)
    }
}

#[cfg(test)]
mod test {
    use std::net::SocketAddr;

    use axum::{
        http::StatusCode,
        routing::{get, post},
        Router,
    };
    use url::Url;

    use super::{Anaconda, AnacondaError};
    use crate::upload::package::ExtractedPackage;
    use crate::upload::test_utils::test_package_path;
    use crate::upload::{opt::ForceOverwrite, test_utils};

    #[tokio::test]
    async fn test_anaconda_create_package() {
        let router = Router::new().route(
            "/package/{owner}/{name}",
            get(|| async { StatusCode::NOT_FOUND }).post(|| async { StatusCode::OK }),
        );
        let url = test_utils::start_test_server(router).await;
        let anaconda = Anaconda::new("test-token".to_string(), url.into());

        let package_path = test_package_path();
        let package = ExtractedPackage::from_package_file(&package_path).unwrap();

        let result = anaconda
            .create_or_update_package("test-owner", &package)
            .await;
        assert!(result.is_ok(), "{:?}", result.unwrap_err());
    }

    #[tokio::test]
    async fn test_anaconda_update_existing_package() {
        let router = Router::new().route(
            "/package/{owner}/{name}",
            get(|| async { StatusCode::OK }).patch(|| async { StatusCode::OK }),
        );
        let url = test_utils::start_test_server(router).await;
        let anaconda = Anaconda::new("test-token".to_string(), url.into());

        let package_path = test_package_path();
        let package = ExtractedPackage::from_package_file(&package_path).unwrap();

        let result = anaconda
            .create_or_update_package("test-owner", &package)
            .await;
        assert!(result.is_ok(), "{:?}", result.unwrap_err());
    }

    #[tokio::test]
    async fn test_anaconda_create_package_server_error() {
        let router = Router::new().route(
            "/package/{owner}/{name}",
            get(|| async { StatusCode::INTERNAL_SERVER_ERROR }),
        );
        let url = test_utils::start_test_server(router).await;
        let anaconda = Anaconda::new("test-token".to_string(), url.into());

        let package_path = test_package_path();
        let package = ExtractedPackage::from_package_file(&package_path).unwrap();

        let err = anaconda
            .create_or_update_package("test-owner", &package)
            .await
            .unwrap_err();
        assert!(
            matches!(err, AnacondaError::UnexpectedStatus { status: 500 }),
            "expected UnexpectedStatus(500), got: {err:?}"
        );
    }

    #[tokio::test]
    async fn test_anaconda_upload_file_conflict_without_force() {
        let router = Router::new().route(
            "/stage/{owner}/{name}/{version}/{subdir}/{filename}",
            post(|| async { StatusCode::CONFLICT }),
        );
        let url = test_utils::start_test_server(router).await;
        let anaconda = Anaconda::new("test-token".to_string(), url.into());

        let package_path = test_package_path();
        let package = ExtractedPackage::from_package_file(&package_path).unwrap();

        let err = anaconda
            .upload_file(
                "test-owner",
                &["main".to_string()],
                ForceOverwrite(false),
                &package,
            )
            .await
            .unwrap_err();
        assert!(
            matches!(err, AnacondaError::FileAlreadyExists(..)),
            "expected FileAlreadyExists, got: {err:?}"
        );
    }

    #[tokio::test]
    async fn test_anaconda_upload_file_no_channels() {
        let package_path = test_package_path();
        let package = ExtractedPackage::from_package_file(&package_path).unwrap();

        let url: Url = "http://127.0.0.1:1".parse().unwrap();
        let anaconda = Anaconda::new("test-token".to_string(), url.into());

        let err = anaconda
            .upload_file("test-owner", &[], ForceOverwrite(false), &package)
            .await
            .unwrap_err();
        assert!(
            matches!(err, AnacondaError::NoChannel),
            "expected NoChannel, got: {err:?}"
        );
    }

    #[tokio::test]
    async fn test_anaconda_upload_file_full_flow() {
        // Bind the listener first so we know the port for the stage response's post_url
        let addr = SocketAddr::new([127, 0, 0, 1].into(), 0);
        let listener = tokio::net::TcpListener::bind(&addr).await.unwrap();
        let addr = listener.local_addr().unwrap();
        let base_url: Url = format!("http://{}:{}", addr.ip(), addr.port())
            .parse()
            .unwrap();

        let stage_handler = {
            let base_url = base_url.clone();
            move || {
                let base_url = base_url.clone();
                async move {
                    let post_url = base_url.join("s3-upload").unwrap();
                    let body = serde_json::json!({
                        "post_url": post_url.to_string(),
                        "form_data": {"key": "value"},
                        "dist_id": "test-dist-id"
                    });
                    (
                        StatusCode::OK,
                        [("content-type", "application/json")],
                        body.to_string(),
                    )
                }
            }
        };

        let router = Router::new()
            .route(
                "/package/{owner}/{name}",
                get(|| async { StatusCode::NOT_FOUND }).post(|| async { StatusCode::OK }),
            )
            .route(
                "/release/{owner}/{name}/{version}",
                get(|| async { StatusCode::NOT_FOUND }).post(|| async { StatusCode::OK }),
            )
            .route(
                "/stage/{owner}/{name}/{version}/{subdir}/{filename}",
                post(stage_handler),
            )
            .route("/s3-upload", post(|| async { StatusCode::OK }))
            .route(
                "/commit/{owner}/{name}/{version}/{subdir}/{filename}",
                post(|| async { StatusCode::OK }),
            );

        tokio::spawn(async move {
            axum::serve(listener, router).await.unwrap();
        });

        let anaconda = Anaconda::new("test-token".to_string(), base_url.into());

        let package_path = test_package_path();
        let package = ExtractedPackage::from_package_file(&package_path).unwrap();

        anaconda
            .create_or_update_package("test-owner", &package)
            .await
            .expect("create_or_update_package failed");

        anaconda
            .create_or_update_release("test-owner", &package)
            .await
            .expect("create_or_update_release failed");

        let result = anaconda
            .upload_file(
                "test-owner",
                &["main".to_string()],
                ForceOverwrite(false),
                &package,
            )
            .await;
        assert!(result.is_ok(), "{:?}", result.unwrap_err());
        assert!(result.unwrap(), "upload_file should return true on success");
    }
}