Skip to main content

A tool for recovering Protocol Buffer definitions from gRPC services using reflection API and generating client code

Project description

PBReflect

License Python

PBReflect is a powerful tool for recovering Protocol Buffer (protobuf) definitions from gRPC services using the reflection API. It allows developers to generate .proto files from running gRPC servers without having access to the original source code.

Features

  • Automatic Discovery: Automatically discovers all services and messages exposed by a gRPC server
  • Proto Generation: Generates complete .proto files with proper package structure
  • TLS Support: Supports secure connections with custom certificates
  • Dependency Resolution: Correctly handles dependencies between proto files
  • Simple CLI: Easy-to-use command-line interface
  • Client Generation: Generate Python client libraries from .proto files with multiple generator strategies

Installation

# Install using pip
pip install pbreflect

# Or using Poetry
poetry add pbreflect

Quick Start

Basic Usage

To recover proto files from a gRPC server:

# Basic usage
pbreflect get-protos -h localhost:50051 -o ./protos

This will connect to the gRPC server at localhost:50051, retrieve all available proto definitions, and save them to the ./protos directory.

Using TLS/SSL

For secure connections, you can use TLS certificates:

# With root certificate only
pbreflect get-protos -h secure.example.com:443 -o ./protos --root-cert ./certs/ca.pem

# With full client authentication
pbreflect get-protos -h secure.example.com:443 -o ./protos \
  --root-cert ./certs/ca.pem \
  --private-key ./certs/client.key \
  --cert-chain ./certs/client.pem

Client Code Generation

PBReflect can generate client code from proto files:

# Generate client code from proto files
pbreflect generate --proto-dir ./protos --output-dir ./generated --gen-type pbreflect

Generator Strategies

PBReflect supports multiple code generation strategies:

  • default: Standard protoc Python output
  • mypy: Standard output with mypy type annotations
  • betterproto: Uses betterproto generator for more Pythonic API
  • pbreflect: Custom generator with enhanced gRPC client support

Example:

# Generate code using the betterproto strategy
pbreflect generate --proto-dir ./protos --output-dir ./generated --gen-type betterproto

CLI Commands

PBReflect provides a comprehensive CLI interface:

pbreflect get-protos  # Recover proto files from a running gRPC server
pbreflect generate    # Generate client code from proto files
pbreflect info        # Display information about available services

Use --help with any command to see all available options.

Programmatic Usage

You can also use PBReflect in your Python code:

from pathlib import Path
from pbreflect.protorecover.recover_service import RecoverService

# Basic usage
with RecoverService("localhost:50051", Path("./protos")) as service:
    service.recover_protos()

# With TLS
with RecoverService(
    "secure.example.com:443",
    Path("./protos"),
    use_tls=True,
    root_certificates_path=Path("./certs/ca.pem"),
    private_key_path=Path("./certs/client.key"),
    certificate_chain_path=Path("./certs/client.pem")
) as service:
    service.recover_protos()

Use Cases

  • API Exploration: Discover and understand the API of a gRPC service
  • Client Development: Generate client code for services without access to original proto files
  • Testing: Create mock clients and servers for testing
  • Reverse Engineering: Analyze and document existing gRPC services
  • Migration: Help migrate from one gRPC implementation to another

Requirements

  • gRPC server with reflection service enabled

How It Works

PBReflect uses the gRPC reflection service to query a server for its service definitions. The reflection service returns FileDescriptorProto messages, which PBReflect then processes to reconstruct the original .proto files.

The process involves:

  1. Connecting to the gRPC server
  2. Querying the reflection service for available services
  3. Retrieving file descriptors for each service
  4. Reconstructing the proto definitions with proper imports
  5. Writing the generated proto files to disk

Limitations

  • The target gRPC server must have the reflection service enabled
  • Some advanced proto features might not be perfectly reconstructed
  • Comments from the original proto files are not recoverable

Contributing

Contributions are welcome! Please see CONTRIBUTING.md for detailed guidelines on:

  • Setting up your development environment
  • Running tests
  • Code style and conventions
  • Pull request process
  • Issue reporting

Publishing

For maintainers, we have documented the release process in PUBLISHING.md, which covers:

  • Version bumping
  • Building packages
  • Publishing to PyPI
  • Creating GitHub releases
  • Documentation updates

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

  • The gRPC team for creating the reflection service
  • All contributors who have helped improve this tool

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pbreflect-1.0.0.tar.gz (38.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pbreflect-1.0.0-py3-none-any.whl (49.4 kB view details)

Uploaded Python 3

File details

Details for the file pbreflect-1.0.0.tar.gz.

File metadata

  • Download URL: pbreflect-1.0.0.tar.gz
  • Upload date:
  • Size: 38.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.12 Linux/6.11.0-1014-azure

File hashes

Hashes for pbreflect-1.0.0.tar.gz
Algorithm Hash digest
SHA256 fbcc735a7543b766eb1c0482785c08a95bca0e8f1084cc86b378697ef283dbe0
MD5 dbaae521ad16dcf457bfc0f79bbc9cc3
BLAKE2b-256 1db9e49f199903c0025f66ec87d4f2873135cb4ed627fdc17d79658842f8306c

See more details on using hashes here.

File details

Details for the file pbreflect-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: pbreflect-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 49.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.12 Linux/6.11.0-1014-azure

File hashes

Hashes for pbreflect-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c0e703a4d08e3bbf68df9fb2cf1ba2a09745571e301f8ee544228d2f979d1b5d
MD5 0c1cfadd4ecfd5dc783d7328a386e31b
BLAKE2b-256 ed689b2ed1c3e3d8af55c0f678f868f80107464f55771885ed0f4152bb76d011

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page