|
1 | 1 | package docker
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "bytes" |
4 | 5 | "context"
|
| 6 | + "encoding/json" |
5 | 7 | "fmt"
|
| 8 | + "io/ioutil" |
6 | 9 | "net"
|
7 | 10 | "os"
|
8 | 11 | "path/filepath"
|
@@ -672,6 +675,35 @@ var userMountToUnixMount = map[string]string{
|
672 | 675 | nstructs.VolumeMountPropagationBidirectional: "rshared",
|
673 | 676 | }
|
674 | 677 |
|
| 678 | +// takes a local seccomp daemon, reads the file contents for sending to the daemon |
| 679 | +// this code modified slightly from the docker CLI code |
| 680 | +// https://github.com/docker/cli/blob/8ef8547eb6934b28497d309d21e280bcd25145f5/cli/command/container/opts.go#L840 |
| 681 | +func parseSecurityOpts(securityOpts []string) ([]string, error) { |
| 682 | + for key, opt := range securityOpts { |
| 683 | + con := strings.SplitN(opt, "=", 2) |
| 684 | + if len(con) == 1 && con[0] != "no-new-privileges" { |
| 685 | + if strings.Contains(opt, ":") { |
| 686 | + con = strings.SplitN(opt, ":", 2) |
| 687 | + } else { |
| 688 | + return securityOpts, fmt.Errorf("invalid security_opt: %q", opt) |
| 689 | + } |
| 690 | + } |
| 691 | + if con[0] == "seccomp" && con[1] != "unconfined" { |
| 692 | + f, err := ioutil.ReadFile(con[1]) |
| 693 | + if err != nil { |
| 694 | + return securityOpts, fmt.Errorf("opening seccomp profile (%s) failed: %v", con[1], err) |
| 695 | + } |
| 696 | + b := bytes.NewBuffer(nil) |
| 697 | + if err := json.Compact(b, f); err != nil { |
| 698 | + return securityOpts, fmt.Errorf("compacting json for seccomp profile (%s) failed: %v", con[1], err) |
| 699 | + } |
| 700 | + securityOpts[key] = fmt.Sprintf("seccomp=%s", b.Bytes()) |
| 701 | + } |
| 702 | + } |
| 703 | + |
| 704 | + return securityOpts, nil |
| 705 | +} |
| 706 | + |
675 | 707 | func (d *Driver) createContainerConfig(task *drivers.TaskConfig, driverConfig *TaskConfig,
|
676 | 708 | imageID string) (docker.CreateContainerOptions, error) {
|
677 | 709 |
|
@@ -895,6 +927,11 @@ func (d *Driver) createContainerConfig(task *drivers.TaskConfig, driverConfig *T
|
895 | 927 | hostConfig.SecurityOpt = driverConfig.SecurityOpt
|
896 | 928 | hostConfig.Sysctls = driverConfig.Sysctl
|
897 | 929 |
|
| 930 | + hostConfig.SecurityOpt, err = parseSecurityOpts(driverConfig.SecurityOpt) |
| 931 | + if err != nil { |
| 932 | + return c, fmt.Errorf("failed to parse security_opt configuration: %v", err) |
| 933 | + } |
| 934 | + |
898 | 935 | ulimits, err := sliceMergeUlimit(driverConfig.Ulimit)
|
899 | 936 | if err != nil {
|
900 | 937 | return c, fmt.Errorf("failed to parse ulimit configuration: %v", err)
|
|
0 commit comments