Skip to content

Commit daa2a36

Browse files
authored
fix(packaging): enable mTLS for local packages (ai-openshell#1271)
1 parent 31f0345 commit daa2a36

17 files changed

Lines changed: 308 additions & 57 deletions

File tree

‎.github/workflows/branch-checks.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ jobs:
128128
stats_bin="${SCCACHE_PATH:-sccache}"
129129
"$stats_bin" --show-stats
130130
status=$?
131-
if [[ $status -ne 0 ]]; then
131+
if [ "$status" -ne 0 ]; then
132132
echo "::warning::sccache stats unavailable (exit $status)"
133133
fi
134134
exit 0

‎.github/workflows/release-dev.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,7 @@ jobs:
501501
docker buildx build \
502502
--file deploy/docker/Dockerfile.gateway-macos \
503503
--build-arg OPENSHELL_CARGO_VERSION="${{ needs.compute-versions.outputs.cargo_version }}" \
504+
--build-arg OPENSHELL_IMAGE_TAG=dev \
504505
--build-arg CARGO_TARGET_CACHE_SCOPE="${{ github.sha }}" \
505506
--target binary \
506507
--output type=local,dest=out/ \

‎.github/workflows/release-tag.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,7 @@ jobs:
620620
docker buildx build \
621621
--file deploy/docker/Dockerfile.gateway-macos \
622622
--build-arg OPENSHELL_CARGO_VERSION="${{ needs.compute-versions.outputs.cargo_version }}" \
623+
--build-arg OPENSHELL_IMAGE_TAG="${{ needs.compute-versions.outputs.semver }}" \
623624
--build-arg CARGO_TARGET_CACHE_SCOPE="${{ github.sha }}" \
624625
--target binary \
625626
--output type=local,dest=out/ \

‎architecture/build.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ target architecture, stages them under `deploy/docker/.build/`, and then uses
2727
Buildx to publish per-architecture images and multi-architecture tags.
2828
Gateway image builds bake the corresponding supervisor image tag into the
2929
gateway binary so Docker sandboxes do not depend on `:latest` by default.
30+
Package formulas also pin Docker supervisor extraction to the matching release
31+
image tag so standalone gateway binaries do not infer image tags from package
32+
versions.
33+
The Homebrew service keeps gateway TLS under the Homebrew state directory but
34+
mirrors Docker sandbox client TLS into `$HOME/.local/state/openshell/homebrew/tls`
35+
at service start, because Docker Desktop bind mounts must use paths visible to
36+
the macOS user's shared home directory.
3037

3138
Local image work should use `mise` tasks rather than direct Docker commands so
3239
the same staging and tagging assumptions are used locally and in CI.

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

Lines changed: 134 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,60 @@ fn mtls_certs_exist_for_endpoint(name: &str, endpoint: &str) -> bool {
643643
})
644644
}
645645

