Skip to content

Commit b0365a1

Browse files
committed
feat(providers): support aws_session_token for temporary source credentials
The explicit AWS source-credential path always passed no session token, so temporary credentials from AWS SSO or a prior AssumeRole (which are only valid with a session token) failed to sign the STS request. Accept an optional secret aws_session_token material and pass it to the AWS SDK. It requires the aws_access_key_id and aws_secret_access_key pair, enforced at both the configure boundary and the mint path. Declare it as optional secret material on the aws and aws-s3 profiles and document it. Signed-off-by: Russell Bryant <[email protected]>
1 parent a5a89f0 commit b0365a1

5 files changed

Lines changed: 233 additions & 1 deletion

File tree

‎crates/openshell-server/src/grpc/provider.rs‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2149,6 +2149,17 @@ pub(super) async fn handle_configure_provider_refresh(
21492149
"aws_access_key_id and aws_secret_access_key must both be set or both omitted",
21502150
));
21512151
}
2152+
// An optional session token supports temporary source credentials (SSO or a
2153+
// prior AssumeRole). It only makes sense alongside the source key pair.
2154+
let has_source_session_token = request
2155+
.material
2156+
.get("aws_session_token")
2157+
.is_some_and(|value| !value.trim().is_empty());
2158+
if has_source_session_token && !has_source_access_key {
2159+
return Err(Status::invalid_argument(
2160+
"aws_session_token requires aws_access_key_id and aws_secret_access_key",
2161+
));
2162+
}
21522163
if request
21532164
.expires_at_ms
21542165
.is_some_and(|expires_at_ms| expires_at_ms < 0)
@@ -6622,6 +6633,65 @@ mod tests {
66226633
assert!(err.message().contains("both be set or both omitted"));
66236634
}
66246635

