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