Search in sources :

Example 1 with RequestMetadataCallback

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

the class GoogleAuthLibraryCallCredentials method applyRequestMetadata.

@Override
public void applyRequestMetadata(RequestInfo info, Executor appExecutor, final MetadataApplier applier) {
    SecurityLevel security = info.getSecurityLevel();
    if (requirePrivacy && security != SecurityLevel.PRIVACY_AND_INTEGRITY) {
        applier.fail(Status.UNAUTHENTICATED.withDescription("Credentials require channel with PRIVACY_AND_INTEGRITY security level. " + "Observed security level: " + security));
        return;
    }
    String authority = checkNotNull(info.getAuthority(), "authority");
    final URI uri;
    try {
        uri = serviceUri(authority, info.getMethodDescriptor());
    } catch (StatusException e) {
        applier.fail(e.getStatus());
        return;
    }
    // Credentials is expected to manage caching internally if the metadata is fetched over
    // the network.
    creds.getRequestMetadata(uri, appExecutor, new RequestMetadataCallback() {

        @Override
        public void onSuccess(Map<String, List<String>> metadata) {
            // Some implementations may pass null metadata.
            // Re-use the headers if getRequestMetadata() returns the same map. It may return a
            // different map based on the provided URI, i.e., for JWT. However, today it does not
            // cache JWT and so we won't bother tring to save its return value based on the URI.
            Metadata headers;
            try {
                synchronized (GoogleAuthLibraryCallCredentials.this) {
                    if (lastMetadata == null || lastMetadata != metadata) {
                        lastHeaders = toHeaders(metadata);
                        lastMetadata = metadata;
                    }
                    headers = lastHeaders;
                }
            } catch (Throwable t) {
                applier.fail(Status.UNAUTHENTICATED.withDescription("Failed to convert credential metadata").withCause(t));
                return;
            }
            applier.apply(headers);
        }

        @Override
        public void onFailure(Throwable e) {
            if (e instanceof IOException) {
                // Since it's an I/O failure, let the call be retried with UNAVAILABLE.
                applier.fail(Status.UNAVAILABLE.withDescription("Credentials failed to obtain metadata").withCause(e));
            } else {
                applier.fail(Status.UNAUTHENTICATED.withDescription("Failed computing credential metadata").withCause(e));
            }
        }
    });
}
Also used : StatusException(io.grpc.StatusException) SecurityLevel(io.grpc.SecurityLevel) Metadata(io.grpc.Metadata) ArrayList(java.util.ArrayList) List(java.util.List) RequestMetadataCallback(com.google.auth.RequestMetadataCallback) IOException(java.io.IOException) URI(java.net.URI)

Example 2 with RequestMetadataCallback

use of com.google.auth.RequestMetadataCallback in project curiostack by curioswitch.

the class GoogleCredentialsDecoratingClient method execute.

@Override
@SuppressWarnings("FutureReturnValueIgnored")
public HttpResponse execute(ClientRequestContext ctx, HttpRequest req) {
    CompletableFuture<HttpResponse> resFuture = new CompletableFuture<>();
    credentials.getRequestMetadata(URI.create(req.path()), authExecutor, new RequestMetadataCallback() {

        @Override
        public void onSuccess(Map<String, List<String>> metadata) {
            metadata.forEach((key, values) -> {
                for (String value : values) {
                    req.headers().add(HttpHeaderNames.of(key), value);
                }
            });
            try {
                ctx.contextAwareEventLoop().submit(() -> resFuture.complete(delegate().execute(ctx, req)));
            } catch (Exception e) {
                resFuture.completeExceptionally(e);
            }
        }

        @Override
        public void onFailure(Throwable t) {
            resFuture.completeExceptionally(t);
        }
    });
    return HttpResponse.from(resFuture);
}
Also used : ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) HttpResponse(com.linecorp.armeria.common.HttpResponse) Executor(java.util.concurrent.Executor) Credentials(com.google.auth.Credentials) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) ClientRequestContext(com.linecorp.armeria.client.ClientRequestContext) HttpHeaderNames(com.linecorp.armeria.common.HttpHeaderNames) Executors(java.util.concurrent.Executors) RequestMetadataCallback(com.google.auth.RequestMetadataCallback) Inject(javax.inject.Inject) HttpRequest(com.linecorp.armeria.common.HttpRequest) List(java.util.List) Map(java.util.Map) Client(com.linecorp.armeria.client.Client) SimpleDecoratingClient(com.linecorp.armeria.client.SimpleDecoratingClient) URI(java.net.URI) CompletableFuture(java.util.concurrent.CompletableFuture) HttpResponse(com.linecorp.armeria.common.HttpResponse) List(java.util.List) RequestMetadataCallback(com.google.auth.RequestMetadataCallback)

Aggregations

RequestMetadataCallback (com.google.auth.RequestMetadataCallback)2 URI (java.net.URI)2 List (java.util.List)2 Credentials (com.google.auth.Credentials)1 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)1 Client (com.linecorp.armeria.client.Client)1 ClientRequestContext (com.linecorp.armeria.client.ClientRequestContext)1 SimpleDecoratingClient (com.linecorp.armeria.client.SimpleDecoratingClient)1 HttpHeaderNames (com.linecorp.armeria.common.HttpHeaderNames)1 HttpRequest (com.linecorp.armeria.common.HttpRequest)1 HttpResponse (com.linecorp.armeria.common.HttpResponse)1 Metadata (io.grpc.Metadata)1 SecurityLevel (io.grpc.SecurityLevel)1 StatusException (io.grpc.StatusException)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 Executor (java.util.concurrent.Executor)1 Executors (java.util.concurrent.Executors)1