@@ -11,12 +11,12 @@ use serde_json;
11
11
#[ derive( Clone ) ]
12
12
pub struct OCIRegistryProvider {
13
13
pub registry : String ,
14
- pub username : String ,
15
- pub password : String ,
14
+ pub username : Option < String > ,
15
+ pub password : Option < String > ,
16
16
}
17
17
18
18
impl OCIRegistryProvider {
19
- pub fn new ( registry : String , username : String , password : String ) -> Self {
19
+ pub fn new ( registry : String , username : Option < String > , password : Option < String > ) -> Self {
20
20
OCIRegistryProvider {
21
21
registry,
22
22
username,
@@ -29,8 +29,7 @@ impl OCIRegistryProvider {
29
29
module : & ModuleResp ,
30
30
zip_base64 : & String ,
31
31
) -> anyhow:: Result < ( ) , anyhow:: Error > {
32
- let client = Client :: default ( ) ;
33
- let auth = RegistryAuth :: Basic ( self . username . clone ( ) , self . password . clone ( ) ) ;
32
+ let ( client, auth) = self . get_client_auth ( ) ;
34
33
let full_path = format ! (
35
34
"{}:{}" ,
36
35
self . registry,
@@ -83,8 +82,7 @@ impl OCIRegistryProvider {
83
82
}
84
83
85
84
pub async fn get_oci ( & self , oci_path : & str ) -> anyhow:: Result < ModuleResp , anyhow:: Error > {
86
- let client = Client :: default ( ) ;
87
- let auth = RegistryAuth :: Basic ( self . username . clone ( ) , self . password . clone ( ) ) ;
85
+ let ( client, auth) = self . get_client_auth ( ) ;
88
86
89
87
let oci_path = self . registry . clone ( ) + "/" + oci_path;
90
88
@@ -123,4 +121,17 @@ impl OCIRegistryProvider {
123
121
124
122
Ok ( module. clone ( ) )
125
123
}
124
+
125
+ fn get_client_auth ( & self ) -> ( Client , RegistryAuth ) {
126
+ let auth = match & self . username {
127
+ None => RegistryAuth :: Anonymous ,
128
+ Some ( username) => {
129
+ if self . password . is_none ( ) || self . password . as_ref ( ) . unwrap ( ) . is_empty ( ) {
130
+ panic ! ( "Password is required for authenticated push" ) ;
131
+ }
132
+ RegistryAuth :: Basic ( username. clone ( ) , self . password . clone ( ) . unwrap ( ) )
133
+ }
134
+ } ;
135
+ ( Client :: default ( ) , auth)
136
+ }
126
137
}
0 commit comments