Skip to content

Commit 4a1f675

Browse files
authored
Merge branch 'main' into dependabot/npm_and_yarn/examples/risk-analysis-asset/frontend/babel/runtime-7.27.0
2 parents 2435019 + 7598b17 commit 4a1f675

File tree

4 files changed

+146
-106
lines changed

4 files changed

+146
-106
lines changed

Diff for: examples/cloud-agentspace-wif-dwd/src/main/java/com/google/cloud/pso/OauthApplication.java

+71-60
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
11
/**
2-
*
3-
* Copyright 2025 Google LLC
4-
5-
Licensed under the Apache License, Version 2.0 (the "License");
6-
you may not use this file except in compliance with the License.
7-
You may obtain a copy of the License at
8-
9-
http://www.apache.org/licenses/LICENSE-2.0
10-
11-
Unless required by applicable law or agreed to in writing, software
12-
distributed under the License is distributed on an "AS IS" BASIS,
13-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14-
See the License for the specific language governing permissions and
15-
limitations under the License.
16-
17-
*/
2+
* Copyright 2025 Google LLC
3+
*
4+
* <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5+
* except in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* <p>http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* <p>Unless required by applicable law or agreed to in writing, software distributed under the
10+
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+
* express or implied. See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
1814
package com.google.cloud.pso;
1915

20-
2116
import com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse;
2217
import com.google.api.client.http.*;
2318
import com.google.api.client.http.javanet.NetHttpTransport;
@@ -33,25 +28,23 @@
3328
import com.google.cloud.iam.credentials.v1.IamCredentialsClient;
3429
import com.google.cloud.iam.credentials.v1.SignJwtRequest;
3530
import com.google.cloud.iam.credentials.v1.SignJwtResponse;
36-
3731
import java.io.IOException;
3832
import java.util.ArrayList;
3933
import java.util.HashMap;
4034
import java.util.Map;
4135
import java.util.concurrent.ExecutionException;
4236

