List of usage examples for com.google.api.client.googleapis.auth.oauth2 GoogleCredential getApplicationDefault
@Beta public static GoogleCredential getApplicationDefault(HttpTransport transport, JsonFactory jsonFactory) throws IOException
From source file:com.google.cloud.sparkdemo.CloudPubsubReceiver.java
License:Apache License
private Pubsub createAuthorizedClient() { try {/*from w w w. jav a2s . c o m*/ // Create the credential HttpTransport httpTransport = Utils.getDefaultTransport(); JsonFactory jsonFactory = Utils.getDefaultJsonFactory(); GoogleCredential credential = GoogleCredential.getApplicationDefault(httpTransport, jsonFactory); if (credential.createScopedRequired()) { credential = credential.createScoped(PubsubScopes.all()); } HttpRequestInitializer initializer = new RetryHttpInitializerWrapper(credential); return new Pubsub.Builder(httpTransport, jsonFactory, initializer) .setApplicationName("spark-pubsub-receiver").build(); } catch (IOException e) { reportError("Unable to create Cloud Pub/sub client.", e); return null; } }
From source file:com.google.cloud.storage.storagetransfer.samples.TransferClientCreator.java
License:Open Source License
/** * Create a Storage Transfer client using application default credentials and other default * settings.//from www . ja v a 2 s. c o m * * @return a Storage Transfer client * @throws IOException * there was an error obtaining application default credentials */ public static Storagetransfer createStorageTransferClient() throws IOException { HttpTransport httpTransport = Utils.getDefaultTransport(); JsonFactory jsonFactory = Utils.getDefaultJsonFactory(); GoogleCredential credential = GoogleCredential.getApplicationDefault(httpTransport, jsonFactory); return createStorageTransferClient(httpTransport, jsonFactory, credential); }
From source file:com.google.crypto.tink.integration.gcpkms.GcpKmsClient.java
License:Apache License
/** * Loads <a href="https://developers.google.com/accounts/docs/application-default-credentials" * default Google Cloud credentials</a>. */// w w w. j a va2s .c o m @Override public KmsClient withDefaultCredentials() throws GeneralSecurityException { try { GoogleCredential credentials = GoogleCredential.getApplicationDefault(new NetHttpTransport(), new JacksonFactory()); return withCredentials(credentials); } catch (IOException e) { throw new GeneralSecurityException("cannot load default credentials", e); } }
From source file:com.google.pubsub.clients.common.MetricsHandler.java
License:Apache License
private void initialize() { synchronized (this) { try {//from w ww. j a v a2s. c o m HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport(); JsonFactory jsonFactory = new JacksonFactory(); GoogleCredential credential = GoogleCredential.getApplicationDefault(transport, jsonFactory); if (credential.createScopedRequired()) { credential = credential.createScoped( Collections.singletonList("https://www.googleapis.com/auth/cloud-platform")); } monitoring = new Monitoring.Builder(transport, jsonFactory, credential) .setApplicationName("Cloud Pub/Sub Loadtest Framework").build(); String zoneId; String instanceId; try { DefaultHttpClient httpClient = new DefaultHttpClient(); httpClient.addRequestInterceptor(new RequestAcceptEncoding()); httpClient.addResponseInterceptor(new ResponseContentEncoding()); HttpConnectionParams.setConnectionTimeout(httpClient.getParams(), 30000); HttpConnectionParams.setSoTimeout(httpClient.getParams(), 30000); HttpConnectionParams.setSoKeepalive(httpClient.getParams(), true); HttpConnectionParams.setStaleCheckingEnabled(httpClient.getParams(), false); HttpConnectionParams.setTcpNoDelay(httpClient.getParams(), true); SchemeRegistry schemeRegistry = httpClient.getConnectionManager().getSchemeRegistry(); schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory())); schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory())); httpClient.setKeepAliveStrategy((response, ctx) -> 30); HttpGet zoneIdRequest = new HttpGet( "http://metadata.google.internal/computeMetadata/v1/instance/zone"); zoneIdRequest.setHeader("Metadata-Flavor", "Google"); HttpResponse zoneIdResponse = httpClient.execute(zoneIdRequest); String tempZoneId = EntityUtils.toString(zoneIdResponse.getEntity()); if (tempZoneId.lastIndexOf("/") >= 0) { zoneId = tempZoneId.substring(tempZoneId.lastIndexOf("/") + 1); } else { zoneId = tempZoneId; } HttpGet instanceIdRequest = new HttpGet( "http://metadata.google.internal/computeMetadata/v1/instance/id"); instanceIdRequest.setHeader("Metadata-Flavor", "Google"); HttpResponse instanceIdResponse = httpClient.execute(instanceIdRequest); instanceId = EntityUtils.toString(instanceIdResponse.getEntity()); } catch (IOException e) { log.info("Unable to connect to metadata server, assuming not on GCE, setting " + "defaults for instance and zone."); instanceId = "local"; zoneId = "us-east1-b"; // Must use a valid cloud zone even if running local. } monitoredResource.setLabels( ImmutableMap.of("project_id", project, "instance_id", instanceId, "zone", zoneId)); createMetrics(); } catch (IOException e) { log.error("Unable to initialize MetricsHandler, trying again.", e); executor.execute(this::initialize); } catch (GeneralSecurityException e) { log.error("Unable to initialize MetricsHandler permanently, credentials error.", e); } } }
From source file:com.google.pubsub.flic.controllers.GCEController.java
License:Apache License
/** Returns a GCEController using default application credentials. */ public static GCEController newGCEController(String projectName, Map<String, Map<ClientParams, Integer>> types, ScheduledExecutorService executor) { try {/*from w ww . ja va 2 s . com*/ HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport(); JsonFactory jsonFactory = new JacksonFactory(); GoogleCredential credential = GoogleCredential.getApplicationDefault(transport, jsonFactory); if (credential.createScopedRequired()) { credential = credential .createScoped(Collections.singletonList("https://www.googleapis.com/auth/cloud-platform")); } return new GCEController(projectName, types, executor, new Storage.Builder(transport, jsonFactory, credential) .setApplicationName("Cloud Pub/Sub Loadtest Framework").build(), new Compute.Builder(transport, jsonFactory, credential) .setApplicationName("Cloud Pub/Sub Loadtest Framework").build(), new Pubsub.Builder(transport, jsonFactory, credential) .setApplicationName("Cloud Pub/Sub Loadtest Framework").build()); } catch (Throwable t) { return null; } }
From source file:com.google.pubsub.flic.Driver.java
License:Apache License
public void run(BiFunction<String, Map<ClientParams, Integer>, Controller> controllerFunction) { try {//from w ww. j a v a 2s . c o m Preconditions.checkArgument( cpsPublisherCount > 0 || cpsSubscriberCount > 0 || kafkaPublisherCount > 0 || kafkaSubscriberCount > 0, "You must set at least one type of client greater than 0."); Preconditions.checkArgument(broker != null || (kafkaPublisherCount == 0 && kafkaSubscriberCount == 0), "If using Kafka you must provide the network address of your broker using the" + "--broker flag."); if (maxPublishLatencyTest) { Preconditions.checkArgument(kafkaPublisherCount > 0 ^ cpsPublisherCount > 0, "If max_publish_latency is specified, there can only be one type of publisher."); } Controller.resourceDirectory = resourceDirectory; Map<ClientParams, Integer> clientParamsMap = new HashMap<>(); clientParamsMap.putAll(ImmutableMap.of(new ClientParams(ClientType.CPS_GCLOUD_JAVA_PUBLISHER, null), cpsPublisherCount, new ClientParams(ClientType.KAFKA_PUBLISHER, null), kafkaPublisherCount, new ClientParams(ClientType.KAFKA_SUBSCRIBER, null), kafkaSubscriberCount)); // Each type of client will have its own topic, so each topic will get // cpsSubscriberCount subscribers cumulatively among each of the subscriptions. for (int i = 0; i < cpsSubscriptionFanout; ++i) { clientParamsMap.put( new ClientParams(ClientType.CPS_GCLOUD_JAVA_SUBSCRIBER, "gcloud-subscription" + i), cpsSubscriberCount / cpsSubscriptionFanout); } Client.messageSize = messageSize; Client.requestRate = 1; Client.loadtestLengthSeconds = loadtestLengthSeconds; Client.publishBatchSize = publishBatchSize; Client.maxMessagesPerPull = cpsMaxMessagesPerPull; Client.pollLength = kafkaPollLength; Client.broker = broker; Client.requestRate = requestRate; Client.maxOutstandingRequests = maxOutstandingRequests; Client.numberOfMessages = numberOfMessages; Controller controller = controllerFunction.apply(project, clientParamsMap); // Start a thread to poll and output results. ScheduledExecutorService pollingExecutor = Executors.newSingleThreadScheduledExecutor(); pollingExecutor.scheduleWithFixedDelay(() -> { synchronized (pollingExecutor) { log.info("==============================================="); printStats(controller.getStatsForAllClientTypes()); log.info("==============================================="); } }, 5, 5, TimeUnit.SECONDS); Map<ClientType, Controller.LoadtestStats> statsMap; AtomicDouble publishLatency = new AtomicDouble(0); final HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport(); final JsonFactory jsonFactory = new JacksonFactory(); final GoogleCredential credential = GoogleCredential.getApplicationDefault(transport, jsonFactory) .createScoped(Collections.singletonList("https://www.googleapis.com/auth/cloud-platform")); final Monitoring monitoring = new Monitoring.Builder(transport, jsonFactory, credential) .setApplicationName("Cloud Pub/Sub Loadtest Framework").build(); final SimpleDateFormat dateFormatter; dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); dateFormatter.setTimeZone(TimeZone.getTimeZone("GMT")); int highestRequestRate = 0; long backlogSize = 0; do { Client.startTime = Timestamp.newBuilder().setSeconds(System.currentTimeMillis() / 1000 + 90) .build(); Client.burnInTimeMillis = (Client.startTime.getSeconds() + burnInDurationSeconds) * 1000; Date startDate = new Date(); startDate.setTime(Client.startTime.getSeconds() * 1000); MessageTracker messageTracker = new MessageTracker(numberOfMessages, cpsPublisherCount); controller.startClients(messageTracker); if (maxSubscriberThroughputTest) { controller.waitForPublisherClients(); ListTimeSeriesResponse response = monitoring.projects().timeSeries().list("projects/" + project) .setFilter( "metric.type = \"pubsub.googleapis.com/subscription/num_undelivered_messages\"") .setIntervalStartTime(dateFormatter.format(startDate)) .setIntervalEndTime(dateFormatter.format(new Date())).execute(); // Get most recent point. Point latestBacklogSize = null; for (TimeSeries timeSeries : response.getTimeSeries()) { for (Point point : timeSeries.getPoints()) { if (latestBacklogSize == null || dateFormatter.parse(point.getInterval().getStartTime()) .after(dateFormatter.parse(latestBacklogSize.getInterval().getStartTime()))) { latestBacklogSize = point; } } } if (latestBacklogSize != null) { backlogSize = latestBacklogSize.getValue().getInt64Value(); } if (backlogSize > maxSubscriberThroughputTestBacklog) { log.info("We accumulated a backlog during this test, refer to the last run " + "for the maximum throughput attained before accumulating backlog."); } } // Wait for the load test to finish. controller.waitForClients(); statsMap = controller.getStatsForAllClientTypes(); if (maxPublishLatencyTest) { statsMap.forEach((type, stats) -> { if (type.isPublisher()) { publishLatency.set(LatencyDistribution.getNthPercentileUpperBound(stats.bucketValues, maxPublishLatencyPercentile)); } }); } if (publishLatency.get() < maxPublishLatencyMillis) { highestRequestRate = Client.requestRate; } Client.requestRate = (int) (Client.requestRate * 1.1); printStats(statsMap); List<MessageIdentifier> missing = messageTracker.getMissing(); if (!missing.isEmpty()) { log.error("Some published messages were not received!"); for (MessageIdentifier identifier : missing) { log.error(String.format("%d:%d", identifier.getPublisherClientId(), identifier.getSequenceNumber())); } } if (spreadsheetId.length() > 0) { // Output results to common Google sheet SheetsService service = new SheetsService(dataStoreDirectory, controller.getTypes()); service.sendToSheets(spreadsheetId, statsMap); } } while ((maxPublishLatencyTest && publishLatency.get() < maxPublishLatencyMillis) || (maxSubscriberThroughputTest && backlogSize < maxSubscriberThroughputTestBacklog)); synchronized (pollingExecutor) { pollingExecutor.shutdownNow(); } if (maxPublishLatencyTest) { // This calculates the request rate of the last successful run. log.info("Maximum Request Rate: " + highestRequestRate); } controller.shutdown(null); System.exit(0); } catch (Throwable t) { log.error("An error occurred...", t); System.exit(1); } }
From source file:com.spotify.google.cloud.pubsub.client.Pubsub.java
License:Apache License
private static Credential defaultCredential() { try {/*from w w w . j av a2 s . c o m*/ return GoogleCredential.getApplicationDefault(Utils.getDefaultTransport(), Utils.getDefaultJsonFactory()); } catch (IOException e) { throw Throwables.propagate(e); } }
From source file:com.spotify.styx.StyxScheduler.java
License:Apache License
private static Container createGkeClient() { try {//from w w w. j a v a 2s. com final HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); final JsonFactory jsonFactory = Utils.getDefaultJsonFactory(); final GoogleCredential credential = GoogleCredential.getApplicationDefault(httpTransport, jsonFactory) .createScoped(ContainerScopes.all()); return new Container.Builder(httpTransport, jsonFactory, credential).setApplicationName(SERVICE_NAME) .build(); } catch (GeneralSecurityException | IOException e) { throw new RuntimeException(e); } }
From source file:com.spotify.styx.StyxScheduler.java
License:Apache License
private static ServiceAccountKeyManager createServiceAccountKeyManager() { try {/* w ww . ja v a 2s . c o m*/ final HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); final JsonFactory jsonFactory = Utils.getDefaultJsonFactory(); final GoogleCredential credential = GoogleCredential.getApplicationDefault(httpTransport, jsonFactory) .createScoped(IamScopes.all()); final Iam iam = new Iam.Builder(httpTransport, jsonFactory, credential).setApplicationName(SERVICE_NAME) .build(); return new ServiceAccountKeyManager(iam); } catch (GeneralSecurityException | IOException e) { throw new RuntimeException(e); } }
From source file:io.druid.storage.google.GoogleStorageDruidModule.java
License:Apache License
@Provides @LazySingleton/*from w w w .j a v a 2 s .com*/ public GoogleStorage getGoogleStorage(final GoogleAccountConfig config) throws IOException, GeneralSecurityException { LOG.info("Building Cloud Storage Client..."); HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); GoogleCredential credential = GoogleCredential.getApplicationDefault(httpTransport, jsonFactory); if (credential.createScopedRequired()) { credential = credential.createScoped(StorageScopes.all()); } Storage storage = new Storage.Builder(httpTransport, jsonFactory, credential) .setApplicationName(APPLICATION_NAME).build(); return new GoogleStorage(storage); }