// +build !windows package main import ( "encoding/json" "fmt" "io/ioutil" "path/filepath" "runtime" "strconv" "strings" "github.com/Sirupsen/logrus" "github.com/containerd/containerd/api/services/execution" protobuf "github.com/gogo/protobuf/types" ocispec "github.com/opencontainers/image-spec/specs-go/v1" specs "github.com/opencontainers/runtime-spec/specs-go" "github.com/urfave/cli" ) const ( rwm = "rwm" rootfsPath = "rootfs" ) var capabilities = []string{ "CAP_CHOWN", "CAP_DAC_OVERRIDE", "CAP_FSETID", "CAP_FOWNER", "CAP_MKNOD", "CAP_NET_RAW", "CAP_SETGID", "CAP_SETUID", "CAP_SETFCAP", "CAP_SETPCAP", "CAP_NET_BIND_SERVICE", "CAP_SYS_CHROOT", "CAP_KILL", "CAP_AUDIT_WRITE", } func spec(id string, config *ocispec.ImageConfig, context *cli.Context) (*specs.Spec, error) { env := []string{ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", } env = append(env, config.Env...) cmd := config.Cmd if v := context.Args().Tail(); len(v) > 0 { cmd = v } var ( // TODO: support overriding entrypoint args = append(config.Entrypoint, cmd...) tty = context.Bool("tty") uid, gid uint32 ) if config.User != "" { parts := strings.Split(config.User, ":") switch len(parts) { case 1: v, err := strconv.ParseUint(parts[0], 0, 10) if err != nil { return nil, err } uid, gid = uint32(v), uint32(v) case 2: v, err := strconv.ParseUint(parts[0], 0, 10) if err != nil { return nil, err } uid = uint32(v) if v, err = strconv.ParseUint(parts[1], 0, 10); err != nil { return nil, err } gid = uint32(v) default: return nil, fmt.Errorf("invalid USER value %s", config.User) } } if tty { env = append(env, "TERM=xterm") } cwd := config.WorkingDir if cwd == "" { cwd = "/" } return &specs.Spec{ Version: specs.Version, Platform: specs.Platform{ OS: runtime.GOOS, Arch: runtime.GOARCH, }, Root: specs.Root{ Path: rootfsPath, Readonly: context.Bool("readonly"), }, Process: specs.Process{ Args: args, Env: env, Terminal: tty, Cwd: cwd, NoNewPrivileges: true, User: specs.User{ UID: uid, GID: gid, }, Capabilities: &specs.LinuxCapabilities{ Bounding: capabilities, Permitted: capabilities, Inheritable: capabilities, Effective: capabilities, Ambient: capabilities, }, Rlimits: []specs.LinuxRlimit{ { Type: "RLIMIT_NOFILE", Hard: uint64(1024), Soft: uint64(1024), }, }, }, Mounts: []specs.Mount{ { Destination: "/proc", Type: "proc", Source: "proc", }, { Destination: "/dev", Type: "tmpfs", Source: "tmpfs", Options: []string{"nosuid", "strictatime", "mode=755", "size=65536k"}, }, { Destination: "/dev/pts", Type: "devpts", Source: "devpts", Options: []string{"nosuid", "noexec", "newinstance", "ptmxmode=0666", "mode=0620", "gid=5"}, }, { Destination: "/dev/shm", Type: "tmpfs", Source: "shm", Options: []string{"nosuid", "noexec", "nodev", "mode=1777", "size=65536k"}, }, { Destination: "/dev/mqueue", Type: "mqueue", Source: "mqueue", Options: []string{"nosuid", "noexec", "nodev"}, }, { Destination: "/sys", Type: "sysfs", Source: "sysfs", Options: []string{"nosuid", "noexec", "nodev", "ro"}, }, { Destination: "/run", Type: "tmpfs", Source: "tmpfs", Options: []string{"nosuid", "strictatime", "mode=755", "size=65536k"}, }, { Destination: "/etc/resolv.conf", Type: "bind", Source: "/etc/resolv.conf", Options: []string{"rbind", "ro"}, }, { Destination: "/etc/hosts", Type: "bind", Source: "/etc/hosts", Options: []string{"rbind", "ro"}, }, { Destination: "/etc/localtime", Type: "bind", Source: "/etc/localtime", Options: []string{"rbind", "ro"}, }, }, Hostname: id, Linux: &specs.Linux{ Resources: &specs.LinuxResources{ Devices: []specs.LinuxDeviceCgroup{ { Allow: false, Access: rwm, }, }, }, Namespaces: []specs.LinuxNamespace{ { Type: "pid", }, { Type: "ipc", }, { Type: "uts", }, { Type: "mount", }, { Type: "network", }, }, }, }, nil } func customSpec(configPath string) (*specs.Spec, error) { b, err := ioutil.ReadFile(configPath) if err != nil { return nil, err } var s specs.Spec if err := json.Unmarshal(b, &s); err != nil { return nil, err } if s.Root.Path != rootfsPath { logrus.Warnf("ignoring Root.Path %q, setting %q forcibly", s.Root.Path, rootfsPath) s.Root.Path = rootfsPath } return &s, nil } func getConfig(context *cli.Context, imageConfig *ocispec.ImageConfig) (*specs.Spec, error) { config := context.String("runtime-config") if config == "" { return spec(context.String("id"), imageConfig, context) } return customSpec(config) } func newCreateRequest(context *cli.Context, imageConfig *ocispec.ImageConfig, id, tmpDir string) (*execution.CreateRequest, error) { s, err := getConfig(context, imageConfig) if err != nil { return nil, err } data, err := json.Marshal(s) if err != nil { return nil, err } create := &execution.CreateRequest{ ID: id, Spec: &protobuf.Any{ TypeUrl: specs.Version, Value: data, }, Runtime: context.String("runtime"), Terminal: context.Bool("tty"), Stdin: filepath.Join(tmpDir, "stdin"), Stdout: filepath.Join(tmpDir, "stdout"), Stderr: filepath.Join(tmpDir, "stderr"), } return create, nil }