Skip to content

Commit

Permalink
Added unit test for auth transform
Browse files Browse the repository at this point in the history
  • Loading branch information
Isawan committed Aug 7, 2023
1 parent 737a2ac commit d0e4687
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 12 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ jobs:
- uses: actions/checkout@v3
- name: Build
run: cargo build --verbose
- name: Run unit tests
run: cargo test --verbose --lib
- name: Start containers
run: docker compose up -d
- name: Install self signed certificate
Expand All @@ -28,7 +30,7 @@ jobs:
timeout-minutes: 1
- name: Show containers
run: docker-compose ps
- name: Run tests
- name: Run integration tests
run: cargo test --verbose --test integration
env:
AWS_ACCESS_KEY_ID: minioadmin
Expand Down
46 changes: 35 additions & 11 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,54 @@ about = "Terraform mirroring proxy"
[[test]]
name = "integration"
path = "integration/main.rs"
#harness = false


# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html


[dependencies]
anyhow = "^1.0.69"
aws-sdk-s3 = {version="^0.25.0"}
axum = { version="0.6.4", features = ["http2", "json"]}
clap = { version = "^4.2.1", features = ["derive", "env", "unicode", "wrap_help"] }
aws-sdk-s3 = { version = "^0.25.0" }
axum = { version = "0.6.4", features = ["http2", "json"] }
clap = { version = "^4.2.1", features = [
"derive",
"env",
"unicode",
"wrap_help",
] }
http = "0.2.8"
hyper = {version= "0.14.24", features = ["full"]}
hyper = { version = "0.14.24", features = ["full"] }
lazy_static = "1.4.0"
reqwest = { version = "0.11.14", features = ["rustls-tls-native-roots", "gzip", "deflate", "brotli", "stream"] }
reqwest = { version = "0.11.14", features = [
"rustls-tls-native-roots",
"gzip",
"deflate",
"brotli",
"stream",
] }
serde = { version = "1.0.152", features = ["serde_derive"] }
serde_json = "1.0.93"
thiserror = "1.0.38"
tower = "0.4.13"
tower-http = { version = "0.3.5", features = ["tracing", "trace", "metrics", "util"] }
tower-http = { version = "0.3.5", features = [
"tracing",
"trace",
"metrics",
"util",
] }
tracing = "0.1.37"
tracing-subscriber = { version = "0.3.16", features = ["env-filter", "json", "tracing-log"] }
tracing-subscriber = { version = "0.3.16", features = [
"env-filter",
"json",
"tracing-log",
] }
url = { version = "^2.3.1", features = ["serde"] }
tokio = {version = "1.26.0", features = ["full"]}
sqlx = { version = "0.7.1", features = ["runtime-tokio", "tls-native-tls", "postgres" ]}
tokio = { version = "1.26.0", features = ["full"] }
sqlx = { version = "0.7.1", features = [
"runtime-tokio",
"tls-native-tls",
"postgres",
] }
tokio-stream = "0.1.12"
aws-config = { version = "0.55.0", features = ["native-tls"] }
aws-endpoint = "0.55.0"
Expand All @@ -50,4 +74,4 @@ tracing-test = { version = "0.2.4", features = ["no-env-filter"] }
rustflags = ["--cfg", "tokio_unstable"]

[profile.release]
debug=1
debug = 1
45 changes: 45 additions & 0 deletions src/credhelper/memory.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use std::{collections::HashMap, marker::Send};

use async_trait::async_trait;

use super::{types::Credential, CredentialHelper};

// Credential helper implementation by storing in the database
#[derive(Clone)]
pub struct MemoryCredentials {
map: HashMap<String, Option<String>>,
}

impl MemoryCredentials {
pub fn new() -> Self {
Self {
map: HashMap::new(),
}
}
}

impl Default for MemoryCredentials {
fn default() -> Self {
Self::new()
}
}

#[async_trait]
impl CredentialHelper for MemoryCredentials {
async fn get(&self, hostname: impl AsRef<str> + Send) -> Result<Credential, anyhow::Error> {
Ok(self
.map
.get(hostname.as_ref())
.map_or(Credential::NotFound, |v| Credential::Entry(v.clone())))
}

async fn store(&mut self, hostname: String, cred: String) -> Result<(), anyhow::Error> {
self.map.insert(hostname, Some(cred));
Ok(())
}

async fn forget(&mut self, hostname: impl AsRef<str> + Send) -> Result<(), anyhow::Error> {
self.map.remove(hostname.as_ref());
Ok(())
}
}
1 change: 1 addition & 0 deletions src/credhelper/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod database;
pub mod memory;
mod types;

pub use types::Credential;
Expand Down
52 changes: 52 additions & 0 deletions src/credhelper/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,55 @@ pub trait CredentialHelper: Sync {
}
}
}

#[cfg(test)]
mod tests {
use super::super::memory::MemoryCredentials;
use super::*;

#[tokio::test]
async fn test_request_transform_known_credential() {
let mut creds = MemoryCredentials::new();
creds
.store("localhost".into(), "password1".into())
.await
.expect("Error occurred");
let client = reqwest::Client::new();
let request = client.get("http://localhost");
let request = creds
.transform(request, "localhost")
.await
.expect("Unexpected error")
.build()
.expect("Remove");
let auth_header = request
.headers()
.get("authorization")
.expect("Header not found");

assert_eq!(
auth_header, "Bearer password1",
"Authorization header not set"
);
}

#[tokio::test]
async fn test_request_transform_unknown_credential() {
let mut creds = MemoryCredentials::new();
creds
.store("localhost".into(), "password1".into())
.await
.expect("Error occurred");
let client = reqwest::Client::new();
let request = client.get("http://test.test");
let request = creds
.transform(request, "test.test")
.await
.expect("Unexpected error")
.build()
.expect("Remove");
let auth_header = request.headers().get("authorization");

assert_eq!(auth_header, None, "Authorization header set");
}
}

0 comments on commit d0e4687

Please sign in to comment.