|
17 | 17 | package com.example.cloudrun;
|
18 | 18 |
|
19 | 19 | // [START run_service_to_service_auth]
|
| 20 | +import com.google.api.client.http.GenericUrl; |
| 21 | +import com.google.api.client.http.HttpRequest; |
| 22 | +import com.google.api.client.http.HttpResponse; |
| 23 | +import com.google.api.client.http.HttpTransport; |
| 24 | +import com.google.api.client.http.javanet.NetHttpTransport; |
| 25 | +import com.google.auth.http.HttpCredentialsAdapter; |
| 26 | +import com.google.auth.oauth2.GoogleCredentials; |
| 27 | +import com.google.auth.oauth2.IdTokenCredentials; |
| 28 | +import com.google.auth.oauth2.IdTokenProvider; |
20 | 29 | import java.io.IOException;
|
21 |
| -import java.util.concurrent.TimeUnit; |
22 |
| -import okhttp3.OkHttpClient; |
23 |
| -import okhttp3.Request; |
24 |
| -import okhttp3.Response; |
25 | 30 |
|
26 | 31 | public class Authentication {
|
27 | 32 |
|
28 |
| - // Instantiate OkHttpClient |
29 |
| - private static final OkHttpClient ok = |
30 |
| - new OkHttpClient.Builder() |
31 |
| - .readTimeout(10, TimeUnit.SECONDS) |
32 |
| - .writeTimeout(10, TimeUnit.SECONDS) |
33 |
| - .build(); |
34 |
| - |
35 | 33 | // makeGetRequest makes a GET request to the specified Cloud Run endpoint,
|
36 |
| - // serviceUrl (must be a complete URL), by authenticating with the Id token |
37 |
| - // obtained from the Metadata API. |
38 |
| - public static Response makeGetRequest(String serviceUrl) throws IOException { |
39 |
| - Request.Builder serviceRequest = new Request.Builder().url(serviceUrl); |
40 |
| - |
41 |
| - // Set up metadata server request |
42 |
| - // https://cloud.google.com/compute/docs/instances/verifying-instance-identity#request_signature |
43 |
| - String tokenUrl = |
44 |
| - String.format( |
45 |
| - "http://metadata/computeMetadata/v1/instance/service-accounts/default/identity?audience=%s", |
46 |
| - serviceUrl); |
47 |
| - Request tokenRequest = |
48 |
| - new Request.Builder().url(tokenUrl).addHeader("Metadata-Flavor", "Google").get().build(); |
49 |
| - // Fetch the token |
50 |
| - try (Response tokenResponse = ok.newCall(tokenRequest).execute()) { |
51 |
| - String token = tokenResponse.body().string(); |
52 |
| - // Provide the token in the request to the receiving service |
53 |
| - serviceRequest.addHeader("Authorization", "Bearer " + token); |
54 |
| - System.out.println("Id token query succeeded."); |
55 |
| - } catch (IOException e) { |
56 |
| - System.out.println("Id token query failed: " + e); |
| 34 | + // serviceUrl (must be a complete URL), by authenticating with an Id token |
| 35 | + // retrieved from Application Default Credentials. |
| 36 | + public static HttpResponse makeGetRequest(String serviceUrl) throws IOException { |
| 37 | + GoogleCredentials credentials = GoogleCredentials.getApplicationDefault(); |
| 38 | + if (!(credentials instanceof IdTokenProvider)) { |
| 39 | + throw new IllegalArgumentException("Credentials are not an instance of IdTokenProvider."); |
57 | 40 | }
|
58 |
| - |
59 |
| - return ok.newCall(serviceRequest.get().build()).execute(); |
| 41 | + IdTokenCredentials tokenCredential = |
| 42 | + IdTokenCredentials.newBuilder() |
| 43 | + .setIdTokenProvider((IdTokenProvider) credentials) |
| 44 | + .setTargetAudience(serviceUrl) |
| 45 | + .build(); |
| 46 | + |
| 47 | + GenericUrl genericUrl = new GenericUrl(serviceUrl); |
| 48 | + HttpCredentialsAdapter adapter = new HttpCredentialsAdapter(tokenCredential); |
| 49 | + HttpTransport transport = new NetHttpTransport(); |
| 50 | + HttpRequest request = transport.createRequestFactory(adapter).buildGetRequest(genericUrl); |
| 51 | + return request.execute(); |
60 | 52 | }
|
61 | 53 | }
|
62 | 54 | // [END run_service_to_service_auth]
|
63 |
| - |
0 commit comments