]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
mgr/telemetry: add anonymize_entity_name function
authorYaarit Hatuka <yaarit@redhat.com>
Sat, 9 Apr 2022 00:33:04 +0000 (20:33 -0400)
committerYaarit Hatuka <yaarit@redhat.com>
Sat, 9 Apr 2022 01:44:29 +0000 (21:44 -0400)
The ability to anonymize entity names should have its own function
to prevent duplicate code.
Will clean up in a separate commit.

Signed-off-by: Yaarit Hatuka <yaarit@redhat.com>
src/pybind/mgr/telemetry/module.py

index a96e6155a0a413fefcd99f5338bcff5a42efbedb..3b49bd6093cecbb6e3cb608c02fdeea685392a96 100644 (file)
@@ -28,6 +28,7 @@ ALL_CHANNELS = ['basic', 'ident', 'crash', 'device', 'perf']
 LICENSE = 'sharing-1-0'
 LICENSE_NAME = 'Community Data License Agreement - Sharing - Version 1.0'
 LICENSE_URL = 'https://cdla.io/sharing-1-0/'
+NO_SALT_CNT = 0
 
 # Latest revision of the telemetry report.  Bump this each time we make
 # *any* change.
@@ -407,6 +408,28 @@ class Module(MgrModule):
             'active_changed': sorted(list(active)),
         }
 
+    def anonymize_entity_name(self, entity_name:str) -> str:
+        if '.' not in entity_name:
+            self.log.debug(f"Cannot split entity name ({entity_name}), no '.' is found")
+            return entity_name
+
+        (etype, eid) = entity_name.split('.', 1)
+        m = hashlib.sha1()
+        salt = ''
+        if self.salt is not None:
+            salt = self.salt
+        # avoid asserting that salt exists
+        if not self.salt:
+            # do not set self.salt to a temp value
+            salt = f"no_salt_found_{NO_SALT_CNT}"
+            NO_SALT_CNT += 1
+            self.log.debug(f"No salt found, created a temp one: {salt}")
+        m.update(salt.encode('utf-8'))
+        m.update(eid.encode('utf-8'))
+        m.update(salt.encode('utf-8'))
+
+        return  etype + '.' + m.hexdigest()
+
     def get_heap_stats(self) -> Dict[str, dict]:
         # Initialize result dict
         result: Dict[str, dict] = defaultdict(lambda: defaultdict(int))