docker_cli_logout_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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/testutil"
  11. "gotest.tools/v3/assert"
  12. )
  13. func (s *DockerRegistryAuthHtpasswdSuite) TestLogoutWithExternalAuth(c *testing.T) {
  14. ctx := testutil.GetContext(c)
  15. s.d.StartWithBusybox(ctx, c)
  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. osPath := os.Getenv("PATH")
  21. testPath := fmt.Sprintf("%s%c%s", osPath, filepath.ListSeparator, absolute)
  22. c.Setenv("PATH", testPath)
  23. repoName := fmt.Sprintf("%v/dockercli/busybox:authtest", privateRegistryURL)
  24. tmp, err := os.MkdirTemp("", "integration-cli-")
  25. assert.NilError(c, err)
  26. defer os.RemoveAll(tmp)
  27. externalAuthConfig := `{ "credsStore": "shell-test" }`
  28. configPath := filepath.Join(tmp, "config.json")
  29. err = os.WriteFile(configPath, []byte(externalAuthConfig), 0o644)
  30. assert.NilError(c, err)
  31. _, err = s.d.Cmd("--config", tmp, "login", "-u", s.reg.Username(), "-p", s.reg.Password(), privateRegistryURL)
  32. assert.NilError(c, err)
  33. b, err := os.ReadFile(configPath)
  34. assert.NilError(c, err)
  35. assert.Assert(c, !strings.Contains(string(b), `"auth":`))
  36. assert.Assert(c, strings.Contains(string(b), privateRegistryURL))
  37. _, err = s.d.Cmd("--config", tmp, "tag", "busybox", repoName)
  38. assert.NilError(c, err)
  39. _, err = s.d.Cmd("--config", tmp, "push", repoName)
  40. assert.NilError(c, err)
  41. _, err = s.d.Cmd("--config", tmp, "logout", privateRegistryURL)
  42. assert.NilError(c, err)
  43. b, err = os.ReadFile(configPath)
  44. assert.NilError(c, err)
  45. assert.Assert(c, !strings.Contains(string(b), privateRegistryURL))
  46. // check I cannot pull anymore
  47. out, err := s.d.Cmd("--config", tmp, "pull", repoName)
  48. assert.ErrorContains(c, err, "", out)
  49. assert.Assert(c, strings.Contains(out, "no basic auth credentials"))
  50. }
  51. // #23100
  52. func (s *DockerRegistryAuthHtpasswdSuite) TestLogoutWithWrongHostnamesStored(c *testing.T) {
  53. workingDir, err := os.Getwd()
  54. assert.NilError(c, err)
  55. absolute, err := filepath.Abs(filepath.Join(workingDir, "fixtures", "auth"))
  56. assert.NilError(c, err)
  57. osPath := os.Getenv("PATH")
  58. testPath := fmt.Sprintf("%s%c%s", osPath, filepath.ListSeparator, absolute)
  59. c.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), 0o644)
  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. }