Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.

Commit afd5649

Browse files
committed
Pod log location must validate container if provided
1 parent c1af9dc commit afd5649

File tree

2 files changed

+114
-4
lines changed

2 files changed

+114
-4
lines changed

pkg/registry/pod/strategy.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,13 +240,18 @@ func LogLocation(getter ResourceGetter, connInfo client.ConnectionInfoGetter, ct
240240
}
241241

242242
// Try to figure out a container
243+
// If a container was provided, it must be valid
243244
container := opts.Container
244-
if container == "" {
245+
if len(container) == 0 {
245246
if len(pod.Spec.Containers) == 1 {
246247
container = pod.Spec.Containers[0].Name
247248
} else {
248249
return nil, nil, errors.NewBadRequest(fmt.Sprintf("a container name must be specified for pod %s", name))
249250
}
251+
} else {
252+
if !podHasContainerWithName(pod, container) {
253+
return nil, nil, errors.NewBadRequest(fmt.Sprintf("container %s is not valid for pod %s", container, name))
254+
}
250255
}
251256
nodeHost := pod.Spec.NodeName
252257
if len(nodeHost) == 0 {
@@ -282,12 +287,21 @@ func LogLocation(getter ResourceGetter, connInfo client.ConnectionInfoGetter, ct
282287
loc := &url.URL{
283288
Scheme: nodeScheme,
284289
Host: fmt.Sprintf("%s:%d", nodeHost, nodePort),
285-
Path: fmt.Sprintf("/containerLogs/%s/%s/%s", pod.Namespace, name, container),
290+
Path: fmt.Sprintf("/containerLogs/%s/%s/%s", pod.Namespace, pod.Name, container),
286291
RawQuery: params.Encode(),
287292
}
288293
return loc, nodeTransport, nil
289294
}
290295

296+
func podHasContainerWithName(pod *api.Pod, containerName string) bool {
297+
for _, c := range pod.Spec.Containers {
298+
if c.Name == containerName {
299+
return true
300+
}
301+
}
302+
return false
303+
}
304+
291305
func streamParams(params url.Values, opts runtime.Object) error {
292306
switch opts := opts.(type) {
293307
case *api.PodExecOptions:
@@ -344,12 +358,17 @@ func streamLocation(getter ResourceGetter, connInfo client.ConnectionInfoGetter,
344358
}
345359

346360
// Try to figure out a container
361+
// If a container was provided, it must be valid
347362
if container == "" {
348363
if len(pod.Spec.Containers) == 1 {
349364
container = pod.Spec.Containers[0].Name
350365
} else {
351366
return nil, nil, errors.NewBadRequest(fmt.Sprintf("a container name must be specified for pod %s", name))
352367
}
368+
} else {
369+
if !podHasContainerWithName(pod, container) {
370+
return nil, nil, errors.NewBadRequest(fmt.Sprintf("container %s is not valid for pod %s", container, name))
371+
}
353372
}
354373
nodeHost := pod.Spec.NodeName
355374
if len(nodeHost) == 0 {
@@ -367,7 +386,7 @@ func streamLocation(getter ResourceGetter, connInfo client.ConnectionInfoGetter,
367386
loc := &url.URL{
368387
Scheme: nodeScheme,
369388
Host: fmt.Sprintf("%s:%d", nodeHost, nodePort),
370-
Path: fmt.Sprintf("/%s/%s/%s/%s", path, pod.Namespace, name, container),
389+
Path: fmt.Sprintf("/%s/%s/%s/%s", path, pod.Namespace, pod.Name, container),
371390
RawQuery: params.Encode(),
372391
}
373392
return loc, nodeTransport, nil
@@ -392,7 +411,7 @@ func PortForwardLocation(getter ResourceGetter, connInfo client.ConnectionInfoGe
392411
loc := &url.URL{
393412
Scheme: nodeScheme,
394413
Host: fmt.Sprintf("%s:%d", nodeHost, nodePort),
395-
Path: fmt.Sprintf("/portForward/%s/%s", pod.Namespace, name),
414+
Path: fmt.Sprintf("/portForward/%s/%s", pod.Namespace, pod.Name),
396415
}
397416
return loc, nodeTransport, nil
398417
}

pkg/registry/pod/strategy_test.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@ limitations under the License.
1717
package pod
1818

1919
import (
20+
"reflect"
2021
"testing"
2122

2223
"k8s.io/kubernetes/pkg/api"
24+
"k8s.io/kubernetes/pkg/api/errors"
25+
"k8s.io/kubernetes/pkg/runtime"
2326
)
2427

2528
func TestCheckGracefulDelete(t *testing.T) {
@@ -76,3 +79,91 @@ func TestCheckGracefulDelete(t *testing.T) {
7679
}
7780
}
7881
}
82+
83+
type mockPodGetter struct {
84+
pod *api.Pod
85+
}
86+
87+
func (g mockPodGetter) Get(api.Context, string) (runtime.Object, error) {
88+
return g.pod, nil
89+
}
90+
91+
func TestCheckLogLocation(t *testing.T) {
92+
ctx := api.NewDefaultContext()
93+
tcs := []struct {
94+
in *api.Pod
95+
opts *api.PodLogOptions
96+
expectedErr error
97+
}{
98+
{
99+
in: &api.Pod{
100+
Spec: api.PodSpec{},
101+
Status: api.PodStatus{},
102+
},
103+
opts: &api.PodLogOptions{},
104+
expectedErr: errors.NewBadRequest("a container name must be specified for pod test"),
105+
},
106+
{
107+
in: &api.Pod{
108+
Spec: api.PodSpec{
109+
Containers: []api.Container{
110+
{Name: "mycontainer"},
111+
},
112+
},
113+
Status: api.PodStatus{},
114+
},
115+
opts: &api.PodLogOptions{},
116+
expectedErr: nil,
117+
},
118+
{
119+
in: &api.Pod{
120+
Spec: api.PodSpec{
121+
Containers: []api.Container{
122+
{Name: "container1"},
123+
{Name: "container2"},
124+
},
125+
},
126+
Status: api.PodStatus{},
127+
},
128+
opts: &api.PodLogOptions{},
129+
expectedErr: errors.NewBadRequest("a container name must be specified for pod test"),
130+
},
131+
{
132+
in: &api.Pod{
133+
Spec: api.PodSpec{
134+
Containers: []api.Container{
135+
{Name: "container1"},
136+
{Name: "container2"},
137+
},
138+
},
139+
Status: api.PodStatus{},
140+
},
141+
opts: &api.PodLogOptions{
142+
Container: "unknown",
143+
},
144+
expectedErr: errors.NewBadRequest("container unknown is not valid for pod test"),
145+
},
146+
{
147+
in: &api.Pod{
148+
Spec: api.PodSpec{
149+
Containers: []api.Container{
150+
{Name: "container1"},
151+
{Name: "container2"},
152+
},
153+
},
154+
Status: api.PodStatus{},
155+
},
156+
opts: &api.PodLogOptions{
157+
Container: "container2",
158+
},
159+
expectedErr: nil,
160+
},
161+
}
162+
for _, tc := range tcs {
163+
getter := &mockPodGetter{tc.in}
164+
_, _, err := LogLocation(getter, nil, ctx, "test", tc.opts)
165+
if !reflect.DeepEqual(err, tc.expectedErr) {
166+
t.Errorf("expected %v, got %v", tc.expectedErr, err)
167+
}
168+
}
169+
}

0 commit comments

Comments
 (0)