io.grpc.CallCredentials.RequestInfo Java Examples

The following examples show how to use io.grpc.CallCredentials.RequestInfo. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CallCredentials2ApplyingTest.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
@Test
public void parameterPropagation_transportSetSecurityLevel() {
  Attributes transportAttrs = Attributes.newBuilder()
      .set(ATTR_KEY, ATTR_VALUE)
      .set(GrpcAttributes.ATTR_SECURITY_LEVEL, SecurityLevel.INTEGRITY)
      .build();
  when(mockTransport.getAttributes()).thenReturn(transportAttrs);

  transport.newStream(method, origHeaders, callOptions);

  ArgumentCaptor<RequestInfo> infoCaptor = ArgumentCaptor.forClass(null);
  verify(mockCreds).applyRequestMetadata(
      infoCaptor.capture(), same(mockExecutor), any(MetadataApplier.class));
  RequestInfo info = infoCaptor.getValue();
  assertSame(method, info.getMethodDescriptor());
  assertSame(ATTR_VALUE, info.getTransportAttrs().get(ATTR_KEY));
  assertSame(AUTHORITY, info.getAuthority());
  assertSame(SecurityLevel.INTEGRITY, info.getSecurityLevel());
}
 
Example #2
Source File: CallCredentials2ApplyingTest.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
@Test
public void fail_delayed() {
  when(mockTransport.getAttributes()).thenReturn(Attributes.EMPTY);

  // Will call applyRequestMetadata(), which is no-op.
  DelayedStream stream = (DelayedStream) transport.newStream(method, origHeaders, callOptions);

  ArgumentCaptor<MetadataApplier> applierCaptor = ArgumentCaptor.forClass(null);
  verify(mockCreds).applyRequestMetadata(
      any(RequestInfo.class), same(mockExecutor), applierCaptor.capture());

  Status error = Status.FAILED_PRECONDITION.withDescription("channel not secure for creds");
  applierCaptor.getValue().fail(error);

  verify(mockTransport, never()).newStream(method, origHeaders, callOptions);
  FailingClientStream failingStream = (FailingClientStream) stream.getRealStream();
  assertSame(error, failingStream.getError());
}
 
Example #3
Source File: CallCredentials2ApplyingTest.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
@Test
public void applyMetadata_delayed() {
  when(mockTransport.getAttributes()).thenReturn(Attributes.EMPTY);

  // Will call applyRequestMetadata(), which is no-op.
  DelayedStream stream = (DelayedStream) transport.newStream(method, origHeaders, callOptions);

  ArgumentCaptor<MetadataApplier> applierCaptor = ArgumentCaptor.forClass(null);
  verify(mockCreds).applyRequestMetadata(
      any(RequestInfo.class), same(mockExecutor), applierCaptor.capture());
  verify(mockTransport, never()).newStream(method, origHeaders, callOptions);

  Metadata headers = new Metadata();
  headers.put(CREDS_KEY, CREDS_VALUE);
  applierCaptor.getValue().apply(headers);

  verify(mockTransport).newStream(method, origHeaders, callOptions);
  assertSame(mockStream, stream.getRealStream());
  assertEquals(CREDS_VALUE, origHeaders.get(CREDS_KEY));
  assertEquals(ORIG_HEADER_VALUE, origHeaders.get(ORIG_HEADER_KEY));
}
 
Example #4
Source File: CallCredentials2ApplyingTest.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
@Test
public void fail_inline() {
  final Status error = Status.FAILED_PRECONDITION.withDescription("channel not secure for creds");
  when(mockTransport.getAttributes()).thenReturn(Attributes.EMPTY);
  doAnswer(new Answer<Void>() {
      @Override
      public Void answer(InvocationOnMock invocation) throws Throwable {
        MetadataApplier applier = (MetadataApplier) invocation.getArguments()[2];
        applier.fail(error);
        return null;
      }
    }).when(mockCreds).applyRequestMetadata(
        any(RequestInfo.class), same(mockExecutor),
        any(io.grpc.CallCredentials2.MetadataApplier.class));

  FailingClientStream stream =
      (FailingClientStream) transport.newStream(method, origHeaders, callOptions);

  verify(mockTransport, never()).newStream(method, origHeaders, callOptions);
  assertSame(error, stream.getError());
}
 
