integration-cli/docker_cli_logout_test.go
0eccc383
 package main
 
 import (
67d752ac
 	"bytes"
0eccc383
 	"fmt"
 	"io/ioutil"
 	"os"
67d752ac
 	"os/exec"
0eccc383
 	"path/filepath"
6345208b
 	"strings"
e25352a4
 	"testing"
0eccc383
 
6345208b
 	"gotest.tools/assert"
0eccc383
 )
 
64a928a3
 func (s *DockerRegistryAuthHtpasswdSuite) TestLogoutWithExternalAuth(c *testing.T) {
8d6df8a0
 	s.d.StartWithBusybox(c)
12828001
 
0eccc383
 	osPath := os.Getenv("PATH")
 	defer os.Setenv("PATH", osPath)
 
 	workingDir, err := os.Getwd()
6345208b
 	assert.NilError(c, err)
0eccc383
 	absolute, err := filepath.Abs(filepath.Join(workingDir, "fixtures", "auth"))
6345208b
 	assert.NilError(c, err)
0eccc383
 	testPath := fmt.Sprintf("%s%c%s", osPath, filepath.ListSeparator, absolute)
 
 	os.Setenv("PATH", testPath)
 
 	repoName := fmt.Sprintf("%v/dockercli/busybox:authtest", privateRegistryURL)
 
 	tmp, err := ioutil.TempDir("", "integration-cli-")
6345208b
 	assert.NilError(c, err)
12828001
 	defer os.RemoveAll(tmp)
0eccc383
 
 	externalAuthConfig := `{ "credsStore": "shell-test" }`
 
 	configPath := filepath.Join(tmp, "config.json")
 	err = ioutil.WriteFile(configPath, []byte(externalAuthConfig), 0644)
6345208b
 	assert.NilError(c, err)
0eccc383
 
12828001
 	_, err = s.d.Cmd("--config", tmp, "login", "-u", s.reg.Username(), "-p", s.reg.Password(), privateRegistryURL)
6345208b
 	assert.NilError(c, err)
0eccc383
 
 	b, err := ioutil.ReadFile(configPath)
6345208b
 	assert.NilError(c, err)
 	assert.Assert(c, !strings.Contains(string(b), `"auth":`))
 	assert.Assert(c, strings.Contains(string(b), privateRegistryURL))
0eccc383
 
12828001
 	_, err = s.d.Cmd("--config", tmp, "tag", "busybox", repoName)
6345208b
 	assert.NilError(c, err)
12828001
 	_, err = s.d.Cmd("--config", tmp, "push", repoName)
6345208b
 	assert.NilError(c, err)
12828001
 	_, err = s.d.Cmd("--config", tmp, "logout", privateRegistryURL)
6345208b
 	assert.NilError(c, err)
0eccc383
 
 	b, err = ioutil.ReadFile(configPath)
6345208b
 	assert.NilError(c, err)
 	assert.Assert(c, !strings.Contains(string(b), privateRegistryURL))
0eccc383
 
 	// check I cannot pull anymore
12828001
 	out, err := s.d.Cmd("--config", tmp, "pull", repoName)
6345208b
 	assert.ErrorContains(c, err, "", out)
 	assert.Assert(c, strings.Contains(out, "no basic auth credentials"))
0eccc383
 }
67d752ac
 
 // #23100
64a928a3
 func (s *DockerRegistryAuthHtpasswdSuite) TestLogoutWithWrongHostnamesStored(c *testing.T) {
67d752ac
 	osPath := os.Getenv("PATH")
 	defer os.Setenv("PATH", osPath)
 
 	workingDir, err := os.Getwd()
6345208b
 	assert.NilError(c, err)
67d752ac
 	absolute, err := filepath.Abs(filepath.Join(workingDir, "fixtures", "auth"))
6345208b
 	assert.NilError(c, err)
67d752ac
 	testPath := fmt.Sprintf("%s%c%s", osPath, filepath.ListSeparator, absolute)
 
 	os.Setenv("PATH", testPath)
 
 	cmd := exec.Command("docker-credential-shell-test", "store")
4300e5e8
 	stdin := bytes.NewReader([]byte(fmt.Sprintf(`{"ServerURL": "https://%s", "Username": "%s", "Secret": "%s"}`, privateRegistryURL, s.reg.Username(), s.reg.Password())))
67d752ac
 	cmd.Stdin = stdin
6345208b
 	assert.NilError(c, cmd.Run())
67d752ac
 
 	tmp, err := ioutil.TempDir("", "integration-cli-")
6345208b
 	assert.NilError(c, err)
67d752ac
 
 	externalAuthConfig := fmt.Sprintf(`{ "auths": {"https://%s": {}}, "credsStore": "shell-test" }`, privateRegistryURL)
 
 	configPath := filepath.Join(tmp, "config.json")
 	err = ioutil.WriteFile(configPath, []byte(externalAuthConfig), 0644)
6345208b
 	assert.NilError(c, err)
67d752ac
 
4300e5e8
 	dockerCmd(c, "--config", tmp, "login", "-u", s.reg.Username(), "-p", s.reg.Password(), privateRegistryURL)
67d752ac
 
 	b, err := ioutil.ReadFile(configPath)
6345208b
 	assert.NilError(c, err)
 	assert.Assert(c, strings.Contains(string(b), fmt.Sprintf(`"https://%s": {}`, privateRegistryURL)))
 	assert.Assert(c, strings.Contains(string(b), fmt.Sprintf(`"%s": {}`, privateRegistryURL)))
67d752ac
 
 	dockerCmd(c, "--config", tmp, "logout", privateRegistryURL)
 
 	b, err = ioutil.ReadFile(configPath)
6345208b
 	assert.NilError(c, err)
 	assert.Assert(c, !strings.Contains(string(b), fmt.Sprintf(`"https://%s": {}`, privateRegistryURL)))
 	assert.Assert(c, !strings.Contains(string(b), fmt.Sprintf(`"%s": {}`, privateRegistryURL)))
67d752ac
 }