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