Search in sources :

Example 1 with HttpTransportFactory

use of com.google.auth.http.HttpTransportFactory in project nifi by apache.

the class AbstractGCSProcessor method getServiceOptions.

@Override
protected StorageOptions getServiceOptions(ProcessContext context, GoogleCredentials credentials) {
    final String projectId = context.getProperty(PROJECT_ID).getValue();
    final Integer retryCount = context.getProperty(RETRY_COUNT).asInteger();
    final String proxyHost = context.getProperty(PROXY_HOST).getValue();
    final Integer proxyPort = context.getProperty(PROXY_PORT).asInteger();
    StorageOptions.Builder storageOptionsBuilder = StorageOptions.newBuilder().setCredentials(credentials).setProjectId(projectId).setRetryParams(RetryParams.newBuilder().setRetryMaxAttempts(retryCount).setRetryMinAttempts(retryCount).build());
    if (!StringUtils.isBlank(proxyHost) && proxyPort > 0) {
        storageOptionsBuilder.setHttpTransportFactory(new HttpTransportFactory() {

            @Override
            public HttpTransport create() {
                final HttpTransport transport = new NetHttpTransport.Builder().setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort))).build();
                return transport;
            }
        });
    }
    return storageOptionsBuilder.build();
}
Also used : NetHttpTransport(com.google.api.client.http.javanet.NetHttpTransport) HttpTransport(com.google.api.client.http.HttpTransport) Proxy(java.net.Proxy) StorageOptions(com.google.cloud.storage.StorageOptions) NetHttpTransport(com.google.api.client.http.javanet.NetHttpTransport) InetSocketAddress(java.net.InetSocketAddress) HttpTransportFactory(com.google.auth.http.HttpTransportFactory)

Example 2 with HttpTransportFactory

use of com.google.auth.http.HttpTransportFactory in project grpc-java by grpc.

the class GoogleAuthLibraryCallCredentialsTest method serviceAccountToJwt.

@Test
public void serviceAccountToJwt() throws Exception {
    KeyPair pair = KeyPairGenerator.getInstance("RSA").generateKeyPair();
    HttpTransportFactory factory = Mockito.mock(HttpTransportFactory.class);
    Mockito.when(factory.create()).thenThrow(new AssertionError());
    ServiceAccountCredentials credentials = ServiceAccountCredentials.newBuilder().setClientEmail("test-email@example.com").setPrivateKey(pair.getPrivate()).setPrivateKeyId("test-private-key-id").setHttpTransportFactory(factory).build();
    GoogleAuthLibraryCallCredentials callCredentials = new GoogleAuthLibraryCallCredentials(credentials);
    callCredentials.applyRequestMetadata(new RequestInfoImpl(), executor, applier);
    assertEquals(0, runPendingRunnables());
    verify(applier).apply(headersCaptor.capture());
    Metadata headers = headersCaptor.getValue();
    String[] authorization = Iterables.toArray(headers.getAll(AUTHORIZATION), String.class);
    assertEquals(1, authorization.length);
    assertTrue(authorization[0], authorization[0].startsWith("Bearer "));
    // JWT is reasonably long. Normal tokens aren't.
    assertTrue(authorization[0], authorization[0].length() > 300);
}
Also used : KeyPair(java.security.KeyPair) Metadata(io.grpc.Metadata) ServiceAccountCredentials(com.google.auth.oauth2.ServiceAccountCredentials) HttpTransportFactory(com.google.auth.http.HttpTransportFactory) Test(org.junit.Test)

Aggregations

HttpTransportFactory (com.google.auth.http.HttpTransportFactory)2 HttpTransport (com.google.api.client.http.HttpTransport)1 NetHttpTransport (com.google.api.client.http.javanet.NetHttpTransport)1 ServiceAccountCredentials (com.google.auth.oauth2.ServiceAccountCredentials)1 StorageOptions (com.google.cloud.storage.StorageOptions)1 Metadata (io.grpc.Metadata)1 InetSocketAddress (java.net.InetSocketAddress)1 Proxy (java.net.Proxy)1 KeyPair (java.security.KeyPair)1 Test (org.junit.Test)1