docker_cli_logout_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package main
  2. import (
  3. "bytes"
  4. "fmt"
  5. "os"
  6. "os/exec"
  7. "path/filepath"
  8. "strings"
  9. "testing"
  10. "github.com/docker/docker/integration-cli/cli"
  11. "github.com/docker/docker/testutil"
  12. "gotest.tools/v3/assert"
  13. )
  14. func (s *DockerRegistryAuthHtpasswdSuite) TestLogoutWithExternalAuth(c *testing.T) {
  15. ctx := testutil.GetContext(c)
  16. s.d.StartWithBusybox(ctx, c)
  17. workingDir, err := os.Getwd()
  18. assert.NilError(c, err)
  19. absolute, err := filepath.Abs(filepath.Join(workingDir, "fixtures", "auth"))
  20. assert.NilError(c, err)
  21. osPath := os.Getenv("PATH")
  22. testPath := fmt.Sprintf("%s%c%s", osPath, filepath.ListSeparator, absolute)
  23. c.Setenv("PATH", testPath)
  24. imgRepoName := fmt.Sprintf("%v/dockercli/busybox:authtest", privateRegistryURL)
  25. tmp, err := os.MkdirTemp("", "integration-cli-")
  26. assert.NilError(c, err)
  27. defer os.RemoveAll(tmp)
  28. externalAuthConfig := `{ "credsStore": "shell-test" }`
  29. configPath := filepath.Join(tmp, "config.json")
  30. err = os.WriteFile(configPath, []byte(externalAuthConfig), 0o644)
  31. assert.NilError(c, err)
  32. _, err = s.d.Cmd("--config", tmp, "login", "-u", s.reg.Username(), "-p", s.reg.Password(), privateRegistryURL)
  33. assert.NilError(c, err)
  34. b, err := os.ReadFile(configPath)
  35. assert.NilError(c, err)
  36. assert.Assert(c, !strings.Contains(string(b), `"auth":`))
  37. assert.Assert(c, strings.Contains(string(b), privateRegistryURL))
  38. _, err = s.d.Cmd("--config", tmp, "tag", "busybox", imgRepoName)
  39. assert.NilError(c, err)
  40. _, err = s.d.Cmd("--config", tmp, "push", imgRepoName)
  41. assert.NilError(c, err)
  42. _, err = s.d.Cmd("--config", tmp, "logout", privateRegistryURL)
  43. assert.NilError(c, err)
  44. b, err = os.ReadFile(configPath)
  45. assert.NilError(c, err)
  46. assert.Assert(c, !strings.Contains(string(b), privateRegistryURL))
  47. // check I cannot pull anymore
  48. out, err := s.d.Cmd("--config", tmp, "pull", imgRepoName)
  49. assert.ErrorContains(c, err, "", out)
  50. assert.Assert(c, strings.Contains(out, "no basic auth credentials"))
  51. }
  52. // #23100
  53. func (s *DockerRegistryAuthHtpasswdSuite) TestLogoutWithWrongHostnamesStored(c *testing.T) {
  54. workingDir, err := os.Getwd()
  55. assert.NilError(c, err)
  56. absolute, err := filepath.Abs(filepath.Join(workingDir, "fixtures", "auth"))
  57. assert.NilError(c, err)
  58. osPath := os.Getenv("PATH")
  59. testPath := fmt.Sprintf("%s%c%s", osPath, filepath.ListSeparator, absolute)
  60. c.Setenv("PATH", testPath)
  61. cmd := exec.Command("docker-credential-shell-test", "store")
  62. stdin := bytes.NewReader([]byte(fmt.Sprintf(`{"ServerURL": "https://%s", "Username": "%s", "Secret": "%s"}`, privateRegistryURL, s.reg.Username(), s.reg.Password())))
  63. cmd.Stdin = stdin
  64. assert.NilError(c, cmd.Run())
  65. tmp, err := os.MkdirTemp("", "integration-cli-")
  66. assert.NilError(c, err)
  67. externalAuthConfig := fmt.Sprintf(`{ "auths": {"https://%s": {}}, "credsStore": "shell-test" }`, privateRegistryURL)
  68. configPath := filepath.Join(tmp, "config.json")
  69. err = os.WriteFile(configPath, []byte(externalAuthConfig), 0o644)
  70. assert.NilError(c, err)
  71. cli.DockerCmd(c, "--config", tmp, "login", "-u", s.reg.Username(), "-p", s.reg.Password(), privateRegistryURL)
  72. b, err := os.ReadFile(configPath)
  73. assert.NilError(c, err)
  74. assert.Assert(c, strings.Contains(string(b), fmt.Sprintf(`"https://%s": {}`, privateRegistryURL)))
  75. assert.Assert(c, strings.Contains(string(b), fmt.Sprintf(`"%s": {}`, privateRegistryURL)))
  76. cli.DockerCmd(c, "--config", tmp, "logout", privateRegistryURL)
  77. b, err = os.ReadFile(configPath)
  78. assert.NilError(c, err)
  79. assert.Assert(c, !strings.Contains(string(b), fmt.Sprintf(`"https://%s": {}`, privateRegistryURL)))
  80. assert.Assert(c, !strings.Contains(string(b), fmt.Sprintf(`"%s": {}`, privateRegistryURL)))
  81. }