Search in sources :

Example 6 with AppIdentityService

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();
    }
}
Also used : AppIdentityService(com.google.appengine.api.appidentity.AppIdentityService) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential)

Example 7 with AppIdentityService

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());
}
Also used : Cookie(javax.servlet.http.Cookie) AppIdentityService(com.google.appengine.api.appidentity.AppIdentityService) WebContext(org.thymeleaf.context.WebContext) Enumeration(java.util.Enumeration) ApiProxy(com.google.apphosting.api.ApiProxy) TreeMap(java.util.TreeMap) Properties(java.util.Properties)

Example 8 with AppIdentityService

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());
    }
}
Also used : AppIdentityService(com.google.appengine.api.appidentity.AppIdentityService) Reader(java.io.Reader) StringReader(java.io.StringReader)

Aggregations

AppIdentityService (com.google.appengine.api.appidentity.AppIdentityService)8 ApiProxy (com.google.apphosting.api.ApiProxy)3 Enumeration (java.util.Enumeration)3 Properties (java.util.Properties)3 Cookie (javax.servlet.http.Cookie)3 InputStreamReader (java.io.InputStreamReader)2 OutputStreamWriter (java.io.OutputStreamWriter)2 HttpURLConnection (java.net.HttpURLConnection)2 URL (java.net.URL)2 ArrayList (java.util.ArrayList)2 TreeMap (java.util.TreeMap)2 JSONObject (org.json.JSONObject)2 WebContext (org.thymeleaf.context.WebContext)2 GoogleCredential (com.google.api.client.googleapis.auth.oauth2.GoogleCredential)1 BaseEncoding (com.google.common.io.BaseEncoding)1 Gson (com.google.gson.Gson)1 BufferedReader (java.io.BufferedReader)1 InputStream (java.io.InputStream)1 PrintWriter (java.io.PrintWriter)1 Reader (java.io.Reader)1