@@ -82,53 +82,70 @@ async fn maybe_gen_certs(config: &Config, tls_config: &TlsConfig) -> Result<()>
8282 }
8383 }
8484 }
85- if !config. debug . insecure_skip_attestation {
86- info ! ( "Using dstack guest agent for certificate generation" ) ;
87- let agent_client = dstack_agent ( ) . context ( "Failed to create dstack client" ) ?;
85+ match config. debug . insecure_skip_attestation {
86+ true => gen_debug_certs ( config, tls_config, alt_names) . await ,
87+ false => gen_prod_certs ( tls_config, alt_names) . await ,
88+ }
89+ }
8890
89- let response = agent_client
90- . get_tls_key ( GetTlsKeyArgs {
91- subject : "dstack-gateway" . to_string ( ) ,
92- alt_names,
93- usage_ra_tls : true ,
94- usage_server_auth : true ,
95- usage_client_auth : true ,
96- not_before : None ,
97- not_after : None ,
98- } )
99- . await ?;
91+ async fn gen_prod_certs ( tls_config : & TlsConfig , alt_names : Vec < String > ) -> Result < ( ) > {
92+ info ! ( "Using dstack guest agent for certificate generation" ) ;
93+ let agent_client = dstack_agent ( ) . context ( "Failed to create dstack client" ) ?;
10094
101- let ca_cert = response
102- . certificate_chain
103- . last ( )
104- . context ( "Empty certificate chain" ) ?
105- . to_string ( ) ;
106- let certs = response. certificate_chain . join ( "\n " ) ;
107- write_cert ( & tls_config. mutual . ca_certs , & ca_cert) ?;
108- write_cert ( & tls_config. certs , & certs) ?;
109- write_cert ( & tls_config. key , & response. key ) ?;
110- return Ok ( ( ) ) ;
111- }
95+ let response = agent_client
96+ . get_tls_key ( GetTlsKeyArgs {
97+ subject : "dstack-gateway" . to_string ( ) ,
98+ alt_names,
99+ usage_ra_tls : true ,
100+ usage_server_auth : true ,
101+ usage_client_auth : true ,
102+ not_before : None ,
103+ not_after : None ,
104+ } )
105+ . await ?;
106+
107+ let ca_cert = response
108+ . certificate_chain
109+ . last ( )
110+ . context ( "Empty certificate chain" ) ?
111+ . to_string ( ) ;
112+ let certs = response. certificate_chain . join ( "\n " ) ;
113+ write_cert ( & tls_config. mutual . ca_certs , & ca_cert) ?;
114+ write_cert ( & tls_config. certs , & certs) ?;
115+ write_cert ( & tls_config. key , & response. key ) ?;
116+ Ok ( ( ) )
117+ }
112118
119+ async fn gen_debug_certs (
120+ config : & Config ,
121+ tls_config : & TlsConfig ,
122+ alt_names : Vec < String > ,
123+ ) -> Result < ( ) > {
113124 let kms_url = config. kms_url . clone ( ) ;
114125 if kms_url. is_empty ( ) {
115126 info ! ( "KMS URL is empty, skipping cert generation" ) ;
116127 return Ok ( ( ) ) ;
117128 }
118129 let kms_url = format ! ( "{kms_url}/prpc" ) ;
119- info ! ( "Getting CA cert from {kms_url}" ) ;
130+ info ! ( "Getting temp CA cert from {kms_url}" ) ;
120131 let client = RaClient :: new ( kms_url, true ) . context ( "Failed to create kms client" ) ?;
121132 let client = dstack_kms_rpc:: kms_client:: KmsClient :: new ( client) ;
122- let ca_cert = client. get_meta ( ) . await ?. ca_cert ;
133+ let temp_ca_response = client. get_temp_ca_cert ( ) . await ?;
134+ let ca_cert = temp_ca_response. temp_ca_cert . clone ( ) ;
135+ let temp_ca =
136+ ra_tls:: cert:: CaCert :: new ( temp_ca_response. temp_ca_cert , temp_ca_response. temp_ca_key )
137+ . context ( "Failed to create temp CA" ) ?;
123138 let key = ra_tls:: rcgen:: KeyPair :: generate ( ) . context ( "Failed to generate key" ) ?;
124- let cert = ra_tls:: cert:: CertRequest :: builder ( )
139+ let cert_req = ra_tls:: cert:: CertRequest :: builder ( )
125140 . key ( & key)
126141 . subject ( "dstack-gateway" )
127142 . alt_names ( & alt_names)
128143 . usage_server_auth ( true )
129- . build ( )
130- . self_signed ( )
131- . context ( "Failed to self-sign rpc cert" ) ?;
144+ . usage_client_auth ( true )
145+ . build ( ) ;
146+ let cert = temp_ca
147+ . sign ( cert_req)
148+ . context ( "Failed to sign rpc cert with temp CA" ) ?;
132149
133150 write_cert ( & tls_config. mutual . ca_certs , & ca_cert) ?;
134151 write_cert ( & tls_config. certs , & cert. pem ( ) ) ?;
0 commit comments