@@ -3,7 +3,7 @@ use base64::{
33 engine:: { DecodePaddingMode , GeneralPurpose , GeneralPurposeConfig } ,
44 Engine ,
55} ;
6- use oci_client:: client:: ClientConfig ;
6+ use oci_client:: client:: { Certificate , CertificateEncoding , ClientConfig } ;
77use secrecy:: { ExposeSecret , SecretString } ;
88use serde:: { Deserialize , Serialize , Serializer } ;
99use wasm_pkg_common:: { config:: RegistryConfig , Error } ;
@@ -50,8 +50,12 @@ impl TryFrom<&RegistryConfig> for OciRegistryConfig {
5050 type Error = Error ;
5151
5252 fn try_from ( registry_config : & RegistryConfig ) -> Result < Self , Self :: Error > {
53- let OciRegistryConfigToml { auth, protocol } =
54- registry_config. backend_config ( "oci" ) ?. unwrap_or_default ( ) ;
53+ let OciRegistryConfigToml {
54+ auth,
55+ protocol,
56+ accept_invalid_certificates,
57+ extra_root_certificates,
58+ } = registry_config. backend_config ( "oci" ) ?. unwrap_or_default ( ) ;
5559 let mut client_config = ClientConfig :: default ( ) ;
5660 if let Some ( protocol) = protocol {
5761 client_config. protocol = oci_client_protocol ( & protocol) ?;
@@ -60,6 +64,12 @@ impl TryFrom<&RegistryConfig> for OciRegistryConfig {
6064 . map ( TryInto :: try_into)
6165 . transpose ( )
6266 . map_err ( Error :: InvalidConfig ) ?;
67+ client_config. accept_invalid_certificates = accept_invalid_certificates;
68+ client_config. extra_root_certificates = extra_root_certificates
69+ . into_iter ( )
70+ . map ( TryInto :: try_into)
71+ . collect :: < Result < Vec < _ > , _ > > ( )
72+ . map_err ( Error :: InvalidConfig ) ?;
6373 Ok ( Self {
6474 client_config,
6575 credentials,
@@ -71,16 +81,31 @@ impl TryFrom<&RegistryConfig> for OciRegistryConfig {
7181struct OciRegistryConfigToml {
7282 auth : Option < TomlAuth > ,
7383 protocol : Option < String > ,
84+ #[ serde( default ) ]
85+ accept_invalid_certificates : bool ,
86+ #[ serde( default ) ]
87+ extra_root_certificates : Vec < TomlCertificate > ,
7488}
7589
7690impl From < OciRegistryConfig > for OciRegistryConfigToml {
7791 fn from ( value : OciRegistryConfig ) -> Self {
92+ let OciRegistryConfig {
93+ client_config,
94+ credentials,
95+ } = value;
96+
7897 OciRegistryConfigToml {
79- auth : value . credentials . map ( |c| TomlAuth :: UsernamePassword {
98+ auth : credentials. map ( |c| TomlAuth :: UsernamePassword {
8099 username : c. username ,
81100 password : c. password ,
82101 } ) ,
83- protocol : Some ( oci_protocol_string ( & value. client_config . protocol ) ) ,
102+ protocol : Some ( oci_protocol_string ( & client_config. protocol ) ) ,
103+ accept_invalid_certificates : client_config. accept_invalid_certificates ,
104+ extra_root_certificates : client_config
105+ . extra_root_certificates
106+ . into_iter ( )
107+ . map ( Into :: into)
108+ . collect ( ) ,
84109 }
85110 }
86111}
@@ -161,6 +186,47 @@ fn serialize_secret<S: Serializer>(
161186 secret. expose_secret ( ) . serialize ( serializer)
162187}
163188
189+ #[ derive( Clone , Deserialize , Serialize ) ]
190+ #[ serde( rename_all = "lowercase" ) ]
191+ enum TomlCertificateEncoding {
192+ Der ,
193+ Pem ,
194+ }
195+
196+ #[ derive( Clone , Deserialize , Serialize ) ]
197+ struct TomlCertificate {
198+ encoding : TomlCertificateEncoding ,
199+ data : String ,
200+ }
201+
202+ impl TryFrom < TomlCertificate > for Certificate {
203+ type Error = anyhow:: Error ;
204+
205+ fn try_from ( value : TomlCertificate ) -> Result < Self , Self :: Error > {
206+ let ( encoding, data) = match value. encoding {
207+ TomlCertificateEncoding :: Der => ( CertificateEncoding :: Der , value. data . into_bytes ( ) ) ,
208+ TomlCertificateEncoding :: Pem => ( CertificateEncoding :: Pem , value. data . into_bytes ( ) ) ,
209+ } ;
210+ Ok ( Self { encoding, data } )
211+ }
212+ }
213+
214+ impl From < Certificate > for TomlCertificate {
215+ fn from ( value : Certificate ) -> Self {
216+ let ( encoding, data) = match value. encoding {
217+ CertificateEncoding :: Der => (
218+ TomlCertificateEncoding :: Der ,
219+ String :: from_utf8_lossy ( & value. data ) . into_owned ( ) ,
220+ ) ,
221+ CertificateEncoding :: Pem => (
222+ TomlCertificateEncoding :: Pem ,
223+ String :: from_utf8_lossy ( & value. data ) . into_owned ( ) ,
224+ ) ,
225+ } ;
226+ Self { encoding, data }
227+ }
228+ }
229+
164230#[ cfg( test) ]
165231mod tests {
166232 use wasm_pkg_common:: config:: RegistryMapping ;
@@ -197,6 +263,8 @@ mod tests {
197263 oci_client:: client:: ClientProtocol :: Http ,
198264 oci_config. client_config. protocol
199265 ) ;
266+ assert ! ( !oci_config. client_config. accept_invalid_certificates) ;
267+ assert ! ( oci_config. client_config. extra_root_certificates. is_empty( ) ) ;
200268
201269 let oci_config: OciRegistryConfig = cfg
202270 . registry_config ( & "wasi.dev" . parse ( ) . unwrap ( ) )
0 commit comments