github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/engine/integration-cli/docker_cli_logout_test.go (about)

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