Search in sources :

Example 1 with SecurityLevel

use of io.grpc.SecurityLevel 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 SecurityLevel

use of io.grpc.SecurityLevel in project grpc-java by grpc.

the class ServerCallImpl method getSecurityLevel.

@Override
public SecurityLevel getSecurityLevel() {
    final Attributes attributes = getAttributes();
    if (attributes == null) {
        return super.getSecurityLevel();
    }
    final SecurityLevel securityLevel = attributes.get(ATTR_SECURITY_LEVEL);
    return securityLevel == null ? super.getSecurityLevel() : securityLevel;
}
Also used : SecurityLevel(io.grpc.SecurityLevel) Attributes(io.grpc.Attributes)

Aggregations

SecurityLevel (io.grpc.SecurityLevel)2 RequestMetadataCallback (com.google.auth.RequestMetadataCallback)1 Attributes (io.grpc.Attributes)1 Metadata (io.grpc.Metadata)1 StatusException (io.grpc.StatusException)1 IOException (java.io.IOException)1 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1