Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 44 additions & 8 deletions crates/openshell-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ use openshell_bootstrap::{
use openshell_cli::completers;
use openshell_cli::run;
use openshell_cli::tls::TlsOptions;
use openshell_core::proto::{GpuResourceRequirements, ServiceAuthorizationMode};
use openshell_core::proto::{
GpuResourceRequirements, ServiceAuthorizationMode, ServiceBackendHttpVersion,
};

/// Resolved gateway context: name + gateway endpoint.
struct GatewayContext {
Expand Down Expand Up @@ -806,6 +808,24 @@ impl From<CliServiceAuthorizationMode> for ServiceAuthorizationMode {
}
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, ValueEnum)]
enum CliServiceBackendHttpVersion {
#[default]
#[value(name = "1.1")]
Http1,
#[value(name = "2")]
Http2,
}

impl From<CliServiceBackendHttpVersion> for ServiceBackendHttpVersion {
fn from(value: CliServiceBackendHttpVersion) -> Self {
match value {
CliServiceBackendHttpVersion::Http1 => Self::Http1,
CliServiceBackendHttpVersion::Http2 => Self::Http2,
}
}
}

#[derive(Clone, Debug, ValueEnum)]
enum CliProviderRefreshStrategy {
Oauth2RefreshToken,
Expand Down Expand Up @@ -2501,6 +2521,10 @@ enum ServiceCommands {
/// Handling for an incoming application Authorization header.
#[arg(long, value_enum, default_value_t)]
authorization_mode: CliServiceAuthorizationMode,

/// HTTP version on the sandbox loopback port. HTTP/2 requires the supervisor application listener.
#[arg(long, value_enum, default_value_t)]
backend_http_version: CliServiceBackendHttpVersion,
},

/// List exposed sandbox service endpoints.
Expand Down Expand Up @@ -3153,6 +3177,7 @@ async fn run_async() -> Result<()> {
service,
target_port,
authorization_mode,
backend_http_version,
} => {
let service = service.unwrap_or_default();
run::service_expose(
Expand All @@ -3161,6 +3186,7 @@ async fn run_async() -> Result<()> {
&service,
target_port,
authorization_mode.into(),
backend_http_version.into(),
&cli.workspace,
&tls,
)
Expand Down Expand Up @@ -7357,12 +7383,14 @@ mod tests {
target_port,
service,
authorization_mode,
backend_http_version,
}),
}) => {
assert_eq!(sandbox, "my-sandbox");
assert_eq!(target_port, 8080);
assert_eq!(service.as_deref(), Some("api"));
assert_eq!(authorization_mode, CliServiceAuthorizationMode::Strip);
assert_eq!(backend_http_version, CliServiceBackendHttpVersion::Http1);
}
other => panic!("expected service expose command, got: {other:?}"),
}
Expand All @@ -7381,6 +7409,7 @@ mod tests {
target_port,
service,
authorization_mode,
..
}),
}) => {
assert_eq!(sandbox, "my-sandbox");
Expand All @@ -7393,7 +7422,7 @@ mod tests {
}

#[test]
fn service_expose_parses_bearer_passthrough() {
fn service_expose_parses_bearer_passthrough_and_http2_backend() {
let cli = Cli::try_parse_from([
"openshell",
"service",
Expand All @@ -7402,19 +7431,26 @@ mod tests {
"4500",
"--authorization-mode",
"bearer-passthrough",
"--backend-http-version",
"2",
])
.expect("service authorization mode should parse");
.expect("service authorization mode and backend HTTP version should parse");

match cli.command {
Some(Commands::Service {
command:
Some(ServiceCommands::Expose {
authorization_mode, ..
authorization_mode,
backend_http_version,
..
}),
}) => assert_eq!(
authorization_mode,
CliServiceAuthorizationMode::BearerPassthrough
),
}) => {
assert_eq!(
authorization_mode,
CliServiceAuthorizationMode::BearerPassthrough
);
assert_eq!(backend_http_version, CliServiceBackendHttpVersion::Http2);
}
other => panic!("expected service expose command, got: {other:?}"),
}
}
Expand Down
62 changes: 22 additions & 40 deletions crates/openshell-cli/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ use openshell_core::proto::{
SandboxPolicy, SandboxResources, SandboxRestartPolicy, SandboxServiceExposure,
SandboxServiceLevel, SandboxSpec, SandboxStartup, SandboxTemplate, SandboxWorkloadConfig,
SandboxWorkloadTemplate, SandboxWorkloadTemplateSpec, ServiceAuthorizationMode,
ServiceEndpointResponse, SettingScope, StartSandboxRequest, StopSandboxRequest,
SuspendSandboxRequest, TcpForwardFrame, TcpForwardInit, TcpRelayTarget, UpdateConfigRequest,
WatchSandboxRequest, exec_sandbox_event, tcp_forward_init,
ServiceBackendHttpVersion, ServiceEndpointResponse, SettingScope, StartSandboxRequest,
StopSandboxRequest, SuspendSandboxRequest, TcpForwardFrame, TcpForwardInit, TcpRelayTarget,
UpdateConfigRequest, WatchSandboxRequest, exec_sandbox_event, tcp_forward_init,
};
use openshell_core::settings;
use openshell_core::{ObjectId, ObjectName, ObjectWorkspace};
Expand Down Expand Up @@ -4349,25 +4349,34 @@ async fn wait_for_lifecycle_phase(
}
}

