@@ -4,8 +4,10 @@ package container
44
55import (
66 "context"
7+ "encoding/binary"
78 "encoding/json"
89 "errors"
10+ "fmt"
911 "github.com/containers/podman/v5/libpod/define"
1012 "github.com/containers/podman/v5/pkg/bindings"
1113 "github.com/containers/podman/v5/pkg/bindings/containers"
@@ -15,6 +17,7 @@ import (
1517 "github.com/docker/docker/api/types/events"
1618 "github.com/falcosecurity/plugins/plugins/container/go-worker/pkg/config"
1719 "github.com/falcosecurity/plugins/plugins/container/go-worker/pkg/event"
20+ "net/netip"
1821 "strconv"
1922 "strings"
2023 "sync"
@@ -41,7 +44,7 @@ func (pc *podmanEngine) copy(ctx context.Context) (Engine, error) {
4144 return newPodmanEngine (ctx , pc .socket )
4245}
4346
44- func (pc * podmanEngine ) ctrToInfo (ctr * define.InspectContainerData ) event.Info {
47+ func (pc * podmanEngine ) ctrToInfo (ctr * define.InspectContainerData ) ( * event.Info , error ) {
4548 cfg := ctr .Config
4649 if cfg == nil {
4750 cfg = & define.InspectContainerConfig {}
@@ -80,9 +83,30 @@ func (pc *podmanEngine) ctrToInfo(ctr *define.InspectContainerData) event.Info {
8083 continue
8184 }
8285 for _ , portBinding := range portBindings {
86+ rawHostIP , rawHostPort := portBinding .HostIP , portBinding .HostPort
87+
88+ // Parse IP address to uint32.
89+ addr , err := netip .ParseAddr (rawHostIP )
90+ if err != nil {
91+ return nil , fmt .Errorf ("error parsing port binding's host IP %s as address: %w" , rawHostIP , err )
92+ }
93+ if addr .Is6 () {
94+ // TODO(ekoops): handle IPv6 addresses.
95+ continue
96+ }
97+ ipv4Addr := addr .As4 ()
98+ hostIP := binary .BigEndian .Uint32 (ipv4Addr [:])
99+
100+ // Parse port as uint16.
101+ hostPort , err := strconv .ParseUint (rawHostPort , 10 , 16 )
102+ if err != nil {
103+ return nil , fmt .Errorf ("error converting port binding's port %s into 16-bit unsigned integer: %w" ,
104+ rawHostPort , err )
105+ }
106+
83107 portMappings = append (portMappings , event.PortMapping {
84- HostIp : portBinding . HostIP ,
85- HostPort : portBinding . HostPort ,
108+ HostIP : hostIP ,
109+ HostPort : ( uint16 )( hostPort ) ,
86110 ContainerPort : containerPort ,
87111 })
88112 }
@@ -149,7 +173,7 @@ func (pc *podmanEngine) ctrToInfo(ctr *define.InspectContainerData) event.Info {
149173 size = * ctr .SizeRw
150174 }
151175
152- return event.Info {
176+ return & event.Info {
153177 Container : event.Container {
154178 Type : typePodman .ToCTValue (),
155179 ID : shortContainerID (ctr .ID ),
@@ -183,7 +207,7 @@ func (pc *podmanEngine) ctrToInfo(ctr *define.InspectContainerData) event.Info {
183207 ReadinessProbe : readinessProbe ,
184208 HealthcheckProbe : healthcheckProbe ,
185209 },
186- }
210+ }, nil
187211}
188212
189213func (pc * podmanEngine ) get (_ context.Context , containerId string ) (* event.Event , error ) {
@@ -192,8 +216,14 @@ func (pc *podmanEngine) get(_ context.Context, containerId string) (*event.Event
192216 if err != nil {
193217 return nil , err
194218 }
219+
220+ info , err := pc .ctrToInfo (ctrInfo )
221+ if err != nil {
222+ return nil , fmt .Errorf ("error converting container to info: %w" , err )
223+ }
224+
195225 return & event.Event {
196- Info : pc . ctrToInfo ( ctrInfo ) ,
226+ Info : * info ,
197227 IsCreate : true ,
198228 }, nil
199229}
@@ -214,7 +244,7 @@ func (pc *podmanEngine) List(_ context.Context) ([]event.Event, error) {
214244 if err != nil {
215245 return nil , err
216246 }
217- for _ , c := range cList {
247+ for idx , c := range cList {
218248 ctrInfo , err := containers .Inspect (pc .pCtx , c .ID , & containers.InspectOptions {Size : & size })
219249 if err != nil {
220250 evts = append (evts , event.Event {
@@ -231,8 +261,13 @@ func (pc *podmanEngine) List(_ context.Context) ([]event.Event, error) {
231261 IsCreate : true ,
232262 })
233263 } else {
264+ info , err := pc .ctrToInfo (ctrInfo )
265+ if err != nil {
266+ return nil , fmt .Errorf ("error converting container %s (index %d) to info: %w" , ctrInfo .ID , idx , err )
267+ }
268+
234269 evts = append (evts , event.Event {
235- Info : pc . ctrToInfo ( ctrInfo ) ,
270+ Info : * info ,
236271 IsCreate : true ,
237272 })
238273 }
@@ -290,17 +325,20 @@ func (pc *podmanEngine) Listen(ctx context.Context, wg *sync.WaitGroup) (<-chan
290325 case events .ActionCreate , events .ActionStart :
291326 ctr , err = containers .Inspect (pc .pCtx , ev .Actor .ID , & containers.InspectOptions {Size : & size })
292327 if err == nil {
293- outCh <- event.Event {
294- Info : pc .ctrToInfo (ctr ),
295- IsCreate : true ,
328+ var info * event.Info
329+ if info , err = pc .ctrToInfo (ctr ); err == nil {
330+ outCh <- event.Event {
331+ Info : * info ,
332+ IsCreate : true ,
333+ }
296334 }
297335 }
298336 case events .ActionRemove :
299337 err = errors .New ("inspect useless on action destroy" )
300338 }
301339
302340 // This is called for ActionRemove
303- // AND as a fallback whenever Inspect fails .
341+ // AND as a fallback whenever Inspect or podmanEngine.ctrToInfo fail .
304342 if err != nil {
305343 // At least send an event with the minimal set of data
306344 outCh <- event.Event {
0 commit comments