fn get_credentials()

in enclave_build/src/docker.rs [86:137]


    fn get_credentials(&self) -> Result<DockerCredentials, DockerError> {
        let host = match Self::parse_docker_host(&self.docker_image) {
            Some(host) => host,
            None => return Err(CredentialsError("Invalid docker image URI!".to_string())),
        };

        let config_file = self.get_config_file()?;

        let config_json: serde_json::Value = serde_json::from_reader(&config_file)
            .map_err(|err| CredentialsError(format!("JSON was not well-formatted: {err}")))?;

        let auths = config_json.get("auths").ok_or_else(|| {
            CredentialsError("Could not find auths key in config JSON".to_string())
        })?;

        if let Value::Object(auths) = auths {
            for (registry_name, registry_auths) in auths.iter() {
                if !registry_name.to_string().contains(&host) {
                    continue;
                }

                let auth = registry_auths
                    .get("auth")
                    .ok_or_else(|| {
                        CredentialsError("Could not find auth key in config JSON".to_string())
                    })?
                    .to_string();

                let auth = auth.replace('"', "");
                let decoded = general_purpose::STANDARD.decode(auth).map_err(|err| {
                    CredentialsError(format!("Invalid Base64 encoding for auth: {err}"))
                })?;
                let decoded = std::str::from_utf8(&decoded).map_err(|err| {
                    CredentialsError(format!("Invalid utf8 encoding for auth: {err}"))
                })?;

                if let Some(index) = decoded.rfind(':') {
                    let (user, after_user) = decoded.split_at(index);
                    let (_, password) = after_user.split_at(1);
                    return Ok(DockerCredentials {
                        username: Some(user.to_string()),
                        password: Some(password.to_string()),
                        ..Default::default()
                    });
                }
            }
        }

        Err(CredentialsError(
            "No credentials found for the current image".to_string(),
        ))
    }