Skip to content

Commit eaf2e4d

Browse files
committed
feat(providers): wire SigV4 signing into aws-s3 profile and fix S3 compatibility
On top of the STS refresh strategy and Jesse's SigV4 proxy signing: - Fix extract_aws_region for S3 virtual-hosted URLs (parts[len-3] instead of parts[1]) so bucket.s3.region.amazonaws.com extracts the region correctly. - Add is_hop_by_hop_header filter to strip Connection, Accept-Encoding, Proxy-Connection, etc. from forwarded requests. - Wire credential_signing and signing_service through EndpointProfile serde and proto conversion so profiles can enable SigV4 signing. - Configure aws-s3 profile: **.amazonaws.com host glob, tls: terminate, credential_signing: sigv4, signing_service: s3, binary allowlist. - Add chunked transfer limitation to manage-providers.mdx. - Add examples/aws-s3-sts.md manual E2E test guide. - Add tests for region extraction and header preservation. Refs NVIDIA#1576
1 parent 7556abe commit eaf2e4d

6 files changed

Lines changed: 339 additions & 13 deletions

File tree

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1434,9 +1434,11 @@ network_policies:
14341434
},
14351435
);
14361436
let violations = validate_sandbox_policy(&policy).unwrap_err();
1437-
assert!(violations
1438-
.iter()
1439-
.any(|v| matches!(v, PolicyViolation::MissingSigningService { .. })));
1437+
assert!(
1438+
violations
1439+
.iter()
1440+
.any(|v| matches!(v, PolicyViolation::MissingSigningService { .. }))
1441+
);
14401442
}
14411443

14421444
#[test]

‎crates/openshell-providers/src/profiles.rs‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ pub struct EndpointProfile {
162162
pub graphql_max_body_bytes: u32,
163163
#[serde(default, skip_serializing_if = "String::is_empty")]
164164
pub path: String,
165+
#[serde(default, skip_serializing_if = "String::is_empty")]
166+
pub credential_signing: String,
167+
#[serde(default, skip_serializing_if = "String::is_empty")]
168+
pub signing_service: String,
165169
}
166170

167171
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
@@ -600,8 +604,8 @@ fn endpoint_to_proto(endpoint: &EndpointProfile) -> NetworkEndpoint {
600604
.collect(),
601605
graphql_max_body_bytes: endpoint.graphql_max_body_bytes,
602606
path: endpoint.path.clone(),
603-
credential_signing: String::new(),
604-
signing_service: String::new(),
607+
credential_signing: endpoint.credential_signing.clone(),
608+
signing_service: endpoint.signing_service.clone(),
605609
}
606610
}
607611

@@ -632,6 +636,8 @@ fn endpoint_from_proto(endpoint: &NetworkEndpoint) -> EndpointProfile {
632636
.collect(),
633637
graphql_max_body_bytes: endpoint.graphql_max_body_bytes,
634638
path: endpoint.path.clone(),
639+
credential_signing: endpoint.credential_signing.clone(),
640+
signing_service: endpoint.signing_service.clone(),
635641
}
636642
}
637643

@@ -1522,6 +1528,11 @@ endpoints:
15221528
let aws_s3 = get_default_profile("aws-s3").expect("aws-s3 profile should exist");
15231529
assert_eq!(aws_s3.display_name, "AWS S3");
15241530
assert!(!aws_s3.endpoints.is_empty());
1525-
assert!(aws_s3.endpoints.iter().any(|e| e.host.contains("amazonaws.com")));
1531+
assert!(
1532+
aws_s3
1533+
.endpoints
1534+
.iter()
1535+
.any(|e| e.host.contains("amazonaws.com"))
1536+
);
15261537
}
15271538
}

‎crates/openshell-sandbox/src/sigv4.rs‎

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,36 @@ use miette::{Result, miette};
1111
use std::time::SystemTime;
1212

1313
/// Extract the AWS region from a standard AWS hostname.
14-
/// Pattern: `<service>.<region>.amazonaws.com` → `<region>`.
14+
///
15+
/// Finds the label immediately before `amazonaws.com`.
16+
/// Works for both `<service>.<region>.amazonaws.com` and
17+
/// `<bucket>.<service>.<region>.amazonaws.com`.
1518
pub fn extract_aws_region(host: &str) -> Option<String> {
1619
let parts: Vec<&str> = host.split('.').collect();
1720
if parts.len() >= 4 && parts[parts.len() - 2] == "amazonaws" && parts[parts.len() - 1] == "com"
1821
{
19-
Some(parts[1].to_string())
22+
Some(parts[parts.len() - 3].to_string())
2023
} else {
2124
None
2225
}
2326
}
2427

