Skip to content
Draft
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
44 changes: 44 additions & 0 deletions crates/openshell-cli/src/commands/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ use crate::commands::common::{
use crate::tls::{TlsOptions, grpc_client};
use dialoguer::Confirm;
use miette::{IntoDiagnostic, Result, WrapErr, miette};
use openshell_core::policy_l7::{
AllowedTransports, effective_endpoint_ports, endpoint_allowed_transports,
};
use openshell_core::proto::ProviderProfileCategory;
use openshell_core::proto::{
AttachSandboxProviderRequest, ConfigureProviderRefreshRequest, CreateProviderRequest,
Expand Down Expand Up @@ -2650,6 +2653,7 @@ fn format_provider_profile_details(profile: &ProviderProfile) -> String {
}
}

let credentialed = ProviderTypeProfile::from_proto(profile).has_credentialed_endpoints();
rendered.push_str("\nEndpoints (declared policy):\n");
if profile.endpoints.is_empty() {
rendered.push_str(" None declared.\n");
Expand Down Expand Up @@ -2698,6 +2702,25 @@ fn format_provider_profile_details(profile: &ProviderProfile) -> String {
None => format!("unknown({})", endpoint.tls),
};
let _ = writeln!(rendered, " TLS: {tls}");
// The gateway derives credential provenance when it attaches the profile.
let mut declared = endpoint.clone();
declared.provider_credentialed = credentialed;
let source = if endpoint.transport == 0 {
" (default)"
} else {
""
};
for port in effective_endpoint_ports(endpoint) {
let Some(allowed) = endpoint_allowed_transports(&declared, port) else {
continue;
};
let label = match allowed {
AllowedTransports::Tls => "tls",
AllowedTransports::Plaintext => "plaintext",
AllowedTransports::Both => "tls, plaintext",
};
let _ = writeln!(rendered, " Transport on {port}: {label}{source}");
}
let _ = writeln!(
rendered,
" Allow uninspected credentials: {}",
Expand Down Expand Up @@ -3167,6 +3190,27 @@ mod tests {
assert!(rendered.contains("Binaries:\n None declared.\n"));
}

#[test]
fn profile_description_shows_transports_for_each_port() {
use openshell_core::proto::{NetworkEndpoint, NetworkTransport};
let mut profile = ProviderProfile {
endpoints: vec![NetworkEndpoint {
host: "api.example.com".into(),
ports: vec![443, 8443],
protocol: "rest".into(),
..Default::default()
}],
..Default::default()
};
let text = format_provider_profile_details(&profile);
assert!(text.contains("Transport on 443: tls (default)"));
assert!(text.contains("Transport on 8443: tls, plaintext (default)"));
profile.endpoints[0].transport = NetworkTransport::Plaintext as i32;
let text = format_provider_profile_details(&profile);
assert!(text.contains("Transport on 443: plaintext\n"));
assert!(text.contains("Transport on 8443: plaintext\n"));
}

#[test]
fn profile_description_distinguishes_tls_skip_and_credential_opt_in() {
let profile = parse_profile_yaml(
Expand Down
11 changes: 11 additions & 0 deletions crates/openshell-core/src/extension_protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ pub const PROTOCOL_MINOR: u32 = 0;
/// Policy format implemented by a peer's registered Cedar backend.
pub const POLICY_CEDAR_V1: &str = "openshell.policy.cedar.v1";

/// Peer enforces explicit endpoint transport settings and transport defaults.
pub const POLICY_ENDPOINT_TRANSPORTS_V1: &str = "openshell.policy.endpoint-transports.v1";

/// Policy capabilities advertised by a supervisor build.
#[must_use]
pub fn policy_capabilities(cedar: bool) -> Vec<String> {
let mut capabilities = capabilities_if(POLICY_CEDAR_V1, cedar);
capabilities.push(POLICY_ENDPOINT_TRANSPORTS_V1.to_string());
capabilities
}

/// Whether a peer advertises the required versioned capability.
#[must_use]
pub fn has_capability(capabilities: &[String], required: &str) -> bool {
Expand Down
248 changes: 245 additions & 3 deletions crates/openshell-core/src/policy_l7.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
//! L7 endpoint field combinations, preventing drift between lint-time
//! and runtime checks.

use crate::proto::{NetworkAccessPreset, NetworkEnforcementMode, NetworkTlsMode};
use crate::proto::{
NetworkAccessPreset, NetworkEndpoint, NetworkEnforcementMode, NetworkTlsMode, NetworkTransport,
};

#[allow(deprecated)]
pub fn network_tls_mode_from_str(value: &str) -> Option<NetworkTlsMode> {
Expand All @@ -32,6 +34,141 @@ pub fn network_tls_mode_to_str(value: i32) -> Option<&'static str> {
}
}

/// The permitted transports for an inspected HTTP connection, after defaults.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AllowedTransports {
Tls,
Plaintext,
Both,
}

impl AllowedTransports {
/// Intersect two transport allowlists on a shared `host:port`.
///
/// Returns `None` when the sets are disjoint (`Tls` vs `Plaintext`) and
/// no single connection transport can satisfy both.
#[must_use]
pub const fn intersect(self, other: Self) -> Option<Self> {
match (self, other) {
(Self::Tls, Self::Tls | Self::Both) | (Self::Both, Self::Tls) => Some(Self::Tls),
(Self::Plaintext, Self::Plaintext | Self::Both) | (Self::Both, Self::Plaintext) => {
Some(Self::Plaintext)
}
(Self::Both, Self::Both) => Some(Self::Both),
(Self::Tls, Self::Plaintext) | (Self::Plaintext, Self::Tls) => None,
}
}
}

pub fn network_transport_from_str(value: &str) -> Option<NetworkTransport> {
match value {
"" => Some(NetworkTransport::Unspecified),
"tls" => Some(NetworkTransport::Tls),
"plaintext" => Some(NetworkTransport::Plaintext),
_ => None,
}
}

pub fn network_transport_to_str(value: i32) -> Option<&'static str> {
match NetworkTransport::try_from(value).ok()? {
NetworkTransport::Unspecified => Some(""),
NetworkTransport::Tls => Some("tls"),
NetworkTransport::Plaintext => Some("plaintext"),
}
}

/// Returns the inspected L7 protocol when `tls` does not skip termination.
#[must_use]
pub fn inspected_l7_protocol(protocol: &str, tls: i32) -> Option<L7Protocol> {
if tls == NetworkTlsMode::Unspecified as i32 {
L7Protocol::parse(protocol)
} else {
None
}
}

/// Validate the authored `transport` without assigning defaults to an omitted field.
pub fn validate_endpoint_transport(
transport: i32,
protocol: &str,
tls: i32,
) -> Option<&'static str> {
parse_endpoint_transport(transport, inspected_l7_protocol(protocol, tls)).err()
}

fn parse_endpoint_transport(
explicit: i32,
protocol: Option<L7Protocol>,
) -> Result<Option<AllowedTransports>, &'static str> {
let transport = match NetworkTransport::try_from(explicit) {
Ok(NetworkTransport::Unspecified) => return Ok(None),
Ok(NetworkTransport::Tls) => AllowedTransports::Tls,
Ok(NetworkTransport::Plaintext) => AllowedTransports::Plaintext,
Err(_) => return Err("transport accepts only tls or plaintext"),
};
if !protocol.is_some_and(L7Protocol::is_http) {
return Err(
"transport requires an inspected HTTP protocol and cannot be combined with tls: skip",
);
}
Ok(Some(transport))
}

/// Resolve once for the destination port. An opaque connection has no HTTP
/// transport policy. Invalid values are errors, never permission to relay raw TCP.
pub fn effective_allowed_transports(
explicit: i32,
protocol: Option<L7Protocol>,
credentialed: bool,
port: u32,
) -> Result<Option<AllowedTransports>, &'static str> {
if let Some(transports) = parse_endpoint_transport(explicit, protocol)? {
return Ok(Some(transports));
}
Ok(protocol.filter(|p| p.is_http()).map(|_| {
if port == 443 || credentialed {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Major: This flips every existing inspected HTTP endpoint on 443 or with credentials to TLS-only with no opt-in, and ties them all to a synchronized gateway and supervisor upgrade through one capability bit. If the tightening stands, the docs need to say the default alone triggers it.

AllowedTransports::Tls
} else {
AllowedTransports::Both
}
}))
}