4337
/**
44-
* This sample demonstrates how to authenticate GCP AgentSapce and discovery API using Domain
45-
* Wide Delegation to impersonate the user
46-
* when using WIF credentials
38+
* This sample demonstrates how to authenticate GCP AgentSapce and discovery API using Domain Wide
39+
* Delegation to impersonate the user when using WIF credentials
4740
*/
4841
public class OauthApplication {
4942

5043
public static void main(String[] args) {
5144

5245
/*
5346
* Configuration details
54-
*
47+
*
5548
*/
5649

5750
String projectId = "project-id"; // Project ID .
@@ -60,12 +53,21 @@ public static void main(String[] args) {
6053
String engineId = "ws-search_engine"; // Engine ID.
6154
String servingConfigId = "default_search"; // Serving configuration. Options: "default_search"
6255
String searchUserEmail = "[email protected]"; // Email-id of the user to impersonate
63-
String serviceAccountId = "[email protected]"; // Service account with the permission on the WIF
56+
String serviceAccountId =
57+
"[email protected]"; // Service account with the permission
58+
// on the WIF
6459

6560
String searchQuery = "Sample Search Query"; // Search Query for the data store.
6661
try {
67-
search(projectId, location, collectionId, engineId, servingConfigId, searchQuery,
68-
searchUserEmail, serviceAccountId);
62+
search(
63+
projectId,
64+
location,
65+
collectionId,
66+
engineId,
67+
servingConfigId,
68+
searchQuery,
69+
searchUserEmail,
70+
serviceAccountId);
6971
} catch (IOException | ExecutionException e) {
7072
throw new RuntimeException(e);
7173
}
@@ -74,16 +76,16 @@ public static void main(String[] args) {
7476
/**
7577
* Performs a search operation in the Google Cloud Discovery Engine.
7678
*
77-
* @param projectId The project ID.
78-
* @param location The location of the data store.
79-
* @param collectionId The collection containing the data store.
80-
* @param engineId The engine ID.
79+
* @param projectId The project ID.
80+
* @param location The location of the data store.
81+
* @param collectionId The collection containing the data store.
82+
* @param engineId The engine ID.
8183
* @param servingConfigId The serving configuration ID.
82-
* @param searchQuery The search query.
84+
* @param searchQuery The search query.
8385
* @param searchUserEmail The email of the user to impersonate.
84-
* @param serviceAccount The service account with the necessary permissions,
85-
* should be same one from credentials.
86-
* @throws IOException If an I/O error occurs.
86+
* @param serviceAccount The service account with the necessary permissions, should be same one
87+
* from credentials.
88+
* @throws IOException If an I/O error occurs.
8789
* @throws ExecutionException If an execution error occurs.
8890
*/
8991
public static void search(
@@ -103,17 +105,19 @@ public static void search(
103105
HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(credentials);
104106
HttpTransport transport = new NetHttpTransport.Builder().build();
105107
transport.createRequestFactory(requestInitializer);
106-
settings = SearchServiceSettings.newBuilder()
107-
.setCredentialsProvider(FixedCredentialsProvider.create(credentials))
108-
.build();
108+
settings =
109+
SearchServiceSettings.newBuilder()
110+
.setCredentialsProvider(FixedCredentialsProvider.create(credentials))
111+
.build();
109112
SearchServiceClient searchServiceClient = SearchServiceClient.create(settings);
110-
SearchRequest request = SearchRequest.newBuilder()
111-
.setServingConfig(
112-
ServingConfigName.formatProjectLocationCollectionEngineServingConfigName(
113-
projectId, location, collectionId, engineId, servingConfigId))
114-
.setQuery(searchQuery)
115-
.setPageSize(10)
116-
.build();
113+
SearchRequest request =
114+
SearchRequest.newBuilder()
115+
.setServingConfig(
116+
ServingConfigName.formatProjectLocationCollectionEngineServingConfigName(
117+
projectId, location, collectionId, engineId, servingConfigId))
118+
.setQuery(searchQuery)
119+
.setPageSize(10)
120+
.build();
117121
Object o = searchServiceClient.search(request).getPage().getResponse();
118122
System.out.println("Response Object: " + o);
119123
SearchResponse response = searchServiceClient.search(request).getPage().getResponse();
@@ -130,7 +134,7 @@ public static void search(
130134
/**
131135
* Generates credentials for a given user email and service account.
132136
*
133-
* @param userEmail The email of the user.
137+
* @param userEmail The email of the user.
134138
* @param serviceAccountId The service account ID.
135139
* @return A Credentials object containing the OAuth2 credentials.
136140
* @throws IOException If an I/O error occurs during credentials generation.
@@ -170,14 +174,13 @@ private static String getAccessToken(String signedJWT) throws IOException {
170174
HttpResponse tokenResponse = tokenRequest.execute();
171175
GoogleTokenResponse googleTokenResponse = tokenResponse.parseAs(GoogleTokenResponse.class);
172176
return googleTokenResponse.getAccessToken();
173-
174177
}
175178

176179
/**
177-
* Generates a signed JWT for a given user email and service account using
178-
* Google IAM Credentials API.
180+
* Generates a signed JWT for a given user email and service account using Google IAM Credentials
181+
* API.
179182
*
180-
* @param userEmail The email of the user.
183+
* @param userEmail The email of the user.
181184
* @param serviceAccountId The service account ID.
182185
* @return The signed JWT.
183186
* @throws IOException If an I/O error occurs during JWT generation.
@@ -188,23 +191,31 @@ private static String generateSingedJwt(String userEmail, String serviceAccountI
188191
long nowSeconds = System.currentTimeMillis() / 1000;
189192
long expSeconds = nowSeconds + 1800; // 30 minutes
190193

191-
String jsonPayload = "{\"aud\":\"https://oauth2.googleapis.com/token\""
192-
+ ",\"exp\":" + expSeconds
193-
+ ",\"iat\":" + nowSeconds
194-
+ ",\"iss\":\"" + serviceAccountId + "\""
195-
+ ",\"sub\":\"" + userEmail + "\""
196-
+ ",\"scope\":\"https://www.googleapis.com/auth/cloud-platform\"}";
194+
String jsonPayload =
195+
"{\"aud\":\"https://oauth2.googleapis.com/token\""
196+
+ ",\"exp\":"
197+
+ expSeconds
198+
+ ",\"iat\":"
199+
+ nowSeconds
200+
+ ",\"iss\":\""
201+
+ serviceAccountId
202+
+ "\""
203+
+ ",\"sub\":\""
204+
+ userEmail
205+
+ "\""
206+
+ ",\"scope\":\"https://www.googleapis.com/auth/cloud-platform\"}";
197207
System.out.println(jsonPayload);
198208

199-
SignJwtRequest request = SignJwtRequest.newBuilder()
200-
.setName("projects/-/serviceAccounts/" + serviceAccountId)
201-
.addAllDelegates(new ArrayList<String>())
202-
.setPayload(jsonPayload)
203-
.build();
209+
SignJwtRequest request =
210+
SignJwtRequest.newBuilder()
211+
.setName("projects/-/serviceAccounts/" + serviceAccountId)
212+
.addAllDelegates(new ArrayList<String>())
213+
.setPayload(jsonPayload)
214+
.build();
204215

205216
SignJwtResponse response = iamCredentialsClient.signJwt(request);
206217
System.out.println("response: " + response);
207218

208219
return response.getSignedJwt();
209220
}
210-
}
221+
}

Diff for: examples/cloud-agentspace-wif-dwd/src/test/java/com/google/cloud/pso/OauthApplicationTest.java

+73-45
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
/**
2-
*
3-
* Copyright 2025 Google LLC
4-
5-
Licensed under the Apache License, Version 2.0 (the "License");
6-
you may not use this file except in compliance with the License.
7-
You may obtain a copy of the License at
8-
9-
http://www.apache.org/licenses/LICENSE-2.0
10-
11-
Unless required by applicable law or agreed to in writing, software
12-
distributed under the License is distributed on an "AS IS" BASIS,
13-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14-
See the License for the specific language governing permissions and
15-
limitations under the License.
16-
17-
*/
2+
* Copyright 2025 Google LLC
3+
*
4+
* <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5+
* except in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* <p>http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* <p>Unless required by applicable law or agreed to in writing, software distributed under the
10+
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+
* express or implied. See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
1814
package com.google.cloud.pso;
1915

2016
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -29,7 +25,6 @@
2925
import com.google.api.client.http.HttpRequestFactory;
3026
import com.google.api.client.http.HttpResponse;
3127
import com.google.api.client.http.HttpTransport;
32-
import com.google.api.client.json.JsonFactory;
3328
import com.google.cloud.discoveryengine.v1alpha.SearchRequest;
3429
import com.google.cloud.discoveryengine.v1alpha.SearchResponse;
3530
import com.google.cloud.discoveryengine.v1alpha.SearchServiceClient;
@@ -72,7 +67,8 @@ void testSearchSuccess() throws Exception {
7267
IamCredentialsClient mockIamCredentialsClient = mock(IamCredentialsClient.class);
7368
SignJwtResponse mockSignJwtResponse = mock(SignJwtResponse.class);
7469
when(mockSignJwtResponse.getSignedJwt()).thenReturn("mockedSignedJwt");
75-
when(mockIamCredentialsClient.signJwt(any(SignJwtRequest.class))).thenReturn(mockSignJwtResponse);
70+
when(mockIamCredentialsClient.signJwt(any(SignJwtRequest.class)))
71+
.thenReturn(mockSignJwtResponse);
7672

7773
// Mocking getAccessToken
7874
GoogleTokenResponse mockGoogleTokenResponse = mock(GoogleTokenResponse.class);
@@ -82,7 +78,8 @@ void testSearchSuccess() throws Exception {
8278
HttpRequest mockHttpRequest = mock(HttpRequest.class);
8379
when(mockHttpRequest.execute()).thenReturn(mockHttpResponse);
8480
HttpRequestFactory mockHttpRequestFactory = mock(HttpRequestFactory.class);
85-
when(mockHttpRequestFactory.buildPostRequest(any(GenericUrl.class), any())).thenReturn(mockHttpRequest);
81+
when(mockHttpRequestFactory.buildPostRequest(any(GenericUrl.class), any()))
82+
.thenReturn(mockHttpRequest);
8683
HttpTransport mockHttpTransport = mock(HttpTransport.class);
8784
when(mockHttpTransport.createRequestFactory()).thenReturn(mockHttpRequestFactory);
8885

@@ -91,34 +88,58 @@ void testSearchSuccess() throws Exception {
9188
SearchResponse mockSearchResponse = mock(SearchResponse.class);
9289
SearchResponse.SearchResult mockSearchResult = mock(SearchResponse.SearchResult.class);
9390
when(mockSearchResult.toString()).thenReturn("Mocked Search Result");
94-
when(mockSearchResponse.getResultsList()).thenReturn(java.util.Collections.singletonList(mockSearchResult));
91+
when(mockSearchResponse.getResultsList())
92+
.thenReturn(java.util.Collections.singletonList(mockSearchResult));
9593
when(mockSearchResponse.getResultsCount()).thenReturn(1);
96-
SearchServiceClient.SearchPagedResponse mockPagedResponse = mock(SearchServiceClient.SearchPagedResponse.class);
97-
when(mockPagedResponse.getPage()).thenReturn(new SearchResponse.SearchResult.Page() {
98-
@Override
99-
public SearchResponse getResponse() {
100-
return mockSearchResponse;
101-
}
102-
});
94+
SearchServiceClient.SearchPagedResponse mockPagedResponse =
95+
mock(SearchServiceClient.SearchPagedResponse.class);
96+
when(mockPagedResponse.getPage())
97+
.thenReturn(
98+
new SearchResponse.SearchResult.Page() {
99+
@Override
100+
public SearchResponse getResponse() {
101+
return mockSearchResponse;
102+
}
103+
});
103104
when(mockSearchServiceClient.search(any(SearchRequest.class))).thenReturn(mockPagedResponse);
104105

105-
try (MockedStatic<IamCredentialsClient> mockedIamCredentialsClient = Mockito.mockStatic(IamCredentialsClient.class);
106-
MockedStatic<SearchServiceClient> mockedSearchServiceClient = Mockito.mockStatic(SearchServiceClient.class);
107-
MockedStatic<OauthApplication> mockedOauthApplication = Mockito.mockStatic(OauthApplication.class);
108-
MockedStatic<HttpTransport> mockedHttpTransport = Mockito.mockStatic(HttpTransport.class)) {
109-
110-
mockedIamCredentialsClient.when(IamCredentialsClient::create).thenReturn(mockIamCredentialsClient);
111-
mockedSearchServiceClient.when(() -> SearchServiceClient.create(any(SearchServiceSettings.class))).thenReturn(mockSearchServiceClient);
106+
try (MockedStatic<IamCredentialsClient> mockedIamCredentialsClient =
107+
Mockito.mockStatic(IamCredentialsClient.class);
108+
MockedStatic<SearchServiceClient> mockedSearchServiceClient =
109+
Mockito.mockStatic(SearchServiceClient.class);
110+
MockedStatic<OauthApplication> mockedOauthApplication =
111+
Mockito.mockStatic(OauthApplication.class);
112+
MockedStatic<HttpTransport> mockedHttpTransport = Mockito.mockStatic(HttpTransport.class)) {
113+
114+
mockedIamCredentialsClient
115+
.when(IamCredentialsClient::create)
116+
.thenReturn(mockIamCredentialsClient);
117+
mockedSearchServiceClient
118+
.when(() -> SearchServiceClient.create(any(SearchServiceSettings.class)))
119+
.thenReturn(mockSearchServiceClient);
112120
mockedHttpTransport.when(HttpTransport::new).thenReturn(mockHttpTransport);
113121
when(mockHttpTransport.createRequestFactory()).thenReturn(mockHttpRequestFactory);
114-
when(mockHttpRequestFactory.buildPostRequest(any(GenericUrl.class), any())).thenReturn(mockHttpRequest);
122+
when(mockHttpRequestFactory.buildPostRequest(any(GenericUrl.class), any()))
123+
.thenReturn(mockHttpRequest);
115124
when(mockHttpRequest.execute()).thenReturn(mockHttpResponse);
116125
when(mockHttpResponse.parseAs(GoogleTokenResponse.class)).thenReturn(mockGoogleTokenResponse);
117126

118-
mockedOauthApplication.when(() -> OauthApplication.generateSingedJwt(searchUserEmail, serviceAccountId)).thenReturn("mockedSignedJwt");
119-
mockedOauthApplication.when(() -> OauthApplication.getAccessToken("mockedSignedJwt")).thenReturn("mockedAccessToken");
120-
121-
OauthApplication.search(projectId, location, collectionId, engineId, servingConfigId, searchQuery, searchUserEmail, serviceAccountId);
127+
mockedOauthApplication
128+
.when(() -> OauthApplication.generateSingedJwt(searchUserEmail, serviceAccountId))
129+
.thenReturn("mockedSignedJwt");
130+
mockedOauthApplication
131+
.when(() -> OauthApplication.getAccessToken("mockedSignedJwt"))
132+
.thenReturn("mockedAccessToken");
133+
134+
OauthApplication.search(
135+
projectId,
136+
location,
137+
collectionId,
138+
engineId,
139+
servingConfigId,
140+
searchQuery,
141+
searchUserEmail,
142+
serviceAccountId);
122143

123144
// Verify that the search method executed without throwing an exception. More specific
124145
// verification would be ideal, but this at least confirms the happy path.
@@ -133,10 +154,14 @@ void testGenerateSingedJwtSuccess() throws IOException {
133154
IamCredentialsClient mockIamCredentialsClient = mock(IamCredentialsClient.class);
134155
SignJwtResponse mockSignJwtResponse = mock(SignJwtResponse.class);
135156
when(mockSignJwtResponse.getSignedJwt()).thenReturn("mockedSignedJwt");
136-
when(mockIamCredentialsClient.signJwt(any(SignJwtRequest.class))).thenReturn(mockSignJwtResponse);
157+
when(mockIamCredentialsClient.signJwt(any(SignJwtRequest.class)))
158+
.thenReturn(mockSignJwtResponse);
137159

138-
try (MockedStatic<IamCredentialsClient> mockedIamCredentialsClient = Mockito.mockStatic(IamCredentialsClient.class)) {
139-
mockedIamCredentialsClient.when(IamCredentialsClient::create).thenReturn(mockIamCredentialsClient);
160+
try (MockedStatic<IamCredentialsClient> mockedIamCredentialsClient =
161+
Mockito.mockStatic(IamCredentialsClient.class)) {
162+
mockedIamCredentialsClient
163+
.when(IamCredentialsClient::create)
164+
.thenReturn(mockIamCredentialsClient);
140165

141166
String signedJwt = OauthApplication.generateSingedJwt(searchUserEmail, serviceAccountId);
142167

@@ -155,14 +180,17 @@ void testGetAccessTokenSuccess() throws IOException {
155180
HttpRequest mockHttpRequest = mock(HttpRequest.class);
156181
when(mockHttpRequest.execute()).thenReturn(mockHttpResponse);
157182
HttpRequestFactory mockHttpRequestFactory = mock(HttpRequestFactory.class);
158-
when(mockHttpRequestFactory.buildPostRequest(any(GenericUrl.class), any())).thenReturn(mockHttpRequest);
183+
when(mockHttpRequestFactory.buildPostRequest(any(GenericUrl.class), any()))
184+
.thenReturn(mockHttpRequest);
159185
HttpTransport mockHttpTransport = mock(HttpTransport.class);
160186
when(mockHttpTransport.createRequestFactory()).thenReturn(mockHttpRequestFactory);
161187

162-
try (MockedStatic<HttpTransport> mockedHttpTransport = Mockito.mockStatic(HttpTransport.class)) {
188+
try (MockedStatic<HttpTransport> mockedHttpTransport =
189+
Mockito.mockStatic(HttpTransport.class)) {
163190
mockedHttpTransport.when(HttpTransport::new).thenReturn(mockHttpTransport);
164191
when(mockHttpTransport.createRequestFactory()).thenReturn(mockHttpRequestFactory);
165-
when(mockHttpRequestFactory.buildPostRequest(any(GenericUrl.class), any())).thenReturn(mockHttpRequest);
192+
when(mockHttpRequestFactory.buildPostRequest(any(GenericUrl.class), any()))
193+
.thenReturn(mockHttpRequest);
166194
when(mockHttpRequest.execute()).thenReturn(mockHttpResponse);
167195
when(mockHttpResponse.parseAs(GoogleTokenResponse.class)).thenReturn(mockGoogleTokenResponse);
168196

0 commit comments

Comments
 (0)