Skip to content

Commit 284d68b

Browse files
authored
test(auth): Integration test for Service Account with Audience (#3260)
* test(auth): Integration test for Service Account with Audience * lint
1 parent cf3e888 commit 284d68b

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

src/auth/integration-tests/src/lib.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,61 @@ pub async fn service_account() -> anyhow::Result<()> {
8787
Ok(())
8888
}
8989

90+
pub async fn service_account_with_audience() -> anyhow::Result<()> {
91+
let project = std::env::var("GOOGLE_CLOUD_PROJECT").expect("GOOGLE_CLOUD_PROJECT not set");
92+
93+
// Create a SecretManager client. When running on GCB, this loads MDS
94+
// credentials for our `integration-test-runner` service account.
95+
let client = SecretManagerService::builder().build().await?;
96+
97+
// Load the ADC json for the principal under test, in this case, a
98+
// service account.
99+
let response = client
100+
.access_secret_version()
101+
.set_name(format!(
102+
"projects/{project}/secrets/test-sa-creds-json/versions/latest"
103+
))
104+
.send()
105+
.await?;
106+
let sa_json = response
107+
.payload
108+
.expect("missing payload in test-sa-creds-json response")
109+
.data;
110+
111+
let sa_json: serde_json::Value = serde_json::from_slice(&sa_json)?;
112+
113+
// Create credentials for the principal under test, but with an audience.
114+
let creds = ServiceAccountCredentialsBuilder::new(sa_json)
115+
.with_access_specifier(
116+
auth::credentials::service_account::AccessSpecifier::from_audience(
117+
"https://secretmanager.googleapis.com/",
118+
),
119+
)
120+
.build()?;
121+
122+
// Construct a new SecretManager client using the credentials.
123+
let client = SecretManagerService::builder()
124+
.with_credentials(creds)
125+
.build()
126+
.await?;
127+
128+
// Access a secret, which only this principal has permissions to do.
129+
let response = client
130+
.access_secret_version()
131+
.set_name(format!(
132+
"projects/{project}/secrets/test-sa-creds-secret/versions/latest"
133+
))
134+
.send()
135+
.await?;
136+
let secret = response
137+
.payload
138+
.expect("missing payload in test-sa-creds-secret response")
139+
.data;
140+
assert_eq!(secret, "service_account");
141+
142+
Ok(())
143+
}
144+
90145
pub async fn impersonated() -> anyhow::Result<()> {
91146
let project = std::env::var("GOOGLE_CLOUD_PROJECT").expect("GOOGLE_CLOUD_PROJECT not set");
92147

src/auth/integration-tests/tests/driver.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ mod driver {
2222
auth_integration_tests::service_account().await
2323
}
2424

25+
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
26+
#[serial_test::serial]
27+
async fn run_service_account_with_audience() -> anyhow::Result<()> {
28+
auth_integration_tests::service_account_with_audience().await
29+
}
30+
2531
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
2632
#[serial_test::serial]
2733
async fn run_impersonated() -> anyhow::Result<()> {

0 commit comments

Comments
 (0)