use anyhow::Result;
use async_trait::async_trait;
use crate::server::registry::{
models::{OciClientAuthConfig, RegistryCredentials},
ImageTagType, RegistryProvider,
};
pub struct OciClientAuthProvider {
#[allow(dead_code)]
config: OciClientAuthConfig,
registry_url: String,
registry_host: String,
client_registry_url: String,
}
impl OciClientAuthProvider {
pub fn new(config: OciClientAuthConfig) -> Result<Self> {
let registry_host = config
.registry_url
.trim_start_matches("https://")
.trim_start_matches("http://")
.split('/')
.next()
.unwrap_or(&config.registry_url)
.to_string();
let namespace = config.namespace.trim_end_matches('/');
let registry_url = if namespace.is_empty() {
config.registry_url.trim_end_matches('/').to_string()
} else {
format!(
"{}/{}",
config.registry_url.trim_end_matches('/'),
namespace
)
};
let client_base = config
.client_registry_url
.as_ref()
.unwrap_or(&config.registry_url);
let client_registry_url = if namespace.is_empty() {
client_base.trim_end_matches('/').to_string()
} else {
format!("{}/{}", client_base.trim_end_matches('/'), namespace)
};
Ok(Self {
config,
registry_url,
registry_host,
client_registry_url,
})
}
}
#[async_trait]
impl RegistryProvider for OciClientAuthProvider {
async fn get_credentials(&self, repository: &str) -> Result<RegistryCredentials> {
tracing::info!("Returning OCI registry info for repository: {}", repository);
Ok(RegistryCredentials {
registry_url: self.client_registry_url.clone(),
username: String::new(), password: String::new(), expires_in: None,
})
}
async fn get_pull_credentials(&self) -> Result<(String, String)> {
Ok((String::new(), String::new()))
}
fn registry_host(&self) -> &str {
&self.registry_host
}
fn registry_url(&self) -> &str {
&self.registry_url
}
fn get_image_tag(&self, repository: &str, tag: &str, tag_type: ImageTagType) -> String {
let registry_url = match tag_type {
ImageTagType::ClientFacing => &self.client_registry_url,
ImageTagType::Internal => &self.registry_url,
};
format!("{}/{}:{}", registry_url, repository, tag)
}
}