/// Token-grant owner IDs alone are not credential provenance: the gateway
/// stamps them on global policy endpoints even when no credential is attached.
pub fn endpoint_is_credentialed(endpoint: &NetworkEndpoint) -> bool {
endpoint.provider_credentialed
|| endpoint.credential_binding.is_some()
|| !endpoint.credential_signing.is_empty()
}

pub fn endpoint_allowed_transports(
endpoint: &NetworkEndpoint,
port: u32,
) -> Option<AllowedTransports> {
effective_allowed_transports(
endpoint.transport,
inspected_l7_protocol(&endpoint.protocol, endpoint.tls),
endpoint_is_credentialed(endpoint),
port,
)
.ok()
.flatten()
}

/// Whether `endpoint` requires a supervisor that advertises
/// `POLICY_ENDPOINT_TRANSPORTS_V1`.
///
/// Only explicit `transport` values trigger capability gating so that
/// gateway-first rolling upgrades (such as Helm/Docker deployments with
/// long-lived sandboxes) do not fail-close existing sandboxes whose policies
/// omit `transport`. Upgraded supervisors still enforce the TLS-by-default
/// rule on port 443 and credentialed endpoints automatically.
#[must_use]
pub fn endpoint_requires_transport_enforcement(endpoint: &NetworkEndpoint) -> bool {
endpoint.transport != NetworkTransport::Unspecified as i32
}

