Skip to content

Update token creation for Cloud Run Markdown sample #3443

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions run/markdown-preview/editor/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@
<artifactId>okhttp</artifactId>
<version>4.7.2</version>
</dependency>
<dependency>
<groupId>com.google.auth</groupId>
<artifactId>google-auth-library-oauth2-http</artifactId>
<version>0.21.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package com.example.cloudrun;

import com.google.auth.oauth2.GoogleCredentials;
import com.google.auth.oauth2.IdTokenCredentials;
import com.google.auth.oauth2.IdTokenProvider;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import okhttp3.MediaType;
Expand All @@ -42,10 +45,11 @@ public String render(@RequestBody Data data) {

String url = System.getenv("EDITOR_UPSTREAM_RENDER_URL");
if (url == null) {
logger.error(
String msg =
"No configuration for upstream render service: "
+ "add EDITOR_UPSTREAM_RENDER_URL environment variable");
throw new IllegalStateException();
+ "add EDITOR_UPSTREAM_RENDER_URL environment variable";
logger.error(msg);
throw new IllegalStateException(msg);
}

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

// [START run_secure_request]
// makeAuthenticatedRequest creates a new HTTP request authenticated by a JSON Web Tokens (JWT)
// retrievd from Application Default Credentials.
public String makeAuthenticatedRequest(String url, String markdown) {
Request.Builder serviceRequest = new Request.Builder().url(url);
String html = "";
try {
// Retrieve Application Default Credentials
GoogleCredentials credentials = GoogleCredentials.getApplicationDefault();
IdTokenCredentials tokenCredentials =
IdTokenCredentials.newBuilder()
.setIdTokenProvider((IdTokenProvider) credentials)
.setTargetAudience(url)
.build();

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

MediaType contentType = MediaType.get("text/plain; charset=utf-8");
okhttp3.RequestBody body = okhttp3.RequestBody.create(markdown, contentType);
String response = "";
try {
Response serviceResponse = ok.newCall(serviceRequest.post(body).build()).execute();
response = serviceResponse.body().string();
Response response = ok.newCall(request).execute();
html = response.body().string();
} catch (IOException e) {
logger.error("Unable to get rendered data", e);
}

return response;
return html;
}
// [END run_secure_request]
}