#[allow(clippy::too_many_arguments)] // user-facing CLI command
pub async fn service_expose(
server: &str,
sandbox: &str,
service: &str,
target_port: u16,
authorization_mode: ServiceAuthorizationMode,
backend_http_version: ServiceBackendHttpVersion,
workspace: &str,
tls: &TlsOptions,
) -> Result<()> {
let response = expose_service_endpoint(
server,
sandbox,
service,
target_port,
authorization_mode,
workspace,
tls,
)
.await?;
let mut client = grpc_client(server, tls).await?;
let response = client
.expose_service(ExposeServiceRequest {
request_id: String::new(),
sandbox: sandbox.to_string(),
name: service.to_string(),
target_port: u32::from(target_port),
domain: true,
authorization_mode: authorization_mode as i32,
backend_http_version: backend_http_version as i32,
workspace_scope: Some(openshell_core::proto::workspace_selector(
workspace.to_string(),
)),
})
.await
.map_err(|status| service_status_error("expose service", "sandbox:write", sandbox, status))?
.into_inner();

if service.is_empty() {
println!(
Expand All @@ -4394,33 +4403,6 @@ pub async fn service_expose(
Ok(())
}

async fn expose_service_endpoint(
server: &str,
sandbox: &str,
service: &str,
target_port: u16,
authorization_mode: ServiceAuthorizationMode,
workspace: &str,
tls: &TlsOptions,
) -> Result<ServiceEndpointResponse> {
let mut client = grpc_client(server, tls).await?;
client
.expose_service(ExposeServiceRequest {
request_id: String::new(),
sandbox: sandbox.to_string(),
name: service.to_string(),
target_port: u32::from(target_port),
domain: true,
authorization_mode: authorization_mode as i32,
workspace_scope: Some(openshell_core::proto::workspace_selector(
workspace.to_string(),
)),
})
.await
.map_err(|status| service_status_error("expose service", "sandbox:write", sandbox, status))
.map(tonic::Response::into_inner)
}

#[allow(clippy::too_many_arguments)] // user-facing CLI command
pub async fn service_list(
server: &str,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2854,6 +2854,7 @@ async fn service_expose_forwards_bearer_passthrough_mode() {
"codex",
4500,
openshell_core::proto::ServiceAuthorizationMode::BearerPassthrough,
openshell_core::proto::ServiceBackendHttpVersion::Http1,
"default",
&tls,
)
Expand Down
3 changes: 2 additions & 1 deletion crates/openshell-sandbox-backend/src/runtime/test_support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,8 @@ async fn relay_io(
}
writer.shutdown().await
};
let read = async {
// End the response stream on EOF from the backend.
let read = async move {
let mut b = vec![0; 16384];
loop {
let n = reader.read(&mut b).await?;
Expand Down
1 change: 1 addition & 0 deletions crates/openshell-server/src/compute/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12349,6 +12349,7 @@ mod tests {
target_port: 8080,
domain: true,
authorization_mode: ServiceAuthorizationMode::Strip as i32,
backend_http_version: 0,
}
}

Expand Down
4 changes: 3 additions & 1 deletion crates/openshell-server/src/grpc/mutation_replay/ordinary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,9 @@ sandbox_scoped_mutation!(
Ok(ServiceEndpointResponse {
application_url,
gateway_url: gateway_url.unwrap_or_else(|| url.clone()),
endpoint: Some(reference.restore(store).await?),
endpoint: Some(service::service_response_endpoint(
reference.restore(store).await?,
)),
url,
})
}
Expand Down
3 changes: 2 additions & 1 deletion crates/openshell-server/src/grpc/sandbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use openshell_core::proto::{
};
use openshell_core::proto::{
BeginRootfsTarStagingRequest, BeginRootfsTarStagingResponse, Sandbox, SandboxPhase,
SandboxRestartPolicy, SandboxTemplate, SshSession,
SandboxRestartPolicy, SandboxTemplate, ServiceBackendHttpVersion, SshSession,
};
use openshell_core::telemetry::{
LifecycleOperation, LifecycleResource, SandboxTemplateSource, TelemetryOutcome,
Expand Down Expand Up @@ -841,6 +841,7 @@ async fn handle_create_sandbox_inner(
exposure.target_port,
exposure.authorization_mode,
)?,
ServiceBackendHttpVersion::Unspecified,
)
.await
{
Expand Down
Loading
Loading