Example usage for com.google.api.client.googleapis.auth.oauth2 GoogleCredential getApplicationDefault

List of usage examples for com.google.api.client.googleapis.auth.oauth2 GoogleCredential getApplicationDefault

Introduction

In this page you can find the example usage for com.google.api.client.googleapis.auth.oauth2 GoogleCredential getApplicationDefault.

Prototype

@Beta
public static GoogleCredential getApplicationDefault() throws IOException 

Source Link

Document

Beta
Returns the Application Default Credentials.

Usage

From source file:com.google.cloud.pubsub.client.demos.appengine.util.PubsubUtils.java

License:Apache License

/**
 * Builds a new Pubsub client and returns it.
 *
 * @param httpTransport HttpTransport for Pubsub client.
 * @param jsonFactory JsonFactory for Pubsub client.
 * @return Pubsub client.//from w ww.jav a 2 s .  c om
 * @throws IOException when we can not get the default credentials.
 */
public static Pubsub getClient(final HttpTransport httpTransport, final JsonFactory jsonFactory)
        throws IOException {
    Preconditions.checkNotNull(httpTransport);
    Preconditions.checkNotNull(jsonFactory);
    GoogleCredential credential = GoogleCredential.getApplicationDefault();
    if (credential.createScopedRequired()) {
        credential = credential.createScoped(PubsubScopes.all());
    }
    // Please use custom HttpRequestInitializer for automatic
    // retry upon failures.
    HttpRequestInitializer initializer = new RetryHttpInitializerWrapper(credential);
    return new Pubsub.Builder(httpTransport, jsonFactory, initializer).setApplicationName(APPLICATION_NAME)
            .build();
}

From source file:com.google.cloud.trace.sdk.LocalMetadataCredentialProvider.java

License:Open Source License

@Override
public Credential getCredential(List<String> scopes) throws CloudTraceException {
    try {//  w ww.  java2 s.c  o m
        return GoogleCredential.getApplicationDefault();
    } catch (IOException e) {
        throw new CloudTraceException("Failed to get default GoogleCredential", e);
    }
}

From source file:com.google.datastore.v1.client.DatastoreHelper.java

License:Open Source License

private static Credential getCredentialFromEnv() throws GeneralSecurityException, IOException {
    if (System.getenv(LOCAL_HOST_ENV_VAR) != null) {
        logger.log(Level.INFO, "{0} environment variable was set. Not using credentials.",
                new Object[] { LOCAL_HOST_ENV_VAR });
        return null;
    }//from ww w  .  j  a v a  2s . c om
    String serviceAccount = System.getenv(SERVICE_ACCOUNT_ENV_VAR);
    String privateKeyFile = System.getenv(PRIVATE_KEY_FILE_ENV_VAR);
    if (serviceAccount != null && privateKeyFile != null) {
        logger.log(Level.INFO,
                "{0} and {1} environment variables were set. " + "Using service account credential.",
                new Object[] { SERVICE_ACCOUNT_ENV_VAR, PRIVATE_KEY_FILE_ENV_VAR });
        return getServiceAccountCredential(serviceAccount, privateKeyFile);
    }
    return GoogleCredential.getApplicationDefault().createScoped(DatastoreOptions.SCOPES);
}

From source file:com.netflix.spinnaker.clouddriver.artifacts.gcs.GcsArtifactCredentials.java

License:Apache License

public GcsArtifactCredentials(String applicationName, GcsArtifactAccount account)
        throws IOException, GeneralSecurityException {
    HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport();
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
    String credentialsPath = account.getJsonPath();

    GoogleCredential credential;/*from  www . j  ava  2 s  . c om*/

    if (!StringUtils.isEmpty(credentialsPath)) {
        FileInputStream stream = new FileInputStream(credentialsPath);
        credential = GoogleCredential.fromStream(stream, transport, jsonFactory)
                .createScoped(Collections.singleton(StorageScopes.DEVSTORAGE_READ_ONLY));

        log.info("Loaded credentials from {}", credentialsPath);
    } else {
        log.info(
                "artifacts.gcs.enabled without artifacts.gcs.[].jsonPath. Using default application credentials.");

        credential = GoogleCredential.getApplicationDefault();
    }

    name = account.getName();
    storage = new Storage.Builder(transport, jsonFactory, credential).setApplicationName(applicationName)
            .build();
}

From source file:com.netflix.spinnaker.clouddriver.google.security.GoogleNamedAccountCredentials.java

License:Apache License