Example #5
Source File: CallCredentials2ApplyingTest.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
@Test
public void applyMetadata_inline() {
  when(mockTransport.getAttributes()).thenReturn(Attributes.EMPTY);
  doAnswer(new Answer<Void>() {
      @Override
      public Void answer(InvocationOnMock invocation) throws Throwable {
        MetadataApplier applier = (MetadataApplier) invocation.getArguments()[2];
        Metadata headers = new Metadata();
        headers.put(CREDS_KEY, CREDS_VALUE);
        applier.apply(headers);
        return null;
      }
    }).when(mockCreds).applyRequestMetadata(
        any(RequestInfo.class), same(mockExecutor),
        any(io.grpc.CallCredentials2.MetadataApplier.class));

  ClientStream stream = transport.newStream(method, origHeaders, callOptions);

  verify(mockTransport).newStream(method, origHeaders, callOptions);
  assertSame(mockStream, stream);
  assertEquals(CREDS_VALUE, origHeaders.get(CREDS_KEY));
  assertEquals(ORIG_HEADER_VALUE, origHeaders.get(ORIG_HEADER_KEY));
}
 
Example #6
Source File: CallCredentials2ApplyingTest.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
@Test
public void parameterPropagation_callOptionsSetAuthority() {
  Attributes transportAttrs = Attributes.newBuilder()
      .set(ATTR_KEY, ATTR_VALUE)
      .build();
  when(mockTransport.getAttributes()).thenReturn(transportAttrs);
  Executor anotherExecutor = mock(Executor.class);

  transport.newStream(method, origHeaders,
      callOptions.withAuthority("calloptions-authority").withExecutor(anotherExecutor));

  ArgumentCaptor<RequestInfo> infoCaptor = ArgumentCaptor.forClass(null);
  verify(mockCreds).applyRequestMetadata(
      infoCaptor.capture(), same(anotherExecutor),
      any(io.grpc.CallCredentials2.MetadataApplier.class));
  RequestInfo info = infoCaptor.getValue();
  assertSame(method, info.getMethodDescriptor());
  assertSame(ATTR_VALUE, info.getTransportAttrs().get(ATTR_KEY));
  assertEquals("calloptions-authority", info.getAuthority());
  assertSame(SecurityLevel.NONE, info.getSecurityLevel());
}
 
Example #7
Source File: CallCredentials2ApplyingTest.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
@Test
public void parameterPropagation_transportSetSecurityLevel() {
  Attributes transportAttrs = Attributes.newBuilder()
      .set(ATTR_KEY, ATTR_VALUE)
      .set(GrpcAttributes.ATTR_SECURITY_LEVEL, SecurityLevel.INTEGRITY)
      .build();
  when(mockTransport.getAttributes()).thenReturn(transportAttrs);

  transport.newStream(method, origHeaders, callOptions);

  ArgumentCaptor<RequestInfo> infoCaptor = ArgumentCaptor.forClass(null);
  verify(mockCreds).applyRequestMetadata(
      infoCaptor.capture(), same(mockExecutor),
      any(io.grpc.CallCredentials2.MetadataApplier.class));
  RequestInfo info = infoCaptor.getValue();
  assertSame(method, info.getMethodDescriptor());
  assertSame(ATTR_VALUE, info.getTransportAttrs().get(ATTR_KEY));
  assertSame(AUTHORITY, info.getAuthority());
  assertSame(SecurityLevel.INTEGRITY, info.getSecurityLevel());
}
 
