16
16
17
17
package com .example .cloudrun ;
18
18
19
+ import com .google .auth .oauth2 .GoogleCredentials ;
20
+ import com .google .auth .oauth2 .IdTokenCredentials ;
21
+ import com .google .auth .oauth2 .IdTokenProvider ;
19
22
import java .io .IOException ;
20
23
import java .util .concurrent .TimeUnit ;
21
24
import okhttp3 .MediaType ;
@@ -42,10 +45,11 @@ public String render(@RequestBody Data data) {
42
45
43
46
String url = System .getenv ("EDITOR_UPSTREAM_RENDER_URL" );
44
47
if (url == null ) {
45
- logger . error (
48
+ String msg =
46
49
"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 );
49
53
}
50
54
51
55
String html = makeAuthenticatedRequest (url , markdown );
@@ -61,42 +65,37 @@ public String render(@RequestBody Data data) {
61
65
.build ();
62
66
63
67
// [START run_secure_request]
68
+ // makeAuthenticatedRequest creates a new HTTP request authenticated by a JSON Web Tokens (JWT)
69
+ // retrievd from Application Default Credentials.
64
70
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 ();
66
80
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 ();
88
92
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 ();
95
95
} catch (IOException e ) {
96
96
logger .error ("Unable to get rendered data" , e );
97
97
}
98
-
99
- return response ;
98
+ return html ;
100
99
}
101
100
// [END run_secure_request]
102
101
}
0 commit comments