@@ -4,13 +4,15 @@ import (
44 "context"
55 "encoding/json"
66 "errors"
7+ "fmt"
78 "github.com/docker/docker/api/types"
89 "github.com/docker/docker/api/types/container"
910 "github.com/docker/docker/api/types/events"
1011 "github.com/docker/docker/api/types/filters"
1112 "github.com/docker/docker/client"
1213 "github.com/falcosecurity/plugins/plugins/container/go-worker/pkg/config"
1314 "github.com/falcosecurity/plugins/plugins/container/go-worker/pkg/event"
15+ "strconv"
1416 "strings"
1517 "sync"
1618 "time"
@@ -102,7 +104,7 @@ func parseHealthcheckProbe(hcheck *container.HealthConfig) *event.Probe {
102104 return & p
103105}
104106
105- func (dc * dockerEngine ) ctrToInfo (ctx context.Context , ctr types.ContainerJSON ) event.Info {
107+ func (dc * dockerEngine ) ctrToInfo (ctx context.Context , ctr types.ContainerJSON ) ( * event.Info , error ) {
106108 hostCfg := ctr .HostConfig
107109 if hostCfg == nil {
108110 hostCfg = & container.HostConfig {
@@ -139,9 +141,22 @@ func (dc *dockerEngine) ctrToInfo(ctx context.Context, ctr types.ContainerJSON)
139141 }
140142 containerPort := port .Int ()
141143 for _ , portBinding := range portBindings {
144+ rawHostIP , rawHostPort := portBinding .HostIP , portBinding .HostPort
145+ hostIP , err := strconv .ParseUint (rawHostIP , 10 , 32 )
146+ if err != nil {
147+ return nil , fmt .Errorf ("error converting port binding's host IP %s into 32-bit unsigned integer: %w" ,
148+ rawHostIP , err )
149+ }
150+
151+ hostPort , err := strconv .ParseUint (rawHostPort , 10 , 16 )
152+ if err != nil {
153+ return nil , fmt .Errorf ("error converting port binding's port %s into 16-bit unsigned integer: %w" ,
154+ rawHostPort , err )
155+ }
156+
142157 portMappings = append (portMappings , event.PortMapping {
143- HostIp : portBinding . HostIP ,
144- HostPort : portBinding . HostPort ,
158+ HostIP : ( uint32 )( hostIP ) ,
159+ HostPort : ( uint16 )( hostPort ) ,
145160 ContainerPort : containerPort ,
146161 })
147162 }
@@ -259,7 +274,7 @@ func (dc *dockerEngine) ctrToInfo(ctx context.Context, ctr types.ContainerJSON)
259274 size = * ctr .SizeRw
260275 }
261276
262- return event.Info {
277+ return & event.Info {
263278 Container : event.Container {
264279 Type : typeDocker .ToCTValue (),
265280 ID : shortContainerID (ctr .ID ),
@@ -293,17 +308,23 @@ func (dc *dockerEngine) ctrToInfo(ctx context.Context, ctr types.ContainerJSON)
293308 ReadinessProbe : readinessProbe ,
294309 HealthcheckProbe : healthcheckProbe ,
295310 },
296- }
311+ }, nil
297312}
298313
299314func (dc * dockerEngine ) get (ctx context.Context , containerId string ) (* event.Event , error ) {
300315 ctrJson , _ , err := dc .ContainerInspectWithRaw (ctx , containerId , config .GetWithSize ())
301316 if err != nil {
302317 return nil , err
303318 }
319+
320+ info , err := dc .ctrToInfo (ctx , ctrJson )
321+ if err != nil {
322+ return nil , fmt .Errorf ("error converting container to info: %w" , err )
323+ }
324+
304325 return & event.Event {
326+ Info : * info ,
305327 IsCreate : true ,
306- Info : dc .ctrToInfo (ctx , ctrJson ),
307328 }, nil
308329}
309330
@@ -340,9 +361,14 @@ func (dc *dockerEngine) List(ctx context.Context) ([]event.Event, error) {
340361 IsCreate : true ,
341362 }
342363 }
364+ info , err := dc .ctrToInfo (ctx , ctrJson )
365+ if err != nil {
366+ return nil , fmt .Errorf ("error converting container %s (index %d) to info: %w" , ctr .ID , idx , err )
367+ }
368+
343369 evts [idx ] = event.Event {
370+ Info : * info ,
344371 IsCreate : true ,
345- Info : dc .ctrToInfo (ctx , ctrJson ),
346372 }
347373 }
348374 return evts , nil
@@ -379,17 +405,20 @@ func (dc *dockerEngine) Listen(ctx context.Context, wg *sync.WaitGroup) (<-chan
379405 case events .ActionCreate , events .ActionStart :
380406 ctrJson , _ , err = dc .ContainerInspectWithRaw (ctx , msg .Actor .ID , config .GetWithSize ())
381407 if err == nil {
382- outCh <- event.Event {
383- Info : dc .ctrToInfo (ctx , ctrJson ),
384- IsCreate : true ,
408+ var info * event.Info
409+ if info , err = dc .ctrToInfo (ctx , ctrJson ); err == nil {
410+ outCh <- event.Event {
411+ Info : * info ,
412+ IsCreate : true ,
413+ }
385414 }
386415 }
387416 case events .ActionDestroy :
388417 err = errors .New ("inspect useless on action destroy" )
389418 }
390419
391420 // This is called for ActionDestroy
392- // AND as a fallback whenever ContainerInspectWithRaw fails .
421+ // AND as a fallback whenever ContainerInspectWithRaw or dockerEngine.ctrToInfo fail .
393422 if err != nil {
394423 // At least send an event with the minimum set of data
395424 outCh <- event.Event {
0 commit comments