6636+
#[tokio::test]
6637+
async fn configure_aws_sts_rejects_session_token_without_pair() {
6638+
use crate::grpc::policy::set_global_bool_setting_for_test;
6639+
6640+
let state = test_server_state().await;
6641+
set_global_bool_setting_for_test(
6642+
state.store.as_ref(),
6643+
openshell_core::settings::PROVIDERS_V2_ENABLED_KEY,
6644+
true,
6645+
)
6646+
.await
6647+
.unwrap();
6648+
6649+
create_provider_record(
6650+
state.store.as_ref(),
6651+
Provider {
6652+
metadata: Some(openshell_core::proto::datamodel::v1::ObjectMeta {
6653+
id: String::new(),
6654+
name: "aws-lone-session".to_string(),
6655+
created_at_ms: 0,
6656+
labels: HashMap::new(),
6657+
resource_version: 0,
6658+
}),
6659+
r#type: "aws".to_string(),
6660+
credentials: HashMap::new(),
6661+
config: HashMap::new(),
6662+
credential_expires_at_ms: HashMap::new(),
6663+
},
6664+
)
6665+
.await
6666+
.unwrap();
6667+
6668+
let err = handle_configure_provider_refresh(
6669+
&state,
6670+
Request::new(ConfigureProviderRefreshRequest {
6671+
provider: "aws-lone-session".to_string(),
6672+
credential_key: "AWS_ACCESS_KEY_ID".to_string(),
6673+
strategy: ProviderCredentialRefreshStrategy::AwsStsAssumeRole as i32,
6674+
material: HashMap::from([
6675+
(
6676+
"role_arn".to_string(),
6677+
"arn:aws:iam::123456789012:role/Test".to_string(),
6678+
),
6679+
(
6680+
"aws_session_token".to_string(),
6681+
"temp-session-token".to_string(),
6682+
),
6683+
]),
6684+
secret_material_keys: vec!["aws_session_token".to_string()],
6685+
expires_at_ms: None,
6686+
}),
6687+
)
6688+
.await
6689+
.unwrap_err();
6690+
6691+
assert_eq!(err.code(), Code::InvalidArgument);
6692+
assert!(err.message().contains("aws_session_token requires"));
6693+
}
6694+
66256695
#[tokio::test]
66266696
async fn configure_aws_sts_persists_resolved_additional_output_keys() {
66276697
use crate::grpc::StoredSettingValue;

‎crates/openshell-server/src/provider_refresh.rs‎

Lines changed: 151 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,9 @@ async fn mint_aws_sts_assume_role(
701701
// Explicit source credentials are all-or-nothing. A lone key must not
702702
// silently fall through to the gateway's ambient identity (CWE-20): the
703703
// caller asked for a specific principal, so an incomplete pair is an error.
704+
// An optional session token supports temporary source credentials (SSO or a
705+
// prior AssumeRole); it requires the access/secret pair.
706+
let session_token = material_value(&state.material, &["aws_session_token"]);
704707
match (
705708
material_value(&state.material, &["aws_access_key_id"]),
706709
material_value(&state.material, &["aws_secret_access_key"]),
@@ -709,12 +712,17 @@ async fn mint_aws_sts_assume_role(
709712
let creds = aws_sdk_sts::config::Credentials::new(
710713
access_key,
711714
secret_key,
712-
None,
715+
session_token,
713716
None,
714717
"openshell-provider-refresh",
715718
);
716719
config_loader = config_loader.credentials_provider(creds);
717720
}
721+
(None, None) if session_token.is_some() => {
722+
return Err(Status::invalid_argument(
723+
"aws_session_token requires aws_access_key_id and aws_secret_access_key",
724+
));
725+
}
718726
(None, None) => {}
719727
_ => {
720728
return Err(Status::invalid_argument(
@@ -1838,6 +1846,148 @@ mod tests {
18381846
}
18391847
}
18401848

1849+
#[tokio::test]
1850+
async fn aws_sts_mint_accepts_session_token_with_source_pair() {
1851+
let mock_server = MockServer::start().await;
1852+
Mock::given(method("POST"))
1853+
.and(body_string_contains("Action=AssumeRole"))
1854+
.respond_with(ResponseTemplate::new(200).set_body_string(
1855+
r#"<AssumeRoleResponse xmlns="https://sts.amazonaws.com/doc/2011-06-15/">
1856+
<AssumeRoleResult>
1857+
<Credentials>
1858+
<AccessKeyId>ASIAMOCKKEY</AccessKeyId>
1859+
<SecretAccessKey>MockSecretAccessKey123</SecretAccessKey>
1860+
<SessionToken>MockSessionTokenXYZ</SessionToken>
1861+
<Expiration>2099-01-01T00:00:00Z</Expiration>
1862+
</Credentials>
1863+
</AssumeRoleResult>
1864+
</AssumeRoleResponse>"#,
1865+
))
1866+
.mount(&mock_server)
1867+
.await;
1868+
1869+
let store = test_store().await;
1870+
crate::grpc::policy::set_global_bool_setting_for_test(
1871+
&store,
1872+
openshell_core::settings::PROVIDERS_V2_ENABLED_KEY,
1873+
true,
1874+
)
1875+
.await
1876+
.unwrap();
1877+
let prov = provider("aws-sts-session", "aws");
1878+
store.put_message(&prov).await.unwrap();
1879+
1880+
// Temporary source credentials: access key + secret + session token.
1881+
let state = new_refresh_state(
1882+
&prov,
1883+
"AWS_ACCESS_KEY_ID",
1884+
NewRefreshStateConfig {
1885+
additional_output_keys: HashMap::from([
1886+
(
1887+
"secret_access_key".to_string(),
1888+
"AWS_SECRET_ACCESS_KEY".to_string(),
1889+
),
1890+
("session_token".to_string(), "AWS_SESSION_TOKEN".to_string()),
1891+
]),
1892+
strategy: ProviderCredentialRefreshStrategy::AwsStsAssumeRole,
1893+
material: HashMap::from([
1894+
(
1895+
"role_arn".to_string(),
1896+
"arn:aws:iam::123456789012:role/TestRole".to_string(),
1897+
),
1898+
("aws_access_key_id".to_string(), "ASIASOURCEKEY".to_string()),
1899+
(
1900+
"aws_secret_access_key".to_string(),
1901+
"SourceSecretKey".to_string(),
1902+
),
1903+
(
1904+
"aws_session_token".to_string(),
1905+
"SourceSessionToken".to_string(),
1906+
),
1907+
("sts_endpoint_url".to_string(), mock_server.uri()),
1908+
]),
1909+
secret_material_keys: vec![
1910+
"aws_secret_access_key".to_string(),
1911+
"aws_session_token".to_string(),
1912+
],
1913+
expires_at_ms: 0,
1914+
token_url: String::new(),
1915+
scopes: Vec::new(),
1916+
refresh_before_seconds: 300,
1917+
max_lifetime_seconds: 3600,
1918+
},
1919+
)
1920+
.unwrap();
1921+
put_refresh_state(&store, &state).await.unwrap();
1922+
1923+
let refreshed = refresh_provider_credential(&store, "aws-sts-session", "AWS_ACCESS_KEY_ID")
1924+
.await
1925+
.unwrap();
1926+
assert_eq!(refreshed.status, "refreshed");
1927+
let stored = store
1928+
.get_message_by_name::<Provider>("aws-sts-session")
1929+
.await
1930+
.unwrap()
1931+
.unwrap();
1932+
assert_eq!(
1933+
stored.credentials.get("AWS_ACCESS_KEY_ID"),
1934+
Some(&"ASIAMOCKKEY".to_string())
1935+
);
1936+
}
1937+
1938+
#[tokio::test]
1939+
async fn aws_sts_mint_rejects_session_token_without_source_pair() {
1940+
let store = test_store().await;
1941+
crate::grpc::policy::set_global_bool_setting_for_test(
1942+
&store,
1943+
openshell_core::settings::PROVIDERS_V2_ENABLED_KEY,
1944+
true,
1945+
)
1946+
.await
1947+
.unwrap();
1948+
let prov = provider("aws-sts-lonesession", "aws");
1949+
store.put_message(&prov).await.unwrap();
1950+
1951+
let state = new_refresh_state(
1952+
&prov,
1953+
"AWS_ACCESS_KEY_ID",
1954+
NewRefreshStateConfig {
1955+
additional_output_keys: HashMap::from([
1956+
(
1957+
"secret_access_key".to_string(),
1958+
"AWS_SECRET_ACCESS_KEY".to_string(),
1959+
),
1960+
("session_token".to_string(), "AWS_SESSION_TOKEN".to_string()),
1961+
]),
1962+
strategy: ProviderCredentialRefreshStrategy::AwsStsAssumeRole,
1963+
material: HashMap::from([
1964+
(
1965+
"role_arn".to_string(),
1966+
"arn:aws:iam::123456789012:role/TestRole".to_string(),
1967+
),
1968+
(
1969+
"aws_session_token".to_string(),
1970+
"SourceSessionToken".to_string(),
1971+
),
1972+
]),
1973+
secret_material_keys: vec!["aws_session_token".to_string()],
1974+
expires_at_ms: 0,
1975+
token_url: String::new(),
1976+
scopes: Vec::new(),
1977+
refresh_before_seconds: 300,
1978+
max_lifetime_seconds: 3600,
1979+
},
1980+
)
1981+
.unwrap();
1982+
put_refresh_state(&store, &state).await.unwrap();
1983+
1984+
let err = refresh_provider_credential(&store, "aws-sts-lonesession", "AWS_ACCESS_KEY_ID")
1985+
.await
1986+
.unwrap_err();
1987+
assert_eq!(err.code(), tonic::Code::InvalidArgument);
1988+
assert!(err.message().contains("aws_session_token requires"));
1989+
}
1990+
18411991
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
18421992
async fn rotation_does_not_resurrect_refresh_deleted_mid_flight() {
18431993
let mock_server = MockServer::start().await;

‎docs/sandboxes/manage-providers.mdx‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,10 @@ Pass the long-lived secret with `--secret-material-env` so the CLI reads it
218218
from its own environment instead of expanding it into a process argument,
219219
where it would be visible in the host process table.
220220

221+
For temporary source credentials (AWS SSO or a prior `AssumeRole`), also pass a
222+
session token with `--secret-material-env aws_session_token=AWS_SESSION_TOKEN`.
223+
It requires the `aws_access_key_id` and `aws_secret_access_key` pair.
224+
221225
Use the generic `aws` profile type for multi-service access and scope endpoints
222226
via sandbox network policy. Use `aws-s3` for S3-specific endpoint rules.
223227

‎providers/aws-s3.yaml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ credentials:
4747
description: Long-lived IAM secret key (only needed if gateway lacks ambient AWS credentials)
4848
required: false
4949
secret: true
50+
- name: aws_session_token
51+
description: Session token for temporary source credentials (SSO or a prior AssumeRole); requires the access/secret pair
52+
required: false
53+
secret: true
5054
- name: secret_access_key
5155
description: AWS secret access key (co-managed with access_key_id)
5256
env_vars: [AWS_SECRET_ACCESS_KEY]

‎providers/aws.yaml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ credentials:
4747
description: Long-lived IAM secret key (only needed if gateway lacks ambient AWS credentials)
4848
required: false
4949
secret: true
50+
- name: aws_session_token
51+
description: Session token for temporary source credentials (SSO or a prior AssumeRole); requires the access/secret pair
52+
required: false
53+
secret: true
5054
- name: secret_access_key
5155
description: AWS secret access key (co-managed with access_key_id)
5256
env_vars: [AWS_SECRET_ACCESS_KEY]

0 commit comments

Comments
 (0)