Browse code

Merge pull request #41411 from pjbgf/simplify-seccomp

Simplify seccomp logic

Tibor Vass authored on 2020/09/25 03:21:19
Showing 1 changed files
... ...
@@ -10,7 +10,6 @@ import (
10 10
 	coci "github.com/containerd/containerd/oci"
11 11
 	"github.com/docker/docker/container"
12 12
 	"github.com/docker/docker/profiles/seccomp"
13
-	specs "github.com/opencontainers/runtime-spec/specs-go"
14 13
 	"github.com/sirupsen/logrus"
15 14
 )
16 15
 
... ...
@@ -19,43 +18,29 @@ const supportsSeccomp = true
19 19
 // WithSeccomp sets the seccomp profile
20 20
 func WithSeccomp(daemon *Daemon, c *container.Container) coci.SpecOpts {
21 21
 	return func(ctx context.Context, _ coci.Client, _ *containers.Container, s *coci.Spec) error {
22
-		var profile *specs.LinuxSeccomp
23
-		var err error
24
-
22
+		if c.SeccompProfile == "unconfined" {
23
+			return nil
24
+		}
25 25
 		if c.HostConfig.Privileged {
26 26
 			return nil
27 27
 		}
28
-
29 28
 		if !daemon.seccompEnabled {
30
-			if c.SeccompProfile != "" && c.SeccompProfile != "unconfined" {
29
+			if c.SeccompProfile != "" {
31 30
 				return fmt.Errorf("seccomp is not enabled in your kernel, cannot run a custom seccomp profile")
32 31
 			}
33 32
 			logrus.Warn("seccomp is not enabled in your kernel, running container without default profile")
34 33
 			c.SeccompProfile = "unconfined"
35
-		}
36
-		if c.SeccompProfile == "unconfined" {
37 34
 			return nil
38 35
 		}
39
-		if c.SeccompProfile != "" {
40
-			profile, err = seccomp.LoadProfile(c.SeccompProfile, s)
41
-			if err != nil {
42
-				return err
43
-			}
44
-		} else {
45
-			if daemon.seccompProfile != nil {
46
-				profile, err = seccomp.LoadProfile(string(daemon.seccompProfile), s)
47
-				if err != nil {
48
-					return err
49
-				}
50
-			} else {
51
-				profile, err = seccomp.GetDefaultProfile(s)
52
-				if err != nil {
53
-					return err
54
-				}
55
-			}
36
+		var err error
37
+		switch {
38
+		case c.SeccompProfile != "":
39
+			s.Linux.Seccomp, err = seccomp.LoadProfile(c.SeccompProfile, s)
40
+		case daemon.seccompProfile != nil:
41
+			s.Linux.Seccomp, err = seccomp.LoadProfile(string(daemon.seccompProfile), s)
42
+		default:
43
+			s.Linux.Seccomp, err = seccomp.GetDefaultProfile(s)
56 44
 		}
57
-
58
-		s.Linux.Seccomp = profile
59
-		return nil
45
+		return err
60 46
 	}
61 47
 }