Skip to content

Commit

Permalink
Merge pull request #6 from refi64/wip/refi64/auth
Browse files Browse the repository at this point in the history
Add support for token authentication for aptly
  • Loading branch information
sjoerdsimons committed Jun 13, 2023
2 parents d99b02b + e6424d4 commit c585bd2
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 18 deletions.
15 changes: 15 additions & 0 deletions aptly-rest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use api::{
repos::{Repo, RepoApi},
snapshots::{Snapshot, SnapshotApi},
};
use reqwest::header;
use serde::{Deserialize, Serialize};
use thiserror::Error;
use url::Url;
Expand All @@ -19,6 +20,8 @@ pub mod utils;
pub enum AptlyRestError {
#[error("Http Request failed {0}")]
Request(#[from] reqwest::Error),
#[error("Invalid authentication token {0}")]
InvalidAuthToken(#[from] header::InvalidHeaderValue),
}

#[derive(Debug, Clone)]
Expand All @@ -35,6 +38,18 @@ impl AptlyRest {
}
}

pub fn new_with_token(url: Url, token: &str) -> Result<Self, AptlyRestError> {
let mut headers = header::HeaderMap::new();
headers.insert(header::AUTHORIZATION, format!("Bearer {token}").parse()?);

Ok(Self {
client: reqwest::ClientBuilder::new()
.default_headers(headers)
.build()?,
url,
})
}

pub async fn version(&self) -> Result<String, AptlyRestError> {
let mut url = self.url.clone();
url.path_segments_mut().unwrap().extend(&["api", "version"]);
Expand Down
2 changes: 1 addition & 1 deletion aptlyctl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ edition = "2021"
[dependencies]
aptly-rest = { path = "../aptly-rest", version = "0.1.0" }
aptly-rest-mock = { version = "0.0.1", path = "../aptly-rest-mock" }
clap = { version = "4", features = ["derive"] }
clap = { version = "4", features = ["derive", "env"] }
color-eyre = "0.6.2"
http = "0.2.9"
serde_json = "1.0.83"
Expand Down
11 changes: 9 additions & 2 deletions aptlyctl/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,12 @@ enum Command {
struct Opts {
#[clap(subcommand)]
command: Command,
/// Url for the aptly rest api endpoint
/// Url for the aptly rest API endpoint
#[clap(short, long, default_value = "http://localhost:8080")]
url: url::Url,
/// Authentication token for the API
#[clap(short, long, env = "APTLY_AUTH_TOKEN")]
auth_token: Option<String>,
}

#[tokio::main]
Expand All @@ -60,7 +63,11 @@ async fn main() -> Result<ExitCode> {
.init();
color_eyre::install().unwrap();
let opts = Opts::parse();
let aptly = AptlyRest::new(opts.url);
let aptly = if let Some(token) = opts.auth_token {
AptlyRest::new_with_token(opts.url, &token)?
} else {
AptlyRest::new(opts.url)
};

match opts.command {
Command::Repo { command } => command.run(&aptly).await,
Expand Down
11 changes: 6 additions & 5 deletions aptlyctl/src/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,13 @@ impl RepoCommand {

RepoCommand::TestExists(args) => {
if let Err(err) = aptly.repo(&args.repo).get().await {
let AptlyRestError::Request(err) = err;
if err.status() == Some(StatusCode::NOT_FOUND) {
return Ok(ExitCode::FAILURE);
} else {
return Err(err.into());
if let AptlyRestError::Request(err) = &err {
if err.status() == Some(StatusCode::NOT_FOUND) {
return Ok(ExitCode::FAILURE);
}
}

return Err(err.into());
}
}

Expand Down
11 changes: 6 additions & 5 deletions aptlyctl/src/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ impl SnapshotCommand {
match self {
SnapshotCommand::TestExists(args) => {
if let Err(err) = aptly.snapshot(&args.snapshot).get().await {
let AptlyRestError::Request(err) = err;
if err.status() == Some(StatusCode::NOT_FOUND) {
return Ok(ExitCode::FAILURE);
} else {
return Err(err.into());
if let AptlyRestError::Request(err) = &err {
if err.status() == Some(StatusCode::NOT_FOUND) {
return Ok(ExitCode::FAILURE);
}
}

return Err(err.into());
}
}
SnapshotCommand::Drop(args) => {
Expand Down
2 changes: 1 addition & 1 deletion obs2aptly/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ tokio = { version = "1.19.2", features = ["full"] }

tokio-util = { version = "0.7.3", features = ["compat"] }
async-walkdir = "0.2.0"
clap = { version = "4", features = ["derive"] }
clap = { version = "4", features = ["derive", "env"] }
debian-packaging = "0.15.0"
futures = "0.3.21"
walker = "1.0.1"
Expand Down
7 changes: 4 additions & 3 deletions obs2aptly/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -860,9 +860,10 @@ impl SyncActions {

const DIR: &str = "obs2aptly";
if let Err(err) = self.aptly.files().directory(DIR.to_owned()).delete().await {
let AptlyRestError::Request(inner) = &err;
if inner.status() != Some(http::StatusCode::NOT_FOUND) {
return Err(err.into());
if let AptlyRestError::Request(inner) = &err {
if inner.status() != Some(http::StatusCode::NOT_FOUND) {
return Err(err.into());
}
}
}

Expand Down
9 changes: 8 additions & 1 deletion obs2aptly/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ struct Opts {
/// Url for the aptly rest api endpoint
#[clap(short, long, default_value = "http://localhost:8080")]
url: url::Url,
/// Authentication token for the API
#[clap(short, long, env = "APTLY_AUTH_TOKEN")]
auth_token: Option<String>,
/// Repo in aptly
aptly_repo: String,
/// Directory with obs repositories
Expand All @@ -30,7 +33,11 @@ async fn main() -> Result<()> {
.init();
color_eyre::install().unwrap();
let opts = Opts::parse();
let aptly = AptlyRest::new(opts.url);
let aptly = if let Some(token) = opts.auth_token {
AptlyRest::new_with_token(opts.url, &token)?
} else {
AptlyRest::new(opts.url)
};

let aptly_contents = AptlyContent::new_from_aptly(&aptly, opts.aptly_repo).await?;
let obs_content = ObsContent::new_from_path(opts.obs_repo).await?;
Expand Down

0 comments on commit c585bd2

Please sign in to comment.