github.com/lacework-dev/go-moby@v20.10.12+incompatible/integration-cli/docker_cli_logout_test.go (about)

     1  package main
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"os"
     8  	"os/exec"
     9  	"path/filepath"
    10  	"strings"
    11  	"testing"
    12  
    13  	"gotest.tools/v3/assert"
    14  )
    15  
    16  func (s *DockerRegistryAuthHtpasswdSuite) TestLogoutWithExternalAuth(c *testing.T) {
    17  	s.d.StartWithBusybox(c)
    18  
    19  	osPath := os.Getenv("PATH")
    20  	defer os.Setenv("PATH", osPath)
    21  
    22  	workingDir, err := os.Getwd()
    23  	assert.NilError(c, err)
    24  	absolute, err := filepath.Abs(filepath.Join(workingDir, "fixtures", "auth"))
    25  	assert.NilError(c, err)
    26  	testPath := fmt.Sprintf("%s%c%s", osPath, filepath.ListSeparator, absolute)
    27  
    28  	os.Setenv("PATH", testPath)
    29  
    30  	repoName := fmt.Sprintf("%v/dockercli/busybox:authtest", privateRegistryURL)
    31  
    32  	tmp, err := ioutil.TempDir("", "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 = ioutil.WriteFile(configPath, []byte(externalAuthConfig), 0644)
    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 := ioutil.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", repoName)
    51  	assert.NilError(c, err)
    52  	_, err = s.d.Cmd("--config", tmp, "push", repoName)
    53  	assert.NilError(c, err)
    54  	_, err = s.d.Cmd("--config", tmp, "logout", privateRegistryURL)
    55  	assert.NilError(c, err)
    56  
    57  	b, err = ioutil.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", repoName)
    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  	osPath := os.Getenv("PATH")
    70  	defer os.Setenv("PATH", osPath)
    71  
    72  	workingDir, err := os.Getwd()
    73  	assert.NilError(c, err)
    74  	absolute, err := filepath.Abs(filepath.Join(workingDir, "fixtures", "auth"))
    75  	assert.NilError(c, err)
    76  	testPath := fmt.Sprintf("%s%c%s", osPath, filepath.ListSeparator, absolute)
    77  
    78  	os.Setenv("PATH", testPath)
    79  
    80  	cmd := exec.Command("docker-credential-shell-test", "store")
    81  	stdin := bytes.NewReader([]byte(fmt.Sprintf(`{"ServerURL": "https://%s", "Username": "%s", "Secret": "%s"}`, privateRegistryURL, s.reg.Username(), s.reg.Password())))
    82  	cmd.Stdin = stdin
    83  	assert.NilError(c, cmd.Run())
    84  
    85  	tmp, err := ioutil.TempDir("", "integration-cli-")
    86  	assert.NilError(c, err)
    87  
    88  	externalAuthConfig := fmt.Sprintf(`{ "auths": {"https://%s": {}}, "credsStore": "shell-test" }`, privateRegistryURL)
    89  
    90  	configPath := filepath.Join(tmp, "config.json")
    91  	err = ioutil.WriteFile(configPath, []byte(externalAuthConfig), 0644)
    92  	assert.NilError(c, err)
    93  
    94  	dockerCmd(c, "--config", tmp, "login", "-u", s.reg.Username(), "-p", s.reg.Password(), privateRegistryURL)
    95  
    96  	b, err := ioutil.ReadFile(configPath)
    97  	assert.NilError(c, err)
    98  	assert.Assert(c, strings.Contains(string(b), fmt.Sprintf(`"https://%s": {}`, privateRegistryURL)))
    99  	assert.Assert(c, strings.Contains(string(b), fmt.Sprintf(`"%s": {}`, privateRegistryURL)))
   100  
   101  	dockerCmd(c, "--config", tmp, "logout", privateRegistryURL)
   102  
   103  	b, err = ioutil.ReadFile(configPath)
   104  	assert.NilError(c, err)
   105  	assert.Assert(c, !strings.Contains(string(b), fmt.Sprintf(`"https://%s": {}`, privateRegistryURL)))
   106  	assert.Assert(c, !strings.Contains(string(b), fmt.Sprintf(`"%s": {}`, privateRegistryURL)))
   107  }