pub fn network_enforcement_mode_from_str(value: &str) -> Option<NetworkEnforcementMode> {
match value {
"" => Some(NetworkEnforcementMode::Unspecified),
Expand Down Expand Up @@ -114,6 +251,16 @@ impl L7Protocol {
}
}

/// Returns `true` for protocols carried over HTTP (`rest`, `websocket`,
/// `graphql`, `json-rpc`, `mcp`).
#[must_use]
pub const fn is_http(self) -> bool {
matches!(
self,
Self::Rest | Self::Websocket | Self::Graphql | Self::JsonRpc | Self::Mcp
)
}

/// Returns `true` for protocols in the JSON-RPC family (`json-rpc`,
/// `mcp`).
pub fn is_jsonrpc_family(self) -> bool {
Expand Down Expand Up @@ -169,6 +316,101 @@ pub fn validate_explicit_tcp_additional_fields(
)]
}

#[cfg(test)]
mod transport_tests {
use super::*;

#[test]
fn defaults_follow_the_actual_port_and_credential_provenance() {
let mut endpoint = NetworkEndpoint {
protocol: "rest".into(),
ports: vec![443, 8443],
token_grant_owner: "global-owner".into(),
..Default::default()
};
assert_eq!(
endpoint_allowed_transports(&endpoint, 443),
Some(AllowedTransports::Tls)
);
assert_eq!(
endpoint_allowed_transports(&endpoint, 8443),
Some(AllowedTransports::Both)
);
endpoint.provider_credentialed = true;
assert_eq!(
endpoint_allowed_transports(&endpoint, 8443),
Some(AllowedTransports::Tls)
);
endpoint.transport = NetworkTransport::Plaintext as i32;
assert_eq!(
endpoint_allowed_transports(&endpoint, 443),
Some(AllowedTransports::Plaintext)
);
endpoint.transport = NetworkTransport::Tls as i32;
assert_eq!(
endpoint_allowed_transports(&endpoint, 8443),
Some(AllowedTransports::Tls)
);
}

#[test]
fn invalid_transport_values_and_opaque_protocols_are_rejected() {
let tls = NetworkTransport::Tls as i32;
assert_eq!(validate_endpoint_transport(0, "rest", 0), None);
assert!(validate_endpoint_transport(99, "rest", 0).is_some());
for protocol in ["", "tcp", "sql", "ssh"] {
assert!(validate_endpoint_transport(tls, protocol, 0).is_some());
assert_eq!(
effective_allowed_transports(0, inspected_l7_protocol(protocol, 0), false, 443),
Ok(None)
);
}
assert!(validate_endpoint_transport(tls, "rest", NetworkTlsMode::Skip as i32).is_some());
}

#[test]
fn intersect_tightens_compatible_sets_and_rejects_disjoint_sets() {
use AllowedTransports::{Both, Plaintext, Tls};

assert_eq!(Tls.intersect(Tls), Some(Tls));
assert_eq!(Tls.intersect(Both), Some(Tls));
assert_eq!(Both.intersect(Tls), Some(Tls));
assert_eq!(Plaintext.intersect(Plaintext), Some(Plaintext));
assert_eq!(Plaintext.intersect(Both), Some(Plaintext));
assert_eq!(Both.intersect(Plaintext), Some(Plaintext));
assert_eq!(Both.intersect(Both), Some(Both));
assert_eq!(Tls.intersect(Plaintext), None);
assert_eq!(Plaintext.intersect(Tls), None);
}

#[test]
fn transport_enforcement_requirement_tracks_explicit_transport_only() {
let mut endpoint = NetworkEndpoint {
protocol: "rest".into(),
port: 8443,
..Default::default()
};
assert!(!endpoint_requires_transport_enforcement(&endpoint));

// Omitted transport on port 443 or credentialed endpoints relies on
// supervisor defaults without fail-closing legacy supervisors during a
// gateway-first rolling upgrade.
endpoint.port = 443;
assert!(!endpoint_requires_transport_enforcement(&endpoint));

endpoint.port = 8443;
endpoint.provider_credentialed = true;
assert!(!endpoint_requires_transport_enforcement(&endpoint));

endpoint.provider_credentialed = false;
endpoint.transport = NetworkTransport::Tls as i32;
assert!(endpoint_requires_transport_enforcement(&endpoint));

endpoint.transport = NetworkTransport::Plaintext as i32;
assert!(endpoint_requires_transport_enforcement(&endpoint));
}
}

#[cfg(test)]
mod agent_transport_tests {
use super::agent_authored_transport_rejection;
Expand Down Expand Up @@ -756,7 +998,7 @@ pub struct EffectiveFilesystem {
/// Returns the ports an endpoint selector lists, as written: `ports`, or else
/// the singular `port`, even when it is unset (0).
#[must_use]
pub fn selector_ports(endpoint: &crate::proto::NetworkEndpoint) -> &[u32] {
pub fn selector_ports(endpoint: &NetworkEndpoint) -> &[u32] {
if endpoint.ports.is_empty() {
std::slice::from_ref(&endpoint.port)
} else {
Expand All @@ -767,7 +1009,7 @@ pub fn selector_ports(endpoint: &crate::proto::NetworkEndpoint) -> &[u32] {
/// Returns the ports an endpoint selector covers: [`selector_ports`]
/// without an unset singular `port`.
#[must_use]
pub fn effective_endpoint_ports(endpoint: &crate::proto::NetworkEndpoint) -> Vec<u32> {
pub fn effective_endpoint_ports(endpoint: &NetworkEndpoint) -> Vec<u32> {
if endpoint.ports.is_empty() {
(endpoint.port != 0)
.then_some(endpoint.port)
Expand Down
Loading
Loading