@@ -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+
90145pub async fn impersonated ( ) -> anyhow:: Result < ( ) > {
91146 let project = std:: env:: var ( "GOOGLE_CLOUD_PROJECT" ) . expect ( "GOOGLE_CLOUD_PROJECT not set" ) ;
92147
0 commit comments