• Stars
    star
    525
  • Rank 81,153 (Top 2 %)
  • Language
    Java
  • License
    BSD 3-Clause "New...
  • Created about 8 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A universal grpc command line client

Polyglot - a universal grpc command line client

Build Status

Polyglot is a grpc client which can talk to any grpc server. In order to make a call, the following are required:

  • A compiled Polyglot binary,
  • the .proto files for the service or grpc reflection enabled on the remote server,
  • and a request proto instance in text format.

In particular, it is not necessary to generate grpc classes for the service or to compile the protos into the Polyglot binary.

Features

  • Supports unary, client streaming, server streaming, and bidi streaming rpcs.
  • Runs on Windows, Mac and Linux.
  • Parses proto files at runtime to discover services. Supports pretty-printing discovered services.
  • Can discover services by reflection if the remote server has reflection enabled
  • Supports authentication via oauth.
  • Accepts request protos through stdin and can output responses to stdout to allow chaining.
  • Supports plain text connections as well as TLS.
  • Supports passing custom grpc metadata over the command line.
  • Supports all protobuf well-known-types, including fields of type "Any".

Usage

Requirements

All you need to run Polyglot is a Java runtime. Binaries for Mac, Linux, and Windows are available from the releases page.

Making a grpc request

The "Hello World" of using Polyglot is to make an rpc call. This can be done using call command as follows:

$ echo <json-request> | java -jar polyglot.jar \
    --proto_discovery_root=<path> \
    call \
    --endpoint=<host>:<port> \
    --full_method=<some.package.Service/doSomething>

For stream requests double newlines \n\n are used to separate your json requests as follows:

$ echo '<json-request-1> \n\n <json-request-2> ... \n\n <json-request-n>' | java -jar polyglot.jar \
    --proto_discovery_root=<path> \
    call \
    --endpoint=<host>:<port> \
    --full_method=<some.package.Service/doSomething>  

For more invocation examples, see the examples directory.

Server reflection

If the remote server has reflection enabled, there is no need to pass the proto files to Polyglot. The example invocation above then becomes:

$ echo <json-request> | java -jar polyglot.jar \
    call \
    --endpoint=<host>:<port> \
    --full_method=<some.package.Service/doSomething>

By default, Polyglot always tries to use reflection before compiling local protos. Reflection can be turned off explicitly by setting the flag --use_reflection=false.

Configuration (optional)

Some of the features of Polyglot (such as Oauth, see below) require some configuration. Moreover, that sort of configuration tends to remain identical across multiple Polyglot runs. In order to improve usability, Polyglot supports loading a configuration set from a file at runtime. This configuration set can contain multiple named Configuration objects (schema defined here). An example configuration could look like this:

{
  "configurations": [
    {
      "name": "production",
      "call_config": {
        "use_tls": "true",
        "oauth_config": {
          "refresh_token_credentials": {
            "token_endpoint_url": "https://auth.example.com/token",
            "client": {
                "id": "example_id",
                "secret": "example_secret"
            },
            "refresh_token_path": "/path/to/refresh/token"
          }
        }
      },
      "proto_config": {
        "proto_discovery_root": "/home/dave/protos",
        "include_paths": [
          "/home/dave/lib"
        ]
      }
    },
    {
      "name": "staging",
      "call_config": {
        "oauth_config": {
          "refresh_token_credentials": {
            "token_endpoint_url": "https://auth-staging.example.com/token"
          }
        }
      },
      "proto_config": {
        "proto_discovery_root": "/home/dave/staging/protos",
        "include_paths": [
          "/home/dave/staging/lib"
        ]
      }
    }
  ]
}

By default, Polyglot tries to find a config file at $HOME/.polyglot/config.pb.json, but this can be overridden with the --config_set_path flag. By default, Polyglot uses the first configuration in the set, but this can be overridden with the --config_name flag.

The general philosophy is for the configuration to drive Polyglot's behavior and for command line flags to allow selectively overriding parts of the configuration. For a full list of what can be configured, please see config.proto.

Using TLS

Polyglot uses statically linked boringssl libraries under the hood and doesn't require the host machine to have any specific libraries. Whether or not the client uses TLS to talk to the server can be controlled using the --use_tls flag or the corresponding configuration entry.

