Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bd4dc63

Browse files
committed
Pod log location must validate container if provided
1 parent a13c86b commit bd4dc63

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

pkg/registry/pod/rest.go

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,13 +195,18 @@ func LogLocation(getter ResourceGetter, connInfo client.ConnectionInfoGetter, ct
195195
}
196196

197197
// Try to figure out a container
198+
// If a container was provided, it must be valid
198199
container := opts.Container
199-
if container == "" {
200+
if len(container) == 0 {
200201
if len(pod.Spec.Containers) == 1 {
201202
container = pod.Spec.Containers[0].Name
202203
} else {
203204
return nil, nil, errors.NewBadRequest(fmt.Sprintf("a container name must be specified for pod %s", name))
204205
}
206+
} else {
207+
if !podHasContainerWithName(pod, container) {
208+
return nil, nil, errors.NewBadRequest(fmt.Sprintf("container %s is not valid for pod %s", container, name))
209+
}
205210
}
206211
nodeHost := pod.Spec.NodeName
207212
if len(nodeHost) == 0 {
@@ -222,12 +227,21 @@ func LogLocation(getter ResourceGetter, connInfo client.ConnectionInfoGetter, ct
222227
loc := &url.URL{
223228
Scheme: nodeScheme,
224229
Host: fmt.Sprintf("%s:%d", nodeHost, nodePort),
225-
Path: fmt.Sprintf("/containerLogs/%s/%s/%s", pod.Namespace, name, container),
230+
Path: fmt.Sprintf("/containerLogs/%s/%s/%s", pod.Namespace, pod.Name, container),
226231
RawQuery: params.Encode(),
227232
}
228233
return loc, nodeTransport, nil
229234
}
230235

236+
func podHasContainerWithName(pod *api.Pod, containerName string) bool {
237+
for _, c := range pod.Spec.Containers {
238+
if c.Name == containerName {
239+
return true
240+
}
241+
}
242+
return false
243+
}
244+
231245
// ExecLocation returns the exec URL for a pod container. If opts.Container is blank
232246
// and only one container is present in the pod, that container is used.
233247
func ExecLocation(getter ResourceGetter, connInfo client.ConnectionInfoGetter, ctx api.Context, name string, opts *api.PodExecOptions) (*url.URL, http.RoundTripper, error) {
@@ -238,13 +252,18 @@ func ExecLocation(getter ResourceGetter, connInfo client.ConnectionInfoGetter, c
238252
}
239253

240254
// Try to figure out a container
255+
// If a container was provided, it must be valid
241256
container := opts.Container
242-
if container == "" {
257+
if len(container) == 0 {
243258
if len(pod.Spec.Containers) == 1 {
244259
container = pod.Spec.Containers[0].Name
245260
} else {
246261
return nil, nil, errors.NewBadRequest(fmt.Sprintf("a container name must be specified for pod %s", name))
247262
}
263+
} else {
264+
if !podHasContainerWithName(pod, container) {
265+
return nil, nil, errors.NewBadRequest(fmt.Sprintf("container %s is not valid for pod %s", container, name))
266+
}
248267
}
249268
nodeHost := pod.Spec.NodeName
250269
if len(nodeHost) == 0 {
@@ -274,7 +293,7 @@ func ExecLocation(getter ResourceGetter, connInfo client.ConnectionInfoGetter, c
274293
loc := &url.URL{
275294
Scheme: nodeScheme,
276295
Host: fmt.Sprintf("%s:%d", nodeHost, nodePort),
277-
Path: fmt.Sprintf("/exec/%s/%s/%s", pod.Namespace, name, container),
296+
Path: fmt.Sprintf("/exec/%s/%s/%s", pod.Namespace, pod.Name, container),
278297
RawQuery: params.Encode(),
279298
}
280299
return loc, nodeTransport, nil
@@ -300,7 +319,7 @@ func PortForwardLocation(getter ResourceGetter, connInfo client.ConnectionInfoGe
300319
loc := &url.URL{
301320
Scheme: nodeScheme,
302321
Host: fmt.Sprintf("%s:%d", nodeHost, nodePort),
303-
Path: fmt.Sprintf("/portForward/%s/%s", pod.Namespace, name),
322+
Path: fmt.Sprintf("/portForward/%s/%s", pod.Namespace, pod.Name),
304323
}
305324
return loc, nodeTransport, nil
306325
}

0 commit comments

Comments
 (0)