Skip to content

Commit 5003ca9

Browse files
committed
fix(cli): explain terminal gateway connection failures
Signed-off-by: Shiju <shiju@nvidia.com>
1 parent 659b45f commit 5003ca9

5 files changed

Lines changed: 127 additions & 9 deletions

File tree

‎crates/openshell-cli/src/main.rs‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use clap::{CommandFactory, FromArgMatches, Parser, Subcommand, ValueEnum, ValueHint};
99
use clap_complete::engine::ArgValueCompleter;
1010
use clap_complete::env::CompleteEnv;
11-
use miette::Result;
11+
use miette::{Result, WrapErr};
1212
use openshell_cli::color::{self, ColorChoice, Colorize};
1313
use std::io::Write;
1414
use std::path::PathBuf;
@@ -4382,7 +4382,27 @@ async fn run_async() -> Result<()> {
43824382
let ctx = current_gateway()?;
43834383
let mut tls = tls.with_gateway_name(&ctx.name);
43844384
apply_auth(&mut tls, &ctx.name)?;
4385-
let channel = openshell_cli::tls::build_channel(&ctx.endpoint, &tls).await?;
4385+
let channel = openshell_cli::tls::build_channel(&ctx.endpoint, &tls)
4386+
.await
4387+
.wrap_err_with(|| {
4388+
let endpoint = url::Url::parse(&ctx.endpoint)
4389+
.ok()
4390+
.filter(|url| matches!(url.scheme(), "http" | "https"))
4391+
.map_or_else(
4392+
|| "the configured endpoint".to_owned(),
4393+
|url| url.origin().ascii_serialization(),
4394+
);
4395+
let gateway = if ctx.name == ctx.endpoint {
4396+
"gateway".to_owned()
4397+
} else {
4398+
format!("gateway '{}'", ctx.name)
4399+
};
4400+
format!(
4401+
"Could not connect to {gateway} at {endpoint}.\n\
4402+
Check that the gateway service or tunnel is running and the endpoint is correct.\n\
4403+
Inspect saved endpoints with: openshell gateway list"
4404+
)
4405+
})?;
43864406
let interceptor = openshell_core::auth::EdgeAuthInterceptor::new(
43874407
tls.oidc_token.as_deref(),
43884408
tls.edge_token.as_deref(),

‎crates/openshell-cli/src/tls.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ fn sdk_client_config(server: &str, tls: &TlsOptions) -> Result<ClientConfig> {
332332
/// filesystem-aware certificate discovery.
333333
pub async fn build_channel(server: &str, tls: &TlsOptions) -> Result<Channel> {
334334
let config = sdk_client_config(server, tls)?;
335-
openshell_sdk::transport::build_channel(&config)
335+
openshell_sdk::transport::build_channel_with_source(&config)
336336
.await
337337
.into_diagnostic()
338338
}

‎crates/openshell-cli/tests/gateway_registration_integration.rs‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,71 @@ fn status_with_unregistered_named_gateway_fails() {
104104
);
105105
}
106106

107+
#[test]
108+
fn term_connection_error_reports_gateway_and_endpoint_override() {
109+
let config_dir = tempfile::tempdir().expect("create user config dir");
110+
let system_dir = tempfile::tempdir().expect("create system config dir");
111+
let listener = std::net::TcpListener::bind("127.0.0.1:0").expect("bind test endpoint");
112+
let port = listener.local_addr().expect("local address").port();
113+
let endpoint = format!("http://127.0.0.1:{port}");
114+
let endpoint_override = format!("{endpoint}/?token=test-secret");
115+
drop(listener);
116+
write_user_gateway_metadata(
117+
config_dir.path(),
118+
"offline",
119+
"http://stored.example.com:17670",
120+
17670,
121+
false,
122+
"plaintext",
123+
);
124+
125+
let named = [
126+
"--gateway",
127+
"offline",
128+
"--gateway-endpoint",
129+
&endpoint_override,
130+
"term",
131+
];
132+
let direct = ["--gateway-endpoint", &endpoint_override, "term"];
133+
for (args, gateway) in [
134+
(named.as_slice(), "gateway 'offline'"),
135+
(direct.as_slice(), "gateway"),
136+
] {
137+
let output = run(config_dir.path(), system_dir.path(), args);
138+
assert_ne!(output.code, 0, "{}", output.combined);
139+
assert!(
140+
output
141+
.combined
142+
.contains(&format!("{gateway} at {endpoint}")),
143+
"{}",
144+
output.combined
145+
);
146+
assert!(
147+
output.combined.contains("openshell gateway list"),
148+
"{}",
149+
output.combined
150+
);
151+
assert!(
152+
output
153+
.combined
154+
.to_ascii_lowercase()
155+
.contains("connection refused"),
156+
"{}",
157+
output.combined
158+
);
159+
assert!(
160+
!output.combined.contains("test-secret"),
161+
"{}",
162+
output.combined
163+
);
164+
assert!(
165+
!output.combined.contains("stored.example.com"),
166+
"{}",
167+
output.combined
168+
);
169+
}
170+
}
171+
107172
#[test]
108173
fn status_with_unregistered_environment_gateway_fails() {
109174
let config_dir = tempfile::tempdir().expect("create user config dir");

‎crates/openshell-sdk/src/transport.rs‎

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,32 @@ async fn edge_tunnel_addr(server: &str, token: &str) -> Result<SocketAddr> {
6767

6868
// ── Channel construction ─────────────────────────────────────────────
6969

70+
/// A failure while preparing or opening a gateway channel.
71+
#[derive(Debug, thiserror::Error)]
72+
#[non_exhaustive]
73+
pub enum ChannelBuildError {
74+
/// Endpoint, authentication tunnel, or TLS setup failed.
75+
#[error(transparent)]
76+
Setup(#[from] SdkError),
77+
/// The transport failed to connect after setup.
78+
#[error("gateway transport failed")]
79+
Transport(#[from] tonic::transport::Error),
80+
}
81+
82+
/// Open a gRPC channel using the SDK's standard error type.
83+
pub async fn build_channel(config: &ClientConfig) -> Result<Channel> {
84+
build_channel_with_source(config)
85+
.await
86+
.map_err(|error| match error {
87+
ChannelBuildError::Setup(error) => error,
88+
ChannelBuildError::Transport(source) => SdkError::connect(source.to_string()),
89+
})
90+
}
91+
7092
/// Open a gRPC channel to the gateway described by `config`.
7193
///
94+
/// Transport failures retain the underlying error chain for diagnostics.
95+
///
7296
/// Routing is determined by `gateway` scheme + `auth` variant +
7397
/// `insecure_skip_verify`.
7498
///
@@ -80,21 +104,24 @@ async fn edge_tunnel_addr(server: &str, token: &str) -> Result<SocketAddr> {
80104
/// | `https://` | `Some(EdgeJwt)` | (any) | tunnel proxy + plaintext to local proxy |
81105
/// | `https://` | (any) | `true` | `InsecureTlsConnector`, no verification |
82106
/// | `https://` | `Some(Oidc)` or `None` | `false` | tonic TLS, optionally pin `ca_cert` and present `client_identity` |
83-
pub async fn build_channel(config: &ClientConfig) -> Result<Channel> {
107+
pub async fn build_channel_with_source(
108+
config: &ClientConfig,
109+
) -> std::result::Result<Channel, ChannelBuildError> {
84110
let gateway = &config.gateway;
85111

86112
// Branch 1 — plaintext.
87113
if gateway.starts_with("http://") {
88114
return standard_endpoint(gateway.clone())?
89115
.connect()
90116
.await
91-
.map_err(|e| SdkError::connect(format!("{e}")));
117+
.map_err(ChannelBuildError::from);
92118
}
93119

94120
if !gateway.starts_with("https://") {
95121
return Err(SdkError::invalid_config(format!(
96122
"gateway URL must start with http:// or https://: {gateway}"
97-
)));
123+
))
124+
.into());
98125
}
99126

100127
// Branch 2 — Cloudflare Access edge JWT: tunnel proxy + plaintext-to-local.
@@ -103,7 +130,7 @@ pub async fn build_channel(config: &ClientConfig) -> Result<Channel> {
103130
return standard_endpoint(format!("http://{local_addr}"))?
104131
.connect()
105132
.await
106-
.map_err(|e| SdkError::connect(format!("{e}")));
133+
.map_err(ChannelBuildError::from);
107134
}
108135

109136
// Branch 3 — insecure TLS (skip cert verification): swap the scheme to
@@ -118,7 +145,7 @@ pub async fn build_channel(config: &ClientConfig) -> Result<Channel> {
118145
return standard_endpoint(http_uri)?
119146
.connect_with_connector(connector)
120147
.await
121-
.map_err(|e| SdkError::connect(format!("{e}")));
148+
.map_err(ChannelBuildError::from);
122149
}
123150

124151
// Branch 4 — anonymous TLS or OIDC bearer over HTTPS: pin `ca_cert` when
@@ -141,7 +168,7 @@ pub async fn build_channel(config: &ClientConfig) -> Result<Channel> {
141168
.map_err(|e| SdkError::tls(format!("{e}")))?
142169
.connect()
143170
.await
144-
.map_err(|e| SdkError::connect(format!("{e}")))
171+
.map_err(ChannelBuildError::from)
145172
}
146173

147174
/// rustls verifier that accepts any server certificate.

‎docs/how-it-works/gateways/overview.mdx‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,12 @@ openshell gateway remove production
164164
165165
## Troubleshoot
166166
167+
If `openshell term` cannot connect, its error shows the gateway name when known
168+
and the address it tried, including a `--gateway-endpoint` override. Inspect saved
169+
endpoints with `openshell gateway list`, which works without a live connection.
170+
Check the underlying error for connection refusals or TLS failures, then
171+
inspect the gateway service or tunnel.
172+
167173
Check gateway health:
168174
169175
```shell

0 commit comments

Comments
 (0)