Skip to content

Commit e574519

Browse files
committed
fix: allow anonymous user to enable local push
1 parent 679afe9 commit e574519

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

env_common/src/interface/cloud_handlers.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,14 @@ impl GenericCloudHandler {
101101
};
102102
let oci_registry = match std::env::var("OCI_REGISTRY_URI") {
103103
Ok(oci_registry) => {
104-
let oci_username = std::env::var("OCI_REGISTRY_USERNAME")
105-
.expect("OCI_REGISTRY_USERNAME environment variable is not set");
106-
let oci_password = std::env::var("OCI_REGISTRY_PASSWORD")
107-
.expect("OCI_REGISTRY_PASSWORD environment variable is not set");
104+
let oci_username = match std::env::var("OCI_REGISTRY_USERNAME") {
105+
Ok(username) => Some(username),
106+
Err(_) => None,
107+
};
108+
let oci_password = match std::env::var("OCI_REGISTRY_PASSWORD") {
109+
Ok(password) => Some(password),
110+
Err(_) => None,
111+
};
108112
Some(OCIRegistryProvider::new(
109113
oci_registry,
110114
oci_username,

env_common/src/logic/api_oci_registry.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ use serde_json;
1111
#[derive(Clone)]
1212
pub struct OCIRegistryProvider {
1313
pub registry: String,
14-
pub username: String,
15-
pub password: String,
14+
pub username: Option<String>,
15+
pub password: Option<String>,
1616
}
1717

1818
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 {
2020
OCIRegistryProvider {
2121
registry,
2222
username,
@@ -29,8 +29,7 @@ impl OCIRegistryProvider {
2929
module: &ModuleResp,
3030
zip_base64: &String,
3131
) -> 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();
3433
let full_path = format!(
3534
"{}:{}",
3635
self.registry,
@@ -83,8 +82,7 @@ impl OCIRegistryProvider {
8382
}
8483

8584
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();
8886

8987
let oci_path = self.registry.clone() + "/" + oci_path;
9088

@@ -123,4 +121,17 @@ impl OCIRegistryProvider {
123121

124122
Ok(module.clone())
125123
}
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+
}
126137
}

0 commit comments

Comments
 (0)