Example #8
Source File: CallCredentials2ApplyingTest.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
@Test
public void parameterPropagation_base() {
  Attributes transportAttrs = Attributes.newBuilder().set(ATTR_KEY, ATTR_VALUE).build();
  when(mockTransport.getAttributes()).thenReturn(transportAttrs);

  transport.newStream(method, origHeaders, callOptions);

  ArgumentCaptor<RequestInfo> infoCaptor = ArgumentCaptor.forClass(null);
  verify(mockCreds).applyRequestMetadata(
      infoCaptor.capture(), same(mockExecutor),
      any(io.grpc.CallCredentials2.MetadataApplier.class));
  RequestInfo info = infoCaptor.getValue();
  assertSame(method, info.getMethodDescriptor());
  assertSame(ATTR_VALUE, info.getTransportAttrs().get(ATTR_KEY));
  assertSame(AUTHORITY, info.getAuthority());
  assertSame(SecurityLevel.NONE, info.getSecurityLevel());
}
 
Example #9
Source File: CallCredentialsApplyingTest.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
@Test
public void fail_delayed() {
  when(mockTransport.getAttributes()).thenReturn(Attributes.EMPTY);

  // Will call applyRequestMetadata(), which is no-op.
  DelayedStream stream = (DelayedStream) transport.newStream(method, origHeaders, callOptions);

  ArgumentCaptor<CallCredentials.MetadataApplier> applierCaptor = ArgumentCaptor.forClass(null);
  verify(mockCreds).applyRequestMetadata(any(RequestInfo.class),
      same(mockExecutor), applierCaptor.capture());

  Status error = Status.FAILED_PRECONDITION.withDescription("channel not secure for creds");
  applierCaptor.getValue().fail(error);

  verify(mockTransport, never()).newStream(method, origHeaders, callOptions);
  FailingClientStream failingStream = (FailingClientStream) stream.getRealStream();
  assertSame(error, failingStream.getError());
}
 
Example #10
Source File: CallCredentialsApplyingTest.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
@Test
public void applyMetadata_delayed() {
  when(mockTransport.getAttributes()).thenReturn(Attributes.EMPTY);

  // Will call applyRequestMetadata(), which is no-op.
  DelayedStream stream = (DelayedStream) transport.newStream(method, origHeaders, callOptions);

  ArgumentCaptor<CallCredentials.MetadataApplier> applierCaptor = ArgumentCaptor.forClass(null);
  verify(mockCreds).applyRequestMetadata(any(RequestInfo.class),
      same(mockExecutor), applierCaptor.capture());
  verify(mockTransport, never()).newStream(method, origHeaders, callOptions);

  Metadata headers = new Metadata();
  headers.put(CREDS_KEY, CREDS_VALUE);
  applierCaptor.getValue().apply(headers);

  verify(mockTransport).newStream(method, origHeaders, callOptions);
  assertSame(mockStream, stream.getRealStream());
  assertEquals(CREDS_VALUE, origHeaders.get(CREDS_KEY));
  assertEquals(ORIG_HEADER_VALUE, origHeaders.get(ORIG_HEADER_KEY));
}
 
Example #11
Source File: CallCredentialsApplyingTest.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
@Test
public void fail_inline() {
  final Status error = Status.FAILED_PRECONDITION.withDescription("channel not secure for creds");
  when(mockTransport.getAttributes()).thenReturn(Attributes.EMPTY);
  doAnswer(new Answer<Void>() {
      @Override
      public Void answer(InvocationOnMock invocation) throws Throwable {
        CallCredentials.MetadataApplier applier =
            (CallCredentials.MetadataApplier) invocation.getArguments()[2];
        applier.fail(error);
        return null;
      }
    }).when(mockCreds).applyRequestMetadata(any(RequestInfo.class),
        same(mockExecutor), any(CallCredentials.MetadataApplier.class));

  FailingClientStream stream =
      (FailingClientStream) transport.newStream(method, origHeaders, callOptions);

  verify(mockTransport, never()).newStream(method, origHeaders, callOptions);
  assertSame(error, stream.getError());
}
 
