github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/cmd/podman/logout.go (about) 1 package main 2 3 import ( 4 "fmt" 5 6 buildahcli "github.com/containers/buildah/pkg/cli" 7 "github.com/containers/image/v5/docker" 8 "github.com/containers/image/v5/pkg/docker/config" 9 "github.com/containers/libpod/cmd/podman/cliconfig" 10 "github.com/containers/libpod/cmd/podman/shared" 11 "github.com/containers/libpod/pkg/registries" 12 "github.com/pkg/errors" 13 "github.com/sirupsen/logrus" 14 "github.com/spf13/cobra" 15 ) 16 17 var ( 18 logoutCommand cliconfig.LogoutValues 19 logoutDescription = "Remove the cached username and password for the registry." 20 _logoutCommand = &cobra.Command{ 21 Use: "logout [flags] REGISTRY", 22 Short: "Logout of a container registry", 23 Long: logoutDescription, 24 RunE: func(cmd *cobra.Command, args []string) error { 25 logoutCommand.InputArgs = args 26 logoutCommand.GlobalFlags = MainGlobalOpts 27 logoutCommand.Remote = remoteclient 28 return logoutCmd(&logoutCommand) 29 }, 30 Example: `podman logout quay.io 31 podman logout --all`, 32 } 33 ) 34 35 func init() { 36 if !remote { 37 _logoutCommand.Example = fmt.Sprintf("%s\n podman logout --authfile authdir/myauths.json quay.io", _logoutCommand.Example) 38 39 } 40 logoutCommand.Command = _logoutCommand 41 logoutCommand.SetHelpTemplate(HelpTemplate()) 42 logoutCommand.SetUsageTemplate(UsageTemplate()) 43 flags := logoutCommand.Flags() 44 flags.BoolVarP(&logoutCommand.All, "all", "a", false, "Remove the cached credentials for all registries in the auth file") 45 flags.StringVar(&logoutCommand.Authfile, "authfile", buildahcli.GetDefaultAuthFile(), "Path of the authentication file. Use REGISTRY_AUTH_FILE environment variable to override") 46 markFlagHiddenForRemoteClient("authfile", flags) 47 } 48 49 // logoutCmd uses the authentication package to remove the authenticated of a registry 50 // stored in the auth.json file 51 func logoutCmd(c *cliconfig.LogoutValues) error { 52 args := c.InputArgs 53 if len(args) > 1 { 54 return errors.Errorf("too many arguments, logout takes at most 1 argument") 55 } 56 var server string 57 if len(args) == 0 && !c.All { 58 registriesFromFile, err := registries.GetRegistries() 59 if err != nil || len(registriesFromFile) == 0 { 60 return errors.Errorf("no registries found in registries.conf, a registry must be provided") 61 } 62 63 server = registriesFromFile[0] 64 logrus.Debugf("registry not specified, default to the first registry %q from registries.conf", server) 65 } 66 if len(args) == 1 { 67 server = scrubServer(args[0]) 68 } 69 70 sc, err := shared.GetSystemContext(c.Authfile) 71 if err != nil { 72 return err 73 } 74 75 if c.All { 76 if err := config.RemoveAllAuthentication(sc); err != nil { 77 return err 78 } 79 fmt.Println("Removed login credentials for all registries") 80 return nil 81 } 82 83 err = config.RemoveAuthentication(sc, server) 84 switch errors.Cause(err) { 85 case nil: 86 fmt.Printf("Removed login credentials for %s\n", server) 87 return nil 88 case config.ErrNotLoggedIn: 89 // username of user logged in to server (if one exists) 90 authConfig, err := config.GetCredentials(sc, server) 91 if err != nil { 92 return errors.Wrapf(err, "error reading auth file") 93 } 94 islogin := docker.CheckAuth(getContext(), sc, authConfig.Username, authConfig.Password, server) 95 if authConfig.IdentityToken != "" && authConfig.Username != "" && authConfig.Password != "" && islogin == nil { 96 fmt.Printf("Not logged into %s with podman. Existing credentials were established via docker login. Please use docker logout instead.\n", server) 97 return nil 98 } 99 fmt.Printf("Not logged into %s\n", server) 100 return nil 101 default: 102 return errors.Wrapf(err, "error logging out of %q", server) 103 } 104 }