diff --git a/appengine-java8/metadata/README.md b/appengine-java8/metadata/README.md index cce04b74c42..34b6577e3f2 100644 --- a/appengine-java8/metadata/README.md +++ b/appengine-java8/metadata/README.md @@ -18,7 +18,7 @@ Use either: ## Maven ### Running locally - $ mvn appengine:run +## NOTE - There is no local Metadata server, so local running isn't available. It is possible to setup an [Emulator](https://medium.com/google-cloud/google-compute-engine-metadata-server-emulator-fe0fb1e5a8b5). ### Deploying diff --git a/appengine-java8/metadata/pom.xml b/appengine-java8/metadata/pom.xml index a3bde4ceec1..0f7a95a3ad8 100644 --- a/appengine-java8/metadata/pom.xml +++ b/appengine-java8/metadata/pom.xml @@ -36,11 +36,6 @@ Copyright 2017 Google Inc. - - com.google.appengine - appengine-api-1.0-sdk - ${appengine.sdk.version} - javax.servlet diff --git a/appengine-java8/metadata/src/main/java/com/example/appengine/standard/MetadataServlet.java b/appengine-java8/metadata/src/main/java/com/example/appengine/standard/MetadataServlet.java index b160d5e3b4e..a46d6c4a845 100644 --- a/appengine-java8/metadata/src/main/java/com/example/appengine/standard/MetadataServlet.java +++ b/appengine-java8/metadata/src/main/java/com/example/appengine/standard/MetadataServlet.java @@ -14,9 +14,6 @@ package com.example.appengine.standard; -import com.google.appengine.api.appidentity.AppIdentityService; -import com.google.appengine.api.appidentity.AppIdentityServiceFactory; -import com.google.appengine.api.utils.SystemProperty; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonParser; @@ -47,18 +44,19 @@ public class MetadataServlet extends HttpServlet { "/computeMetadata/v1/project/project-id", "/computeMetadata/v1/instance/zone", "/computeMetadata/v1/instance/service-accounts/default/aliases", + "/computeMetadata/v1/instance/service-accounts/default/email", "/computeMetadata/v1/instance/service-accounts/default/", "/computeMetadata/v1/instance/service-accounts/default/scopes", -// Tokens work - but are a security risk to display -// "/computeMetadata/v1/instance/service-accounts/default/token" + // Tokens work - but are a security risk to display + // "/computeMetadata/v1/instance/service-accounts/default/token" }; final String[] metaServiceAcct = { "/computeMetadata/v1/instance/service-accounts/{account}/aliases", "/computeMetadata/v1/instance/service-accounts/{account}/email", "/computeMetadata/v1/instance/service-accounts/{account}/scopes", -// Tokens work - but are a security risk to display -// "/computeMetadata/v1/instance/service-accounts/{account}/token" + // Tokens work - but are a security risk to display + // "/computeMetadata/v1/instance/service-accounts/{account}/token" }; private final String metadata = "http://metadata.google.internal"; @@ -90,7 +88,7 @@ String fetchMetadata(String key) throws IOException { String fetchJsonMetadata(String prefix) throws IOException { Request request = new Request.Builder() - .url(metadata + prefix ) + .url(metadata + prefix) .addHeader("Metadata-Flavor", "Google") .get() .build(); @@ -121,20 +119,25 @@ public void init() { @Override public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { - final AppIdentityService appIdentity = AppIdentityServiceFactory.getAppIdentityService(); + String defaultServiceAccount = ""; WebContext ctx = new WebContext(req, resp, getServletContext(), req.getLocale()); resp.setContentType("text/html"); - ctx.setVariable("production", SystemProperty.environment.value().name()); + String environment = + (String) System.getProperties().get("com.google.appengine.runtime.environment"); + ctx.setVariable("production", environment); // The metadata server is only on a production system - if (SystemProperty.environment.value() == SystemProperty.Environment.Value.Production) { + if (environment.equals("Production")) { TreeMap m = new TreeMap<>(); for (String key : metaPath) { m.put(key, fetchMetadata(key)); + if (key.contains("default/email")) { + defaultServiceAccount = m.get(key); + } } ctx.setVariable("Metadata", m.descendingMap()); @@ -142,7 +145,7 @@ public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOExc m = new TreeMap<>(); for (String key : metaServiceAcct) { // substitute a service account for {account} - key = key.replace("{account}", appIdentity.getServiceAccountName()); + key = key.replace("{account}", defaultServiceAccount); m.put(key, fetchMetadata(key)); } ctx.setVariable("sam", m.descendingMap());