Search in sources :

Example 1 with MtlsProvider

use of com.google.api.gax.rpc.mtls.MtlsProvider in project gax-java by googleapis.

the class ClientContextTest method testAlwaysUseMtlsEndpoint.

@Test
public void testAlwaysUseMtlsEndpoint() throws IOException {
    // Test the case that mTLS endpoint is always used.
    boolean switchToMtlsEndpointAllowed = true;
    MtlsProvider provider = new FakeMtlsProvider(false, MtlsEndpointUsagePolicy.ALWAYS, null, "", false);
    String endpointSelected = ClientContext.getEndpoint(endpoint, mtlsEndpoint, switchToMtlsEndpointAllowed, provider);
    assertEquals(mtlsEndpoint, endpointSelected);
}
Also used : FakeMtlsProvider(com.google.api.gax.rpc.testing.FakeMtlsProvider) MtlsProvider(com.google.api.gax.rpc.mtls.MtlsProvider) FakeMtlsProvider(com.google.api.gax.rpc.testing.FakeMtlsProvider) Test(org.junit.Test)

Example 2 with MtlsProvider

use of com.google.api.gax.rpc.mtls.MtlsProvider in project gax-java by googleapis.

the class ClientContextTest method testAutoUseMtlsEndpoint.

@Test
public void testAutoUseMtlsEndpoint() throws IOException {
    // Test the case client certificate exists and mTLS endpoint is selected.
    boolean switchToMtlsEndpointAllowed = true;
    MtlsProvider provider = new FakeMtlsProvider(true, MtlsEndpointUsagePolicy.AUTO, FakeMtlsProvider.createTestMtlsKeyStore(), "", false);
    String endpointSelected = ClientContext.getEndpoint(endpoint, mtlsEndpoint, switchToMtlsEndpointAllowed, provider);
    assertEquals(mtlsEndpoint, endpointSelected);
}
Also used : FakeMtlsProvider(com.google.api.gax.rpc.testing.FakeMtlsProvider) MtlsProvider(com.google.api.gax.rpc.mtls.MtlsProvider) FakeMtlsProvider(com.google.api.gax.rpc.testing.FakeMtlsProvider) Test(org.junit.Test)

Example 3 with MtlsProvider

use of com.google.api.gax.rpc.mtls.MtlsProvider in project gax-java by googleapis.

the class ClientContextTest method testNeverUseMtlsEndpoint.

@Test
public void testNeverUseMtlsEndpoint() throws IOException {
    // Test the case that mTLS endpoint is never used.
    boolean switchToMtlsEndpointAllowed = true;
    MtlsProvider provider = new FakeMtlsProvider(true, MtlsEndpointUsagePolicy.NEVER, FakeMtlsProvider.createTestMtlsKeyStore(), "", false);
    String endpointSelected = ClientContext.getEndpoint(endpoint, mtlsEndpoint, switchToMtlsEndpointAllowed, provider);
    assertEquals(endpoint, endpointSelected);
}
Also used : FakeMtlsProvider(com.google.api.gax.rpc.testing.FakeMtlsProvider) MtlsProvider(com.google.api.gax.rpc.mtls.MtlsProvider) FakeMtlsProvider(com.google.api.gax.rpc.testing.FakeMtlsProvider) Test(org.junit.Test)

Example 4 with MtlsProvider

use of com.google.api.gax.rpc.mtls.MtlsProvider in project gax-java by googleapis.

the class ClientContext method create.

/**
 * Instantiates the executor, credentials, and transport context based on the given client
 * settings.
 */
