exec_linux_test.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. //go:build linux
  2. // +build linux
  3. package daemon
  4. import (
  5. "testing"
  6. containertypes "github.com/docker/docker/api/types/container"
  7. "github.com/docker/docker/container"
  8. "github.com/docker/docker/daemon/exec"
  9. "github.com/opencontainers/runc/libcontainer/apparmor"
  10. specs "github.com/opencontainers/runtime-spec/specs-go"
  11. "gotest.tools/v3/assert"
  12. )
  13. func TestExecSetPlatformOpt(t *testing.T) {
  14. if !apparmor.IsEnabled() {
  15. t.Skip("requires AppArmor to be enabled")
  16. }
  17. d := &Daemon{}
  18. c := &container.Container{AppArmorProfile: "my-custom-profile"}
  19. ec := &exec.Config{}
  20. p := &specs.Process{}
  21. err := d.execSetPlatformOpt(c, ec, p)
  22. assert.NilError(t, err)
  23. assert.Equal(t, "my-custom-profile", p.ApparmorProfile)
  24. }
  25. // TestExecSetPlatformOptPrivileged verifies that `docker exec --privileged`
  26. // does not disable AppArmor profiles. Exec currently inherits the `Privileged`
  27. // configuration of the container. See https://github.com/moby/moby/pull/31773#discussion_r105586900
  28. //
  29. // This behavior may change in future, but test for the behavior to prevent it
  30. // from being changed accidentally.
  31. func TestExecSetPlatformOptPrivileged(t *testing.T) {
  32. if !apparmor.IsEnabled() {
  33. t.Skip("requires AppArmor to be enabled")
  34. }
  35. d := &Daemon{}
  36. c := &container.Container{AppArmorProfile: "my-custom-profile"}
  37. ec := &exec.Config{Privileged: true}
  38. p := &specs.Process{}
  39. err := d.execSetPlatformOpt(c, ec, p)
  40. assert.NilError(t, err)
  41. assert.Equal(t, "my-custom-profile", p.ApparmorProfile)
  42. c.HostConfig = &containertypes.HostConfig{Privileged: true}
  43. err = d.execSetPlatformOpt(c, ec, p)
  44. assert.NilError(t, err)
  45. assert.Equal(t, unconfinedAppArmorProfile, p.ApparmorProfile)
  46. }