1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
use anyhow::Result;
use tracing::warn;
use wasmcloud_control_interface::RegistryCredential;
use wasmcloud_core::{RegistryAuth, RegistryConfig, RegistryType};

/// Extension trait to enable converting between registry credentials
pub trait RegistryCredentialExt {
    /// Convert a [`RegistryCredential`] to a [`RegistryConfig`]
    fn into_registry_config(self) -> Result<RegistryConfig>;
}

impl RegistryCredentialExt for RegistryCredential {
    fn into_registry_config(self) -> Result<RegistryConfig> {
        RegistryConfig::builder()
            .reg_type(match self.registry_type() {
                "oci" => RegistryType::Oci,
                registry_type => {
                    warn!(%registry_type, "unknown registry type, defaulting to OCI");
                    RegistryType::Oci
                }
            })
            .auth(match (self.username(), self.password(), self.token()) {
                (Some(username), Some(password), _) => RegistryAuth::Basic(username.into(), password.into()),
                (None, None, Some(token)) => RegistryAuth::Token(token.into()),
                (None, None, None) => RegistryAuth::Anonymous,
                (_, _, _) => {
                    warn!("invalid combination of registry credentials, defaulting to no authentication");
                    RegistryAuth::Anonymous
                }
            })
            .allow_latest(false)
            .allow_insecure(false)
            .additional_ca_paths(Vec::new())
            .build()
    }
}