Example #12
Source File: CallCredentialsApplyingTest.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
@Test
public void applyMetadata_inline() {
  when(mockTransport.getAttributes()).thenReturn(Attributes.EMPTY);
  doAnswer(new Answer<Void>() {
      @Override
      public Void answer(InvocationOnMock invocation) throws Throwable {
        CallCredentials.MetadataApplier applier =
            (CallCredentials.MetadataApplier) invocation.getArguments()[2];
        Metadata headers = new Metadata();
        headers.put(CREDS_KEY, CREDS_VALUE);
        applier.apply(headers);
        return null;
      }
    }).when(mockCreds).applyRequestMetadata(any(RequestInfo.class),
        same(mockExecutor), any(CallCredentials.MetadataApplier.class));

  ClientStream stream = transport.newStream(method, origHeaders, callOptions);

  verify(mockTransport).newStream(method, origHeaders, callOptions);
  assertSame(mockStream, stream);
  assertEquals(CREDS_VALUE, origHeaders.get(CREDS_KEY));
  assertEquals(ORIG_HEADER_VALUE, origHeaders.get(ORIG_HEADER_KEY));
}
 
Example #13
Source File: CallCredentialsApplyingTest.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
@Test
public void parameterPropagation_overrideByCallOptions() {
  Attributes transportAttrs = Attributes.newBuilder()
      .set(ATTR_KEY, ATTR_VALUE)
      .set(GrpcAttributes.ATTR_SECURITY_LEVEL, SecurityLevel.INTEGRITY)
      .build();
  when(mockTransport.getAttributes()).thenReturn(transportAttrs);
  Executor anotherExecutor = mock(Executor.class);

  transport.newStream(method, origHeaders,
      callOptions.withAuthority("calloptions-authority").withExecutor(anotherExecutor));

  ArgumentCaptor<RequestInfo> infoCaptor = ArgumentCaptor.forClass(null);
  verify(mockCreds).applyRequestMetadata(infoCaptor.capture(),
      same(anotherExecutor), any(CallCredentials.MetadataApplier.class));
  RequestInfo info = infoCaptor.getValue();
  assertSame(transportAttrs, info.getTransportAttrs());
  assertSame(method, info.getMethodDescriptor());
  assertEquals("calloptions-authority", info.getAuthority());
  assertSame(SecurityLevel.INTEGRITY, info.getSecurityLevel());
}
 
Example #14
Source File: CallCredentialsApplyingTest.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
@Test
public void parameterPropagation_base() {
  Attributes transportAttrs = Attributes.newBuilder().set(ATTR_KEY, ATTR_VALUE).build();
  when(mockTransport.getAttributes()).thenReturn(transportAttrs);

  transport.newStream(method, origHeaders, callOptions);

  ArgumentCaptor<RequestInfo> infoCaptor = ArgumentCaptor.forClass(null);
  verify(mockCreds).applyRequestMetadata(infoCaptor.capture(), same(mockExecutor),
      any(CallCredentials.MetadataApplier.class));
  RequestInfo info = infoCaptor.getValue();
  assertSame(transportAttrs, info.getTransportAttrs());
  assertSame(method, info.getMethodDescriptor());
  assertSame(AUTHORITY, info.getAuthority());
  assertSame(SecurityLevel.NONE, info.getSecurityLevel());
}
 
Example #15
Source File: CallCredentials2ApplyingTest.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
@Test
public void fail_delayed() {
  when(mockTransport.getAttributes()).thenReturn(Attributes.EMPTY);

  // Will call applyRequestMetadata(), which is no-op.
  DelayedStream stream = (DelayedStream) transport.newStream(method, origHeaders, callOptions);

  ArgumentCaptor<MetadataApplier> applierCaptor = ArgumentCaptor.forClass(null);
  verify(mockCreds).applyRequestMetadata(
      any(RequestInfo.class), same(mockExecutor), applierCaptor.capture());

  Status error = Status.FAILED_PRECONDITION.withDescription("channel not secure for creds");
  applierCaptor.getValue().fail(error);

  verify(mockTransport, never()).newStream(method, origHeaders, callOptions);
  FailingClientStream failingStream = (FailingClientStream) stream.getRealStream();
  assertSame(error, failingStream.getError());
}
 
