Skip to content

Commit bfcd1c6

Browse files
authored
Improve task network config validation in Fault Injection handlers (#4676)
1 parent f8c09a2 commit bfcd1c6

File tree

3 files changed

+148
-4
lines changed

3 files changed

+148
-4
lines changed

agent/vendor/github.com/aws/amazon-ecs-agent/ecs-agent/tmds/handlers/fault/v1/handlers/handlers.go

Lines changed: 5 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ecs-agent/tmds/handlers/fault/v1/handlers/handlers.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,8 +1316,11 @@ func validateTaskNetworkConfig(taskNetworkConfig *state.TaskNetworkConfig) error
13161316
}
13171317

13181318
// Device name is required to inject network faults to given ENI in the task.
1319-
if taskNetworkConfig.NetworkNamespaces[0].NetworkInterfaces[0].DeviceName == "" {
1320-
return errors.New("no ENI device name in the network namespace within task network config")
1319+
// Verify all network interfaces have a non-empty DeviceName
1320+
for i, netInterface := range taskNetworkConfig.NetworkNamespaces[0].NetworkInterfaces {
1321+
if netInterface.DeviceName == "" {
1322+
return fmt.Errorf("no ENI device name for network interface %d in the network namespace within task network config", i)
1323+
}
13211324
}
13221325

13231326
return nil
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"). You may
4+
// not use this file except in compliance with the License. A copy of the
5+
// License is located at
6+
//
7+
// http://aws.amazon.com/apache2.0/
8+
//
9+
// or in the "license" file accompanying this file. This file is distributed
10+
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+
// express or implied. See the License for the specific language governing
12+
// permissions and limitations under the License.
13+
14+
package handlers
15+
16+
import (
17+
"testing"
18+
19+
state "github.com/aws/amazon-ecs-agent/ecs-agent/tmds/handlers/v4/state"
20+
"github.com/stretchr/testify/assert"
21+
)
22+
23+
func TestValidateTaskNetworkConfig(t *testing.T) {
24+
testCases := []struct {
25+
name string
26+
config *state.TaskNetworkConfig
27+
expectedError string
28+
}{
29+
{
30+
name: "nil config",
31+
config: nil,
32+
expectedError: "TaskNetworkConfig is empty within task metadata",
33+
},
34+
{
35+
name: "empty network namespaces",
36+
config: &state.TaskNetworkConfig{},
37+
expectedError: "empty network namespaces within task network config",
38+
},
39+
{
40+
name: "empty network namespace path",
41+
config: &state.TaskNetworkConfig{
42+
NetworkNamespaces: []*state.NetworkNamespace{{}},
43+
},
44+
expectedError: "no path in the network namespace within task network config",
45+
},
46+
{
47+
name: "empty network interfaces",
48+
config: &state.TaskNetworkConfig{
49+
NetworkNamespaces: []*state.NetworkNamespace{
50+
{
51+
Path: "/proc/1234/ns/net",
52+
},
53+
},
54+
},
55+
expectedError: "empty network interfaces within task network config",
56+
},
57+
{
58+
name: "first interface missing device name",
59+
config: &state.TaskNetworkConfig{
60+
NetworkNamespaces: []*state.NetworkNamespace{
61+
{
62+
Path: "/proc/1234/ns/net",
63+
NetworkInterfaces: []*state.NetworkInterface{
64+
{},
65+
{
66+
DeviceName: "eth1",
67+
},
68+
},
69+
},
70+
},
71+
},
72+
expectedError: "no ENI device name for network interface 0 in the network namespace within task network config",
73+
},
74+
{
75+
name: "second interface missing device name",
76+
config: &state.TaskNetworkConfig{
77+
NetworkNamespaces: []*state.NetworkNamespace{
78+
{
79+
Path: "/proc/1234/ns/net",
80+
NetworkInterfaces: []*state.NetworkInterface{
81+
{
82+
DeviceName: "eth0",
83+
},
84+
{},
85+
},
86+
},
87+
},
88+
},
89+
expectedError: "no ENI device name for network interface 1 in the network namespace within task network config",
90+
},
91+
{
92+
name: "valid config with single interface",
93+
config: &state.TaskNetworkConfig{
94+
NetworkNamespaces: []*state.NetworkNamespace{
95+
{
96+
Path: "/proc/1234/ns/net",
97+
NetworkInterfaces: []*state.NetworkInterface{
98+
{
99+
DeviceName: "eth0",
100+
},
101+
},
102+
},
103+
},
104+
},
105+
expectedError: "",
106+
},
107+
{
108+
name: "valid config with multiple interfaces",
109+
config: &state.TaskNetworkConfig{
110+
NetworkNamespaces: []*state.NetworkNamespace{
111+
{
112+
Path: "/proc/1234/ns/net",
113+
NetworkInterfaces: []*state.NetworkInterface{
114+
{
115+
DeviceName: "eth0",
116+
},
117+
{
118+
DeviceName: "eth1",
119+
},
120+
},
121+
},
122+
},
123+
},
124+
expectedError: "",
125+
},
126+
}
127+
128+
for _, tc := range testCases {
129+
t.Run(tc.name, func(t *testing.T) {
130+
err := validateTaskNetworkConfig(tc.config)
131+
if tc.expectedError == "" {
132+
assert.NoError(t, err)
133+
} else {
134+
assert.EqualError(t, err, tc.expectedError)
135+
}
136+
})
137+
}
138+
}

0 commit comments

Comments
 (0)