Polyglot can also do client certificate authentication with the --tls_client_cert_path and --tls_client_key_path flags. If the hostname on the server does not match the endpoint (e.g. connecting to localhost, but the server thinks it's foo.example.com), --tls_client_override_authority=foo.example.com can be used.

Authenticating requests using OAuth

Polyglot has built-in support for authentication of requests using OAuth tokens in two ways:

  • Loading an access token from disk and attaching it to the request.
  • Loading a refresh token from disk, exchanging it for an access token, and attaching the access token to the request.

In order to use this feature, Polyglot needs an OauthConfiguration inside its Configuration. For details on how to populate the OauthConfiguration, please see the documentation of the fields in config.proto.

Listing services

Polyglot supports printing a list of all the discovered services using the list_services command. This command can be invoked as follows:

$ java -jar polyglot.jar \
    --proto_discovery_root=<path> \
    list_services

The printed services can be filtered using --service_filter=<service_name> or --method_filter=<method_name>, and the --with_message flag can be used to also print the exact format of the requests.

Custom metadata

It is possible to add custom grpc metadata to calls made using Polyglot by setting the --metadata=key1:value1,key2:value2 flag.

Configuring logs

Polyglot uses the slf4j logging framework with the org.slf4j.impl.SimpleLogger implementation. It also redirects grpc's JUL logs to slf4j. To change the default log level, specify the org.slf4j.simpleLogger.defaultLogLevel JVM argument, e.g., by passing the argument -Dorg.slf4j.simpleLogger.defaultLogLevel=debug.

Build requirements

In order to build Polyglot from source, you will need:

Building a binary

$ bazel build src/main/java/me/dinowernli/grpc/polyglot

After calling this, you should have a fresh binary at:

./bazel-bin/src/main/java/me/dinowernli/grpc/polyglot

If you would like to build a deployable (fat) jar, run:

$ bazel build src/main/java/me/dinowernli/grpc/polyglot:polyglot_deploy.jar

Running the examples

Example invocations can be found in the examples directory. In order to run a simple rpc call, invoke run-server.sh followed by (in a different terminal) call-command-example.sh.

Building and running tests

$ bazel test //src/...

Main contributors

More Repositories

1

grpc-gateway

gRPC to JSON proxy generator following the gRPC HTTP spec
Go
17,198
star
2

awesome-grpc

A curated list of useful resources for gRPC
7,163
star
3

go-grpc-middleware

Golang gRPC Middlewares: interceptor chaining, auth, logging, retries and more.
Go
6,058
star
4

grpc-spring

Spring Boot starter module for gRPC framework.
Java
3,328
star
5

grpc-health-probe

A command-line tool to perform health-checks for gRPC applications in Kubernetes and elsewhere
Go
1,357
star
6

go-grpc-prometheus

Prometheus monitoring for your gRPC Go servers.
Go
1,328
star
7

grpc-opentracing

OpenTracing is a set of consistent, expressive, vendor-neutral APIs for distributed tracing and context propagation
Python
465
star
8

java-grpc-prometheus

Java interceptors which can be used to monitor Grpc services using Prometheus.
Java
220
star
9

grpc-httpjson-transcoding

Transcoding to provide HTTP/JSON interface for gRPC Service
C++
154
star
10

protoc-gen-grpc-gateway-ts

protoc-gen-grpc-gateway-ts is a Typescript client generator for the grpc-gateway project. It generates idiomatic Typescript clients that connect the web frontend and golang backend fronted by grpc-gateway.
Go
136
star
11

grpc-simon-says

Multiplayer Simon Says game using bidirectional gRPC streaming
Go
132
star
12

grpcdebug

grpcdebug is a command line interface focusing on simplifying the debugging process of gRPC applications.
Go
124
star
13

grpc-cloud-run-example

Go
114
star
14

meetup-kit

gRPC meet up kit in a box
37
star
15

grift

This repository hosts gRPC's support for Thrift IDL and protocol
Java
10
star
16

grpc-exchange-o-gram

Exchange-o-gram demo
C#
8
star
17

grpcz-stackdriver

grpcz-monitoring - description: standalone agent for monitoring statistics from grpc library and publishing to various sinks (like stackdriver, serial port etc).
4
star
18

proto-converter

C++
2
star
19

proto-field-extraction

C++
1
star