use of io.grpc.CallCredentials in project grpc-java by grpc.
the class AuthClientTest method setUp.
@Before
public void setUp() throws IOException {
// Generate a unique in-process server name.
String serverName = InProcessServerBuilder.generateName();
// Create a server, add service, start, and register for automatic graceful shutdown.
grpcCleanup.register(InProcessServerBuilder.forName(serverName).directExecutor().addService(ServerInterceptors.intercept(new GreeterGrpc.GreeterImplBase() {
@Override
public void sayHello(HelloRequest request, StreamObserver<HelloReply> responseObserver) {
HelloReply reply = HelloReply.newBuilder().setMessage("AuthClientTest user=" + request.getName()).build();
responseObserver.onNext(reply);
responseObserver.onCompleted();
}
}, mockServerInterceptor)).build().start());
CallCredentials credentials = new JwtCredential("test-client");
ManagedChannel channel = InProcessChannelBuilder.forName(serverName).directExecutor().build();
client = new AuthClient(credentials, channel);
}
use of io.grpc.CallCredentials in project grpc-java by grpc.
the class AuthClient method main.
/**
* Greet server. If provided, the first element of {@code args} is the name to use in the greeting
* and the second is the client identifier to set in JWT
*/
public static void main(String[] args) throws Exception {
String host = "localhost";
int port = 50051;
String user = "world";
String clientId = "default-client";
if (args.length > 0) {
// Use the arg as the server host if provided
host = args[0];
}
if (args.length > 1) {
// Use the second argument as the server port if provided
port = Integer.parseInt(args[1]);
}
if (args.length > 2) {
// Use the the third argument as the name to greet if provided
user = args[2];
}
if (args.length > 3) {
// Use the fourth argument as the client identifier if provided
clientId = args[3];
}
CallCredentials credentials = new JwtCredential(clientId);
AuthClient client = new AuthClient(credentials, host, port);
try {
client.greet(user);
} finally {
client.shutdown();
}
}
use of io.grpc.CallCredentials in project grpc-java by grpc.
the class ProtocolNegotiatorsTest method fromClient_composite.
@Test
public void fromClient_composite() {
CallCredentials callCredentials = mock(CallCredentials.class);
ProtocolNegotiators.FromChannelCredentialsResult result = ProtocolNegotiators.from(CompositeChannelCredentials.create(TlsChannelCredentials.create(), callCredentials));
assertThat(result.error).isNull();
assertThat(result.callCredentials).isSameInstanceAs(callCredentials);
assertThat(result.negotiator).isInstanceOf(ProtocolNegotiators.TlsProtocolNegotiatorClientFactory.class);
result = ProtocolNegotiators.from(CompositeChannelCredentials.create(InsecureChannelCredentials.create(), callCredentials));
assertThat(result.error).isNull();
assertThat(result.callCredentials).isSameInstanceAs(callCredentials);
assertThat(result.negotiator).isInstanceOf(ProtocolNegotiators.PlaintextProtocolNegotiatorClientFactory.class);
}
use of io.grpc.CallCredentials in project grpc-java by grpc.
the class OkHttpChannelBuilderTest method sslSocketFactoryFrom_composite.
@Test
public void sslSocketFactoryFrom_composite() {
CallCredentials callCredentials = mock(CallCredentials.class);
OkHttpChannelBuilder.SslSocketFactoryResult result = OkHttpChannelBuilder.sslSocketFactoryFrom(CompositeChannelCredentials.create(TlsChannelCredentials.create(), callCredentials));
assertThat(result.error).isNull();
assertThat(result.callCredentials).isSameInstanceAs(callCredentials);
assertThat(result.factory).isNotNull();
result = OkHttpChannelBuilder.sslSocketFactoryFrom(CompositeChannelCredentials.create(InsecureChannelCredentials.create(), callCredentials));
assertThat(result.error).isNull();
assertThat(result.callCredentials).isSameInstanceAs(callCredentials);
assertThat(result.factory).isNull();
}
use of io.grpc.CallCredentials in project beam by apache.
the class GrpcWindmillServer method initializeWindmillService.
private synchronized void initializeWindmillService(Set<HostAndPort> endpoints) throws IOException {
LOG.info("Initializing Streaming Engine GRPC client for endpoints: {}", endpoints);
this.stubList.clear();
this.syncStubList.clear();
this.endpoints = ImmutableSet.<HostAndPort>copyOf(endpoints);
for (HostAndPort endpoint : this.endpoints) {
if ("localhost".equals(endpoint.getHost())) {
initializeLocalHost(endpoint.getPort());
} else {
CallCredentials creds = MoreCallCredentials.from(new VendoredCredentialsAdapter(options.getGcpCredential()));
this.stubList.add(CloudWindmillServiceV1Alpha1Grpc.newStub(remoteChannel(endpoint)).withCallCredentials(creds));
this.syncStubList.add(CloudWindmillServiceV1Alpha1Grpc.newBlockingStub(remoteChannel(endpoint)).withCallCredentials(creds));
}
}
}
Aggregations