Search in sources :

Example 36 with AccessToken

use of io.vertx.reactivex.ext.auth.oauth2.AccessToken in project helios by spotify.

the class AuthenticatingHttpConnector method connect.

@Override
public HttpURLConnection connect(final URI uri, final String method, final byte[] entity, final Map<String, List<String>> headers) throws HeliosException {
    final Endpoint endpoint = endpointIterator.next();
    // convert the URI whose hostname portion is a domain name into a URI where the host is an IP
    // as we expect there to be several different IP addresses besides a common domain name
    final URI ipUri;
    try {
        ipUri = toIpUri(endpoint, uri);
    } catch (URISyntaxException e) {
        throw new HeliosException(e);
    }
    try {
        log.debug("connecting to {}", ipUri);
        final Optional<AccessToken> accessTokenOpt = accessTokenSupplier.get();
        if (accessTokenOpt.isPresent()) {
            final String token = accessTokenOpt.get().getTokenValue();
            headers.put("Authorization", singletonList("Bearer " + token));
            log.debug("Add Authorization header with bearer token");
        }
        if (clientCertificatePath.isPresent()) {
            // prioritize using the certificate file if set
            return connectWithCertificateFile(ipUri, method, entity, headers);
        } else if (agentProxy.isPresent() && !identities.isEmpty()) {
            // ssh-agent based authentication
            return connectWithIdentities(identities, ipUri, method, entity, headers);
        } else {
            // no authentication
            return doConnect(ipUri, method, entity, headers);
        }
    } catch (ConnectException | SocketTimeoutException | UnknownHostException e) {
        // UnknownHostException happens if we can't resolve hostname into IP address.
        // UnknownHostException's getMessage method returns just the hostname which is a
        // useless message, so log the exception class name to provide more info.
        log.debug(e.toString());
        throw new HeliosException("Unable to connect to master: " + ipUri, e);
    } catch (IOException e) {
        throw new HeliosException("Unexpected error connecting to " + ipUri, e);
    }
}
Also used : HeliosException(com.spotify.helios.common.HeliosException) SocketTimeoutException(java.net.SocketTimeoutException) UnknownHostException(java.net.UnknownHostException) AccessToken(com.google.auth.oauth2.AccessToken) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URI(java.net.URI) ConnectException(java.net.ConnectException)

Example 37 with AccessToken

use of io.vertx.reactivex.ext.auth.oauth2.AccessToken in project helios by spotify.

the class AuthenticatingHttpConnectorTest method createAuthenticatingConnectorWithAccessToken.

private AuthenticatingHttpConnector createAuthenticatingConnectorWithAccessToken(final Optional<AgentProxy> proxy, final List<Identity> identities) {
    final EndpointIterator endpointIterator = EndpointIterator.of(endpoints);
    final AccessToken accessToken = new AccessToken("<token>", null);
    return new AuthenticatingHttpConnector(USER, Suppliers.ofInstance(Optional.of(accessToken)), proxy, Optional.<CertKeyPaths>absent(), endpointIterator, connector, identities);
}
Also used : AccessToken(com.google.auth.oauth2.AccessToken)

Example 38 with AccessToken

use of io.vertx.reactivex.ext.auth.oauth2.AccessToken in project helios by spotify.

the class GoogleCredentialsAccessTokenSupplierTest method testGetWithStaticToken.

@Test
public void testGetWithStaticToken() {
    final AccessToken token = new AccessToken("token", null);
    final GoogleCredentialsAccessTokenSupplier supplier = new GoogleCredentialsAccessTokenSupplier(true, token, null);
    assertThat(supplier.get(), equalTo(Optional.of(token)));
}
Also used : AccessToken(com.google.auth.oauth2.AccessToken) Test(org.junit.Test)

Example 39 with AccessToken

use of io.vertx.reactivex.ext.auth.oauth2.AccessToken in project grpc-java by grpc.

the class GoogleAuthLibraryCallCredentialsTest method googleCredential_privacyAndIntegrityAllowed.

