mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
c7b488fbc8
The API did not treat invalid JSON payloads as a 400 error, as a result returning a 500 error; Before this change, an invalid JSON body would return a 500 error; ```bash curl -v \ --unix-socket /var/run/docker.sock \ -X POST \ "http://localhost/v1.30/networks/create" \ -H "Content-Type: application/json" \ -d '{invalid json' ``` ``` > POST /v1.30/networks/create HTTP/1.1 > Host: localhost > User-Agent: curl/7.52.1 > Accept: */* > Content-Type: application/json > Content-Length: 13 > * upload completely sent off: 13 out of 13 bytes < HTTP/1.1 500 Internal Server Error < Api-Version: 1.40 < Content-Type: application/json < Docker-Experimental: false < Ostype: linux < Server: Docker/dev (linux) < Date: Mon, 05 Nov 2018 11:55:20 GMT < Content-Length: 79 < {"message":"invalid character 'i' looking for beginning of object key string"} ``` Empty request: ```bash curl -v \ --unix-socket /var/run/docker.sock \ -X POST \ "http://localhost/v1.30/networks/create" \ -H "Content-Type: application/json" ``` ``` > POST /v1.30/networks/create HTTP/1.1 > Host: localhost > User-Agent: curl/7.54.0 > Accept: */* > Content-Type: application/json > < HTTP/1.1 500 Internal Server Error < Api-Version: 1.38 < Content-Length: 18 < Content-Type: application/json < Date: Mon, 05 Nov 2018 12:00:18 GMT < Docker-Experimental: true < Ostype: linux < Server: Docker/18.06.1-ce (linux) < {"message":"EOF"} ``` After this change, a 400 is returned; ```bash curl -v \ --unix-socket /var/run/docker.sock \ -X POST \ "http://localhost/v1.30/networks/create" \ -H "Content-Type: application/json" \ -d '{invalid json' ``` ``` > POST /v1.30/networks/create HTTP/1.1 > Host: localhost > User-Agent: curl/7.52.1 > Accept: */* > Content-Type: application/json > Content-Length: 13 > * upload completely sent off: 13 out of 13 bytes < HTTP/1.1 400 Bad Request < Api-Version: 1.40 < Content-Type: application/json < Docker-Experimental: false < Ostype: linux < Server: Docker/dev (linux) < Date: Mon, 05 Nov 2018 11:57:15 GMT < Content-Length: 79 < {"message":"invalid character 'i' looking for beginning of object key string"} ``` Empty request: ```bash curl -v \ --unix-socket /var/run/docker.sock \ -X POST \ "http://localhost/v1.30/networks/create" \ -H "Content-Type: application/json" ``` ``` > POST /v1.30/networks/create HTTP/1.1 > Host: localhost > User-Agent: curl/7.52.1 > Accept: */* > Content-Type: application/json > < HTTP/1.1 400 Bad Request < Api-Version: 1.40 < Content-Type: application/json < Docker-Experimental: false < Ostype: linux < Server: Docker/dev (linux) < Date: Mon, 05 Nov 2018 11:59:22 GMT < Content-Length: 49 < {"message":"got EOF while reading request body"} ``` Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
156 lines
4.4 KiB
Go
156 lines
4.4 KiB
Go
package container // import "github.com/docker/docker/api/server/router/container"
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/docker/docker/api/server/httputils"
|
|
"github.com/docker/docker/api/types"
|
|
"github.com/docker/docker/api/types/versions"
|
|
"github.com/docker/docker/errdefs"
|
|
"github.com/docker/docker/pkg/stdcopy"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
func (s *containerRouter) getExecByID(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
eConfig, err := s.backend.ContainerExecInspect(vars["id"])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return httputils.WriteJSON(w, http.StatusOK, eConfig)
|
|
}
|
|
|
|
type execCommandError struct{}
|
|
|
|
func (execCommandError) Error() string {
|
|
return "No exec command specified"
|
|
}
|
|
|
|
func (execCommandError) InvalidParameter() {}
|
|
|
|
func (s *containerRouter) postContainerExecCreate(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
if err := httputils.ParseForm(r); err != nil {
|
|
return err
|
|
}
|
|
if err := httputils.CheckForJSON(r); err != nil {
|
|
return err
|
|
}
|
|
name := vars["name"]
|
|
|
|
execConfig := &types.ExecConfig{}
|
|
if err := json.NewDecoder(r.Body).Decode(execConfig); err != nil {
|
|
if err == io.EOF {
|
|
return errdefs.InvalidParameter(errors.New("got EOF while reading request body"))
|
|
}
|
|
return errdefs.InvalidParameter(err)
|
|
}
|
|
|
|
if len(execConfig.Cmd) == 0 {
|
|
return execCommandError{}
|
|
}
|
|
|
|
// Register an instance of Exec in container.
|
|
id, err := s.backend.ContainerExecCreate(name, execConfig)
|
|
if err != nil {
|
|
logrus.Errorf("Error setting up exec command in container %s: %v", name, err)
|
|
return err
|
|
}
|
|
|
|
return httputils.WriteJSON(w, http.StatusCreated, &types.IDResponse{
|
|
ID: id,
|
|
})
|
|
}
|
|
|
|
// TODO(vishh): Refactor the code to avoid having to specify stream config as part of both create and start.
|
|
func (s *containerRouter) postContainerExecStart(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
if err := httputils.ParseForm(r); err != nil {
|
|
return err
|
|
}
|
|
|
|
version := httputils.VersionFromContext(ctx)
|
|
if versions.GreaterThan(version, "1.21") {
|
|
if err := httputils.CheckForJSON(r); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
var (
|
|
execName = vars["name"]
|
|
stdin, inStream io.ReadCloser
|
|
stdout, stderr, outStream io.Writer
|
|
)
|
|
|
|
execStartCheck := &types.ExecStartCheck{}
|
|
if err := json.NewDecoder(r.Body).Decode(execStartCheck); err != nil {
|
|
if err == io.EOF {
|
|
return errdefs.InvalidParameter(errors.New("got EOF while reading request body"))
|
|
}
|
|
return errdefs.InvalidParameter(err)
|
|
}
|
|
|
|
if exists, err := s.backend.ExecExists(execName); !exists {
|
|
return err
|
|
}
|
|
|
|
if !execStartCheck.Detach {
|
|
var err error
|
|
// Setting up the streaming http interface.
|
|
inStream, outStream, err = httputils.HijackConnection(w)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer httputils.CloseStreams(inStream, outStream)
|
|
|
|
if _, ok := r.Header["Upgrade"]; ok {
|
|
fmt.Fprint(outStream, "HTTP/1.1 101 UPGRADED\r\nContent-Type: application/vnd.docker.raw-stream\r\nConnection: Upgrade\r\nUpgrade: tcp\r\n")
|
|
} else {
|
|
fmt.Fprint(outStream, "HTTP/1.1 200 OK\r\nContent-Type: application/vnd.docker.raw-stream\r\n")
|
|
}
|
|
|
|
// copy headers that were removed as part of hijack
|
|
if err := w.Header().WriteSubset(outStream, nil); err != nil {
|
|
return err
|
|
}
|
|
fmt.Fprint(outStream, "\r\n")
|
|
|
|
stdin = inStream
|
|
stdout = outStream
|
|
if !execStartCheck.Tty {
|
|
stderr = stdcopy.NewStdWriter(outStream, stdcopy.Stderr)
|
|
stdout = stdcopy.NewStdWriter(outStream, stdcopy.Stdout)
|
|
}
|
|
}
|
|
|
|
// Now run the user process in container.
|
|
// Maybe we should we pass ctx here if we're not detaching?
|
|
if err := s.backend.ContainerExecStart(context.Background(), execName, stdin, stdout, stderr); err != nil {
|
|
if execStartCheck.Detach {
|
|
return err
|
|
}
|
|
stdout.Write([]byte(err.Error() + "\r\n"))
|
|
logrus.Errorf("Error running exec %s in container: %v", execName, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *containerRouter) postContainerExecResize(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
if err := httputils.ParseForm(r); err != nil {
|
|
return err
|
|
}
|
|
height, err := strconv.Atoi(r.Form.Get("h"))
|
|
if err != nil {
|
|
return errdefs.InvalidParameter(err)
|
|
}
|
|
width, err := strconv.Atoi(r.Form.Get("w"))
|
|
if err != nil {
|
|
return errdefs.InvalidParameter(err)
|
|
}
|
|
|
|
return s.backend.ContainerExecResize(vars["name"], height, width)
|
|
}
|