Skip to content

Commit

Permalink
cli/authn: Update token filepath handling, directory creation, and er…
Browse files Browse the repository at this point in the history
…ror management

- Rename function `tokfile` to `getTokenFilePath` for better clarity.
- Rename instances of `tokenFile` in `logoutUserHandler`, `loginUserHandler`, and `revokeTokenHandler` with `tokenFilePath` for better clarity.
- Replace `os.Create` with `os.CreateDir` and revert to returning the token filepath and err in `getTokenFilePath`.
- Streamline error handling for getting token file path.
- Replace `os.Remove` with `cos.RemoveFile` in `logoutUserHandler`.

Signed-off-by: Ryan Koo <[email protected]>
  • Loading branch information
rkoo19 committed Aug 28, 2024
1 parent 9e7655d commit 60b137f
Showing 1 changed file with 31 additions and 38 deletions.
69 changes: 31 additions & 38 deletions cmd/cli/cli/auth_hdlr.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,35 +370,29 @@ func loginUserHandler(c *cli.Context) (err error) {
if err != nil {
return err
}
tokenFile, newlyCreated, err := tokfile(c)
if err != nil && !os.IsNotExist(err) {
tokenFilePath, err := getTokenFilePath(c)
if err != nil {
return err
}
if !newlyCreated {
actionWarn(c, fmt.Sprintf("token %q exists - overwriting.\n", tokenFile))
}
if err := jsp.Save(tokenFile, token, jsp.Plain(), nil); err != nil {
return fmt.Errorf("failed to write token %q: %v", tokenFile, err)
if err := jsp.Save(tokenFilePath, token, jsp.Plain(), nil); err != nil {
return fmt.Errorf("failed to write token %q: %v", tokenFilePath, err)
}
fmt.Fprintf(c.App.Writer, "Logged in (%s)\n", tokenFile)
fmt.Fprintf(c.App.Writer, "Logged in (%s)\n", tokenFilePath)
return nil
}

func logoutUserHandler(c *cli.Context) (err error) {
tokenFile, _, err := tokfile(c)
tokenFilePath, err := getTokenFilePath(c)
if err != nil {
if tokenFile == "" {
return err
}
return fmt.Errorf("cannot logout %q: %v", tokenFile, err)
return fmt.Errorf("cannot logout: %v", err)
}
if err := revokeTokenHandler(c); err != nil {
return err
}
if err := os.Remove(tokenFile); err != nil {
return fmt.Errorf("failed to logout %q: %v", tokenFile, err)
if err := cos.RemoveFile(tokenFilePath); err != nil {
return fmt.Errorf("failed to logout, could not remove token file %q: %v", tokenFilePath, err)
}
fmt.Fprintf(c.App.Writer, "Logged out (removed/revoked %q)\n", tokenFile)
fmt.Fprintf(c.App.Writer, "Logged out (removed/revoked %q)\n", tokenFilePath)
return nil
}

Expand Down Expand Up @@ -696,21 +690,20 @@ func parseClusterSpecs(c *cli.Context) (cluSpec authn.CluACL, err error) {
}

func revokeTokenHandler(c *cli.Context) (err error) {
tokenFile, _, err := tokfile(c)
tokenFilePath, err := getTokenFilePath(c)
if err != nil {
return err
}
b, err := os.ReadFile(tokenFile)
b, err := os.ReadFile(tokenFilePath)
if err != nil {
return fmt.Errorf("failed to read token %q: %v", tokenFile, err)
return fmt.Errorf("failed to read token %q: %v", tokenFilePath, err)
}
msg := &authn.TokenMsg{}
if err := jsoniter.Unmarshal(b, msg); err != nil {
return fmt.Errorf("invalid token %q format: %v", tokenFile, err)
return fmt.Errorf("invalid token %q format: %v", tokenFilePath, err)
}
return authn.RevokeToken(authParams, msg.Token)
}

func showAuthConfigHandler(c *cli.Context) (err error) {
conf, err := authn.GetConfig(authParams)
if err != nil {
Expand Down Expand Up @@ -768,24 +761,24 @@ func setAuthConfigHandler(c *cli.Context) (err error) {
return authn.SetConfig(authParams, conf)
}

// compare with: api/authn/loadtoken.go
func tokfile(c *cli.Context) (string, bool, error) {
tokenFile := parseStrFlag(c, tokenFileFlag)
if tokenFile == "" {
tokenFile = os.Getenv(env.AuthN.TokenFile)
// getTokenFilePath retrieves the file path for the authentication token.
// It checks the token file flag, environment variable, and default location in order of precedence.
// If the file does not exist, it attempts to create the necessary directories.
func getTokenFilePath(c *cli.Context) (string, error) {
tokenFilePath := parseStrFlag(c, tokenFileFlag)
if tokenFilePath == "" {
tokenFilePath = os.Getenv(env.AuthN.TokenFile)
}
if tokenFile == "" {
tokenFile = filepath.Join(config.ConfigDir, fname.Token)
if tokenFilePath == "" {
tokenFilePath = filepath.Join(config.ConfigDir, fname.Token)
}
// Check if the token file exists.
if err := cos.Stat(tokenFile); err == nil {
return tokenFile, false, nil
}
// Attempt to create the token file if it does not exist.
file, err := os.Create(tokenFile)
if err != nil {
return "", false, fmt.Errorf("failed to create token file %s: %w", tokenFile, err)
if err := cos.Stat(tokenFilePath); err != nil {
if !os.IsNotExist(err) {
return "", fmt.Errorf("failed to access token file %q: %v", tokenFilePath, err)
}
if createErr := cos.CreateDir(filepath.Dir(tokenFilePath)); createErr != nil {
return "", fmt.Errorf("failed to create directory for token file %q: %v", tokenFilePath, createErr)
}
}
defer file.Close()
return tokenFile, true, nil
return tokenFilePath, nil
}

0 comments on commit 60b137f

Please sign in to comment.