use gcp_rust_tools::ObservabilityClient;
use std::env;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let cloud_run_url = env::var("CLOUD_RUN_URL").map_err(|_| {
"Missing CLOUD_RUN_URL. Example: https://my-service-abc-uc.a.run.app/healthz".to_string()
})?;
let client = ObservabilityClient::new(None, Some("cloud-run-oidc-test".to_string())).await?;
let token = client
.get_identity_token_for_audience(&cloud_run_url)
.await?;
println!(
"Generated audience-bound identity token (length={})",
token.len()
);
let output = tokio::process::Command::new("curl")
.args([
"-sS",
"-w",
"\n%{http_code}",
"-H",
&format!("Authorization: Bearer {}", token),
&cloud_run_url,
])
.output()
.await?;
let response = String::from_utf8_lossy(&output.stdout);
let mut lines: Vec<&str> = response.lines().collect();
let status = lines.pop().unwrap_or("000");
let body = lines.join("\n");
println!("Cloud Run status: {}", status);
println!("Cloud Run body:\n{}", body);
if !status.starts_with('2') {
return Err(format!("Cloud Run invocation failed with status {}", status).into());
}
Ok(())
}