Skip to content

Commit d647792

Browse files
committed
refactor: deduplicate KMS auth helpers between onboard and upgrade_authority
Move shared helper functions (dstack_client, app_attest, pad64, ensure_self_kms_allowed, ensure_kms_allowed) into upgrade_authority.rs and reuse them from onboard_service.rs. Remove the no-op ensure_remote_kms_allowed wrapper.
1 parent dec6ee3 commit d647792

2 files changed

Lines changed: 40 additions & 63 deletions

File tree

kms/src/main_service/upgrade_authority.rs

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// SPDX-License-Identifier: Apache-2.0
44

5-
use crate::config::AuthApi;
5+
use crate::config::{AuthApi, KmsConfig};
66
use anyhow::{bail, Context, Result};
77
use dstack_guest_agent_rpc::{
88
dstack_guest_client::DstackGuestClient, AttestResponse, RawQuoteArgs,
@@ -188,19 +188,51 @@ fn url_join(url: &str, path: &str) -> String {
188188
url
189189
}
190190

191-
fn dstack_client() -> DstackGuestClient<PrpcClient> {
191+
pub(crate) fn dstack_client() -> DstackGuestClient<PrpcClient> {
192192
let address = dstack_types::dstack_agent_address();
193193
let http_client = PrpcClient::new(address);
194194
DstackGuestClient::new(http_client)
195195
}
196196

197-
async fn app_attest(report_data: Vec<u8>) -> Result<AttestResponse> {
197+
pub(crate) async fn app_attest(report_data: Vec<u8>) -> Result<AttestResponse> {
198198
dstack_client().attest(RawQuoteArgs { report_data }).await
199199
}
200200

201-
fn pad64(hash: [u8; 32]) -> Vec<u8> {
201+
pub(crate) fn pad64(hash: [u8; 32]) -> Vec<u8> {
202202
let mut padded = Vec::with_capacity(64);
203203
padded.extend_from_slice(&hash);
204204
padded.resize(64, 0);
205205
padded
206206
}
207+
208+
pub(crate) async fn ensure_self_kms_allowed(cfg: &KmsConfig) -> Result<()> {
209+
let boot_info = local_kms_boot_info(cfg.pccs_url.as_deref())
210+
.await
211+
.context("failed to build local KMS boot info")?;
212+
let response = cfg
213+
.auth_api
214+
.is_app_allowed(&boot_info, true)
215+
.await
216+
.context("failed to call KMS auth check")?;
217+
if !response.is_allowed {
218+
bail!("boot denied: {}", response.reason);
219+
}
220+
Ok(())
221+
}
222+
223+
pub(crate) async fn ensure_kms_allowed(
224+
cfg: &KmsConfig,
225+
attestation: &VerifiedAttestation,
226+
) -> Result<()> {
227+
let boot_info = build_boot_info(attestation, false, "")
228+
.context("failed to build KMS boot info from attestation")?;
229+
let response = cfg
230+
.auth_api
231+
.is_app_allowed(&boot_info, true)
232+
.await
233+
.context("failed to call KMS auth check")?;
234+
if !response.is_allowed {
235+
bail!("boot denied: {}", response.reason);
236+
}
237+
Ok(())
238+
}

kms/src/onboard_service.rs

Lines changed: 4 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,13 @@
55
use std::sync::{Arc, Mutex};
66

77
use anyhow::{bail, Context, Result};
8-
use dstack_guest_agent_rpc::{
9-
dstack_guest_client::DstackGuestClient, AttestResponse, RawQuoteArgs,
10-
};
118
use dstack_kms_rpc::{
129
kms_client::KmsClient,
1310
onboard_server::{OnboardRpc, OnboardServer},
1411
AttestationInfoResponse, BootstrapRequest, BootstrapResponse, GetKmsKeyRequest, OnboardRequest,
1512
OnboardResponse,
1613
};
1714
use fs_err as fs;
18-
use http_client::prpc::PrpcClient;
1915
use k256::ecdsa::SigningKey;
2016
use ra_rpc::{
2117
client::{CertInfo, RaClient, RaClientConfig},
@@ -30,7 +26,9 @@ use safe_write::safe_write;
3026

3127
use crate::{
3228
config::KmsConfig,
33-
main_service::upgrade_authority::{build_boot_info, local_kms_boot_info},
29+
main_service::upgrade_authority::{
30+
app_attest, dstack_client, ensure_kms_allowed, ensure_self_kms_allowed, pad64,
31+
},
3432
};
3533

3634
#[derive(Clone)]
@@ -260,7 +258,7 @@ impl Keys {
260258
.map_err(|_| anyhow::anyhow!("source attestation mutex poisoned"))?
261259
.clone()
262260
.context("Missing source KMS attestation")?;
263-
ensure_remote_kms_allowed(cfg, &source_attestation)
261+
ensure_kms_allowed(cfg, &source_attestation)
264262
.await
265263
.context("Source KMS is not allowed for onboarding")?;
266264

@@ -349,52 +347,6 @@ pub(crate) async fn bootstrap_keys(cfg: &KmsConfig) -> Result<()> {
349347
Ok(())
350348
}
351349

352-
fn dstack_client() -> DstackGuestClient<PrpcClient> {
353-
let address = dstack_types::dstack_agent_address();
354-
let http_client = PrpcClient::new(address);
355-
DstackGuestClient::new(http_client)
356-
}
357-
358-
async fn app_attest(report_data: Vec<u8>) -> Result<AttestResponse> {
359-
dstack_client().attest(RawQuoteArgs { report_data }).await
360-
}
361-
362-
async fn ensure_self_kms_allowed(cfg: &KmsConfig) -> Result<()> {
363-
let boot_info = local_kms_boot_info(cfg.pccs_url.as_deref())
364-
.await
365-
.context("Failed to build local KMS boot info")?;
366-
let response = cfg
367-
.auth_api
368-
.is_app_allowed(&boot_info, true)
369-
.await
370-
.context("Failed to call KMS auth check")?;
371-
if !response.is_allowed {
372-
bail!("Boot denied: {}", response.reason);
373-
}
374-
Ok(())
375-
}
376-
377-
async fn ensure_remote_kms_allowed(
378-
cfg: &KmsConfig,
379-
attestation: &VerifiedAttestation,
380-
) -> Result<()> {
381-
ensure_kms_allowed(cfg, attestation).await
382-
}
383-
384-
async fn ensure_kms_allowed(cfg: &KmsConfig, attestation: &VerifiedAttestation) -> Result<()> {
385-
let boot_info = build_boot_info(attestation, false, "")
386-
.context("Failed to build KMS boot info from attestation")?;
387-
let response = cfg
388-
.auth_api
389-
.is_app_allowed(&boot_info, true)
390-
.await
391-
.context("Failed to call KMS auth check")?;
392-
if !response.is_allowed {
393-
bail!("Boot denied: {}", response.reason);
394-
}
395-
Ok(())
396-
}
397-
398350
async fn attest_keys(p256_pubkey: &[u8], k256_pubkey: &[u8]) -> Result<Vec<u8>> {
399351
let p256_hex = hex::encode(p256_pubkey);
400352
let k256_hex = hex::encode(k256_pubkey);
@@ -412,13 +364,6 @@ fn keccak256(msg: &[u8]) -> [u8; 32] {
412364
hasher.finalize().into()
413365
}
414366

415-
fn pad64(hash: [u8; 32]) -> Vec<u8> {
416-
let mut padded = Vec::with_capacity(64);
417-
padded.extend_from_slice(&hash);
418-
padded.resize(64, 0);
419-
padded
420-
}
421-
422367
async fn gen_ra_cert(ca_cert_pem: String, ca_key_pem: String) -> Result<(String, String)> {
423368
use ra_tls::cert::CertRequest;
424369
use ra_tls::rcgen::{KeyPair, PKCS_ECDSA_P256_SHA256};

0 commit comments

Comments
 (0)