@@ -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 :
@@ -40,6 +38,7 @@ def extract_container_id(cgroup: str) -> str:
4038class EventType (Enum ):
4139 """Enumeration for different types of file activity events."""
4240
41+ UNKNOWN = 0
4342 OPEN = 1
4443 CREATION = 2
4544 UNLINK = 3
@@ -191,25 +190,26 @@ def container_id(self) -> str:
191190 def loginuid (self ) -> int :
192191 return self ._loginuid
193192
194- def diff (self , other : ProcessSignal ) -> dict | None :
193+ def diff (self , other : Process ) -> dict | None :
195194 """
196- Compare this Process with a ProcessSignal protobuf message.
195+ Compare this Process with another Process instance.
196+
197+ PID comparison is skipped if self.pid is None.
197198
198199 Args:
199- other: ProcessSignal protobuf message to compare against
200+ other: Process instance to compare against.
200201
201202 Returns:
202- None if identical, dict of differences if not matching
203+ None if identical, dict of differences if not matching.
203204 """
204205 diff = {}
205206
206- # Compare each field
207207 if self .pid is not None :
208208 Event ._diff_field (diff , 'pid' , self .pid , other .pid )
209209
210210 Event ._diff_field (diff , 'uid' , self .uid , other .uid )
211211 Event ._diff_field (diff , 'gid' , self .gid , other .gid )
212- Event ._diff_field (diff , 'exe_path' , self .exe_path , other .exec_file_path )
212+ Event ._diff_field (diff , 'exe_path' , self .exe_path , other .exe_path )
213213 Event ._diff_field (diff , 'args' , self .args , other .args )
214214 Event ._diff_field (diff , 'name' , self .name , other .name )
215215 Event ._diff_field (
@@ -218,7 +218,7 @@ def diff(self, other: ProcessSignal) -> dict | None:
218218 self .container_id ,
219219 other .container_id ,
220220 )
221- Event ._diff_field (diff , 'loginuid' , self .loginuid , other .login_uid )
221+ Event ._diff_field (diff , 'loginuid' , self .loginuid , other .loginuid )
222222
223223 return diff if diff else None
224224
@@ -328,128 +328,91 @@ def _diff_path(
328328 diff : dict ,
329329 name : str ,
330330 expected : str | Pattern [str ] | None ,
331- actual : str ,
331+ actual : str | Pattern [ str ] | None ,
332332 ):
333333 """
334334 Compare paths with regex pattern support.
335+
336+ When expected is a compiled regex pattern, actual must be a
337+ string that matches it. Otherwise a simple equality check is
338+ performed.
335339 """
336340 if isinstance (expected , Pattern ):
337- if not expected .match (actual ):
341+ if not isinstance ( actual , str ) or not expected .match (actual ):
338342 diff [name ] = {'expected' : f'{ expected } ' , 'actual' : actual }
339343 elif expected != actual :
340344 diff [name ] = {'expected' : expected , 'actual' : actual }
341345
342- def diff (self , other : FileActivity ) -> dict | None :
346+ def diff (self , other : Event ) -> dict | None :
343347 """
344- Compare this Event with a FileActivity protobuf message.
348+ Compare this Event with another Event instance.
349+
350+ Both gRPC and OTLP servers translate their native messages
351+ into Event objects, so this method provides a single
352+ protocol-agnostic comparison path.
345353
346354 Args:
347- other: FileActivity protobuf message to compare against
355+ other: Event instance to compare against.
348356
349357 Returns:
350- None if identical, dict of differences if not matching
358+ None if identical, dict of differences if not matching.
351359 """
352360 diff = {}
353361
354- # Check process differences first
355362 process_diff = self .process .diff (other .process )
356363 if process_diff is not None :
357364 diff ['process' ] = process_diff
358365
359- # Check event type
360- event_type_expected = self .event_type .name .lower ()
361- event_type_actual = other .WhichOneof ('file' )
362-
363366 Event ._diff_field (
364367 diff ,
365368 'event_type' ,
366- event_type_expected ,
367- event_type_actual ,
369+ self . event_type ,
370+ other . event_type ,
368371 )
369372 if diff :
370373 return diff
371374
372- # Get the appropriate event field based on type
373- event_field = getattr (other , event_type_expected )
374-
375375 # Rename handling is a bit different to the rest, since it has
376376 # new and old paths.
377- if self .event_type == EventType .RENAME :
378- Event ._diff_path (diff , 'new_file' , self .file , event_field .new .path )
377+ if self .event_type != EventType .RENAME :
378+ Event ._diff_path (diff , 'file' , self .file , other .file )
379+ Event ._diff_path (diff , 'host_path' , self .host_path , other .host_path )
380+ else :
381+ Event ._diff_path (diff , 'new_file' , self .file , other .file )
379382 Event ._diff_path (
380- diff ,
381- 'new_host_path' ,
382- self .host_path ,
383- event_field .new .host_path ,
383+ diff , 'new_host_path' , self .host_path , other .host_path
384384 )
385+ Event ._diff_path (diff , 'old_file' , self .old_file , other .old_file )
385386 Event ._diff_path (
386- diff ,
387- 'old_file' ,
388- self .old_file ,
389- event_field .old .path ,
387+ diff , 'old_host_path' , self .old_host_path , other .old_host_path
390388 )
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- )
409389
410390 if self .event_type == EventType .PERMISSION :
411- Event ._diff_field (diff , 'mode' , self .mode , event_field .mode )
391+ Event ._diff_field (diff , 'mode' , self .mode , other .mode )
412392 elif self .event_type == EventType .OWNERSHIP :
413393 Event ._diff_field (
414- diff ,
415- 'owner_uid' ,
416- self .owner_uid ,
417- event_field .uid ,
394+ diff , 'owner_uid' , self .owner_uid , other .owner_uid
418395 )
419396 Event ._diff_field (
420- diff ,
421- 'owner_gid' ,
422- self .owner_gid ,
423- event_field .gid ,
397+ diff , 'owner_gid' , self .owner_gid , other .owner_gid
424398 )
425399 elif self .event_type in (EventType .XATTR_SET , EventType .XATTR_REMOVE ):
426400 Event ._diff_field (
427- diff ,
428- 'xattr_name' ,
429- self .xattr_name ,
430- event_field .xattr_name ,
401+ diff , 'xattr_name' , self .xattr_name , other .xattr_name
431402 )
432403 elif self .event_type == EventType .ACL :
433404 Event ._diff_field (
434405 diff ,
435406 'acl_type' ,
436407 self .acl_type ,
437- event_field .acl_type ,
408+ other .acl_type ,
438409 )
439410 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- ]
448411 Event ._diff_field (
449412 diff ,
450413 'acl_entries' ,
451414 self .acl_entries ,
452- actual_entries ,
415+ other . acl_entries ,
453416 )
454417
455418 return diff if diff else None
0 commit comments