check: return 'origin' property instead of 'badge' for official host

which is obtained from the cached InRelease file and also works for
mirrors, host aliases, direct IPs.

The has_official_uri function was replaced by origin_from_uris.

Signed-off-by: Fabian Ebner <f.ebner@proxmox.com>
This commit is contained in:
Fabian Ebner 2021-06-30 12:20:17 +02:00 committed by Thomas Lamprecht
parent bb0ff2ac73
commit 87ea23ec83
3 changed files with 39 additions and 19 deletions

View File

@ -373,13 +373,22 @@ impl APTRepositoryFile {
let mut infos = vec![]; let mut infos = vec![];
for (n, repo) in self.repositories.iter().enumerate() { for (n, repo) in self.repositories.iter().enumerate() {
if repo.has_official_uri() { let mut origin = match repo.get_cached_origin() {
Ok(option) => option,
Err(_) => None,
};
if origin.is_none() {
origin = repo.origin_from_uris();
}
if let Some(origin) = origin {
infos.push(APTRepositoryInfo { infos.push(APTRepositoryInfo {
path: self.path.clone(), path: self.path.clone(),
index: n, index: n,
kind: "badge".to_string(), kind: "origin".to_string(),
property: Some("URIs".to_string()), property: None,
message: "official host name".to_string(), message: origin,
}); });
} }
} }

View File

@ -288,21 +288,23 @@ impl APTRepository {
.any(|suite| suite_variant(suite).0 == base_suite) .any(|suite| suite_variant(suite).0 == base_suite)
} }
/// Checks if an official host is configured in the repository. /// Guess the origin from the repository's URIs.
pub fn has_official_uri(&self) -> bool { ///
/// Intended to be used as a fallback for get_cached_origin.
pub fn origin_from_uris(&self) -> Option<String> {
for uri in self.uris.iter() { for uri in self.uris.iter() {
if let Some(host) = host_from_uri(uri) { if let Some(host) = host_from_uri(uri) {
if host == "proxmox.com" if host == "proxmox.com" || host.ends_with(".proxmox.com") {
|| host.ends_with(".proxmox.com") return Some("Proxmox".to_string());
|| host == "debian.org" }
|| host.ends_with(".debian.org")
{ if host == "debian.org" || host.ends_with(".debian.org") {
return true; return Some("Debian".to_string());
} }
} }
} }
false None
} }
/// Get the `Origin:` value from a cached InRelease file. /// Get the `Origin:` value from a cached InRelease file.

View File

@ -171,6 +171,11 @@ fn test_check_repositories() -> Result<(), Error> {
let test_dir = std::env::current_dir()?.join("tests"); let test_dir = std::env::current_dir()?.join("tests");
let read_dir = test_dir.join("sources.list.d"); let read_dir = test_dir.join("sources.list.d");
proxmox_apt::config::init(APTConfig::new(
Some(&test_dir.into_os_string().into_string().unwrap()),
None,
));
let absolute_suite_list = read_dir.join("absolute_suite.list"); let absolute_suite_list = read_dir.join("absolute_suite.list");
let mut file = APTRepositoryFile::new(&absolute_suite_list)?.unwrap(); let mut file = APTRepositoryFile::new(&absolute_suite_list)?.unwrap();
file.parse()?; file.parse()?;
@ -184,14 +189,18 @@ fn test_check_repositories() -> Result<(), Error> {
let path_string = pve_list.into_os_string().into_string().unwrap(); let path_string = pve_list.into_os_string().into_string().unwrap();
let origins = [
"Debian", "Debian", "Proxmox", "Proxmox", "Proxmox", "Debian",
];
let mut expected_infos = vec![]; let mut expected_infos = vec![];
for n in 0..=5 { for n in 0..=5 {
expected_infos.push(APTRepositoryInfo { expected_infos.push(APTRepositoryInfo {
path: path_string.clone(), path: path_string.clone(),
index: n, index: n,
property: Some("URIs".to_string()), property: None,
kind: "badge".to_string(), kind: "origin".to_string(),
message: "official host name".to_string(), message: origins[n].to_string(),
}); });
} }
expected_infos.sort(); expected_infos.sort();
@ -255,9 +264,9 @@ fn test_check_repositories() -> Result<(), Error> {
expected_infos.push(APTRepositoryInfo { expected_infos.push(APTRepositoryInfo {
path: path_string.clone(), path: path_string.clone(),
index: n, index: n,
property: Some("URIs".to_string()), property: None,
kind: "badge".to_string(), kind: "origin".to_string(),
message: "official host name".to_string(), message: "Debian".to_string(),
}); });
} }
expected_infos.sort(); expected_infos.sort();