public inbox for pmg-devel@lists.proxmox.com
 help / color / mirror / Atom feed
* [pmg-devel] [PATCH pmg-api] utils: check if file changed before reusing its hash
@ 2023-08-31 13:33 Maximiliano Sandoval
  2023-08-31 14:00 ` Thomas Lamprecht
  2023-08-31 14:17 ` Fabian Grünbichler
  0 siblings, 2 replies; 3+ messages in thread
From: Maximiliano Sandoval @ 2023-08-31 13:33 UTC (permalink / raw)
  To: pmg-devel

We cache the hash of this file, it makes sense to first check if the
file changed via `stat` and recompute the hash if needed.

Signed-off-by: Maximiliano Sandoval <m.sandoval@proxmox.com>
---
 src/PMG/Utils.pm | 28 ++++++++++++++++++++++++----
 1 file changed, 24 insertions(+), 4 deletions(-)

diff --git a/src/PMG/Utils.pm b/src/PMG/Utils.pm
index c19b31f..f8e6b7c 100644
--- a/src/PMG/Utils.pm
+++ b/src/PMG/Utils.pm
@@ -49,6 +49,8 @@ postgres_admin_cmd
 try_decode_utf8
 );
 
+my $host_rsa_key_path = '/etc/ssh/ssh_host_rsa_key.pub';
+
 my $valid_pmg_realms = ['pam', 'pmg', 'quarantine'];
 
 PVE::JSONSchema::register_standard_option('realm', {
@@ -1353,14 +1355,32 @@ sub scan_journal_for_rbl_rejects {
 }
 
 my $hwaddress;
+my $hwaddress_st = {};
+
+sub get_server_id {
+    my $sshkey = PVE::Tools::file_get_contents($host_rsa_key_path);
+    return uc(Digest::MD5::md5_hex($sshkey));
+}
 
 sub get_hwaddress {
+    my $st = stat($host_rsa_key_path);
 
-    return $hwaddress if defined ($hwaddress);
+    if (! defined($hwaddress)) {
+	$hwaddress_st->{mtime} = $st->mtime;
+	$hwaddress_st->{ino} = $st->ino;
+	$hwaddress_st->{dev} = $st->dev;
+	$hwaddress = get_server_id();
+    }
+
+    if ($hwaddress_st->{mtime} != $st->mtime
+	|| $hwaddress_st->{ino} != $st->ino
+	|| $hwaddress_st->{dev} != $st->dev) {
+	$hwaddress_st->{mtime} = $st->mtime;
+	$hwaddress_st->{ino} = $st->ino;
+	$hwaddress_st->{dev} = $st->dev;
 
-    my $fn = '/etc/ssh/ssh_host_rsa_key.pub';
-    my $sshkey = PVE::Tools::file_get_contents($fn);
-    $hwaddress = uc(Digest::MD5::md5_hex($sshkey));
+	$hwaddress = get_server_id();
+    }
 
     return $hwaddress;
 }
-- 
2.39.2





^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-08-31 14:18 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-08-31 13:33 [pmg-devel] [PATCH pmg-api] utils: check if file changed before reusing its hash Maximiliano Sandoval
2023-08-31 14:00 ` Thomas Lamprecht
2023-08-31 14:17 ` Fabian Grünbichler

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal