diff options
| author | mo khan <mo@mokhan.ca> | 2025-07-02 12:32:27 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2025-07-02 12:32:27 -0600 |
| commit | a577c62277e3d651b66fd68dbe800bf3ab5c4921 (patch) | |
| tree | 7ae4e79fc84c539c12fb0313d0d3cc929b2e12ae /tests/support | |
| parent | c2b8edab01b23fde6cc196a3349ad6aa19a93299 (diff) | |
| parent | 0b610d061e45811130d8cf3919037fdc9513e340 (diff) | |
Merge branch 'rs' into 'main'
Re-write the authorization daemon in rust
See merge request gitlab-org/software-supply-chain-security/authorization/authzd!1
Diffstat (limited to 'tests/support')
| -rw-r--r-- | tests/support/factory_bot.rs | 58 | ||||
| -rw-r--r-- | tests/support/mod.rs | 1 |
2 files changed, 59 insertions, 0 deletions
diff --git a/tests/support/factory_bot.rs b/tests/support/factory_bot.rs new file mode 100644 index 00000000..15c6f1f3 --- /dev/null +++ b/tests/support/factory_bot.rs @@ -0,0 +1,58 @@ +use envoy_types::ext_authz::v3::pb::CheckRequest; +use envoy_types::pb::envoy::service::auth::v3::AttributeContext; +use envoy_types::pb::envoy::service::auth::v3::attribute_context::{HttpRequest, Request}; +use std::collections::HashMap; +use std::net::SocketAddr; +use tonic::transport::Channel; + +#[allow(dead_code)] +pub fn build<T: Default>() -> T { + return please::build(); +} + +pub fn build_with<T, F>(initializer: F) -> T +where + T: Default, + F: std::ops::FnOnce(&mut T), +{ + return please::build_with(initializer); +} + +pub fn build_request(f: impl std::ops::FnOnce(&mut HttpRequest)) -> CheckRequest { + build_with(|item: &mut CheckRequest| { + item.attributes = Some(please::build_with(|item: &mut AttributeContext| { + item.request = Some(please::build_with(|item: &mut Request| { + item.http = Some(please::build_with(|item: &mut HttpRequest| f(item))); + })); + })); + }) +} + +pub fn build_headers(headers: Vec<(String, String)>) -> HashMap<String, String> { + return build_with(|item: &mut HashMap<String, String>| { + for (key, value) in headers { + item.insert(key, value); + } + }); +} + +pub fn build_cedar_authorizer() -> authzd::CedarAuthorizer { + let realpath = std::fs::canonicalize("./etc/authzd").unwrap(); + let path = realpath.as_path(); + authzd::CedarAuthorizer::new_from(path) +} + +pub async fn build_channel(addr: SocketAddr) -> Channel { + Channel::from_shared(format!("http://{}", addr)) + .expect("Failed to create channel") + .connect() + .await + .expect("Failed to connect to server") +} + +pub async fn build_rpc_client<T, F>(addr: SocketAddr, f: F) -> T +where + F: FnOnce(Channel) -> T, +{ + f(build_channel(addr).await) +} diff --git a/tests/support/mod.rs b/tests/support/mod.rs new file mode 100644 index 00000000..5e2a6d78 --- /dev/null +++ b/tests/support/mod.rs @@ -0,0 +1 @@ +pub mod factory_bot; |
