mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
feat: delete API token in /logout API (#1770)
* delete API token in logout api * add deleteapikeybyid to databasefake * set blank cookie on logout always * refactor logout flow, add unit tests * update logout messsage * use read-only file mode for windows * fix file mode on windows for cleanup * change file permissions on windows * assert error is not nil * refactor cli * try different file mode on windows * try different file mode on windows * try keeping the files open on Windows * fix the error message on Windows
This commit is contained in:
+30
-22
@@ -3,6 +3,7 @@ package cli
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"golang.org/x/xerrors"
|
||||
@@ -15,11 +16,16 @@ func logout() *cobra.Command {
|
||||
Use: "logout",
|
||||
Short: "Remove the local authenticated session",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
var isLoggedOut bool
|
||||
client, err := createClient(cmd)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var errors []error
|
||||
|
||||
config := createConfig(cmd)
|
||||
|
||||
_, err := cliui.Prompt(cmd, cliui.PromptOptions{
|
||||
_, err = cliui.Prompt(cmd, cliui.PromptOptions{
|
||||
Text: "Are you sure you want to logout?",
|
||||
IsConfirm: true,
|
||||
Default: "yes",
|
||||
@@ -28,38 +34,40 @@ func logout() *cobra.Command {
|
||||
return err
|
||||
}
|
||||
|
||||
err = config.URL().Delete()
|
||||
err = client.Logout(cmd.Context())
|
||||
if err != nil {
|
||||
// Only throw error if the URL configuration file is present,
|
||||
// otherwise the user is already logged out, and we proceed
|
||||
if !os.IsNotExist(err) {
|
||||
return xerrors.Errorf("remove URL file: %w", err)
|
||||
}
|
||||
isLoggedOut = true
|
||||
errors = append(errors, xerrors.Errorf("logout api: %w", err))
|
||||
}
|
||||
|
||||
err = config.URL().Delete()
|
||||
// Only throw error if the URL configuration file is present,
|
||||
// otherwise the user is already logged out, and we proceed
|
||||
if err != nil && !os.IsNotExist(err) {
|
||||
errors = append(errors, xerrors.Errorf("remove URL file: %w", err))
|
||||
}
|
||||
|
||||
err = config.Session().Delete()
|
||||
if err != nil {
|
||||
// Only throw error if the session configuration file is present,
|
||||
// otherwise the user is already logged out, and we proceed
|
||||
if !os.IsNotExist(err) {
|
||||
return xerrors.Errorf("remove session file: %w", err)
|
||||
}
|
||||
isLoggedOut = true
|
||||
// Only throw error if the session configuration file is present,
|
||||
// otherwise the user is already logged out, and we proceed
|
||||
if err != nil && !os.IsNotExist(err) {
|
||||
errors = append(errors, xerrors.Errorf("remove session file: %w", err))
|
||||
}
|
||||
|
||||
err = config.Organization().Delete()
|
||||
// If the organization configuration file is absent, we still proceed
|
||||
if err != nil && !os.IsNotExist(err) {
|
||||
return xerrors.Errorf("remove organization file: %w", err)
|
||||
errors = append(errors, xerrors.Errorf("remove organization file: %w", err))
|
||||
}
|
||||
|
||||
// If the user was already logged out, we show them a different message
|
||||
if isLoggedOut {
|
||||
_, _ = fmt.Fprintf(cmd.OutOrStdout(), notLoggedInMessage+"\n")
|
||||
} else {
|
||||
_, _ = fmt.Fprintf(cmd.OutOrStdout(), caret+"Successfully logged out.\n")
|
||||
if len(errors) > 0 {
|
||||
var errorStringBuilder strings.Builder
|
||||
for _, err := range errors {
|
||||
_, _ = fmt.Fprint(&errorStringBuilder, "\t"+err.Error()+"\n")
|
||||
}
|
||||
errorString := strings.TrimRight(errorStringBuilder.String(), "\n")
|
||||
return xerrors.New("Failed to log out.\n" + errorString)
|
||||
}
|
||||
_, _ = fmt.Fprintf(cmd.OutOrStdout(), caret+"You are no longer logged in. You can log in using 'coder login <url>'.\n")
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
+73
-16
@@ -1,7 +1,10 @@
|
||||
package cli_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -21,7 +24,7 @@ func TestLogout(t *testing.T) {
|
||||
pty := ptytest.New(t)
|
||||
config := login(t, pty)
|
||||
|
||||
// ensure session files exist
|
||||
// Ensure session files exist.
|
||||
require.FileExists(t, string(config.URL()))
|
||||
require.FileExists(t, string(config.Session()))
|
||||
|
||||
@@ -40,7 +43,7 @@ func TestLogout(t *testing.T) {
|
||||
|
||||
pty.ExpectMatch("Are you sure you want to logout?")
|
||||
pty.WriteLine("yes")
|
||||
pty.ExpectMatch("Successfully logged out")
|
||||
pty.ExpectMatch("You are no longer logged in. You can log in using 'coder login <url>'.")
|
||||
<-logoutChan
|
||||
})
|
||||
t.Run("SkipPrompt", func(t *testing.T) {
|
||||
@@ -49,7 +52,7 @@ func TestLogout(t *testing.T) {
|
||||
pty := ptytest.New(t)
|
||||
config := login(t, pty)
|
||||
|
||||
// ensure session files exist
|
||||
// Ensure session files exist.
|
||||
require.FileExists(t, string(config.URL()))
|
||||
require.FileExists(t, string(config.Session()))
|
||||
|
||||
@@ -66,7 +69,7 @@ func TestLogout(t *testing.T) {
|
||||
assert.NoFileExists(t, string(config.Session()))
|
||||
}()
|
||||
|
||||
pty.ExpectMatch("Successfully logged out")
|
||||
pty.ExpectMatch("You are no longer logged in. You can log in using 'coder login <url>'.")
|
||||
<-logoutChan
|
||||
})
|
||||
t.Run("NoURLFile", func(t *testing.T) {
|
||||
@@ -75,7 +78,7 @@ func TestLogout(t *testing.T) {
|
||||
pty := ptytest.New(t)
|
||||
config := login(t, pty)
|
||||
|
||||
// ensure session files exist
|
||||
// Ensure session files exist.
|
||||
require.FileExists(t, string(config.URL()))
|
||||
require.FileExists(t, string(config.Session()))
|
||||
|
||||
@@ -91,14 +94,9 @@ func TestLogout(t *testing.T) {
|
||||
go func() {
|
||||
defer close(logoutChan)
|
||||
err := logout.Execute()
|
||||
assert.NoError(t, err)
|
||||
assert.NoFileExists(t, string(config.URL()))
|
||||
assert.NoFileExists(t, string(config.Session()))
|
||||
assert.EqualError(t, err, "You are not logged in. Try logging in using 'coder login <url>'.")
|
||||
}()
|
||||
|
||||
pty.ExpectMatch("Are you sure you want to logout?")
|
||||
pty.WriteLine("yes")
|
||||
pty.ExpectMatch("You are not logged in. Try logging in using 'coder login <url>'.")
|
||||
<-logoutChan
|
||||
})
|
||||
t.Run("NoSessionFile", func(t *testing.T) {
|
||||
@@ -107,7 +105,7 @@ func TestLogout(t *testing.T) {
|
||||
pty := ptytest.New(t)
|
||||
config := login(t, pty)
|
||||
|
||||
// ensure session files exist
|
||||
// Ensure session files exist.
|
||||
require.FileExists(t, string(config.URL()))
|
||||
require.FileExists(t, string(config.Session()))
|
||||
|
||||
@@ -123,14 +121,73 @@ func TestLogout(t *testing.T) {
|
||||
go func() {
|
||||
defer close(logoutChan)
|
||||
err = logout.Execute()
|
||||
assert.NoError(t, err)
|
||||
assert.NoFileExists(t, string(config.URL()))
|
||||
assert.NoFileExists(t, string(config.Session()))
|
||||
assert.EqualError(t, err, "You are not logged in. Try logging in using 'coder login <url>'.")
|
||||
}()
|
||||
|
||||
<-logoutChan
|
||||
})
|
||||
t.Run("CannotDeleteFiles", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
pty := ptytest.New(t)
|
||||
config := login(t, pty)
|
||||
|
||||
// Ensure session files exist.
|
||||
require.FileExists(t, string(config.URL()))
|
||||
require.FileExists(t, string(config.Session()))
|
||||
|
||||
var (
|
||||
err error
|
||||
urlFile *os.File
|
||||
sessionFile *os.File
|
||||
)
|
||||
if runtime.GOOS == "windows" {
|
||||
// Opening the files so Windows does not allow deleting them.
|
||||
urlFile, err = os.Open(string(config.URL()))
|
||||
require.NoError(t, err)
|
||||
sessionFile, err = os.Open(string(config.Session()))
|
||||
require.NoError(t, err)
|
||||
} else {
|
||||
// Changing the permissions to throw error during deletion.
|
||||
err = os.Chmod(string(config), 0500)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
if runtime.GOOS == "windows" {
|
||||
// Closing the opened files for cleanup.
|
||||
err = urlFile.Close()
|
||||
require.NoError(t, err)
|
||||
err = sessionFile.Close()
|
||||
require.NoError(t, err)
|
||||
} else {
|
||||
// Setting the permissions back for cleanup.
|
||||
err = os.Chmod(string(config), 0700)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
})
|
||||
|
||||
logoutChan := make(chan struct{})
|
||||
logout, _ := clitest.New(t, "logout", "--global-config", string(config))
|
||||
|
||||
logout.SetIn(pty.Input())
|
||||
logout.SetOut(pty.Output())
|
||||
|
||||
go func() {
|
||||
defer close(logoutChan)
|
||||
err := logout.Execute()
|
||||
assert.NotNil(t, err)
|
||||
var errorMessage string
|
||||
if runtime.GOOS == "windows" {
|
||||
errorMessage = "The process cannot access the file because it is being used by another process."
|
||||
} else {
|
||||
errorMessage = "permission denied"
|
||||
}
|
||||
errRegex := regexp.MustCompile(fmt.Sprintf("Failed to log out.\n\tremove URL file: .+: %s\n\tremove session file: .+: %s", errorMessage, errorMessage))
|
||||
assert.Regexp(t, errRegex, err.Error())
|
||||
}()
|
||||
|
||||
pty.ExpectMatch("Are you sure you want to logout?")
|
||||
pty.WriteLine("yes")
|
||||
pty.ExpectMatch("You are not logged in. Try logging in using 'coder login <url>'.")
|
||||
<-logoutChan
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user