-
Notifications
You must be signed in to change notification settings - Fork 2
/
action_network.go
307 lines (262 loc) · 9.72 KB
/
action_network.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: 2023 Steadybit GmbH
package extcontainer
import (
"context"
"encoding/json"
"fmt"
"github.com/google/uuid"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/rs/zerolog/log"
"github.com/steadybit/action-kit/go/action_kit_api/v2"
"github.com/steadybit/action-kit/go/action_kit_commons/network"
"github.com/steadybit/action-kit/go/action_kit_commons/runc"
"github.com/steadybit/action-kit/go/action_kit_sdk"
"github.com/steadybit/extension-container/config"
"github.com/steadybit/extension-kit"
"github.com/steadybit/extension-kit/extutil"
"net"
"runtime/trace"
)
type networkOptsProvider func(ctx context.Context, sidecar network.SidecarOpts, request action_kit_api.PrepareActionRequestBody) (network.Opts, error)
type networkOptsDecoder func(data json.RawMessage) (network.Opts, error)
type networkAction struct {
runc runc.Runc
description action_kit_api.ActionDescription
optsProvider networkOptsProvider
optsDecoder networkOptsDecoder
}
type NetworkActionState struct {
ExecutionId uuid.UUID
NetworkOpts json.RawMessage
Sidecar network.SidecarOpts
ContainerID string
}
// Make sure networkAction implements all required interfaces
var _ action_kit_sdk.Action[NetworkActionState] = (*networkAction)(nil)
var _ action_kit_sdk.ActionWithStop[NetworkActionState] = (*networkAction)(nil)
var commonNetworkParameters = []action_kit_api.ActionParameter{
{
Name: "duration",
Label: "Duration",
Description: extutil.Ptr("How long should the network be affected?"),
Type: action_kit_api.Duration,
DefaultValue: extutil.Ptr("30s"),
Required: extutil.Ptr(true),
Order: extutil.Ptr(0),
},
{
Name: "failOnHostNetwork",
Label: "Fail on Host Network",
Description: extutil.Ptr("Should the action fail if the container is using host network?"),
Type: action_kit_api.Boolean,
DefaultValue: extutil.Ptr("true"),
Required: extutil.Ptr(true),
Order: extutil.Ptr(100),
},
{
Name: "hostname",
Label: "Hostname",
Description: extutil.Ptr("Restrict to/from which hosts the traffic is affected."),
Type: action_kit_api.StringArray,
DefaultValue: extutil.Ptr(""),
Advanced: extutil.Ptr(true),
Order: extutil.Ptr(101),
},
{
Name: "ip",
Label: "IP Address",
Description: extutil.Ptr("Restrict to/from which IP addresses the traffic is affected."),
Type: action_kit_api.StringArray,
DefaultValue: extutil.Ptr(""),
Advanced: extutil.Ptr(true),
Order: extutil.Ptr(102),
},
{
Name: "port",
Label: "Ports",
Description: extutil.Ptr("Restrict to/from which ports the traffic is affected."),
Type: action_kit_api.StringArray,
DefaultValue: extutil.Ptr(""),
Advanced: extutil.Ptr(true),
Order: extutil.Ptr(103),
},
}
func (a *networkAction) NewEmptyState() NetworkActionState {
return NetworkActionState{}
}
func (a *networkAction) Describe() action_kit_api.ActionDescription {
return a.description
}
func (a *networkAction) Prepare(ctx context.Context, state *NetworkActionState, request action_kit_api.PrepareActionRequestBody) (*action_kit_api.PrepareResult, error) {
ctx, task := trace.NewTask(ctx, "action_network.Prepare")
defer task.End()
trace.Log(ctx, "actionId", a.description.Id)
trace.Log(ctx, "executionId", request.ExecutionId.String())
containerId := request.Target.Attributes["container.id"]
if len(containerId) == 0 {
return nil, extension_kit.ToError("Target is missing the 'container.id' attribute.", nil)
}
state.ContainerID = containerId[0]
processInfo, err := getProcessInforForContainer(ctx, a.runc, RemovePrefix(state.ContainerID))
if err != nil {
return nil, extension_kit.ToError("Failed to prepare network settings.", err)
}
state.Sidecar = network.SidecarOpts{
TargetProcess: processInfo,
ImagePath: "/",
IdSuffix: RemovePrefix(state.ContainerID)[:8],
}
if extutil.ToBool(request.Config["failOnHostNetwork"]) && isUsingHostNetwork(processInfo.Namespaces) {
return &action_kit_api.PrepareResult{
Error: &action_kit_api.ActionKitError{
Title: "Container is using host network and failOnHostNetwork = true.",
Status: extutil.Ptr(action_kit_api.Failed),
},
}, nil
}
opts, err := a.optsProvider(ctx, state.Sidecar, request)
if err != nil {
return nil, extension_kit.ToError("Failed to prepare network settings.", err)
}
rawOpts, err := json.Marshal(opts)
if err != nil {
return nil, extension_kit.ToError("Failed to serialize network settings.", err)
}
state.NetworkOpts = rawOpts
state.ExecutionId = request.ExecutionId
return nil, nil
}
func isUsingHostNetwork(ns []runc.LinuxNamespace) bool {
for _, n := range ns {
if n.Type == specs.NetworkNamespace {
return n.Path == "/proc/1/ns/net"
}
}
return true
}
func (a *networkAction) Start(ctx context.Context, state *NetworkActionState) (*action_kit_api.StartResult, error) {
ctx, task := trace.NewTask(ctx, "action_network.Start")
defer task.End()
trace.Log(ctx, "actionId", a.description.Id)
trace.Log(ctx, "executionId", state.ExecutionId.String())
opts, err := a.optsDecoder(state.NetworkOpts)
if err != nil {
return nil, extension_kit.ToError("Failed to deserialize network settings.", err)
}
err = network.Apply(ctx, a.runc, state.Sidecar, opts)
if err != nil {
return nil, extension_kit.ToError(fmt.Sprintf("Failed to apply network settings for container %s", state.ContainerID), err)
}
return &action_kit_api.StartResult{
Messages: extutil.Ptr([]action_kit_api.Message{
{
Level: extutil.Ptr(action_kit_api.Info),
Message: fmt.Sprintf("ContainerId: %s %s", state.ContainerID, opts.String()),
},
}),
}, nil
}
func (a *networkAction) Stop(ctx context.Context, state *NetworkActionState) (*action_kit_api.StopResult, error) {
ctx, task := trace.NewTask(ctx, "action_network.Stop")
defer task.End()
trace.Log(ctx, "actionId", a.description.Id)
trace.Log(ctx, "executionId", state.ExecutionId.String())
if err := runc.NamespacesExists(ctx, state.Sidecar.TargetProcess.Namespaces, specs.NetworkNamespace); err != nil {
log.Info().
Str("containerId", state.ContainerID).
AnErr("reason", err).
Msg("skipping revert network config")
return &action_kit_api.StopResult{
Messages: &[]action_kit_api.Message{
{
Level: extutil.Ptr(action_kit_api.Info),
Message: fmt.Sprintf("Skipped revert network config. Target container %s exited? %s", state.ContainerID, err),
},
},
}, nil
}
opts, err := a.optsDecoder(state.NetworkOpts)
if err != nil {
return nil, extension_kit.ToError("Failed to deserialize network settings.", err)
}
if err := network.Revert(ctx, a.runc, state.Sidecar, opts); err != nil {
return nil, extension_kit.ToError("Failed to revert network settings.", err)
}
return nil, nil
}
func parsePortRanges(raw []string) ([]network.PortRange, error) {
if raw == nil {
return nil, nil
}
var ranges []network.PortRange
for _, r := range raw {
if len(r) == 0 {
continue
}
parsed, err := network.ParsePortRange(r)
if err != nil {
return nil, err
}
ranges = append(ranges, parsed)
}
return ranges, nil
}
func mapToNetworkFilter(ctx context.Context, r runc.Runc, sidecat network.SidecarOpts, actionConfig map[string]interface{}, restrictedEndpoints []action_kit_api.RestrictedEndpoint) (network.Filter, error) {
toResolve := append(
extutil.ToStringArray(actionConfig["ip"]),
extutil.ToStringArray(actionConfig["hostname"])...,
)
dig := network.HostnameResolver{Dig: &network.RuncDigRunner{Runc: r, Sidecar: sidecat}}
includeIps, err := dig.Resolve(ctx, toResolve...)
if err != nil {
return network.Filter{}, err
}
//if no hostname/ip specified we affect all ips
includeCidrs := network.NetAny
if len(includeIps) > 0 {
includeCidrs = network.IpToNet(includeIps)
}
portRanges, err := parsePortRanges(extutil.ToStringArray(actionConfig["port"]))
if err != nil {
return network.Filter{}, err
}
if len(portRanges) == 0 {
//if no hostname/ip specified we affect all ports
portRanges = []network.PortRange{network.PortRangeAny}
}
includes := network.NewNetWithPortRanges(includeCidrs, portRanges...)
var excludes []network.NetWithPortRange
for _, restrictedEndpoint := range restrictedEndpoints {
log.Debug().Msgf("Adding restricted endpoint %s (%s) => %s:%d-%d", restrictedEndpoint.Name, restrictedEndpoint.Url, restrictedEndpoint.Cidr, restrictedEndpoint.PortMin, restrictedEndpoint.PortMax)
_, cidr, err := net.ParseCIDR(restrictedEndpoint.Cidr)
if err != nil {
return network.Filter{}, fmt.Errorf("invalid cidr %s: %w", restrictedEndpoint.Cidr, err)
}
excludes = append(excludes, network.NewNetWithPortRanges([]net.IPNet{*cidr}, network.PortRange{From: uint16(restrictedEndpoint.PortMin), To: uint16(restrictedEndpoint.PortMax)})...)
}
ownIps := network.GetOwnIPs()
ownPort := config.Config.Port
ownHealthPort := config.Config.HealthPort
nets := network.IpToNet(ownIps)
log.Debug().Msgf("Adding own ip %s to exclude list (Ports %d and %d)", ownIps, ownPort, ownHealthPort)
excludes = append(excludes, network.NewNetWithPortRanges(nets, network.PortRange{From: ownPort, To: ownPort})...)
excludes = append(excludes, network.NewNetWithPortRanges(nets, network.PortRange{From: ownHealthPort, To: ownHealthPort})...)
return network.Filter{
Include: includes,
Exclude: excludes,
}, nil
}
func readNetworkInterfaces(ctx context.Context, r runc.Runc, sidecar network.SidecarOpts) ([]string, error) {
ifcs, err := network.ListInterfaces(ctx, r, sidecar)
if err != nil {
return nil, err
}
var ifcNames []string
for _, ifc := range ifcs {
if ifc.HasFlag("UP") && !ifc.HasFlag("LOOPBACK") {
ifcNames = append(ifcNames, ifc.Name)
}
}
return ifcNames, nil
}