Skip to content
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
2 changes: 1 addition & 1 deletion appengine-java8/metadata/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 0 additions & 5 deletions appengine-java8/metadata/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@ Copyright 2017 Google Inc.
<!-- [END compiler] -->

<dependencies>
<dependency>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-api-1.0-sdk</artifactId>
<version>${appengine.sdk.version}</version>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -121,28 +119,33 @@ 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<String, String> 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());

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());
Expand Down