Skip to content

Commit 77084db

Browse files
committed
fix(plugins/container): redefine port binding port and IP as integers
Signed-off-by: Leonardo Di Giovanna <leonardodigiovanna1@gmail.com>
1 parent e1a0753 commit 77084db

File tree

3 files changed

+82
-25
lines changed

3 files changed

+82
-25
lines changed

plugins/container/go-worker/pkg/container/docker.go

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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

299314
func (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{

plugins/container/go-worker/pkg/container/podman.go

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"context"
77
"encoding/json"
88
"errors"
9+
"fmt"
910
"github.com/containers/podman/v5/libpod/define"
1011
"github.com/containers/podman/v5/pkg/bindings"
1112
"github.com/containers/podman/v5/pkg/bindings/containers"
@@ -41,7 +42,7 @@ func (pc *podmanEngine) copy(ctx context.Context) (Engine, error) {
4142
return newPodmanEngine(ctx, pc.socket)
4243
}
4344

44-
func (pc *podmanEngine) ctrToInfo(ctr *define.InspectContainerData) event.Info {
45+
func (pc *podmanEngine) ctrToInfo(ctr *define.InspectContainerData) (*event.Info, error) {
4546
cfg := ctr.Config
4647
if cfg == nil {
4748
cfg = &define.InspectContainerConfig{}
@@ -80,9 +81,22 @@ func (pc *podmanEngine) ctrToInfo(ctr *define.InspectContainerData) event.Info {
8081
continue
8182
}
8283
for _, portBinding := range portBindings {
84+
rawHostIP, rawHostPort := portBinding.HostIP, portBinding.HostPort
85+
hostIP, err := strconv.ParseUint(rawHostIP, 10, 32)
86+
if err != nil {
87+
return nil, fmt.Errorf("error converting port binding's host IP %s into 32-bit unsigned integer: %w",
88+
rawHostIP, err)
89+
}
90+
91+
hostPort, err := strconv.ParseUint(rawHostPort, 10, 16)
92+
if err != nil {
93+
return nil, fmt.Errorf("error converting port binding's port %s into 16-bit unsigned integer: %w",
94+
rawHostPort, err)
95+
}
96+
8397
portMappings = append(portMappings, event.PortMapping{
84-
HostIp: portBinding.HostIP,
85-
HostPort: portBinding.HostPort,
98+
HostIP: (uint32)(hostIP),
99+
HostPort: (uint16)(hostPort),
86100
ContainerPort: containerPort,
87101
})
88102
}
@@ -149,7 +163,7 @@ func (pc *podmanEngine) ctrToInfo(ctr *define.InspectContainerData) event.Info {
149163
size = *ctr.SizeRw
150164
}
151165

152-
return event.Info{
166+
return &event.Info{
153167
Container: event.Container{
154168
Type: typePodman.ToCTValue(),
155169
ID: shortContainerID(ctr.ID),
@@ -183,7 +197,7 @@ func (pc *podmanEngine) ctrToInfo(ctr *define.InspectContainerData) event.Info {
183197
ReadinessProbe: readinessProbe,
184198
HealthcheckProbe: healthcheckProbe,
185199
},
186-
}
200+
}, nil
187201
}
188202

189203
func (pc *podmanEngine) get(_ context.Context, containerId string) (*event.Event, error) {
@@ -192,8 +206,14 @@ func (pc *podmanEngine) get(_ context.Context, containerId string) (*event.Event
192206
if err != nil {
193207
return nil, err
194208
}
209+
210+
info, err := pc.ctrToInfo(ctrInfo)
211+
if err != nil {
212+
return nil, fmt.Errorf("error converting container to info: %w", err)
213+
}
214+
195215
return &event.Event{
196-
Info: pc.ctrToInfo(ctrInfo),
216+
Info: *info,
197217
IsCreate: true,
198218
}, nil
199219
}
@@ -214,7 +234,7 @@ func (pc *podmanEngine) List(_ context.Context) ([]event.Event, error) {
214234
if err != nil {
215235
return nil, err
216236
}
217-
for _, c := range cList {
237+
for idx, c := range cList {
218238
ctrInfo, err := containers.Inspect(pc.pCtx, c.ID, &containers.InspectOptions{Size: &size})
219239
if err != nil {
220240
evts = append(evts, event.Event{
@@ -231,8 +251,13 @@ func (pc *podmanEngine) List(_ context.Context) ([]event.Event, error) {
231251
IsCreate: true,
232252
})
233253
} else {
254+
info, err := pc.ctrToInfo(ctrInfo)
255+
if err != nil {
256+
return nil, fmt.Errorf("error converting container %s (index %d) to info: %w", ctrInfo.ID, idx, err)
257+
}
258+
234259
evts = append(evts, event.Event{
235-
Info: pc.ctrToInfo(ctrInfo),
260+
Info: *info,
236261
IsCreate: true,
237262
})
238263
}
@@ -290,17 +315,20 @@ func (pc *podmanEngine) Listen(ctx context.Context, wg *sync.WaitGroup) (<-chan
290315
case events.ActionCreate, events.ActionStart:
291316
ctr, err = containers.Inspect(pc.pCtx, ev.Actor.ID, &containers.InspectOptions{Size: &size})
292317
if err == nil {
293-
outCh <- event.Event{
294-
Info: pc.ctrToInfo(ctr),
295-
IsCreate: true,
318+
var info *event.Info
319+
if info, err = pc.ctrToInfo(ctr); err == nil {
320+
outCh <- event.Event{
321+
Info: *info,
322+
IsCreate: true,
323+
}
296324
}
297325
}
298326
case events.ActionRemove:
299327
err = errors.New("inspect useless on action destroy")
300328
}
301329

302330
// This is called for ActionRemove
303-
// AND as a fallback whenever Inspect fails.
331+
// AND as a fallback whenever Inspect or podmanEngine.ctrToInfo fail.
304332
if err != nil {
305333
// At least send an event with the minimal set of data
306334
outCh <- event.Event{

plugins/container/go-worker/pkg/event/event.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package event
33
import "encoding/json"
44

55
type PortMapping struct {
6-
HostIp string `json:"HostIp"`
7-
HostPort string `json:"HostPort"`
6+
HostIP uint32 `json:"HostIp"`
7+
HostPort uint16 `json:"HostPort"`
88
ContainerPort int `json:"ContainerPort"`
99
}
1010

0 commit comments

Comments
 (0)