github.com/iaas-resource-provision/iaas-rpc@v1.0.7-0.20211021023331-ed21f798c408/internal/command/logout.go (about) 1 package command 2 3 import ( 4 "fmt" 5 "path/filepath" 6 "strings" 7 8 svchost "github.com/iaas-resource-provision/iaas-rpc-svchost" 9 "github.com/iaas-resource-provision/iaas-rpc/internal/command/cliconfig" 10 "github.com/iaas-resource-provision/iaas-rpc/internal/tfdiags" 11 ) 12 13 // LogoutCommand is a Command implementation which removes stored credentials 14 // for a remote service host. 15 type LogoutCommand struct { 16 Meta 17 } 18 19 // Run implements cli.Command. 20 func (c *LogoutCommand) Run(args []string) int { 21 args = c.Meta.process(args) 22 cmdFlags := c.Meta.defaultFlagSet("logout") 23 cmdFlags.Usage = func() { c.Ui.Error(c.Help()) } 24 if err := cmdFlags.Parse(args); err != nil { 25 return 1 26 } 27 28 args = cmdFlags.Args() 29 if len(args) > 1 { 30 c.Ui.Error( 31 "The logout command expects at most one argument: the host to log out of.") 32 cmdFlags.Usage() 33 return 1 34 } 35 36 var diags tfdiags.Diagnostics 37 38 givenHostname := "app.terraform.io" 39 if len(args) != 0 { 40 givenHostname = args[0] 41 } 42 43 hostname, err := svchost.ForComparison(givenHostname) 44 if err != nil { 45 diags = diags.Append(tfdiags.Sourceless( 46 tfdiags.Error, 47 "Invalid hostname", 48 fmt.Sprintf("The given hostname %q is not valid: %s.", givenHostname, err.Error()), 49 )) 50 c.showDiagnostics(diags) 51 return 1 52 } 53 54 // From now on, since we've validated the given hostname, we should use 55 // dispHostname in the UI to ensure we're presenting it in the canonical 56 // form, in case that helps users with debugging when things aren't 57 // working as expected. (Perhaps the normalization is part of the cause.) 58 dispHostname := hostname.ForDisplay() 59 60 creds := c.Services.CredentialsSource().(*cliconfig.CredentialsSource) 61 filename, _ := creds.CredentialsFilePath() 62 credsCtx := &loginCredentialsContext{ 63 Location: creds.HostCredentialsLocation(hostname), 64 LocalFilename: filename, // empty in the very unlikely event that we can't select a config directory for this user 65 HelperType: creds.CredentialsHelperType(), 66 } 67 68 if credsCtx.Location == cliconfig.CredentialsInOtherFile { 69 diags = diags.Append(tfdiags.Sourceless( 70 tfdiags.Error, 71 fmt.Sprintf("Credentials for %s are manually configured", dispHostname), 72 "The \"terraform logout\" command cannot log out because credentials for this host are manually configured in a CLI configuration file.\n\nTo log out, revoke the existing credentials and remove that block from the CLI configuration.", 73 )) 74 } 75 76 if diags.HasErrors() { 77 c.showDiagnostics(diags) 78 return 1 79 } 80 81 // credsCtx might not be set if we're using a mock credentials source 82 // in a test, but it should always be set in normal use. 83 if credsCtx != nil { 84 switch credsCtx.Location { 85 case cliconfig.CredentialsNotAvailable: 86 c.Ui.Output(fmt.Sprintf("No credentials for %s are stored.\n", dispHostname)) 87 return 0 88 case cliconfig.CredentialsViaHelper: 89 c.Ui.Output(fmt.Sprintf("Removing the stored credentials for %s from the configured\n%q credentials helper.\n", dispHostname, credsCtx.HelperType)) 90 case cliconfig.CredentialsInPrimaryFile: 91 c.Ui.Output(fmt.Sprintf("Removing the stored credentials for %s from the following file:\n %s\n", dispHostname, credsCtx.LocalFilename)) 92 } 93 } 94 95 err = creds.ForgetForHost(hostname) 96 if err != nil { 97 diags = diags.Append(tfdiags.Sourceless( 98 tfdiags.Error, 99 "Failed to remove API token", 100 fmt.Sprintf("Unable to remove stored API token: %s", err), 101 )) 102 } 103 104 c.showDiagnostics(diags) 105 if diags.HasErrors() { 106 return 1 107 } 108 109 c.Ui.Output( 110 fmt.Sprintf( 111 c.Colorize().Color(strings.TrimSpace(` 112 [green][bold]Success![reset] [bold]Terraform has removed the stored API token for %s.[reset] 113 `)), 114 dispHostname, 115 ) + "\n", 116 ) 117 118 return 0 119 } 120 121 // Help implements cli.Command. 122 func (c *LogoutCommand) Help() string { 123 defaultFile := c.defaultOutputFile() 124 if defaultFile == "" { 125 // Because this is just for the help message and it's very unlikely 126 // that a user wouldn't have a functioning home directory anyway, 127 // we'll just use a placeholder here. The real command has some 128 // more complex behavior for this case. This result is not correct 129 // on all platforms, but given how unlikely we are to hit this case 130 // that seems okay. 131 defaultFile = "~/.terraform/credentials.tfrc.json" 132 } 133 134 helpText := ` 135 Usage: terraform [global options] logout [hostname] 136 137 Removes locally-stored credentials for specified hostname. 138 139 Note: the API token is only removed from local storage, not destroyed on the 140 remote server, so it will remain valid until manually revoked. 141 142 If no hostname is provided, the default hostname is app.terraform.io. 143 %s 144 ` 145 return strings.TrimSpace(helpText) 146 } 147 148 // Synopsis implements cli.Command. 149 func (c *LogoutCommand) Synopsis() string { 150 return "Remove locally-stored credentials for a remote host" 151 } 152 153 func (c *LogoutCommand) defaultOutputFile() string { 154 if c.CLIConfigDir == "" { 155 return "" // no default available 156 } 157 return filepath.Join(c.CLIConfigDir, "credentials.tfrc.json") 158 }