28+
fn is_hop_by_hop_header(lower: &str) -> bool {
29+
matches!(
30+
lower,
31+
"connection"
32+
| "proxy-connection"
33+
| "keep-alive"
34+
| "transfer-encoding"
35+
| "te"
36+
| "trailer"
37+
| "upgrade"
38+
| "proxy-authorization"
39+
| "proxy-authenticate"
40+
| "accept-encoding"
41+
)
42+
}
43+
2544
/// Strip AWS auth headers from raw HTTP request bytes.
2645
///
2746
/// Removes `Authorization`, `X-Amz-Date`, `X-Amz-Security-Token`, and
@@ -104,11 +123,11 @@ pub fn apply_sigv4_to_request(
104123
}
105124
});
106125

107-
// Collect all non-AWS headers for forwarding, and a subset for signing.
126+
// Preserve all original headers except AWS auth headers (regenerated by
127+
// the signer) and hop-by-hop headers (may be modified by the proxy or
128+
// transport between signing and delivery, breaking the signature).
108129
// Only host, content-type, and content-length are included in the SigV4
109-
// signature. Signing all headers causes failures when the proxy or
110-
// transport modifies unsigned-by-convention headers (Connection,
111-
// Accept-Encoding, etc.) between signing and delivery.
130+
// signature to avoid signing transport-mutable headers.
112131
let mut headers_to_sign: Vec<(String, String)> = Vec::new();
113132
let mut all_headers: Vec<(String, String)> = Vec::new();
114133
for line in lines.iter().skip(1) {
@@ -121,6 +140,7 @@ pub fn apply_sigv4_to_request(
121140
|| lower.starts_with("x-amz-date")
122141
|| lower.starts_with("x-amz-security-token")
123142
|| lower.starts_with("x-amz-content-sha256")
143+
|| is_hop_by_hop_header(&lower)
124144
{
125145
continue;
126146
}
@@ -213,6 +233,12 @@ mod tests {
213233
assert_eq!(region, "us-east-1");
214234
}
215235

236+
#[test]
237+
fn extract_region_from_s3_virtual_hosted_hostname() {
238+
let region = extract_aws_region("my-bucket.s3.us-east-1.amazonaws.com").unwrap();
239+
assert_eq!(region, "us-east-1");
240+
}
241+
216242
#[test]
217243
fn non_aws_hostname_returns_none() {
218244
assert!(extract_aws_region("api.anthropic.com").is_none());
@@ -300,4 +326,27 @@ mod tests {
300326
assert!(!result_str.contains("old-invalid-sig"));
301327
assert!(!result_str.contains("old-date"));
302328
}
329+
330+
#[test]
331+
fn sign_preserves_s3_headers_and_strips_hop_by_hop() {
332+
let raw = b"PUT /object.txt HTTP/1.1\r\nHost: my-bucket.s3.us-east-1.amazonaws.com\r\nContent-Type: text/plain\r\nContent-Length: 5\r\nx-amz-acl: private\r\nx-amz-server-side-encryption: AES256\r\nRange: bytes=0-99\r\nConnection: keep-alive\r\nAccept-Encoding: gzip\r\nProxy-Connection: keep-alive\r\n\r\nhello";
333+
let result = apply_sigv4_to_request(
334+
raw,
335+
"my-bucket.s3.us-east-1.amazonaws.com",
336+
"us-east-1",
337+
"s3",
338+
"AKIATEST",
339+
"secret",
340+
None,
341+
)
342+
.unwrap();
343+
let result_str = String::from_utf8_lossy(&result);
344+
assert!(result_str.contains("x-amz-acl: private"));
345+
assert!(result_str.contains("x-amz-server-side-encryption: AES256"));
346+
assert!(result_str.contains("range: bytes=0-99"));
347+
assert!(!result_str.contains("connection:"));
348+
assert!(!result_str.contains("accept-encoding:"));
349+
assert!(!result_str.contains("proxy-connection:"));
350+
assert!(result_str.contains("authorization: AWS4-HMAC-SHA256"));
351+
}
303352
}

‎docs/sandboxes/manage-providers.mdx‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,11 @@ openshell provider refresh configure my-aws \
182182
Use the generic `aws` profile type for multi-service access and scope endpoints
183183
via sandbox network policy. Use `aws-s3` for S3-specific endpoint rules.
184184

185+
The proxy re-signs requests using SigV4 before forwarding to AWS. Clients that
186+
send `Content-Length` bodies (curl, most CLI tools) work. Clients that use
187+
`Transfer-Encoding: chunked` (e.g., boto3 `put_object`) are not yet supported
188+
by the SigV4 relay and will fail.
189+
185190
External refresh systems should continue to push new current credentials through
186191
`openshell provider update`. The `--credential-expires-at` option works for
187192
static credentials, externally refreshed credentials, and gateway-managed

0 commit comments

Comments
 (0)