Example #16
Source File: CallCredentials2ApplyingTest.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
@Test
public void applyMetadata_delayed() {
  when(mockTransport.getAttributes()).thenReturn(Attributes.EMPTY);

  // Will call applyRequestMetadata(), which is no-op.
  DelayedStream stream = (DelayedStream) transport.newStream(method, origHeaders, callOptions);

  ArgumentCaptor<MetadataApplier> applierCaptor = ArgumentCaptor.forClass(null);
  verify(mockCreds).applyRequestMetadata(
      any(RequestInfo.class), same(mockExecutor), applierCaptor.capture());
  verify(mockTransport, never()).newStream(method, origHeaders, callOptions);

  Metadata headers = new Metadata();
  headers.put(CREDS_KEY, CREDS_VALUE);
  applierCaptor.getValue().apply(headers);

  verify(mockTransport).newStream(method, origHeaders, callOptions);
  assertSame(mockStream, stream.getRealStream());
  assertEquals(CREDS_VALUE, origHeaders.get(CREDS_KEY));
  assertEquals(ORIG_HEADER_VALUE, origHeaders.get(ORIG_HEADER_KEY));
}
 
Example #17
Source File: CallCredentials2ApplyingTest.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
@Test
public void fail_inline() {
  final Status error = Status.FAILED_PRECONDITION.withDescription("channel not secure for creds");
  when(mockTransport.getAttributes()).thenReturn(Attributes.EMPTY);
  doAnswer(new Answer<Void>() {
      @Override
      public Void answer(InvocationOnMock invocation) throws Throwable {
        MetadataApplier applier = (MetadataApplier) invocation.getArguments()[2];
        applier.fail(error);
        return null;
      }
    }).when(mockCreds).applyRequestMetadata(
        any(RequestInfo.class), same(mockExecutor), any(MetadataApplier.class));

  FailingClientStream stream =
      (FailingClientStream) transport.newStream(method, origHeaders, callOptions);

  verify(mockTransport, never()).newStream(method, origHeaders, callOptions);
  assertSame(error, stream.getError());
}
 
Example #18
Source File: CallCredentials2ApplyingTest.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
@Test
public void applyMetadata_inline() {
  when(mockTransport.getAttributes()).thenReturn(Attributes.EMPTY);
  doAnswer(new Answer<Void>() {
      @Override
      public Void answer(InvocationOnMock invocation) throws Throwable {
        MetadataApplier applier = (MetadataApplier) invocation.getArguments()[2];
        Metadata headers = new Metadata();
        headers.put(CREDS_KEY, CREDS_VALUE);
        applier.apply(headers);
        return null;
      }
    }).when(mockCreds).applyRequestMetadata(
        any(RequestInfo.class), same(mockExecutor), any(MetadataApplier.class));

  ClientStream stream = transport.newStream(method, origHeaders, callOptions);

  verify(mockTransport).newStream(method, origHeaders, callOptions);
  assertSame(mockStream, stream);
  assertEquals(CREDS_VALUE, origHeaders.get(CREDS_KEY));
  assertEquals(ORIG_HEADER_VALUE, origHeaders.get(ORIG_HEADER_KEY));
}
 
