docker_cli_authz_plugin_v2_test.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // +build !windows
  2. package main
  3. import (
  4. "fmt"
  5. "strings"
  6. "github.com/docker/docker/integration-cli/checker"
  7. "github.com/docker/docker/integration-cli/daemon"
  8. "github.com/go-check/check"
  9. )
  10. var (
  11. authzPluginName = "riyaz/authz-no-volume-plugin"
  12. authzPluginTag = "latest"
  13. authzPluginNameWithTag = authzPluginName + ":" + authzPluginTag
  14. authzPluginBadManifestName = "riyaz/authz-plugin-bad-manifest"
  15. nonexistentAuthzPluginName = "riyaz/nonexistent-authz-plugin"
  16. )
  17. func init() {
  18. check.Suite(&DockerAuthzV2Suite{
  19. ds: &DockerSuite{},
  20. })
  21. }
  22. type DockerAuthzV2Suite struct {
  23. ds *DockerSuite
  24. d *daemon.Daemon
  25. }
  26. func (s *DockerAuthzV2Suite) SetUpTest(c *check.C) {
  27. testRequires(c, DaemonIsLinux, Network)
  28. s.d = daemon.New(c, dockerBinary, dockerdBinary, daemon.Config{
  29. Experimental: testEnv.ExperimentalDaemon(),
  30. })
  31. s.d.Start(c)
  32. }
  33. func (s *DockerAuthzV2Suite) TearDownTest(c *check.C) {
  34. if s.d != nil {
  35. s.d.Stop(c)
  36. s.ds.TearDownTest(c)
  37. }
  38. }
  39. func (s *DockerAuthzV2Suite) TestAuthZPluginAllowNonVolumeRequest(c *check.C) {
  40. testRequires(c, DaemonIsLinux, IsAmd64, Network)
  41. existingContainers := ExistingContainerIDs(c)
  42. // Install authz plugin
  43. _, err := s.d.Cmd("plugin", "install", "--grant-all-permissions", authzPluginNameWithTag)
  44. c.Assert(err, checker.IsNil)
  45. // start the daemon with the plugin and load busybox, --net=none build fails otherwise
  46. // because it needs to pull busybox
  47. s.d.Restart(c, "--authorization-plugin="+authzPluginNameWithTag)
  48. s.d.LoadBusybox(c)
  49. // defer disabling the plugin
  50. defer func() {
  51. s.d.Restart(c)
  52. _, err = s.d.Cmd("plugin", "disable", authzPluginNameWithTag)
  53. c.Assert(err, checker.IsNil)
  54. _, err = s.d.Cmd("plugin", "rm", authzPluginNameWithTag)
  55. c.Assert(err, checker.IsNil)
  56. }()
  57. // Ensure docker run command and accompanying docker ps are successful
  58. out, err := s.d.Cmd("run", "-d", "busybox", "top")
  59. c.Assert(err, check.IsNil)
  60. id := strings.TrimSpace(out)
  61. out, err = s.d.Cmd("ps")
  62. c.Assert(err, check.IsNil)
  63. c.Assert(assertContainerList(RemoveOutputForExistingElements(out, existingContainers), []string{id}), check.Equals, true)
  64. }
  65. func (s *DockerAuthzV2Suite) TestAuthZPluginDisable(c *check.C) {
  66. testRequires(c, DaemonIsLinux, IsAmd64, Network)
  67. // Install authz plugin
  68. _, err := s.d.Cmd("plugin", "install", "--grant-all-permissions", authzPluginNameWithTag)
  69. c.Assert(err, checker.IsNil)
  70. // start the daemon with the plugin and load busybox, --net=none build fails otherwise
  71. // because it needs to pull busybox
  72. s.d.Restart(c, "--authorization-plugin="+authzPluginNameWithTag)
  73. s.d.LoadBusybox(c)
  74. // defer removing the plugin
  75. defer func() {
  76. s.d.Restart(c)
  77. _, err = s.d.Cmd("plugin", "rm", "-f", authzPluginNameWithTag)
  78. c.Assert(err, checker.IsNil)
  79. }()
  80. out, err := s.d.Cmd("volume", "create")
  81. c.Assert(err, check.NotNil)
  82. c.Assert(out, checker.Contains, fmt.Sprintf("Error response from daemon: plugin %s failed with error:", authzPluginNameWithTag))
  83. // disable the plugin
  84. _, err = s.d.Cmd("plugin", "disable", authzPluginNameWithTag)
  85. c.Assert(err, checker.IsNil)
  86. // now test to see if the docker api works.
  87. _, err = s.d.Cmd("volume", "create")
  88. c.Assert(err, checker.IsNil)
  89. }
  90. func (s *DockerAuthzV2Suite) TestAuthZPluginRejectVolumeRequests(c *check.C) {
  91. testRequires(c, DaemonIsLinux, IsAmd64, Network)
  92. // Install authz plugin
  93. _, err := s.d.Cmd("plugin", "install", "--grant-all-permissions", authzPluginNameWithTag)
  94. c.Assert(err, checker.IsNil)
  95. // restart the daemon with the plugin
  96. s.d.Restart(c, "--authorization-plugin="+authzPluginNameWithTag)
  97. // defer disabling the plugin
  98. defer func() {
  99. s.d.Restart(c)
  100. _, err = s.d.Cmd("plugin", "disable", authzPluginNameWithTag)
  101. c.Assert(err, checker.IsNil)
  102. _, err = s.d.Cmd("plugin", "rm", authzPluginNameWithTag)
  103. c.Assert(err, checker.IsNil)
  104. }()
  105. out, err := s.d.Cmd("volume", "create")
  106. c.Assert(err, check.NotNil)
  107. c.Assert(out, checker.Contains, fmt.Sprintf("Error response from daemon: plugin %s failed with error:", authzPluginNameWithTag))
  108. out, err = s.d.Cmd("volume", "ls")
  109. c.Assert(err, check.NotNil)
  110. c.Assert(out, checker.Contains, fmt.Sprintf("Error response from daemon: plugin %s failed with error:", authzPluginNameWithTag))
  111. // The plugin will block the command before it can determine the volume does not exist
  112. out, err = s.d.Cmd("volume", "rm", "test")
  113. c.Assert(err, check.NotNil)
  114. c.Assert(out, checker.Contains, fmt.Sprintf("Error response from daemon: plugin %s failed with error:", authzPluginNameWithTag))
  115. out, err = s.d.Cmd("volume", "inspect", "test")
  116. c.Assert(err, check.NotNil)
  117. c.Assert(out, checker.Contains, fmt.Sprintf("Error response from daemon: plugin %s failed with error:", authzPluginNameWithTag))
  118. out, err = s.d.Cmd("volume", "prune", "-f")
  119. c.Assert(err, check.NotNil)
  120. c.Assert(out, checker.Contains, fmt.Sprintf("Error response from daemon: plugin %s failed with error:", authzPluginNameWithTag))
  121. }
  122. func (s *DockerAuthzV2Suite) TestAuthZPluginBadManifestFailsDaemonStart(c *check.C) {
  123. testRequires(c, DaemonIsLinux, IsAmd64, Network)
  124. // Install authz plugin with bad manifest
  125. _, err := s.d.Cmd("plugin", "install", "--grant-all-permissions", authzPluginBadManifestName)
  126. c.Assert(err, checker.IsNil)
  127. // start the daemon with the plugin, it will error
  128. c.Assert(s.d.RestartWithError("--authorization-plugin="+authzPluginBadManifestName), check.NotNil)
  129. // restarting the daemon without requiring the plugin will succeed
  130. s.d.Restart(c)
  131. }
  132. func (s *DockerAuthzV2Suite) TestNonexistentAuthZPluginFailsDaemonStart(c *check.C) {
  133. testRequires(c, DaemonIsLinux, Network)
  134. // start the daemon with a non-existent authz plugin, it will error
  135. c.Assert(s.d.RestartWithError("--authorization-plugin="+nonexistentAuthzPluginName), check.NotNil)
  136. // restarting the daemon without requiring the plugin will succeed
  137. s.d.Start(c)
  138. }