Skip to content

Commit ca18712

Browse files
authored
feat(gateway): add gRPC server reflection (#3161)
* feat(gateway): add gRPC server reflection Signed-off-by: Krzysztof Malczuk <kmalczuk@redhat.com> * fix(gateway): rate limit gRPC reflection queries Signed-off-by: Krzysztof Malczuk <kmalczuk@redhat.com> * fix(gateway): comply with gRPC reflection protocol Signed-off-by: Krzysztof Malczuk <kmalczuk@redhat.com> * fix(gateway): complete reflection symbol lookups Signed-off-by: Krzysztof Malczuk <kmalczuk@redhat.com> --------- Signed-off-by: Krzysztof Malczuk <kmalczuk@redhat.com>
1 parent 77c5ff6 commit ca18712

10 files changed

Lines changed: 1129 additions & 6 deletions

File tree

‎Cargo.lock‎

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ tokio = { version = "1.43", features = ["full"] }
1919
# gRPC/Protobuf
2020
tonic = "0.14"
2121
tonic-types = "0.14"
22+
tonic-reflection = "0.14"
2223
tonic-prost = "0.14"
2324
tonic-prost-build = "0.14"
2425
prost = "0.14"

‎README.md‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,23 @@ The installer sets up the CLI and a local gateway. The default sandbox image is
4949
- [Tutorials](https://docs.nvidia.com/openshell/latest/tutorials/first-network-policy): step-by-step policy and agent walkthroughs.
5050
- [Prerelease and development builds](https://docs.nvidia.com/openshell/latest/about/installation#prerelease-and-development-builds): try an upcoming release or the latest commit on `main`.
5151

52+
### Test the gRPC API with grpcurl
53+
54+
The gateway serves the gRPC reflection v1 protocol. After starting a local
55+
plaintext gateway, use `grpcurl` without checking out or supplying the proto
56+
files:
57+
58+
```shell
59+
grpcurl -plaintext localhost:18080 list
60+
grpcurl -plaintext localhost:18080 describe openshell.v1.OpenShell
61+
grpcurl -plaintext -d '{}' localhost:18080 openshell.v1.OpenShell/Health
62+
```
63+
64+
The service list contains the public `openshell.v1.OpenShell` API. Reflection
65+
does not advertise the gateway's internal compute-driver, credential-driver,
66+
interceptor, or middleware services. For a TLS gateway, omit `-plaintext` and
67+
supply the CA and client certificate options required by the deployment.
68+
5269
## Agent Skills
5370

5471
Install the public OpenShell skills for your coding agent:

‎crates/openshell-server/Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ libc = "0.2"
3939

4040
# gRPC
4141
tonic = { workspace = true, features = ["channel", "tls-native-roots"] }
42+
tonic-reflection = { workspace = true }
4243
prost = { workspace = true }
4344
prost-reflect = { workspace = true }
4445
prost-types = { workspace = true }

‎crates/openshell-server/src/auth/oidc.rs‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ use tracing::{debug, error, info, warn};
3232
/// These are structural bypasses for gRPC infrastructure that doesn't map to a
3333
/// single RPC method. Per-method bypasses (e.g. `Health`) are declared at the
3434
/// handler with `auth_mode: "unauthenticated"` in the proto annotation.
35-
const UNAUTHENTICATED_PREFIXES: &[&str] = &["/grpc.reflection.", "/grpc.health."];
35+
const UNAUTHENTICATED_PREFIXES: &[&str] =
36+
&[crate::multiplex::REFLECTION_PATH_PREFIX, "/grpc.health."];
3637

3738
/// Returns `true` if the method needs no authentication at all.
3839
pub fn is_unauthenticated_method(path: &str) -> bool {
@@ -1205,10 +1206,13 @@ mod tests {
12051206
#[test]
12061207
fn reflection_is_unauthenticated() {
12071208
assert!(is_unauthenticated_method(
1209+
"/grpc.reflection.v1.ServerReflection/ServerReflectionInfo"
1210+
));
1211+
assert!(!is_unauthenticated_method(
12081212
"/grpc.reflection.v1alpha.ServerReflection/ServerReflectionInfo"
12091213
));
1210-
assert!(is_unauthenticated_method(
1211-
"/grpc.reflection.v1.ServerReflection/ServerReflectionInfo"
1214+
assert!(!is_unauthenticated_method(
1215+
"/grpc.reflection.v2.ServerReflection/ServerReflectionInfo"
12121216
));
12131217
}
12141218

‎crates/openshell-server/src/lib.rs‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ pub(crate) mod policy_store;
3838
mod provider_profile_sources;
3939
mod provider_refresh;
4040
mod readiness;
41+
mod reflection;
4142
mod sandbox_index;
4243
mod sandbox_watch;
4344
mod service_routing;
@@ -349,6 +350,9 @@ pub struct ServerState {
349350
/// Gateway-wide gRPC request rate limiter shared by every multiplex path.
350351
pub(crate) grpc_rate_limiter: Option<multiplex::GrpcRateLimiter>,
351352

353+
/// Immutable public reflection index and its per-query rate limiter.
354+
pub(crate) reflection_service: reflection::GatewayReflectionServer,
355+
352356
/// Per-sandbox bound on extension credential minting, which resolves the
353357
/// caller's effective policy on every request.
354358
pub(crate) extension_mint_limiter: auth::extension_mint_limit::ExtensionMintLimiter,
@@ -426,6 +430,8 @@ impl ServerState {
426430
compute.configure_ssh_identities(credentials.clone());
427431
let peer_endpoint = derive_peer_endpoint(&config);
428432
let grpc_rate_limiter = multiplex::GrpcRateLimiter::from_config(&config);
433+
let reflection_service = reflection::build_gateway_reflection_service(&config)
434+
.expect("compiled public gateway descriptors must be valid");
429435
let admin_role = config
430436
.oidc
431437
.as_ref()
@@ -458,6 +464,7 @@ impl ServerState {
458464
compute_driver_authenticator: None,
459465
peer_authenticator: None,
460466
grpc_rate_limiter,
467+
reflection_service,
461468
gateway_interceptors: None,
462469
provider_profile_sources:
463470
provider_profile_sources::ProviderProfileSources::with_default_sources(),
@@ -2219,6 +2226,68 @@ mod tests {
22192226
(listen_addr, shutdown_tx, handle, tls_dir)
22202227
}
22212228

2229+
async fn start_plaintext_gateway_listener()
2230+
-> (SocketAddr, watch::Sender<bool>, tokio::task::JoinHandle<()>) {
2231+
let listener = TcpListener::bind("127.0.0.1:0")
2232+
.await
2233+
.expect("failed to bind test listener");
2234+
let listen_addr = listener.local_addr().expect("failed to read local addr");
2235+
let state = test_state(listen_addr, false).await;
2236+
let service = MultiplexService::new(state);
2237+
let (shutdown_tx, shutdown_rx) = watch::channel(false);
2238+
let handle = tokio::spawn(serve_gateway_listener(
2239+
BoundGatewayListener {
2240+
listener,
2241+
address: listen_addr,
2242+
},
2243+
service,
2244+
None,
2245+
false,
2246+
shutdown_rx,
2247+
));
2248+
(listen_addr, shutdown_tx, handle)
2249+
}
2250+
2251+
#[tokio::test]
2252+
async fn production_gateway_listener_serves_reflection() {
2253+
use tonic_reflection::pb::v1::{
2254+
ServerReflectionRequest, server_reflection_client::ServerReflectionClient,
2255+
server_reflection_request::MessageRequest, server_reflection_response::MessageResponse,
2256+
};
2257+
2258+
let (addr, shutdown, handle) = start_plaintext_gateway_listener().await;
2259+
let channel = tonic::transport::Channel::from_shared(format!("http://{addr}"))
2260+
.unwrap()
2261+
.connect()
2262+
.await
2263+
.unwrap();
2264+
let mut client = ServerReflectionClient::new(channel);
2265+
let request = ServerReflectionRequest {
2266+
host: String::new(),
2267+
message_request: Some(MessageRequest::ListServices(String::new())),
2268+
};
2269+
let mut responses = client
2270+
.server_reflection_info(tokio_stream::iter([request]))
2271+
.await
2272+
.unwrap()
2273+
.into_inner();
2274+
let response = responses.message().await.unwrap().unwrap();
2275+
let Some(MessageResponse::ListServicesResponse(services)) = response.message_response
2276+
else {
2277+
panic!("expected a reflection list-services response");
2278+
};
2279+
assert_eq!(
2280+
services
2281+
.service
2282+
.iter()
2283+
.map(|service| service.name.as_str())
2284+
.collect::<Vec<_>>(),
2285+
["openshell.v1.OpenShell"]
2286+
);
2287+
2288+
stop_listener(shutdown, handle).await;
2289+
}
2290+
22222291
async fn send_plain_http(addr: SocketAddr, request: String) -> String {
22232292
let connect_addr: SocketAddr = format!("127.0.0.1:{}", addr.port())
22242293
.parse()

0 commit comments

Comments
 (0)