docker_cli_logout_test.go 3.5 KB

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