Skip to content

Commit 8e23ea9

Browse files
committed
refactor(vmm): centralize simulated TPM capability
1 parent d627337 commit 8e23ea9

5 files changed

Lines changed: 25 additions & 42 deletions

File tree

dstack/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dstack/crates/mock-attestation/src/lib.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ pub mod tpm;
3030

3131
pub const MOCK_SEED_LEN: usize = 32;
3232

33+
/// Returns whether the simulated platform provides its own TPM device.
34+
pub fn platform_provides_tpm(platform: dstack_types::TeeVariant) -> bool {
35+
matches!(
36+
platform,
37+
dstack_types::TeeVariant::DstackGcpTdx | dstack_types::TeeVariant::DstackAwsNitroTpm
38+
)
39+
}
40+
3341
pub fn parse_seed(value: &str) -> Result<[u8; MOCK_SEED_LEN]> {
3442
let bytes = hex::decode(value).context("mock attestation seed must be hex")?;
3543
bytes
@@ -148,4 +156,15 @@ mod tests {
148156
assert!(dir.path().join(path).exists());
149157
}
150158
}
159+
160+
#[test]
161+
fn identifies_platforms_that_provide_a_tpm() {
162+
assert!(platform_provides_tpm(
163+
dstack_types::TeeVariant::DstackGcpTdx
164+
));
165+
assert!(platform_provides_tpm(
166+
dstack_types::TeeVariant::DstackAwsNitroTpm
167+
));
168+
assert!(!platform_provides_tpm(dstack_types::TeeVariant::DstackTdx));
169+
}
151170
}

dstack/vmm/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ load_config.workspace = true
4949
key-provider-client.workspace = true
5050
dstack-types.workspace = true
5151
dstack-mr.workspace = true
52+
mock-attestation = { path = "../crates/mock-attestation" }
5253
hex_fmt.workspace = true
5354
lspci.workspace = true
5455
base64.workspace = true

dstack/vmm/src/app.rs

Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,7 +1109,6 @@ impl App {
11091109
&manifest,
11101110
&hex::encode(compose_hash),
11111111
mr_config,
1112-
app_compose.key_provider(),
11131112
app_compose.requirements.as_ref(),
11141113
)?;
11151114
fs::write(shared_dir.join(SYS_CONFIG), &sys_config_str)
@@ -1292,7 +1291,6 @@ pub(crate) fn make_sys_config(
12921291
manifest: &Manifest,
12931292
compose_hash: &str,
12941293
mr_config: Option<String>,
1295-
key_provider: dstack_types::KeyProviderKind,
12961294
requirements: Option<&dstack_types::Requirements>,
12971295
) -> Result<String> {
12981296
let image_path = cfg.image.path.join(&manifest.image);
@@ -1312,13 +1310,12 @@ pub(crate) fn make_sys_config(
13121310
bail!("Unsupported image version: {img_ver:?}");
13131311
}
13141312

1315-
let vm_config = make_vm_config_with_key_provider(
1313+
let vm_config = make_vm_config(
13161314
cfg,
13171315
manifest,
13181316
&image,
13191317
compose_hash,
13201318
mr_config.clone(),
1321-
key_provider,
13221319
requirements,
13231320
)?;
13241321
let mut sys_config = json!({
@@ -1386,33 +1383,12 @@ fn tdx_attestation_variant_from_requirements(
13861383
})
13871384
}
13881385

1389-
#[cfg(test)]
13901386
fn make_vm_config(
1391-
cfg: &Config,
1392-
manifest: &Manifest,
1393-
image: &Image,
1394-
compose_hash: &str,
1395-
mr_config: Option<String>,
1396-
requirements: Option<&dstack_types::Requirements>,
1397-
) -> Result<serde_json::Value> {
1398-
make_vm_config_with_key_provider(
1399-
cfg,
1400-
manifest,
1401-
image,
1402-
compose_hash,
1403-
mr_config,
1404-
dstack_types::KeyProviderKind::None,
1405-
requirements,
1406-
)
1407-
}
1408-
1409-
fn make_vm_config_with_key_provider(
14101387
cfg: &Config,
14111388
manifest: &Manifest,
14121389
image: &Image,
14131390
_compose_hash: &str,
14141391
mr_config: Option<String>,
1415-
_key_provider: dstack_types::KeyProviderKind,
14161392
requirements: Option<&dstack_types::Requirements>,
14171393
) -> Result<serde_json::Value> {
14181394
let platform = cfg.cvm.resolved_platform();
@@ -1516,19 +1492,12 @@ fn make_vm_config_with_key_provider(
15161492
Ok(config)
15171493
}
15181494

1519-
pub(crate) fn simulated_platform_provides_tpm(platform: Option<dstack_types::TeeVariant>) -> bool {
1520-
matches!(
1521-
platform,
1522-
Some(dstack_types::TeeVariant::DstackGcpTdx | dstack_types::TeeVariant::DstackAwsNitroTpm)
1523-
)
1524-
}
1525-
15261495
pub(crate) fn needs_swtpm(
15271496
key_provider: dstack_types::KeyProviderKind,
15281497
simulated_tee: Option<dstack_types::TeeVariant>,
15291498
) -> bool {
15301499
matches!(key_provider, dstack_types::KeyProviderKind::Tpm)
1531-
&& !simulated_platform_provides_tpm(simulated_tee)
1500+
&& !simulated_tee.is_some_and(mock_attestation::platform_provides_tpm)
15321501
}
15331502

15341503
#[cfg(test)]
@@ -2115,14 +2084,8 @@ mod tests {
21152084
let build_hash = Sha256::digest(sha256sum.as_bytes()).to_vec();
21162085
fs::write(image_dir.join("digest.txt"), hex::encode(&build_hash))?;
21172086

2118-
let sys_config_document = make_sys_config(
2119-
&config,
2120-
&manifest,
2121-
&compose_hash,
2122-
Some(mr_config),
2123-
dstack_types::KeyProviderKind::None,
2124-
None,
2125-
)?;
2087+
let sys_config_document =
2088+
make_sys_config(&config, &manifest, &compose_hash, Some(mr_config), None)?;
21262089
let sys_config: serde_json::Value = serde_json::from_str(&sys_config_document)?;
21272090
assert!(sys_config.get("tee_simulator").is_none());
21282091
assert_eq!(sys_config["pccs_url"], config.cvm.pccs_url);

dstack/vmm/src/one_shot.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,6 @@ Compose file content (first 200 chars):
265265
&manifest,
266266
&compose_hash,
267267
mr_config,
268-
app_compose.key_provider(),
269268
app_compose.requirements.as_ref(),
270269
)?;
271270
let sys_config_path = vm_work_dir.shared_dir().join(".sys-config.json");

0 commit comments

Comments
 (0)