Search in sources :

Example 1 with Credentials

use of com.google.auth.Credentials in project beam by apache.

the class V1TestUtil method getDatastore.

/**
   * Build a new datastore client.
   */
static Datastore getDatastore(PipelineOptions pipelineOptions, String projectId) {
    Credentials credential = pipelineOptions.as(GcpOptions.class).getGcpCredential();
    HttpRequestInitializer initializer;
    if (credential != null) {
        initializer = new ChainingHttpRequestInitializer(new HttpCredentialsAdapter(credential), new RetryHttpRequestInitializer());
    } else {
        initializer = new RetryHttpRequestInitializer();
    }
    DatastoreOptions.Builder builder = new DatastoreOptions.Builder().projectId(projectId).initializer(initializer);
    return DatastoreFactory.get().create(builder.build());
}
Also used : RetryHttpRequestInitializer(org.apache.beam.sdk.util.RetryHttpRequestInitializer) GcpOptions(org.apache.beam.sdk.extensions.gcp.options.GcpOptions) HttpCredentialsAdapter(com.google.auth.http.HttpCredentialsAdapter) DatastoreOptions(com.google.datastore.v1.client.DatastoreOptions) RetryHttpRequestInitializer(org.apache.beam.sdk.util.RetryHttpRequestInitializer) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer) ChainingHttpRequestInitializer(com.google.cloud.hadoop.util.ChainingHttpRequestInitializer) Credentials(com.google.auth.Credentials) ChainingHttpRequestInitializer(com.google.cloud.hadoop.util.ChainingHttpRequestInitializer)

Example 2 with Credentials

use of com.google.auth.Credentials in project google-cloud-java by GoogleCloudPlatform.

the class GrpcTransportOptions method setUpChannelProvider.

/**
   * Returns a channel provider from the given default provider.
   */
public static ChannelProvider setUpChannelProvider(InstantiatingChannelProvider.Builder providerBuilder, ServiceOptions<?, ?> serviceOptions) {
    providerBuilder.setEndpoint(serviceOptions.getHost()).setClientLibHeader(ServiceOptions.getGoogApiClientLibName(), firstNonNull(serviceOptions.getLibraryVersion(), ""));
    Credentials scopedCredentials = serviceOptions.getScopedCredentials();
    if (scopedCredentials != null && scopedCredentials != NoCredentials.getInstance()) {
        providerBuilder.setCredentialsProvider(FixedCredentialsProvider.create(scopedCredentials));
    }
    return providerBuilder.build();
}
Also used : NoCredentials(com.google.cloud.NoCredentials) Credentials(com.google.auth.Credentials)

Example 3 with Credentials

use of com.google.auth.Credentials in project google-cloud-java by GoogleCloudPlatform.

the class HttpTransportOptions method getHttpRequestInitializer.

/**
   * Returns a request initializer responsible for initializing requests according to service
   * options.
   */
public HttpRequestInitializer getHttpRequestInitializer(final ServiceOptions<?, ?> serviceOptions) {
    Credentials scopedCredentials = serviceOptions.getScopedCredentials();
    final HttpRequestInitializer delegate = scopedCredentials != null && scopedCredentials != NoCredentials.getInstance() ? new HttpCredentialsAdapter(scopedCredentials) : null;
    return new HttpRequestInitializer() {

        @Override
        public void initialize(HttpRequest httpRequest) throws IOException {
            if (delegate != null) {
                delegate.initialize(httpRequest);
            }
            if (connectTimeout >= 0) {
                httpRequest.setConnectTimeout(connectTimeout);
            }
            if (readTimeout >= 0) {
                httpRequest.setReadTimeout(readTimeout);
            }
            HttpHeaders headers = httpRequest.getHeaders();
            headers.set("x-goog-api-client", getXGoogApiClientHeader(serviceOptions));
        }
    };
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) HttpHeaders(com.google.api.client.http.HttpHeaders) HttpCredentialsAdapter(com.google.auth.http.HttpCredentialsAdapter) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer) NoCredentials(com.google.cloud.NoCredentials) Credentials(com.google.auth.Credentials)