Example #19
Source File: CallCredentials2ApplyingTest.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
@Test
public void parameterPropagation_callOptionsSetAuthority() {
  Attributes transportAttrs = Attributes.newBuilder()
      .set(ATTR_KEY, ATTR_VALUE)
      .build();
  when(mockTransport.getAttributes()).thenReturn(transportAttrs);
  Executor anotherExecutor = mock(Executor.class);

  transport.newStream(method, origHeaders,
      callOptions.withAuthority("calloptions-authority").withExecutor(anotherExecutor));

  ArgumentCaptor<RequestInfo> infoCaptor = ArgumentCaptor.forClass(null);
  verify(mockCreds).applyRequestMetadata(
      infoCaptor.capture(), same(anotherExecutor), any(MetadataApplier.class));
  RequestInfo info = infoCaptor.getValue();
  assertSame(method, info.getMethodDescriptor());
  assertSame(ATTR_VALUE, info.getTransportAttrs().get(ATTR_KEY));
  assertEquals("calloptions-authority", info.getAuthority());
  assertSame(SecurityLevel.NONE, info.getSecurityLevel());
}
 
Example #20
Source File: CallCredentials2ApplyingTest.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
@Test
public void parameterPropagation_base() {
  Attributes transportAttrs = Attributes.newBuilder().set(ATTR_KEY, ATTR_VALUE).build();
  when(mockTransport.getAttributes()).thenReturn(transportAttrs);

  transport.newStream(method, origHeaders, callOptions);

  ArgumentCaptor<RequestInfo> infoCaptor = ArgumentCaptor.forClass(null);
  verify(mockCreds).applyRequestMetadata(
      infoCaptor.capture(), same(mockExecutor), any(MetadataApplier.class));
  RequestInfo info = infoCaptor.getValue();
  assertSame(method, info.getMethodDescriptor());
  assertSame(ATTR_VALUE, info.getTransportAttrs().get(ATTR_KEY));
  assertSame(AUTHORITY, info.getAuthority());
  assertSame(SecurityLevel.NONE, info.getSecurityLevel());
}
 
Example #21
Source File: CallCredentialsApplyingTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Test
public void credentialThrows() {
  final RuntimeException ex = new RuntimeException();
  when(mockTransport.getAttributes()).thenReturn(Attributes.EMPTY);
  doThrow(ex).when(mockCreds).applyRequestMetadata(
      any(RequestInfo.class), same(mockExecutor),
      any(CallCredentials.MetadataApplier.class));

  FailingClientStream stream =
      (FailingClientStream) transport.newStream(method, origHeaders, callOptions);

  verify(mockTransport, never()).newStream(method, origHeaders, callOptions);
  assertEquals(Status.Code.UNAUTHENTICATED, stream.getError().getCode());
  assertSame(ex, stream.getError().getCause());
}
 
Example #22
Source File: CallCredentials2ApplyingTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Test
public void credentialThrows() {
  final RuntimeException ex = new RuntimeException();
  when(mockTransport.getAttributes()).thenReturn(Attributes.EMPTY);
  doThrow(ex).when(mockCreds).applyRequestMetadata(
      any(RequestInfo.class), same(mockExecutor),
      any(io.grpc.CallCredentials2.MetadataApplier.class));

  FailingClientStream stream =
      (FailingClientStream) transport.newStream(method, origHeaders, callOptions);

  verify(mockTransport, never()).newStream(method, origHeaders, callOptions);
  assertEquals(Status.Code.UNAUTHENTICATED, stream.getError().getCode());
  assertSame(ex, stream.getError().getCause());
}
 
Example #23
Source File: CallCredentials2ApplyingTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Test
public void credentialThrows() {
  final RuntimeException ex = new RuntimeException();
  when(mockTransport.getAttributes()).thenReturn(Attributes.EMPTY);
  doThrow(ex).when(mockCreds).applyRequestMetadata(
      any(RequestInfo.class), same(mockExecutor), any(MetadataApplier.class));

  FailingClientStream stream =
      (FailingClientStream) transport.newStream(method, origHeaders, callOptions);

  verify(mockTransport, never()).newStream(method, origHeaders, callOptions);
  assertEquals(Status.Code.UNAUTHENTICATED, stream.getError().getCode());
  assertSame(ex, stream.getError().getCause());
}
 
