Skip to content

Commit bdc9374

Browse files
authored
Update token creation (#3443)
1 parent fa7afe3 commit bdc9374

File tree

2 files changed

+37
-33
lines changed

2 files changed

+37
-33
lines changed

run/markdown-preview/editor/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@
5656
<artifactId>okhttp</artifactId>
5757
<version>4.8.0</version>
5858
</dependency>
59+
<dependency>
60+
<groupId>com.google.auth</groupId>
61+
<artifactId>google-auth-library-oauth2-http</artifactId>
62+
<version>0.21.1</version>
63+
</dependency>
5964
<dependency>
6065
<groupId>junit</groupId>
6166
<artifactId>junit</artifactId>

run/markdown-preview/editor/src/main/java/com/example/cloudrun/RenderController.java

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
package com.example.cloudrun;
1818

19+
import com.google.auth.oauth2.GoogleCredentials;
20+
import com.google.auth.oauth2.IdTokenCredentials;
21+
import com.google.auth.oauth2.IdTokenProvider;
1922
import java.io.IOException;
2023
import java.util.concurrent.TimeUnit;
2124
import okhttp3.MediaType;
@@ -42,10 +45,11 @@ public String render(@RequestBody Data data) {
4245

4346
String url = System.getenv("EDITOR_UPSTREAM_RENDER_URL");
4447
if (url == null) {
45-
logger.error(
48+
String msg =
4649
"No configuration for upstream render service: "
47-
+ "add EDITOR_UPSTREAM_RENDER_URL environment variable");
48-
throw new IllegalStateException();
50+
+ "add EDITOR_UPSTREAM_RENDER_URL environment variable";
51+
logger.error(msg);
52+
throw new IllegalStateException(msg);
4953
}
5054

5155
String html = makeAuthenticatedRequest(url, markdown);
@@ -61,42 +65,37 @@ public String render(@RequestBody Data data) {
6165
.build();
6266

6367
// [START run_secure_request]
68+
// makeAuthenticatedRequest creates a new HTTP request authenticated by a JSON Web Tokens (JWT)
69+
// retrievd from Application Default Credentials.
6470
public String makeAuthenticatedRequest(String url, String markdown) {
65-
Request.Builder serviceRequest = new Request.Builder().url(url);
71+
String html = "";
72+
try {
73+
// Retrieve Application Default Credentials
74+
GoogleCredentials credentials = GoogleCredentials.getApplicationDefault();
75+
IdTokenCredentials tokenCredentials =
76+
IdTokenCredentials.newBuilder()
77+
.setIdTokenProvider((IdTokenProvider) credentials)
78+
.setTargetAudience(url)
79+
.build();
6680

67-
// If env var, "EDITOR_UPSTREAM_UNAUTHENTICATED", is not set then use authentication
68-
Boolean authenticated = !Boolean.valueOf(System.getenv("EDITOR_UPSTREAM_UNAUTHENTICATED"));
69-
if (authenticated) {
70-
// Set up metadata server request
71-
// https://cloud.google.com/compute/docs/instances/verifying-instance-identity#request_signature
72-
String tokenUrl =
73-
String.format(
74-
"http://metadata/computeMetadata/v1/instance/service-accounts/default/identity?audience=%s",
75-
url);
76-
Request tokenRequest =
77-
new Request.Builder().url(tokenUrl).addHeader("Metadata-Flavor", "Google").get().build();
78-
try {
79-
// Fetch the token
80-
Response tokenResponse = ok.newCall(tokenRequest).execute();
81-
String token = tokenResponse.body().string();
82-
// Provide the token in the request to the receiving service
83-
serviceRequest.addHeader("Authorization", "Bearer " + token);
84-
} catch (IOException e) {
85-
logger.error("Unable to get authorization token", e);
86-
}
87-
}
81+
// Create an ID token
82+
String token = tokenCredentials.refreshAccessToken().getTokenValue();
83+
// Instantiate HTTP request
84+
MediaType contentType = MediaType.get("text/plain; charset=utf-8");
85+
okhttp3.RequestBody body = okhttp3.RequestBody.create(markdown, contentType);
86+
Request request =
87+
new Request.Builder()
88+
.url(url)
89+
.addHeader("Authorization", "Bearer " + token)
90+
.post(body)
91+
.build();
8892

89-
MediaType contentType = MediaType.get("text/plain; charset=utf-8");
90-
okhttp3.RequestBody body = okhttp3.RequestBody.create(markdown, contentType);
91-
String response = "";
92-
try {
93-
Response serviceResponse = ok.newCall(serviceRequest.post(body).build()).execute();
94-
response = serviceResponse.body().string();
93+
Response response = ok.newCall(request).execute();
94+
html = response.body().string();
9595
} catch (IOException e) {
9696
logger.error("Unable to get rendered data", e);
9797
}
98-
99-
return response;
98+
return html;
10099
}
101100
// [END run_secure_request]
102101
}

0 commit comments

Comments
 (0)