|
14 | 14 |
|
15 | 15 | package com.example.appengine.standard; |
16 | 16 |
|
17 | | -import com.google.appengine.api.appidentity.AppIdentityService; |
18 | | -import com.google.appengine.api.appidentity.AppIdentityServiceFactory; |
19 | | -import com.google.appengine.api.utils.SystemProperty; |
20 | 17 | import com.google.gson.Gson; |
21 | 18 | import com.google.gson.GsonBuilder; |
22 | 19 | import com.google.gson.JsonParser; |
@@ -47,18 +44,19 @@ public class MetadataServlet extends HttpServlet { |
47 | 44 | "/computeMetadata/v1/project/project-id", |
48 | 45 | "/computeMetadata/v1/instance/zone", |
49 | 46 | "/computeMetadata/v1/instance/service-accounts/default/aliases", |
| 47 | + "/computeMetadata/v1/instance/service-accounts/default/email", |
50 | 48 | "/computeMetadata/v1/instance/service-accounts/default/", |
51 | 49 | "/computeMetadata/v1/instance/service-accounts/default/scopes", |
52 | | -// Tokens work - but are a security risk to display |
53 | | -// "/computeMetadata/v1/instance/service-accounts/default/token" |
| 50 | + // Tokens work - but are a security risk to display |
| 51 | + // "/computeMetadata/v1/instance/service-accounts/default/token" |
54 | 52 | }; |
55 | 53 |
|
56 | 54 | final String[] metaServiceAcct = { |
57 | 55 | "/computeMetadata/v1/instance/service-accounts/{account}/aliases", |
58 | 56 | "/computeMetadata/v1/instance/service-accounts/{account}/email", |
59 | 57 | "/computeMetadata/v1/instance/service-accounts/{account}/scopes", |
60 | | -// Tokens work - but are a security risk to display |
61 | | -// "/computeMetadata/v1/instance/service-accounts/{account}/token" |
| 58 | + // Tokens work - but are a security risk to display |
| 59 | + // "/computeMetadata/v1/instance/service-accounts/{account}/token" |
62 | 60 | }; |
63 | 61 |
|
64 | 62 | private final String metadata = "http://metadata.google.internal"; |
@@ -90,7 +88,7 @@ String fetchMetadata(String key) throws IOException { |
90 | 88 |
|
91 | 89 | String fetchJsonMetadata(String prefix) throws IOException { |
92 | 90 | Request request = new Request.Builder() |
93 | | - .url(metadata + prefix ) |
| 91 | + .url(metadata + prefix) |
94 | 92 | .addHeader("Metadata-Flavor", "Google") |
95 | 93 | .get() |
96 | 94 | .build(); |
@@ -121,28 +119,33 @@ public void init() { |
121 | 119 |
|
122 | 120 | @Override |
123 | 121 | public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { |
124 | | - final AppIdentityService appIdentity = AppIdentityServiceFactory.getAppIdentityService(); |
| 122 | + String defaultServiceAccount = ""; |
125 | 123 | WebContext ctx = new WebContext(req, resp, getServletContext(), req.getLocale()); |
126 | 124 |
|
127 | 125 | resp.setContentType("text/html"); |
128 | 126 |
|
129 | | - ctx.setVariable("production", SystemProperty.environment.value().name()); |
| 127 | + String environment = |
| 128 | + (String) System.getProperties().get("com.google.appengine.runtime.environment"); |
| 129 | + ctx.setVariable("production", environment); |
130 | 130 |
|
131 | 131 | // The metadata server is only on a production system |
132 | | - if (SystemProperty.environment.value() == SystemProperty.Environment.Value.Production) { |
| 132 | + if (environment.equals("Production")) { |
133 | 133 |
|
134 | 134 | TreeMap<String, String> m = new TreeMap<>(); |
135 | 135 |
|
136 | 136 | for (String key : metaPath) { |
137 | 137 | m.put(key, fetchMetadata(key)); |
| 138 | + if (key.contains("default/email")) { |
| 139 | + defaultServiceAccount = m.get(key); |
| 140 | + } |
138 | 141 | } |
139 | 142 |
|
140 | 143 | ctx.setVariable("Metadata", m.descendingMap()); |
141 | 144 |
|
142 | 145 | m = new TreeMap<>(); |
143 | 146 | for (String key : metaServiceAcct) { |
144 | 147 | // substitute a service account for {account} |
145 | | - key = key.replace("{account}", appIdentity.getServiceAccountName()); |
| 148 | + key = key.replace("{account}", defaultServiceAccount); |
146 | 149 | m.put(key, fetchMetadata(key)); |
147 | 150 | } |
148 | 151 | ctx.setVariable("sam", m.descendingMap()); |
|
0 commit comments