Example 4 with Credentials

use of com.google.auth.Credentials in project google-cloud-java by GoogleCloudPlatform.

the class Subscriber method startPollingConnections.

private void startPollingConnections() throws IOException {
    synchronized (pollingSubscriberConnections) {
        Credentials credentials = credentialsProvider.getCredentials();
        CallCredentials callCredentials = credentials == null ? null : MoreCallCredentials.from(credentials);
        for (int i = 0; i < numChannels; i++) {
            SubscriberFutureStub stub = SubscriberGrpc.newFutureStub(channels.get(i));
            if (callCredentials != null) {
                stub = stub.withCallCredentials(callCredentials);
            }
            pollingSubscriberConnections.add(new PollingSubscriberConnection(cachedSubscriptionNameString, receiver, ackExpirationPadding, maxAckExtensionPeriod, ackLatencyDistribution, stub, flowController, flowControlSettings.getMaxOutstandingElementCount(), executor, alarmsExecutor, clock));
        }
        startConnections(pollingSubscriberConnections, new Listener() {

            @Override
            public void failed(State from, Throwable failure) {
                // If a connection failed is because of a fatal error, we should fail the
                // whole subscriber.
                stopAllPollingConnections();
                try {
                    notifyFailed(failure);
                } catch (IllegalStateException e) {
                    if (isRunning()) {
                        throw e;
                    }
                // It could happen that we are shutting down while some channels fail.
                }
            }
        });
    }
}
Also used : CallCredentials(io.grpc.CallCredentials) MoreCallCredentials(io.grpc.auth.MoreCallCredentials) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials) Credentials(com.google.auth.Credentials) CallCredentials(io.grpc.CallCredentials) MoreCallCredentials(io.grpc.auth.MoreCallCredentials) SubscriberFutureStub(com.google.pubsub.v1.SubscriberGrpc.SubscriberFutureStub)

Example 5 with Credentials

use of com.google.auth.Credentials in project beam by apache.

the class BigqueryMatcher method newBigqueryClient.

@VisibleForTesting
Bigquery newBigqueryClient(String applicationName) {
    HttpTransport transport = Transport.getTransport();
    JsonFactory jsonFactory = Transport.getJsonFactory();
    Credentials credential = getDefaultCredential();
    return new Bigquery.Builder(transport, jsonFactory, new HttpCredentialsAdapter(credential)).setApplicationName(applicationName).build();
}
Also used : HttpTransport(com.google.api.client.http.HttpTransport) HttpCredentialsAdapter(com.google.auth.http.HttpCredentialsAdapter) JsonFactory(com.google.api.client.json.JsonFactory) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials) Credentials(com.google.auth.Credentials) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

Credentials (com.google.auth.Credentials)5 HttpCredentialsAdapter (com.google.auth.http.HttpCredentialsAdapter)3 HttpRequestInitializer (com.google.api.client.http.HttpRequestInitializer)2 GoogleCredentials (com.google.auth.oauth2.GoogleCredentials)2 NoCredentials (com.google.cloud.NoCredentials)2 HttpHeaders (com.google.api.client.http.HttpHeaders)1 HttpRequest (com.google.api.client.http.HttpRequest)1 HttpTransport (com.google.api.client.http.HttpTransport)1 JsonFactory (com.google.api.client.json.JsonFactory)1 ChainingHttpRequestInitializer (com.google.cloud.hadoop.util.ChainingHttpRequestInitializer)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 DatastoreOptions (com.google.datastore.v1.client.DatastoreOptions)1 SubscriberFutureStub (com.google.pubsub.v1.SubscriberGrpc.SubscriberFutureStub)1 CallCredentials (io.grpc.CallCredentials)1 MoreCallCredentials (io.grpc.auth.MoreCallCredentials)1 GcpOptions (org.apache.beam.sdk.extensions.gcp.options.GcpOptions)1 RetryHttpRequestInitializer (org.apache.beam.sdk.util.RetryHttpRequestInitializer)1