use of com.google.appengine.api.appidentity.AppIdentityService in project workbench by all-of-us.
the class FireCloudConfig method getWorkbenchServiceAccountAccessToken.
private String getWorkbenchServiceAccountAccessToken(WorkbenchEnvironment workbenchEnvironment) throws IOException {
// when running in Cloud.
if (workbenchEnvironment.isDevelopment()) {
GoogleCredential credential = GoogleCredential.getApplicationDefault().createScoped(Arrays.asList(BILLING_SCOPES));
credential.refreshToken();
return credential.getAccessToken();
} else {
AppIdentityService appIdentity = AppIdentityServiceFactory.getAppIdentityService();
final AppIdentityService.GetAccessTokenResult accessTokenResult = appIdentity.getAccessToken(Arrays.asList(BILLING_SCOPES));
return accessTokenResult.getAccessToken();
}
}
use of com.google.appengine.api.appidentity.AppIdentityService in project java-docs-samples by GoogleCloudPlatform.
the class GaeInfoServlet method doGet.
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String key = "";
final AppIdentityService appIdentity = AppIdentityServiceFactory.getAppIdentityService();
WebContext ctx = new WebContext(req, resp, getServletContext(), req.getLocale());
resp.setContentType("text/html");
ctx.setVariable("production", SystemProperty.environment.value().name());
ctx.setVariable("ServiceAccountName", appIdentity.getServiceAccountName());
ctx.setVariable("gcs", appIdentity.getDefaultGcsBucketName());
ctx.setVariable("appId", SystemProperty.applicationId.get());
ctx.setVariable("appVer", SystemProperty.applicationVersion.get());
ctx.setVariable("version", SystemProperty.version.get());
ctx.setVariable("environment", SystemProperty.environment.get());
// Environment Atributes
ApiProxy.Environment env = ApiProxy.getCurrentEnvironment();
Map<String, Object> attr = env.getAttributes();
TreeMap<String, String> m = new TreeMap<>();
for (String k : attr.keySet()) {
Object o = attr.get(k);
if (o.getClass().getCanonicalName().equals("java.lang.String")) {
m.put(k, (String) o);
} else if (o.getClass().getCanonicalName().equals("java.lang.Boolean")) {
m.put(k, ((Boolean) o).toString());
} else {
m.put(k, "a " + o.getClass().getCanonicalName());
}
}
ctx.setVariable("attribs", m);
m = new TreeMap<>();
for (Enumeration<String> e = req.getHeaderNames(); e.hasMoreElements(); ) {
key = e.nextElement();
m.put(key, req.getHeader(key));
}
ctx.setVariable("headers", m);
Cookie[] cookies = req.getCookies();
m = new TreeMap<>();
if (cookies != null && cookies.length != 0) {
for (Cookie co : cookies) {
m.put(co.getName(), co.getValue());
}
}
ctx.setVariable("cookies", m);
Properties properties = System.getProperties();
m = new TreeMap<>();
for (Enumeration e = properties.propertyNames(); e.hasMoreElements(); ) {
key = (String) e.nextElement();
m.put(key, (String) properties.get(key));
}
ctx.setVariable("systemprops", m);
Map<String, String> envVar = System.getenv();
m = new TreeMap<>(envVar);
ctx.setVariable("envvar", m);
// The metadata server is only on a production system
if (SystemProperty.environment.value() == SystemProperty.Environment.Value.Production) {
m = new TreeMap<>();
for (String k : metaPath) {
m.put(k, fetchMetadata(k));
}
ctx.setVariable("Metadata", m.descendingMap());
m = new TreeMap<>();
for (String k : metaServiceAcct) {
// substitute a service account for {account}
k = k.replace("{account}", appIdentity.getServiceAccountName());
m.put(k, fetchMetadata(k));
}
ctx.setVariable("sam", m.descendingMap());
// Recursivly get all info about service accounts -- Note tokens are leftout by default.
ctx.setVariable("rsa", fetchJsonMetadata("/computeMetadata/v1/instance/service-accounts/?recursive=true"));
// Recursivly get all data on Metadata server.
ctx.setVariable("ram", fetchJsonMetadata("/?recursive=true"));
}
templateEngine.process("index", ctx, resp.getWriter());
}
use of com.google.appengine.api.appidentity.AppIdentityService in project activityinfo by bedatadriven.
the class ConfigModule method tryToLoadFromDefaultBucket.
private void tryToLoadFromDefaultBucket(Properties properties) {
try {
GcsService gcsService = GcsServiceFactory.createGcsService(RetryParams.getDefaultInstance());
AppIdentityService appIdentity = AppIdentityServiceFactory.getAppIdentityService();
GcsFilename fileName = new GcsFilename(appIdentity.getDefaultGcsBucketName(), "config.properties");
logger.log(Level.INFO, "Trying to read configuration from GCS at" + fileName);
GcsInputChannel readChannel = gcsService.openReadChannel(fileName, 0);
try (Reader reader = Channels.newReader(readChannel, Charsets.UTF_8.name())) {
properties.load(reader);
logger.log(Level.INFO, "Read config from GCS.");
}
} catch (Exception e) {
logger.log(Level.INFO, "Could not read configuration properties from GCS: " + e.getMessage());
}
}
Aggregations