From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <f.ebner@proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by lists.proxmox.com (Postfix) with ESMTPS id D0D0371CE9
 for <pve-devel@lists.proxmox.com>; Wed, 30 Jun 2021 12:20:25 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id CE8ED18627
 for <pve-devel@lists.proxmox.com>; Wed, 30 Jun 2021 12:20:25 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com
 [94.136.29.106])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by firstgate.proxmox.com (Proxmox) with ESMTPS id AE8E218604
 for <pve-devel@lists.proxmox.com>; Wed, 30 Jun 2021 12:20:23 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1])
 by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 83D99430C7
 for <pve-devel@lists.proxmox.com>; Wed, 30 Jun 2021 12:20:23 +0200 (CEST)
From: Fabian Ebner <f.ebner@proxmox.com>
To: pve-devel@lists.proxmox.com
Date: Wed, 30 Jun 2021 12:20:17 +0200
Message-Id: <20210630102019.39057-3-f.ebner@proxmox.com>
X-Mailer: git-send-email 2.30.2
In-Reply-To: <20210630102019.39057-1-f.ebner@proxmox.com>
References: <20210630102019.39057-1-f.ebner@proxmox.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL 0.628 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
 URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See
 http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more
 information. [proxmox.com, repository.rs, repositories.rs, file.rs]
Subject: [pve-devel] [PATCH proxmox-apt 2/3] check: return 'origin' property
 instead of 'badge' for official host
X-BeenThere: pve-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/>
List-Post: <mailto:pve-devel@lists.proxmox.com>
List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Wed, 30 Jun 2021 10:20:25 -0000

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>
---
 src/repositories/file.rs       | 17 +++++++++++++----
 src/repositories/repository.rs | 20 +++++++++++---------
 tests/repositories.rs          | 21 +++++++++++++++------
 3 files changed, 39 insertions(+), 19 deletions(-)

diff --git a/src/repositories/file.rs b/src/repositories/file.rs
index 6225f1c..447fd0a 100644
--- a/src/repositories/file.rs
+++ b/src/repositories/file.rs
@@ -373,13 +373,22 @@ impl APTRepositoryFile {
         let mut infos = vec![];
 
         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 {
                     path: self.path.clone(),
                     index: n,
-                    kind: "badge".to_string(),
-                    property: Some("URIs".to_string()),
-                    message: "official host name".to_string(),
+                    kind: "origin".to_string(),
+                    property: None,
+                    message: origin,
                 });
             }
         }
diff --git a/src/repositories/repository.rs b/src/repositories/repository.rs
index 5eba068..cf17380 100644
--- a/src/repositories/repository.rs
+++ b/src/repositories/repository.rs
@@ -288,21 +288,23 @@ impl APTRepository {
             .any(|suite| suite_variant(suite).0 == base_suite)
     }
 
-    /// Checks if an official host is configured in the repository.
-    pub fn has_official_uri(&self) -> bool {
+    /// Guess the origin from the repository's URIs.
+    ///
+    /// 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() {
             if let Some(host) = host_from_uri(uri) {
-                if host == "proxmox.com"
-                    || host.ends_with(".proxmox.com")
-                    || host == "debian.org"
-                    || host.ends_with(".debian.org")
-                {
-                    return true;
+                if host == "proxmox.com" || host.ends_with(".proxmox.com") {
+                    return Some("Proxmox".to_string());
+                }
+
+                if host == "debian.org" || host.ends_with(".debian.org") {
+                    return Some("Debian".to_string());
                 }
             }
         }
 
-        false
+        None
     }
 
     /// Get the `Origin:` value from a cached InRelease file.
diff --git a/tests/repositories.rs b/tests/repositories.rs
index 4067e60..3265bce 100644
--- a/tests/repositories.rs
+++ b/tests/repositories.rs
@@ -171,6 +171,11 @@ fn test_check_repositories() -> Result<(), Error> {
     let test_dir = std::env::current_dir()?.join("tests");
     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 mut file = APTRepositoryFile::new(&absolute_suite_list)?.unwrap();
     file.parse()?;
@@ -184,14 +189,18 @@ fn test_check_repositories() -> Result<(), Error> {
 
     let path_string = pve_list.into_os_string().into_string().unwrap();
 
+    let origins = [
+        "Debian", "Debian", "Proxmox", "Proxmox", "Proxmox", "Debian",
+    ];
+
     let mut expected_infos = vec![];
     for n in 0..=5 {
         expected_infos.push(APTRepositoryInfo {
             path: path_string.clone(),
             index: n,
-            property: Some("URIs".to_string()),
-            kind: "badge".to_string(),
-            message: "official host name".to_string(),
+            property: None,
+            kind: "origin".to_string(),
+            message: origins[n].to_string(),
         });
     }
     expected_infos.sort();
@@ -255,9 +264,9 @@ fn test_check_repositories() -> Result<(), Error> {
         expected_infos.push(APTRepositoryInfo {
             path: path_string.clone(),
             index: n,
-            property: Some("URIs".to_string()),
-            kind: "badge".to_string(),
-            message: "official host name".to_string(),
+            property: None,
+            kind: "origin".to_string(),
+            message: "Debian".to_string(),
         });
     }
     expected_infos.sort();
-- 
2.30.2