@@ -15,8 +15,6 @@ def override(func): # type: ignore[reportMissingParameterType]
1515
1616
1717import utils
18- from internalapi .sensor .collector_pb2 import ProcessSignal
19- from internalapi .sensor .sfa_pb2 import FileActivity
2018
2119
2220def extract_container_id (cgroup : str ) -> str :
@@ -191,25 +189,26 @@ def container_id(self) -> str:
191189 def loginuid (self ) -> int :
192190 return self ._loginuid
193191
194- def diff (self , other : ProcessSignal ) -> dict | None :
192+ def diff (self , other : Process ) -> dict | None :
195193 """
196- Compare this Process with a ProcessSignal protobuf message.
194+ Compare this Process with another Process instance.
195+
196+ PID comparison is skipped if self.pid is None.
197197
198198 Args:
199- other: ProcessSignal protobuf message to compare against
199+ other: Process instance to compare against.
200200
201201 Returns:
202- None if identical, dict of differences if not matching
202+ None if identical, dict of differences if not matching.
203203 """
204204 diff = {}
205205
206- # Compare each field
207206 if self .pid is not None :
208207 Event ._diff_field (diff , 'pid' , self .pid , other .pid )
209208
210209 Event ._diff_field (diff , 'uid' , self .uid , other .uid )
211210 Event ._diff_field (diff , 'gid' , self .gid , other .gid )
212- Event ._diff_field (diff , 'exe_path' , self .exe_path , other .exec_file_path )
211+ Event ._diff_field (diff , 'exe_path' , self .exe_path , other .exe_path )
213212 Event ._diff_field (diff , 'args' , self .args , other .args )
214213 Event ._diff_field (diff , 'name' , self .name , other .name )
215214 Event ._diff_field (
@@ -218,7 +217,7 @@ def diff(self, other: ProcessSignal) -> dict | None:
218217 self .container_id ,
219218 other .container_id ,
220219 )
221- Event ._diff_field (diff , 'loginuid' , self .loginuid , other .login_uid )
220+ Event ._diff_field (diff , 'loginuid' , self .loginuid , other .loginuid )
222221
223222 return diff if diff else None
224223
@@ -328,128 +327,91 @@ def _diff_path(
328327 diff : dict ,
329328 name : str ,
330329 expected : str | Pattern [str ] | None ,
331- actual : str ,
330+ actual : str | Pattern [ str ] | None ,
332331 ):
333332 """
334333 Compare paths with regex pattern support.
334+
335+ When expected is a compiled regex pattern, actual must be a
336+ string that matches it. Otherwise a simple equality check is
337+ performed.
335338 """
336339 if isinstance (expected , Pattern ):
337- if not expected .match (actual ):
340+ if not isinstance ( actual , str ) or not expected .match (actual ):
338341 diff [name ] = {'expected' : f'{ expected } ' , 'actual' : actual }
339342 elif expected != actual :
340343 diff [name ] = {'expected' : expected , 'actual' : actual }
341344
342- def diff (self , other : FileActivity ) -> dict | None :
345+ def diff (self , other : Event ) -> dict | None :
343346 """
344- Compare this Event with a FileActivity protobuf message.
347+ Compare this Event with another Event instance.
348+
349+ Both gRPC and OTLP servers translate their native messages
350+ into Event objects, so this method provides a single
351+ protocol-agnostic comparison path.
345352
346353 Args:
347- other: FileActivity protobuf message to compare against
354+ other: Event instance to compare against.
348355
349356 Returns:
350- None if identical, dict of differences if not matching
357+ None if identical, dict of differences if not matching.
351358 """
352359 diff = {}
353360
354- # Check process differences first
355361 process_diff = self .process .diff (other .process )
356362 if process_diff is not None :
357363 diff ['process' ] = process_diff
358364
359- # Check event type
360- event_type_expected = self .event_type .name .lower ()
361- event_type_actual = other .WhichOneof ('file' )
362-
363365 Event ._diff_field (
364366 diff ,
365367 'event_type' ,
366- event_type_expected ,
367- event_type_actual ,
368+ self . event_type ,
369+ other . event_type ,
368370 )
369371 if diff :
370372 return diff
371373
372- # Get the appropriate event field based on type
373- event_field = getattr (other , event_type_expected )
374-
375374 # Rename handling is a bit different to the rest, since it has
376375 # new and old paths.
377- if self .event_type == EventType .RENAME :
378- Event ._diff_path (diff , 'new_file' , self .file , event_field .new .path )
376+ if self .event_type != EventType .RENAME :
377+ Event ._diff_path (diff , 'file' , self .file , other .file )
378+ Event ._diff_path (diff , 'host_path' , self .host_path , other .host_path )
379+ else :
380+ Event ._diff_path (diff , 'new_file' , self .file , other .file )
379381 Event ._diff_path (
380- diff ,
381- 'new_host_path' ,
382- self .host_path ,
383- event_field .new .host_path ,
382+ diff , 'new_host_path' , self .host_path , other .host_path
384383 )
384+ Event ._diff_path (diff , 'old_file' , self .old_file , other .old_file )
385385 Event ._diff_path (
386- diff ,
387- 'old_file' ,
388- self .old_file ,
389- event_field .old .path ,
386+ diff , 'old_host_path' , self .old_host_path , other .old_host_path
390387 )
391- Event ._diff_path (
392- diff ,
393- 'old_host_path' ,
394- self .old_host_path ,
395- event_field .old .host_path ,
396- )
397- return diff if diff else None
398-
399- # Compare file and host_path (common to all event types)
400- # All event types have .activity.path and .activity.host_path
401- # accessed differently
402- Event ._diff_path (diff , 'file' , self .file , event_field .activity .path )
403- Event ._diff_path (
404- diff ,
405- 'host_path' ,
406- self .host_path ,
407- event_field .activity .host_path ,
408- )
409388
410389 if self .event_type == EventType .PERMISSION :
411- Event ._diff_field (diff , 'mode' , self .mode , event_field .mode )
390+ Event ._diff_field (diff , 'mode' , self .mode , other .mode )
412391 elif self .event_type == EventType .OWNERSHIP :
413392 Event ._diff_field (
414- diff ,
415- 'owner_uid' ,
416- self .owner_uid ,
417- event_field .uid ,
393+ diff , 'owner_uid' , self .owner_uid , other .owner_uid
418394 )
419395 Event ._diff_field (
420- diff ,
421- 'owner_gid' ,
422- self .owner_gid ,
423- event_field .gid ,
396+ diff , 'owner_gid' , self .owner_gid , other .owner_gid
424397 )
425398 elif self .event_type in (EventType .XATTR_SET , EventType .XATTR_REMOVE ):
426399 Event ._diff_field (
427- diff ,
428- 'xattr_name' ,
429- self .xattr_name ,
430- event_field .xattr_name ,
400+ diff , 'xattr_name' , self .xattr_name , other .xattr_name
431401 )
432402 elif self .event_type == EventType .ACL :
433403 Event ._diff_field (
434404 diff ,
435405 'acl_type' ,
436406 self .acl_type ,
437- event_field .acl_type ,
407+ other .acl_type ,
438408 )
439409 if self .acl_entries is not None :
440- actual_entries = [
441- {
442- 'tag' : e .tag ,
443- 'perm' : e .perm ,
444- 'id' : e .id ,
445- }
446- for e in event_field .entries
447- ]
448410 Event ._diff_field (
449411 diff ,
450412 'acl_entries' ,
451413 self .acl_entries ,
452- actual_entries ,
414+ other . acl_entries ,
453415 )
454416
455417 return diff if diff else None
0 commit comments