public static ClientContext create(StubSettings settings) throws IOException {
    ApiClock clock = settings.getClock();
    ExecutorProvider backgroundExecutorProvider = settings.getBackgroundExecutorProvider();
    final ScheduledExecutorService backgroundExecutor = backgroundExecutorProvider.getExecutor();
    Credentials credentials = settings.getCredentialsProvider().getCredentials();
    if (settings.getQuotaProjectId() != null) {
        // If the quotaProjectId is set, wrap original credentials with correct quotaProjectId as
        // QuotaProjectIdHidingCredentials.
        // Ensure that a custom set quota project id takes priority over one detected by credentials.
        // Avoid the backend receiving possibly conflict values of quotaProjectId
        credentials = new QuotaProjectIdHidingCredentials(credentials);
    }
    TransportChannelProvider transportChannelProvider = settings.getTransportChannelProvider();
    // will have a default executor if it needs one.
    if (transportChannelProvider.needsExecutor() && settings.getExecutorProvider() != null) {
        transportChannelProvider = transportChannelProvider.withExecutor(backgroundExecutor);
    }
    Map<String, String> headers = getHeadersFromSettings(settings);
    if (transportChannelProvider.needsHeaders()) {
        transportChannelProvider = transportChannelProvider.withHeaders(headers);
    }
    if (transportChannelProvider.needsCredentials() && credentials != null) {
        transportChannelProvider = transportChannelProvider.withCredentials(credentials);
    }
    String endpoint = getEndpoint(settings.getEndpoint(), settings.getMtlsEndpoint(), settings.getSwitchToMtlsEndpointAllowed(), new MtlsProvider());
    if (transportChannelProvider.needsEndpoint()) {
        transportChannelProvider = transportChannelProvider.withEndpoint(endpoint);
    }
    TransportChannel transportChannel = transportChannelProvider.getTransportChannel();
    ApiCallContext defaultCallContext = transportChannel.getEmptyCallContext().withTransportChannel(transportChannel);
    if (credentials != null) {
        defaultCallContext = defaultCallContext.withCredentials(credentials);
    }
    WatchdogProvider watchdogProvider = settings.getStreamWatchdogProvider();
    @Nullable Watchdog watchdog = null;
    if (watchdogProvider != null) {
        if (watchdogProvider.needsCheckInterval()) {
            watchdogProvider = watchdogProvider.withCheckInterval(settings.getStreamWatchdogCheckInterval());
        }
        if (watchdogProvider.needsClock()) {
            watchdogProvider = watchdogProvider.withClock(clock);
        }
        if (watchdogProvider.needsExecutor()) {
            watchdogProvider = watchdogProvider.withExecutor(backgroundExecutor);
        }
        watchdog = watchdogProvider.getWatchdog();
    }
    ImmutableList.Builder<BackgroundResource> backgroundResources = ImmutableList.builder();
    if (transportChannelProvider.shouldAutoClose()) {
        backgroundResources.add(transportChannel);
    }
    if (backgroundExecutorProvider.shouldAutoClose()) {
        backgroundResources.add(new ExecutorAsBackgroundResource(backgroundExecutor));
    }
    if (watchdogProvider != null && watchdogProvider.shouldAutoClose()) {
        backgroundResources.add(watchdog);
    }
    return newBuilder().setBackgroundResources(backgroundResources.build()).setExecutor(backgroundExecutor).setCredentials(credentials).setTransportChannel(transportChannel).setHeaders(ImmutableMap.copyOf(settings.getHeaderProvider().getHeaders())).setInternalHeaders(ImmutableMap.copyOf(settings.getInternalHeaderProvider().getHeaders())).setClock(clock).setDefaultCallContext(defaultCallContext).setEndpoint(settings.getEndpoint()).setQuotaProjectId(settings.getQuotaProjectId()).setStreamWatchdog(watchdog).setStreamWatchdogCheckInterval(settings.getStreamWatchdogCheckInterval()).setTracerFactory(settings.getTracerFactory()).build();
}
Also used : ApiClock(com.google.api.core.ApiClock) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ImmutableList(com.google.common.collect.ImmutableList) QuotaProjectIdHidingCredentials(com.google.api.gax.rpc.internal.QuotaProjectIdHidingCredentials) ExecutorAsBackgroundResource(com.google.api.gax.core.ExecutorAsBackgroundResource) BackgroundResource(com.google.api.gax.core.BackgroundResource) ExecutorAsBackgroundResource(com.google.api.gax.core.ExecutorAsBackgroundResource) MtlsProvider(com.google.api.gax.rpc.mtls.MtlsProvider) ExecutorProvider(com.google.api.gax.core.ExecutorProvider) Credentials(com.google.auth.Credentials) QuotaProjectIdHidingCredentials(com.google.api.gax.rpc.internal.QuotaProjectIdHidingCredentials) Nullable(javax.annotation.Nullable)

Example 5 with MtlsProvider

use of com.google.api.gax.rpc.mtls.MtlsProvider in project gax-java by googleapis.

the class ClientContextTest method testNoClientCertificate.

@Test
public void testNoClientCertificate() throws IOException {
    // Test the case that client certificates doesn't exists so the original endpoint is selected.
    boolean switchToMtlsEndpointAllowed = true;
    MtlsProvider provider = new FakeMtlsProvider(true, MtlsEndpointUsagePolicy.AUTO, null, "", false);
    String endpointSelected = ClientContext.getEndpoint(endpoint, mtlsEndpoint, switchToMtlsEndpointAllowed, provider);
    assertEquals(endpoint, endpointSelected);
}
Also used : FakeMtlsProvider(com.google.api.gax.rpc.testing.FakeMtlsProvider) MtlsProvider(com.google.api.gax.rpc.mtls.MtlsProvider) FakeMtlsProvider(com.google.api.gax.rpc.testing.FakeMtlsProvider) Test(org.junit.Test)

Aggregations

MtlsProvider (com.google.api.gax.rpc.mtls.MtlsProvider)7 FakeMtlsProvider (com.google.api.gax.rpc.testing.FakeMtlsProvider)6 Test (org.junit.Test)6 ApiClock (com.google.api.core.ApiClock)1 BackgroundResource (com.google.api.gax.core.BackgroundResource)1 ExecutorAsBackgroundResource (com.google.api.gax.core.ExecutorAsBackgroundResource)1 ExecutorProvider (com.google.api.gax.core.ExecutorProvider)1 QuotaProjectIdHidingCredentials (com.google.api.gax.rpc.internal.QuotaProjectIdHidingCredentials)1 Credentials (com.google.auth.Credentials)1 ImmutableList (com.google.common.collect.ImmutableList)1 IOException (java.io.IOException)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 Nullable (javax.annotation.Nullable)1