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

feat: artifactory upload #439

Merged
merged 3 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
34 changes: 33 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ struct RebuildOpts {
#[derive(Parser)]
struct UploadOpts {
/// The package file to upload
#[clap(short, long)]
package_file: PathBuf,

/// The server type
Expand All @@ -189,6 +188,7 @@ struct UploadOpts {
#[derive(Clone, Debug, PartialEq, Parser)]
enum ServerType {
Quetz(QuetzOpts),
Artifactory(ArtifactoryOpts),
}

#[derive(Clone, Debug, PartialEq, Parser)]
Expand All @@ -208,6 +208,27 @@ struct QuetzOpts {
api_key: Option<String>,
}

#[derive(Clone, Debug, PartialEq, Parser)]
/// Options for uploading to a Artifactory channel
/// Authentication is used from the keychain / auth-file
struct ArtifactoryOpts {
/// The URL to your Artifactory server
#[arg(short, long, env = "ARTIFACTORY_SERVER_URL")]
url: Url,

/// The URL to your channel
#[arg(short, long, env = "ARTIFACTORY_CHANNEL")]
channel: String,

/// Your Artifactory username
#[arg(short, long, env = "ARTIFACTORY_USERNAME")]
username: Option<String>,

/// Your Artifactory password
#[arg(short, long, env = "ARTIFACTORY_PASSWORD")]
password: Option<String>,
}

#[tokio::main]
async fn main() -> miette::Result<()> {
let args = App::parse();
Expand Down Expand Up @@ -579,6 +600,17 @@ async fn upload_from_args(args: UploadOpts) -> miette::Result<()> {
)
.await?;
}
ServerType::Artifactory(artifactory_opts) => {
upload::upload_package_to_artifactory(
&store,
artifactory_opts.username,
artifactory_opts.password,
args.package_file,
artifactory_opts.url,
artifactory_opts.channel,
)
.await?;
}
}

Ok(())
Expand Down
78 changes: 77 additions & 1 deletion src/upload.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::path::PathBuf;

use miette::IntoDiagnostic;
use miette::{Context, IntoDiagnostic};
use rattler_conda_types::package::{IndexJson, PackageFile};
use rattler_networking::{redact_known_secrets_from_error, Authentication, AuthenticationStorage};
use reqwest::Method;
use sha2::Digest;
Expand Down Expand Up @@ -70,3 +71,78 @@ pub async fn upload_package_to_quetz(
info!("Package was successfully uploaded to Quetz server");
Ok(())
}

pub async fn upload_package_to_artifactory(
storage: &AuthenticationStorage,
username: Option<String>,
password: Option<String>,
package_file: PathBuf,
url: Url,
channel: String,
) -> miette::Result<()> {
let package_dir = tempfile::tempdir().unwrap();
0xbe7a marked this conversation as resolved.
Show resolved Hide resolved
rattler_package_streaming::fs::extract(&package_file, package_dir.path()).into_diagnostic()?;

let index_json = IndexJson::from_package_directory(package_dir.path()).into_diagnostic()?;
let subdir = index_json
.subdir
.ok_or_else(|| miette::miette!("index.json of the package has no subdirectory. Cannot determine which directory to upload to"))?;

let (username, password) = match (username, password) {
(Some(u), Some(p)) => (u, p),
(Some(_), _) | (_, Some(_)) => {
return Err(miette::miette!("A username and password is required for authentication with artifactory, only one was given"));
}
_ => match storage.get_by_url(url.clone()) {
Ok((_, Some(Authentication::BasicHTTP { username, password }))) => (username, password),
Ok((_, Some(_))) => {
return Err(miette::miette!("A username and password is required for authentication with artifactory.
Authentication information found in the keychain / auth file, but it was not a username and password"));
}
Ok((_, None)) => {
return Err(miette::miette!(
"No username and password was given and none was found in the keychain / auth file"
));
}
Err(e) => {
return Err(miette::miette!(
"Failed to get authentication information form keychain: {e}"
));
}
},
};

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

let package_name = package_file
.file_name()
.expect("no filename found")
.to_string_lossy();

let upload_url = url
.join(&format!("{}/{}/{}", channel, subdir, package_name))
.into_diagnostic()?;

let bytes = tokio::fs::read(package_file).await.into_diagnostic()?;

client
.request(Method::PUT, upload_url)
.body(bytes)
.basic_auth(username, Some(password))
.send()
.await
.map_err(redact_known_secrets_from_error)
.into_diagnostic()
.wrap_err("Sending package to artifactory server failed")?
.error_for_status_ref()
.map_err(redact_known_secrets_from_error)
.into_diagnostic()
.wrap_err("Artifactory responded with error")?;

info!("Package was successfully uploaded to artifactory server");

Ok(())
}