A tool for recovering Protocol Buffer definitions from gRPC services using reflection API
Project description
PBReflect
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
.protofiles 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
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
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
- Python 3.8 or higher
- 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:
- Connecting to the gRPC server
- Querying the reflection service for available services
- Retrieving file descriptors for each service
- Reconstructing the proto definitions with proper imports
- 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 feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pbreflect-0.1.0.tar.gz.
File metadata
- Download URL: pbreflect-0.1.0.tar.gz
- Upload date:
- Size: 23.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.22
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
108acbd47a1fc2fb55d0b95e1201dac98a96fa02ae665205b2e3863096268496
|
|
| MD5 |
633642aacceedd3139186b18b111e124
|
|
| BLAKE2b-256 |
95853dc96cbe13b979e3eebb76bbf20c5772bd8f4914fcf6850e84534b5c1e87
|
File details
Details for the file pbreflect-0.1.0-py3-none-any.whl.
File metadata
- Download URL: pbreflect-0.1.0-py3-none-any.whl
- Upload date:
- Size: 26.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.22
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2e473f0d146a986ed0e6386100392c76b432219353489b4a49ce6ee73ddf53ed
|
|
| MD5 |
c3256d930b1f145bf1541a3e90420495
|
|
| BLAKE2b-256 |
c6ec4483f5efd26fd548facafc59286f49e2b6a935a733a2d67e8f7b68e93cb0
|