@@ -51,6 +51,91 @@ func TestInternalProcessesRegex(t *testing.T) {
5151 require .True (t , internalProcessRegex .MatchString ("datadog-agent/bin/process-agent" ))
5252 require .True (t , internalProcessRegex .MatchString ("datadog-agent/bin/security-agent" ))
5353 require .True (t , internalProcessRegex .MatchString ("datadog-agent/bin/otel-agent" ))
54+ require .True (t , internalProcessRegex .MatchString ("/opt/datadog-agent/embedded/bin/host-profiler" ))
55+ require .False (t , internalProcessRegex .MatchString ("/opt/customer/bin/host-profiler" ))
56+ }
57+
58+ func TestAttachLibraryHonorsExcludeInternal (t * testing.T ) {
59+ const (
60+ pid = uint32 (1 )
61+ libPath = "/usr/lib/libssl.so.3"
62+ )
63+
64+ tests := []struct {
65+ name string
66+ exe string
67+ excludeTargets ExcludeMode
68+ createProcess bool
69+ expectedError error
70+ expectRegistration bool
71+ }{
72+ {
73+ name : "host profiler is excluded" ,
74+ exe : "/opt/datadog-agent/embedded/bin/host-profiler" ,
75+ excludeTargets : ExcludeInternal ,
76+ createProcess : true ,
77+ expectedError : ErrInternalDDogProcessRejected ,
78+ },
79+ {
80+ name : "non-internal process is registered" ,
81+ exe : "/usr/bin/curl" ,
82+ excludeTargets : ExcludeInternal ,
83+ createProcess : true ,
84+ expectRegistration : true ,
85+ },
86+ {
87+ name : "internal exclusion is opt-in" ,
88+ exe : "/opt/datadog-agent/embedded/bin/host-profiler" ,
89+ createProcess : true ,
90+ expectRegistration : true ,
91+ },
92+ {
93+ name : "process no longer exists" ,
94+ excludeTargets : ExcludeInternal ,
95+ expectedError : os .ErrNotExist ,
96+ },
97+ }
98+
99+ for _ , tt := range tests {
100+ t .Run (tt .name , func (t * testing.T ) {
101+ var entries []kernel.FakeProcFSEntry
102+ if tt .createProcess {
103+ entries = append (entries , kernel.FakeProcFSEntry {Pid : pid , Cmdline : tt .exe , Command : tt .exe , Exe : tt .exe })
104+ }
105+ procRoot := kernel .CreateFakeProcFS (t , entries )
106+
107+ config := AttacherConfig {
108+ ProcRoot : procRoot ,
109+ ExcludeTargets : tt .excludeTargets ,
110+ Rules : []* AttachRule {
111+ {
112+ Targets : AttachToSharedLibraries ,
113+ LibraryNameRegex : regexp .MustCompile (`libssl\.so` ),
114+ },
115+ },
116+ SharedLibsLibsets : []sharedlibraries.Libset {sharedlibraries .LibsetCrypto },
117+ }
118+ ua , err := NewUprobeAttacher (testModuleName , testAttacherName , config , & MockManager {}, nil , AttacherDependencies {ProcessMonitor : newMockProcessMonitor ()})
119+ require .NoError (t , err )
120+
121+ registry := & MockFileRegistry {}
122+ if tt .expectRegistration {
123+ registry .On ("Register" , libPath , pid , mock .Anything , mock .Anything ).Return (nil ).Once ()
124+ }
125+ ua .fileRegistry = registry
126+
127+ err = ua .AttachLibrary (libPath , pid )
128+ if tt .expectedError == nil {
129+ require .NoError (t , err )
130+ } else {
131+ require .ErrorIs (t , err , tt .expectedError )
132+ }
133+ registry .AssertExpectations (t )
134+ if ! tt .expectRegistration {
135+ registry .AssertNotCalled (t , "Register" , mock .Anything , mock .Anything , mock .Anything , mock .Anything )
136+ }
137+ })
138+ }
54139}
55140
56141func TestAttachPidReturnsCorrectErrors (t * testing.T ) {
@@ -413,25 +498,6 @@ func TestAttachToBinaryContainerdTmpReturnsErrEnvironment(t *testing.T) {
413498 require .ErrorIs (t , err , utils .ErrEnvironment )
414499}
415500
416- func TestGetExecutablePath (t * testing.T ) {
417- exe := "/bin/bash"
418- procRoot := kernel .CreateFakeProcFS (t , []kernel.FakeProcFSEntry {{Pid : 1 , Cmdline : "" , Command : exe , Exe : exe }})
419- config := AttacherConfig {
420- ProcRoot : procRoot ,
421- }
422- ua , err := NewUprobeAttacher (testModuleName , testAttacherName , config , & MockManager {}, nil , AttacherDependencies {ProcessMonitor : newMockProcessMonitor ()})
423- require .NoError (t , err )
424- require .NotNil (t , ua )
425-
426- path , err := ua .getExecutablePath (1 )
427- require .NoError (t , err , "failed to get executable path for existing PID" )
428- require .Equal (t , path , exe )
429-
430- path , err = ua .getExecutablePath (404 )
431- require .Error (t , err , "should fail to get executable path for non-existing PID" )
432- require .Empty (t , path , "should return empty path for non-existing PID" )
433- }
434-
435501const mapsFileSample = `
43650208048000-08049000 r-xp 00000000 03:00 8312 /opt/test
43750308049000-0804a000 rw-p 00001000 03:00 8312 /opt/test
0 commit comments