From 5f9ba5b51cd2cf995e075356c4bd5e37767de123 Mon Sep 17 00:00:00 2001
From: Alexander Olofsson <alexander.olofsson@liu.se>
Date: Mon, 20 May 2024 16:23:43 +0200
Subject: [PATCH] Ensure stored UUIDs are of correct format

---
 app/models/foreman_uuid_boot/uuidboot_host_facet.rb | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/app/models/foreman_uuid_boot/uuidboot_host_facet.rb b/app/models/foreman_uuid_boot/uuidboot_host_facet.rb
index 49cb8b7..b5340a0 100644
--- a/app/models/foreman_uuid_boot/uuidboot_host_facet.rb
+++ b/app/models/foreman_uuid_boot/uuidboot_host_facet.rb
@@ -4,16 +4,19 @@ module ForemanUuidBoot
   class UuidbootHostFacet < ApplicationRecord
     include Facets::Base
 
+    UUID_REX = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/.freeze
+
     def self.populate_fields_from_facts(host, parser, _type, _proxy)
       uuid = parser.facts.dig('dmi', 'product', 'uuid') || parser.facts['uuid']
 
-      if uuid
+      if uuid && uuid =~ UUID_REX
         facet = host.uuidboot_facet || host.build_uuidboot_facet
         facet.uuid = uuid
         facet.save if facet.changed? || !facet.persisted?
         facet
       else
         host.uuidboot_facet&.destroy
+        host.uuidboot_facet = nil
         nil
       end
     end
-- 
GitLab