646+
fn package_managed_tls_dirs() -> Vec<PathBuf> {
647+
if let Some(path) = std::env::var_os("OPENSHELL_LOCAL_TLS_DIR") {
648+
return vec![PathBuf::from(path)];
649+
}
650+
651+
let mut dirs = Vec::new();
652+
653+
if cfg!(target_os = "macos") {
654+
dirs.push(PathBuf::from("/opt/homebrew/var/openshell/tls"));
655+
dirs.push(PathBuf::from("/usr/local/var/openshell/tls"));
656+
}
657+
658+
let state_dir = std::env::var_os("XDG_STATE_HOME")
659+
.map(PathBuf::from)
660+
.or_else(|| std::env::var_os("HOME").map(|home| PathBuf::from(home).join(".local/state")));
661+
if let Some(state_dir) = state_dir {
662+
dirs.push(state_dir.join("openshell/tls"));
663+
}
664+
665+
dirs
666+
}
667+
668+
fn import_local_package_mtls_bundle(name: &str) -> Result<Option<PathBuf>> {
669+
for dir in package_managed_tls_dirs() {
670+
let ca = dir.join("ca.crt");
671+
let cert = dir.join("client/tls.crt");
672+
let key = dir.join("client/tls.key");
673+
if !(ca.is_file() && cert.is_file() && key.is_file()) {
674+
continue;
675+
}
676+
677+
let bundle = openshell_bootstrap::pki::PkiBundle {
678+
ca_cert_pem: std::fs::read_to_string(&ca)
679+
.into_diagnostic()
680+
.wrap_err_with(|| format!("failed to read {}", ca.display()))?,
681+
ca_key_pem: String::new(),
682+
server_cert_pem: String::new(),
683+
server_key_pem: String::new(),
684+
client_cert_pem: std::fs::read_to_string(&cert)
685+
.into_diagnostic()
686+
.wrap_err_with(|| format!("failed to read {}", cert.display()))?,
687+
client_key_pem: std::fs::read_to_string(&key)
688+
.into_diagnostic()
689+
.wrap_err_with(|| format!("failed to read {}", key.display()))?,
690+
};
691+
openshell_bootstrap::mtls::store_pki_bundle(name, &bundle)
692+
.wrap_err_with(|| format!("failed to store mTLS bundle for gateway '{name}'"))?;
693+
694+
return Ok(Some(dir));
695+
}
696+
697+
Ok(None)
698+
}
699+
646700
fn plaintext_gateway_is_remote(endpoint: &str, remote: Option<&str>, local: bool) -> bool {
647701
if local {
648702
return false;
@@ -924,16 +978,13 @@ pub async fn gateway_add(
924978

925979
// Verify the gateway is reachable.
926980
let tls = TlsOptions::default();
927-
match http_health_check(&endpoint, &tls).await {
928-
Ok(Some(status)) if status.is_success() => {}
929-
_ => {
930-
eprintln!(
931-
"{} Gateway is not reachable at {endpoint}",
932-
"⚠".yellow().bold(),
933-
);
934-
if !has_mtls_certs {
935-
eprintln!(" Verify the gateway is running and the endpoint is correct.");
936-
}
981+
if !gateway_reachable(&endpoint, &tls).await {
982+
eprintln!(
983+
"{} Gateway is not reachable at {endpoint}",
984+
"⚠".yellow().bold(),
985+
);
986+
if !has_mtls_certs {
987+
eprintln!(" Verify the gateway is running and the endpoint is correct.");
937988
}
938989
}
939990

@@ -951,7 +1002,13 @@ pub async fn gateway_add(
9511002

9521003
if remote.is_some() || local {
9531004
// mTLS gateway (remote or local).
954-
let certs_on_disk = mtls_certs_exist_for_endpoint(name, &endpoint);
1005+
let imported_mtls_dir = if local {
1006+
import_local_package_mtls_bundle(name)?
1007+
} else {
1008+
None
1009+
};
1010+
let certs_on_disk =
1011+
imported_mtls_dir.is_some() || mtls_certs_exist_for_endpoint(name, &endpoint);
9551012
if !certs_on_disk {
9561013
return Err(miette::miette!(
9571014
"mTLS certificates for gateway '{name}' were not found.\n\
@@ -984,14 +1041,11 @@ pub async fn gateway_add(
9841041

9851042
// Verify the gateway is reachable over mTLS.
9861043
let tls = TlsOptions::default().with_gateway_name(name);
987-
match http_health_check(&endpoint, &tls).await {
988-
Ok(Some(status)) if status.is_success() => {}
989-
_ => {
990-
eprintln!(
991-
"{} Gateway is not reachable at {endpoint}. Verify the gateway is running.",
992-
"⚠".yellow().bold(),
993-
);
994-
}
1044+
if !gateway_reachable(&endpoint, &tls).await {
1045+
eprintln!(
1046+
"{} Gateway is not reachable at {endpoint}. Verify the gateway is running.",
1047+
"⚠".yellow().bold(),
1048+
);
9951049
}
9961050

9971051
eprintln!(
@@ -1252,6 +1306,16 @@ async fn http_health_check(server: &str, tls: &TlsOptions) -> Result<Option<Stat
12521306
Ok(Some(resp.status()))
12531307
}
12541308

1309+
async fn gateway_reachable(server: &str, tls: &TlsOptions) -> bool {
1310+
if let Ok(mut client) = grpc_client(server, tls).await
1311+
&& client.health(HealthRequest {}).await.is_ok()
1312+
{
1313+
return true;
1314+
}
1315+
1316+
matches!(http_health_check(server, tls).await, Ok(Some(status)) if status.is_success())
1317+
}
1318+
12551319
fn remove_gateway_registration(name: &str) {
12561320
if let Err(err) = openshell_bootstrap::edge_token::remove_edge_token(name) {
12571321
tracing::debug!("failed to remove edge token: {err}");
@@ -5391,18 +5455,18 @@ mod tests {
53915455
TlsOptions, dockerfile_sources_supported_for_gateway, format_gateway_select_header,
53925456
format_gateway_select_items, format_provider_attachment_table, gateway_add,
53935457
gateway_auth_label, gateway_env_override_warning, gateway_select_with, gateway_type_label,
5394-
git_sync_files, http_health_check, image_requests_gpu, inferred_provider_type,
5395-
parse_cli_setting_value, parse_credential_pairs, plaintext_gateway_is_remote,
5396-
provisioning_timeout_message, ready_false_condition_message, resolve_from,
5397-
sandbox_should_persist,
5458+
git_sync_files, http_health_check, image_requests_gpu, import_local_package_mtls_bundle,
5459+
inferred_provider_type, package_managed_tls_dirs, parse_cli_setting_value,
5460+
parse_credential_pairs, plaintext_gateway_is_remote, provisioning_timeout_message,
5461+
ready_false_condition_message, resolve_from, sandbox_should_persist,
53985462
};
53995463
use crate::TEST_ENV_LOCK;
54005464
use hyper::StatusCode;
54015465
use openshell_bootstrap::{load_active_gateway, load_gateway_metadata, store_gateway_metadata};
54025466
use std::fs;
54035467
use std::io::{Read, Write};
54045468
use std::net::TcpListener;
5405-
use std::path::Path;
5469+
use std::path::{Path, PathBuf};
54065470
use std::process::Command;
54075471
use std::thread;
54085472

@@ -6023,6 +6087,52 @@ mod tests {
60236087
assert_eq!(gateway_auth_label(&gateway), "mtls");
60246088
}
60256089

6090+
#[test]
6091+
fn package_managed_tls_dirs_respects_override() {
6092+
let _guard = TEST_ENV_LOCK
6093+
.lock()
6094+
.unwrap_or_else(std::sync::PoisonError::into_inner);
6095+
let _tls_dir = EnvVarGuard::set("OPENSHELL_LOCAL_TLS_DIR", "/tmp/openshell-test-tls");
6096+
6097+
assert_eq!(
6098+
package_managed_tls_dirs(),
6099+
vec![PathBuf::from("/tmp/openshell-test-tls")],
6100+
);
6101+
}
6102+
6103+
#[test]
6104+
fn import_local_package_mtls_bundle_copies_client_materials() {
6105+
let tmpdir = tempfile::tempdir().expect("create tmpdir");
6106+
let package_tls = tmpdir.path().join("package-tls");
6107+
fs::create_dir_all(package_tls.join("client")).expect("create package tls dir");
6108+
fs::write(package_tls.join("ca.crt"), "ca").expect("write ca");
6109+
fs::write(package_tls.join("client/tls.crt"), "client cert").expect("write cert");
6110+
fs::write(package_tls.join("client/tls.key"), "client key").expect("write key");
6111+
6112+
with_tmp_xdg(tmpdir.path(), || {
6113+
let _tls_dir = EnvVarGuard::set(
6114+
"OPENSHELL_LOCAL_TLS_DIR",
6115+
package_tls.to_str().expect("temp path should be utf-8"),
6116+
);
6117+
6118+
let imported =
6119+
import_local_package_mtls_bundle("openshell").expect("import local bundle");
6120+
6121+
assert_eq!(imported.as_deref(), Some(package_tls.as_path()));
6122+
6123+
let mtls = tmpdir.path().join("openshell/gateways/openshell/mtls");
6124+
assert_eq!(fs::read_to_string(mtls.join("ca.crt")).unwrap(), "ca");
6125+
assert_eq!(
6126+
fs::read_to_string(mtls.join("tls.crt")).unwrap(),
6127+
"client cert",
6128+
);
6129+
assert_eq!(
6130+
fs::read_to_string(mtls.join("tls.key")).unwrap(),
6131+
"client key",
6132+
);
6133+
});
6134+
}
6135+
60266136
#[test]
60276137
fn plaintext_gateway_locality_infers_loopback_endpoints_as_local() {
60286138
assert!(!plaintext_gateway_is_remote(

‎crates/openshell-driver-docker/src/lib.rs‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ pub fn default_docker_supervisor_image() -> String {
8989
/// fallback covers image build wrappers that already tag the gateway and
9090
/// supervisor together. Standalone release binaries also patch the Cargo
9191
/// package version, so use it when it has been set to a real release value.
92-
fn default_docker_supervisor_image_tag() -> &'static str {
92+
fn default_docker_supervisor_image_tag() -> String {
9393
resolve_default_docker_supervisor_image_tag(
9494
option_env!("OPENSHELL_IMAGE_TAG"),
9595
option_env!("IMAGE_TAG"),
@@ -101,8 +101,8 @@ fn resolve_default_docker_supervisor_image_tag(
101101
openshell_image_tag: Option<&'static str>,
102102
image_tag: Option<&'static str>,
103103
cargo_pkg_version: &'static str,
104-
) -> &'static str {
105-
openshell_image_tag
104+
) -> String {
105+
let tag = openshell_image_tag
106106
.filter(|tag| !tag.is_empty())
107107
.or_else(|| image_tag.filter(|tag| !tag.is_empty()))
108108
.unwrap_or_else(|| {
@@ -111,7 +111,9 @@ fn resolve_default_docker_supervisor_image_tag(
111111
} else {
112112
cargo_pkg_version
113113
}
114-
})
114+
});
115+
116+
tag.replace('+', "-")
115117
}
116118

117119
/// Queried by the Docker driver to decide when a sandbox's supervisor

‎crates/openshell-driver-docker/src/tests.rs‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,22 @@ fn docker_supervisor_image_tag_prefers_explicit_build_tags() {
722722
);
723723
}
724724

725+
#[test]
726+
fn docker_supervisor_image_tag_sanitizes_build_metadata_for_docker() {
727+
assert_eq!(
728+
resolve_default_docker_supervisor_image_tag(None, None, "0.0.37-dev.156+g1d3b741ee"),
729+
"0.0.37-dev.156-g1d3b741ee",
730+
);
731+
assert_eq!(
732+
resolve_default_docker_supervisor_image_tag(
733+
Some("0.0.37-dev.156+g1d3b741ee"),
734+
None,
735+
"0.0.0",
736+
),
737+
"0.0.37-dev.156-g1d3b741ee",
738+
);
739+
}
740+
725741
#[test]
726742
fn supervisor_cache_path_namespaces_by_digest_under_openshell_data_dir() {
727743
let base = PathBuf::from("/var/cache/share");

0 commit comments

Comments
 (0)