github.com/ssdev-go/moby@v17.12.1-ce-rc2+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  
    11  	"github.com/docker/docker/integration-cli/checker"
    12  	"github.com/go-check/check"
    13  )
    14  
    15  func (s *DockerRegistryAuthHtpasswdSuite) TestLogoutWithExternalAuth(c *check.C) {
    16  	s.d.StartWithBusybox(c)
    17  
    18  	osPath := os.Getenv("PATH")
    19  	defer os.Setenv("PATH", osPath)
    20  
    21  	workingDir, err := os.Getwd()
    22  	c.Assert(err, checker.IsNil)
    23  	absolute, err := filepath.Abs(filepath.Join(workingDir, "fixtures", "auth"))
    24  	c.Assert(err, checker.IsNil)
    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 := ioutil.TempDir("", "integration-cli-")
    32  	c.Assert(err, checker.IsNil)
    33  	defer os.RemoveAll(tmp)
    34  
    35  	externalAuthConfig := `{ "credsStore": "shell-test" }`
    36  
    37  	configPath := filepath.Join(tmp, "config.json")
    38  	err = ioutil.WriteFile(configPath, []byte(externalAuthConfig), 0644)
    39  	c.Assert(err, checker.IsNil)
    40  
    41  	_, err = s.d.Cmd("--config", tmp, "login", "-u", s.reg.Username(), "-p", s.reg.Password(), privateRegistryURL)
    42  	c.Assert(err, checker.IsNil)
    43  
    44  	b, err := ioutil.ReadFile(configPath)
    45  	c.Assert(err, checker.IsNil)
    46  	c.Assert(string(b), checker.Not(checker.Contains), "\"auth\":")
    47  	c.Assert(string(b), checker.Contains, privateRegistryURL)
    48  
    49  	_, err = s.d.Cmd("--config", tmp, "tag", "busybox", repoName)
    50  	c.Assert(err, checker.IsNil)
    51  	_, err = s.d.Cmd("--config", tmp, "push", repoName)
    52  	c.Assert(err, checker.IsNil)
    53  	_, err = s.d.Cmd("--config", tmp, "logout", privateRegistryURL)
    54  	c.Assert(err, checker.IsNil)
    55  
    56  	b, err = ioutil.ReadFile(configPath)
    57  	c.Assert(err, checker.IsNil)
    58  	c.Assert(string(b), checker.Not(checker.Contains), privateRegistryURL)
    59  
    60  	// check I cannot pull anymore
    61  	out, err := s.d.Cmd("--config", tmp, "pull", repoName)
    62  	c.Assert(err, check.NotNil, check.Commentf(out))
    63  	c.Assert(out, checker.Contains, "no basic auth credentials")
    64  }
    65  
    66  // #23100
    67  func (s *DockerRegistryAuthHtpasswdSuite) TestLogoutWithWrongHostnamesStored(c *check.C) {
    68  	osPath := os.Getenv("PATH")
    69  	defer os.Setenv("PATH", osPath)
    70  
    71  	workingDir, err := os.Getwd()
    72  	c.Assert(err, checker.IsNil)
    73  	absolute, err := filepath.Abs(filepath.Join(workingDir, "fixtures", "auth"))
    74  	c.Assert(err, checker.IsNil)
    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  	c.Assert(cmd.Run(), checker.IsNil)
    83  
    84  	tmp, err := ioutil.TempDir("", "integration-cli-")
    85  	c.Assert(err, checker.IsNil)
    86  
    87  	externalAuthConfig := fmt.Sprintf(`{ "auths": {"https://%s": {}}, "credsStore": "shell-test" }`, privateRegistryURL)
    88  
    89  	configPath := filepath.Join(tmp, "config.json")
    90  	err = ioutil.WriteFile(configPath, []byte(externalAuthConfig), 0644)
    91  	c.Assert(err, checker.IsNil)
    92  
    93  	dockerCmd(c, "--config", tmp, "login", "-u", s.reg.Username(), "-p", s.reg.Password(), privateRegistryURL)
    94  
    95  	b, err := ioutil.ReadFile(configPath)
    96  	c.Assert(err, checker.IsNil)
    97  	c.Assert(string(b), checker.Contains, fmt.Sprintf("\"https://%s\": {}", privateRegistryURL))
    98  	c.Assert(string(b), checker.Contains, fmt.Sprintf("\"%s\": {}", privateRegistryURL))
    99  
   100  	dockerCmd(c, "--config", tmp, "logout", privateRegistryURL)
   101  
   102  	b, err = ioutil.ReadFile(configPath)
   103  	c.Assert(err, checker.IsNil)
   104  	c.Assert(string(b), checker.Not(checker.Contains), fmt.Sprintf("\"https://%s\": {}", privateRegistryURL))
   105  	c.Assert(string(b), checker.Not(checker.Contains), fmt.Sprintf("\"%s\": {}", privateRegistryURL))
   106  }