@Test
public void googleCredential_privacyAndIntegrityAllowed() {
    final AccessToken token = new AccessToken("allyourbase", new Date(Long.MAX_VALUE));
    final Credentials credentials = GoogleCredentials.create(token);
    GoogleAuthLibraryCallCredentials callCredentials = new GoogleAuthLibraryCallCredentials(credentials);
    callCredentials.applyRequestMetadata(new RequestInfoImpl(SecurityLevel.PRIVACY_AND_INTEGRITY), executor, applier);
    runPendingRunnables();
    verify(applier).apply(headersCaptor.capture());
    Metadata headers = headersCaptor.getValue();
    Iterable<String> authorization = headers.getAll(AUTHORIZATION);
    assertArrayEquals(new String[] { "Bearer allyourbase" }, Iterables.toArray(authorization, String.class));
}
Also used : AccessToken(com.google.auth.oauth2.AccessToken) Metadata(io.grpc.Metadata) Date(java.util.Date) Credentials(com.google.auth.Credentials) CallCredentials(io.grpc.CallCredentials) OAuth2Credentials(com.google.auth.oauth2.OAuth2Credentials) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials) ServiceAccountCredentials(com.google.auth.oauth2.ServiceAccountCredentials) Test(org.junit.Test)

Example 40 with AccessToken

use of io.vertx.reactivex.ext.auth.oauth2.AccessToken in project grpc-java by grpc.

the class GoogleAuthLibraryCallCredentialsTest method googleCredential_integrityDenied.

@Test
public void googleCredential_integrityDenied() {
    final AccessToken token = new AccessToken("allyourbase", new Date(Long.MAX_VALUE));
    final Credentials credentials = GoogleCredentials.create(token);
    // Anything less than PRIVACY_AND_INTEGRITY should fail
    GoogleAuthLibraryCallCredentials callCredentials = new GoogleAuthLibraryCallCredentials(credentials);
    callCredentials.applyRequestMetadata(new RequestInfoImpl(SecurityLevel.INTEGRITY), executor, applier);
    runPendingRunnables();
    verify(applier).fail(statusCaptor.capture());
    Status status = statusCaptor.getValue();
    assertEquals(Status.Code.UNAUTHENTICATED, status.getCode());
}
Also used : Status(io.grpc.Status) AccessToken(com.google.auth.oauth2.AccessToken) Date(java.util.Date) Credentials(com.google.auth.Credentials) CallCredentials(io.grpc.CallCredentials) OAuth2Credentials(com.google.auth.oauth2.OAuth2Credentials) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials) ServiceAccountCredentials(com.google.auth.oauth2.ServiceAccountCredentials) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)25 AccessToken (com.google.auth.oauth2.AccessToken)21 AccessToken (io.vertx.ext.auth.oauth2.AccessToken)13 Date (java.util.Date)9 JsonObject (io.vertx.core.json.JsonObject)8 IOException (java.io.IOException)8 GoogleCredentials (com.google.auth.oauth2.GoogleCredentials)7 OAuth2TokenImpl (io.vertx.ext.auth.oauth2.impl.OAuth2TokenImpl)7 OAuth2Credentials (com.google.auth.oauth2.OAuth2Credentials)5 Client (javax.ws.rs.client.Client)5 AccessToken (org.glassfish.jersey.client.oauth1.AccessToken)5 ConsumerCredentials (org.glassfish.jersey.client.oauth1.ConsumerCredentials)5 Metadata (io.grpc.Metadata)4 URI (java.net.URI)4 Feature (javax.ws.rs.core.Feature)4 JerseyTest (org.glassfish.jersey.test.JerseyTest)4 ServiceAccountCredentials (com.google.auth.oauth2.ServiceAccountCredentials)3 WebTarget (javax.ws.rs.client.WebTarget)3 Response (javax.ws.rs.core.Response)3 LoggingFeature (org.glassfish.jersey.logging.LoggingFeature)3