private GoogleCredentials buildCredentials() {
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
    HttpTransport httpTransport = buildHttpTransport();

    try {/*from w  ww.j a va 2 s .c  o m*/
        if (jsonKey != null) {
            try (InputStream credentialStream = new ByteArrayInputStream(jsonKey.getBytes("UTF-8"))) {
                // JSON key was specified in matching config on key server.
                GoogleCredential credential = GoogleCredential
                        .fromStream(credentialStream, httpTransport, jsonFactory)
                        .createScoped(Collections.singleton(ComputeScopes.COMPUTE));
                Compute compute = new Compute.Builder(httpTransport, jsonFactory, null)
                        .setApplicationName(applicationName)
                        .setHttpRequestInitializer(setHttpTimeout(credential))
                        .setServicePath(alphaListed ? COMPUTE_ALPHA_SERVICE_PATH : COMPUTE_SERVICE_PATH)
                        .build();

                return new GoogleCredentials(projectName, compute, alphaListed, imageProjects,
                        requiredGroupMembership, accountName);
            }
        } else {
            // No JSON key was specified in matching config on key server, so use application default credentials.
            GoogleCredential credential = GoogleCredential.getApplicationDefault();
            Compute compute = new Compute.Builder(httpTransport, jsonFactory, credential)
                    .setApplicationName(applicationName).build();

            return new GoogleCredentials(projectName, compute, alphaListed, imageProjects,
                    requiredGroupMembership, accountName);
        }
    } catch (IOException ioe) {
        throw new RuntimeException("failed to create credentials", ioe);
    }
}

From source file:com.netflix.spinnaker.fiat.roles.google.GoogleDirectoryUserRolesProvider.java

License:Apache License

private GoogleCredential getGoogleCredential() {
    try {/*w  w w . j a  va 2  s .co  m*/
        if (StringUtils.isNotEmpty(config.getCredentialPath())) {
            return GoogleCredential.fromStream(new FileInputStream(config.getCredentialPath()));
        } else {
            return GoogleCredential.getApplicationDefault();
        }
    } catch (IOException ioe) {
        throw new RuntimeException(ioe);
    }
}

From source file:com.netflix.spinnaker.front50.model.GcsStorageService.java

License:Apache License

private GoogleCredential loadCredential(HttpTransport transport, JsonFactory factory, String jsonPath)
        throws IOException {
    GoogleCredential credential;/*from  w ww .  j  a  v  a2  s .c o  m*/
    if (!jsonPath.isEmpty()) {
        FileInputStream stream = new FileInputStream(jsonPath);
        credential = GoogleCredential.fromStream(stream, transport, factory)
                .createScoped(Collections.singleton(StorageScopes.DEVSTORAGE_FULL_CONTROL));
        log.info("Loaded credentials from from " + jsonPath);
    } else {
        log.info("spinnaker.gcs.enabled without spinnaker.gcs.jsonPath. "
                + "Using default application credentials. Using default credentials.");
        credential = GoogleCredential.getApplicationDefault();
    }
    return credential;
}

From source file:com.netflix.spinnaker.halyard.backup.kms.v1.google.GoogleKms.java

License:Apache License

private static GoogleCredential loadKmsCredential(HttpTransport transport, JsonFactory factory, String jsonPath)
        throws IOException {
    GoogleCredential credential;//from w w w .j  av a  2s.co  m
    if (!jsonPath.isEmpty()) {
        FileInputStream stream = new FileInputStream(jsonPath);
        credential = GoogleCredential.fromStream(stream, transport, factory);
        log.info("Loaded kms credentials from " + jsonPath);
    } else {
        log.info("Using kms default application credentials.");
        credential = GoogleCredential.getApplicationDefault();
    }

    if (credential.createScopedRequired()) {
        credential = credential.createScoped(CloudKMSScopes.all());
    }

    return credential;
}

From source file:com.netflix.spinnaker.halyard.backup.kms.v1.google.GoogleStorage.java

License:Apache License

private static GoogleCredential loadStorageCredential(HttpTransport transport, JsonFactory factory,
        String jsonPath) throws IOException {
    GoogleCredential credential;//from   w w w  .  ja v a2 s. c om
    if (!jsonPath.isEmpty()) {
        FileInputStream stream = new FileInputStream(jsonPath);
        credential = GoogleCredential.fromStream(stream, transport, factory);
        log.info("Loaded storage credentials from " + jsonPath);
    } else {
        log.info("Using storage default application credentials.");
        credential = GoogleCredential.getApplicationDefault();
    }

    if (credential.createScopedRequired()) {
        credential = credential.createScoped(StorageScopes.all());
    }

    return credential;
}

From source file:com.netflix.spinnaker.halyard.core.registry.v1.GoogleProfileReader.java

License:Apache License

private Storage createGoogleStorage(boolean useApplicationDefaultCreds) {
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
    String applicationName = "Spinnaker/Halyard";
    HttpRequestInitializer requestInitializer;

    try {//from ww w .j  av a2s.  c o m
        GoogleCredential credential = useApplicationDefaultCreds ? GoogleCredential.getApplicationDefault()
                : new GoogleCredential();
        if (credential.createScopedRequired()) {
            credential = credential.createScoped(Collections.singleton(StorageScopes.DEVSTORAGE_FULL_CONTROL));
        }
        requestInitializer = GoogleCredentials.setHttpTimeout(credential);

        log.info("Loaded application default credential for reading BOMs & profiles.");
    } catch (Exception e) {
        requestInitializer = GoogleCredentials.retryRequestInitializer();
        log.debug(
                "No application default credential could be loaded for reading BOMs & profiles. Continuing unauthenticated: {}",
                e.getMessage());
    }

    return new Storage.Builder(GoogleCredentials.buildHttpTransport(), jsonFactory, requestInitializer)
            .setApplicationName(applicationName).build();
}