Example #24
Source File: CallCredentialsApplyingTransportFactory.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
@Override
@SuppressWarnings("deprecation")
public ClientStream newStream(
    final MethodDescriptor<?, ?> method, Metadata headers, final CallOptions callOptions) {
  CallCredentials creds = callOptions.getCredentials();
  if (creds != null) {
    MetadataApplierImpl applier = new MetadataApplierImpl(
        delegate, method, headers, callOptions);
    RequestInfo requestInfo = new RequestInfo() {
        @Override
        public MethodDescriptor<?, ?> getMethodDescriptor() {
          return method;
        }

        @Override
        public SecurityLevel getSecurityLevel() {
          return firstNonNull(
              delegate.getAttributes().get(GrpcAttributes.ATTR_SECURITY_LEVEL),
              SecurityLevel.NONE);
        }

        @Override
        public String getAuthority() {
          return firstNonNull(callOptions.getAuthority(), authority);
        }

        @Override
        public Attributes getTransportAttrs() {
          return delegate.getAttributes();
        }
      };
    try {
      creds.applyRequestMetadata(
          requestInfo, firstNonNull(callOptions.getExecutor(), appExecutor), applier);
    } catch (Throwable t) {
      applier.fail(Status.UNAUTHENTICATED
          .withDescription("Credentials should use fail() instead of throwing exceptions")
          .withCause(t));
    }
    return applier.returnStream();
  } else {
    return delegate.newStream(method, headers, callOptions);
  }
}
 
Example #25
Source File: CallCredentialsDecoratingClient.java    From armeria with Apache License 2.0 4 votes vote down vote up
@Override
protected HttpResponse execute(PooledHttpClient client, ClientRequestContext ctx, PooledHttpRequest req) {
    final CompletableFuture<HttpResponse> response = new CompletableFuture<>();

    final RequestInfo requestInfo = new RequestInfo() {
        @Override
        public MethodDescriptor<?, ?> getMethodDescriptor() {
            return method;
        }

        @Override
        public SecurityLevel getSecurityLevel() {
            // Semantics of SecurityLevel aren't very clear but we follow the pattern of the upstream
            // client.
            // https://github.com/grpc/grpc-java/blob/bf2a66c8a2d52be41afd7090c151984a3ce64e0d/okhttp/src/main/java/io/grpc/okhttp/OkHttpClientTransport.java#L586
            return ctx.sessionProtocol().isTls() ? SecurityLevel.PRIVACY_AND_INTEGRITY : SecurityLevel.NONE;
        }

        @Override
        public String getAuthority() {
            return authority;
        }

        @Override
        public Attributes getTransportAttrs() {
            // There is a race condition where the first request to an endpoint will not have transport
            // attributes available yet. It seems unlikely that CallCredentials could ever use these
            // attributes reliably, so for now don't return them and revisit if anyone needs them.

            // The most popular CallCredentials, GoogleAuthLibraryCallCredentials, do not use the transport
            // attributes.
            // https://github.com/grpc/grpc-java/blob/master/auth/src/main/java/io/grpc/auth/GoogleAuthLibraryCallCredentials.java
            return Attributes.EMPTY;
        }
    };

    credentials.applyRequestMetadata(
            requestInfo,
            CommonPools.blockingTaskExecutor(),
            new MetadataApplier() {
                @Override
                public void apply(Metadata metadata) {
                    ctx.mutateAdditionalRequestHeaders(
                            headers -> MetadataUtil.fillHeaders(metadata, headers));
                    try {
                        response.complete(client.execute(ctx, req));
                    } catch (Exception e) {
                        response.completeExceptionally(e);
                    }
                }

                @Override
                public void fail(Status status) {
                    response.completeExceptionally(status.asRuntimeException());
                }
            });

    return HttpResponse.from(response);
}