Skip to content
Open
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
8 changes: 7 additions & 1 deletion dstack/crates/dstack-cli-core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,8 @@ pub struct VmmRender {
pub key_provider_addr: String,
/// local key-provider port.
pub key_provider_port: u32,
/// Host-reachable KMS URL used by the VMM itself (e.g. `https://127.0.0.1:<port>`).
pub kms_url: String,
/// KMS URLs injected into app CVMs (the guest-visible KMS address).
pub kms_urls: Vec<String>,
/// confidential-computing platform (selects qemu/share-mode for the CVMs).
Expand All @@ -308,6 +310,7 @@ impl Default for VmmRender {
host_api_port: 10000,
key_provider_addr: "127.0.0.1".to_string(),
key_provider_port: 3443,
kms_url: String::new(),
kms_urls: Vec::new(),
platform: Platform::Tdx,
auth_enabled: false,
Expand All @@ -332,7 +335,7 @@ keep_alive = 10
log_level = "info"
address = "{dashboard_addr}"
reuse = true
kms_url = ""
kms_url = "{kms_url}"
event_buffer_size = 20
node_name = ""
run_path = "{vm_path}"
Expand Down Expand Up @@ -436,6 +439,7 @@ port = {kp_port}
Platform::AmdSevSnp => "vhd",
Platform::Tdx => "9p",
},
kms_url = r.kms_url,
kms_urls = r
.kms_urls
.iter()
Expand Down Expand Up @@ -469,12 +473,14 @@ mod tests {
dashboard_addr: "tcp:127.0.0.1:19080".into(),
cid_start: 2000,
host_api_port: 10001,
kms_url: "https://127.0.0.1:13443".into(),
auth_enabled: true,
auth_token: "deadbeef".into(),
..Default::default()
};
let rendered = vmm_toml(&r);
assert!(rendered.contains(r#"address = "tcp:127.0.0.1:19080""#));
assert!(rendered.contains(r#"kms_url = "https://127.0.0.1:13443""#));
assert!(rendered.contains("cid_start = 2000"));
assert!(rendered.contains("port = 10001"));
assert!(rendered.contains("enabled = true"));
Expand Down
10 changes: 7 additions & 3 deletions dstack/crates/dstackup/src/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,13 @@ pub(crate) async fn cmd_install(mut o: InstallOpts, release_api_base_url: &str)

// KMS host port + URL, resolved during preflight so conflicts are caught
// before image download, builds, or systemd writes.
let kms_urls = if o.no_kms {
vec![]
let (kms_url, kms_urls) = if o.no_kms {
(String::new(), vec![])
} else {
vec![format!("https://10.0.2.2:{kms_port}")]
(
format!("https://127.0.0.1:{kms_port}"),
vec![format!("https://10.0.2.2:{kms_port}")],
)
};

// 9. render configs.
Expand All @@ -174,6 +177,7 @@ pub(crate) async fn cmd_install(mut o: InstallOpts, release_api_base_url: &str)
host_api_port: o.host_api_port,
key_provider_addr: kp_addr,
key_provider_port: kp_port as u32,
kms_url,
kms_urls: kms_urls.clone(),
platform,
auth_enabled: true,
Expand Down
71 changes: 66 additions & 5 deletions dstack/vmm/src/app/mr_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,28 @@ fn is_snp_launch(manifest: &Manifest, platform: CvmPlatform) -> bool {
|| (!manifest.no_tee && platform == CvmPlatform::AmdSevSnp)
}

/// KMS URLs the VMM tries from the host to resolve the current app epoch.
///
/// When the VM manifest does not override `kms_urls`, prefer the host-side
/// top-level `config.kms_url` ahead of `config.cvm.kms_urls`: under QEMU
/// user-mode networking `cvm.kms_urls` carries the guest-only `10.0.2.2`
/// alias, which is not routable from the host.
fn app_epoch_kms_urls<'a>(manifest: &'a Manifest, config: &'a Config) -> Vec<&'a str> {
if !manifest.kms_urls.is_empty() {
return manifest.kms_urls.iter().map(String::as_str).collect();
}
let mut urls = Vec::new();
if !config.kms_url.is_empty() {
urls.push(config.kms_url.as_str());
}
for url in &config.cvm.kms_urls {
if !urls.contains(&url.as_str()) {
urls.push(url.as_str());
}
}
urls
}

/// The app epoch a launch binds in MrConfigV3: the pin, or on SEV-SNP with KMS
/// keys the KMS's current app epoch, since SEV-SNP has no runtime register to
/// measure the epoch the guest is served. Images predating the field get none.
Expand All @@ -154,11 +176,7 @@ pub(crate) async fn launch_app_epoch(
{
return Ok(manifest.app_epoch);
}
let kms_urls = if manifest.kms_urls.is_empty() {
&config.cvm.kms_urls
} else {
&manifest.kms_urls
};
let kms_urls = app_epoch_kms_urls(manifest, config);
if kms_urls.is_empty() {
bail!("an amd sev-snp launch with KMS keys needs a KMS URL to read the app epoch");
}
Expand Down Expand Up @@ -372,4 +390,47 @@ mod tests {
let document: serde_json::Value = serde_json::from_str(&document).unwrap();
assert_eq!(document["init_script_hashes"], serde_json::json!([]));
}

#[test]
fn app_epoch_kms_urls_prefers_host_kms_url() {
let mut config: Config = crate::Figment::from(crate::config::load_config_figment(None))
.extract()
.unwrap();
config.kms_url = "https://127.0.0.1:13443".into();
config.cvm.kms_urls = vec![
"https://10.0.2.2:13443".into(),
"https://127.0.0.1:13443".into(),
];

let mut manifest = Manifest::from_json(serde_json::json!({
"id": "vm-1",
"name": "vm-1",
"app_id": "11".repeat(20),
"vcpu": 2,
"memory": 2048,
"disk_size": 1024,
"image": "dstack-0.6.7",
"port_map": [],
"created_at_ms": 0
}))
.unwrap();

assert_eq!(
app_epoch_kms_urls(&manifest, &config),
vec!["https://127.0.0.1:13443", "https://10.0.2.2:13443"]
);

manifest.kms_urls = vec!["https://override.example".into()];
assert_eq!(
app_epoch_kms_urls(&manifest, &config),
vec!["https://override.example"]
);

manifest.kms_urls.clear();
config.kms_url.clear();
assert_eq!(
app_epoch_kms_urls(&manifest, &config),
vec!["https://10.0.2.2:13443", "https://127.0.0.1:13443"]
);
}
}
14 changes: 14 additions & 0 deletions dstack/vmm/src/tests/test_vmm_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,20 @@ def rpc(method, params=None):
methods = [c.args[0] for c in self.cli.rpc_call.call_args_list]
self.assertNotIn("CreateVm", methods)

# --env-file with --kms-encrypt-url works without overriding manifest kms_urls.
capability = {"sealed_env": True}
args.env_file = "env"
args.app_epoch = None
args.kms_url = None
args.kms_encrypt_url = "https://127.0.0.1:13444"
client.return_value.request.return_value = (200, self.response)
self.cli.rpc_call.reset_mock()
self.cli.create_vm(args)
method, payload = self.cli.rpc_call.call_args.args
self.assertEqual(method, "CreateVm")
self.assertNotIn("kms_urls", payload)
self.decrypt(payload)


if __name__ == "__main__":
unittest.main()
6 changes: 3 additions & 3 deletions dstack/vmm/src/vmm-cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -1165,11 +1165,11 @@ def create_vm(self, args) -> None:

envs = parse_env_file(args.env_file)

# Validate: --env-file requires --kms-url and kms_enabled in compose
# Validate: --env-file requires --kms-url or --kms-encrypt-url, and kms_enabled in compose
if envs:
if not args.kms_url:
if not args.kms_url and not args.kms_encrypt_url:
raise Exception(
"--env-file requires --kms-url to encrypt environment variables"
"--env-file requires --kms-url or --kms-encrypt-url to encrypt environment variables"
)
try:
compose_json = json.loads(compose_content)
Expand Down
Loading