Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions cmd/cmd_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@
package main

import (
"fmt"
"net/url"
"os"
"strings"

"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/pelicanplatform/pelican/config"
"github.com/pelicanplatform/pelican/param"
)

Expand Down Expand Up @@ -58,3 +61,22 @@ func getPreferredCaches() ([]*url.URL, error) {

return caches, nil
}

const (
incorrectPasswordAccessMessage = "Failed to access local credential file - entered incorrect local decryption password"
incorrectPasswordResetMessage = "Failed to reset password - entered incorrect local decryption password"
)

func handleIncorrectPassword(err error, actionMessage string) bool {
if err == nil || !errors.Is(err, config.ErrIncorrectPassword) {
return false
}
fmt.Fprintln(os.Stderr, actionMessage)
fmt.Fprintln(os.Stderr, "If you have forgotten your password, you can reset the local state (deleting all on-disk credentials)")
fmt.Fprintf(os.Stderr, "by running '%s credentials reset-local'\n", os.Args[0])
return true
}

func handleCredentialPasswordError(err error) bool {
return handleIncorrectPassword(err, incorrectPasswordAccessMessage)
}
16 changes: 15 additions & 1 deletion cmd/config_mgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,26 @@ func addConfigSubcommands(configCmd *cobra.Command) {
Run: func(cmd *cobra.Command, args []string) {
err := config.ResetPassword()
if err != nil {
fmt.Fprintln(os.Stderr, "Failed to get reset password:", err)
if handleIncorrectPassword(err, incorrectPasswordResetMessage) {
os.Exit(1)
}
fmt.Fprintln(os.Stderr, "Failed to reset password:", err)
os.Exit(1)
}
},
})

configCmd.AddCommand(&cobra.Command{
Use: "reset-local",
Short: "Delete all local credentials for the current user",
Long: "Delete all local credentials for the current user",
Run: func(cmd *cobra.Command, args []string) {
if err := config.DeleteCredentials(); err != nil {
fmt.Fprintln(os.Stderr, "Failed to delete local credentials:", err)
os.Exit(1)
}
},
})
}

func printOauthConfig() {
Expand Down
3 changes: 3 additions & 0 deletions cmd/object_copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ func copyMain(cmd *cobra.Command, args []string) {

// Exit with failure
if result != nil {
if handleCredentialPasswordError(result) {
os.Exit(1)
}
// Print the list of errors
errMsg := result.Error()
var te *client.TransferErrors
Expand Down
3 changes: 3 additions & 0 deletions cmd/object_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ func deleteMain(cmd *cobra.Command, args []string) error {
err = client.DoDelete(ctx, remoteDestination, isRecursive, client.WithTokenLocation(tokenLocation))

if err != nil {
if handleCredentialPasswordError(err) {
os.Exit(1)
}
log.Errorf("Failure deleting %s: %v", remoteDestination, err.Error())
os.Exit(1)
}
Expand Down
3 changes: 3 additions & 0 deletions cmd/object_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ func getMain(cmd *cobra.Command, args []string) {
// Exit with failure
if attemptErr != nil {
// Print the list of errors
if handleCredentialPasswordError(attemptErr) {
os.Exit(1)
}
errMsg := attemptErr.Error()
var pe error_codes.PelicanError
var te *client.TransferErrors
Expand Down
3 changes: 3 additions & 0 deletions cmd/object_ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ func listMain(cmd *cobra.Command, args []string) error {

// Exit with failure
if err != nil {
if handleCredentialPasswordError(err) {
os.Exit(1)
}
// Print the list of errors
errMsg := err.Error()
var te *client.TransferErrors
Expand Down
3 changes: 3 additions & 0 deletions cmd/object_prestage.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ func prestageMain(cmd *cobra.Command, args []string) {
// Exit with failure
if err != nil {
// Print the list of errors
if handleCredentialPasswordError(err) {
os.Exit(1)
}
errMsg := err.Error()
var pe error_codes.PelicanError
var te *client.TransferErrors
Expand Down
3 changes: 3 additions & 0 deletions cmd/object_put.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,9 @@ func putMain(cmd *cobra.Command, args []string) {

// Exit with failure
if result != nil {
if handleCredentialPasswordError(result) {
os.Exit(1)
}
// Print the list of errors
errMsg := result.Error()
var te *client.TransferErrors
Expand Down
3 changes: 3 additions & 0 deletions cmd/object_stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ func statMain(cmd *cobra.Command, args []string) {

// Exit with failure
if err != nil {
if handleCredentialPasswordError(err) {
os.Exit(1)
}
// Print the list of errors
errMsg := err.Error()
var te *client.TransferErrors
Expand Down
3 changes: 3 additions & 0 deletions cmd/object_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@ func syncMain(cmd *cobra.Command, args []string) {

// Exit with failure
if err != nil {
if handleCredentialPasswordError(err) {
os.Exit(1)
}
// Print the list of errors
errMsg := err.Error()
var pe error_codes.PelicanError
Expand Down
18 changes: 18 additions & 0 deletions config/encrypted.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"io"
"os"
"path/filepath"
"strings"

log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
Expand All @@ -48,6 +49,8 @@ import (
// the password again later.
var setEmptyPassword = false

var ErrIncorrectPassword = errors.New("incorrect password")

func GetEncryptedConfigName() (string, error) {
configDir := viper.GetString("ConfigDir")
if GetPreferredPrefix() == PelicanPrefix || IsRootExecution() {
Expand Down Expand Up @@ -79,6 +82,18 @@ func EncryptedConfigExists() (bool, error) {
return true, nil
}

// Delete the user's local credential file
func DeleteCredentials() error {
filename, err := GetEncryptedConfigName()
if err != nil {
return err
}
if err = os.Remove(filename); errors.Is(err, os.ErrNotExist) {
return nil
}
return err
}

// Return the PEM-formatted contents of the encrypted configuration file
func GetEncryptedContents() (string, error) {
filename, err := GetEncryptedConfigName()
Expand Down Expand Up @@ -242,6 +257,9 @@ func GetCredentialConfigContents() (OSDFConfig, error) {
return config, errors.New("Encrypted key present; must have non-empty password")
}
if key, err = pkcs8.ParsePKCS8PrivateKey(block.Bytes, password); err != nil {
if strings.Contains(err.Error(), "pkcs8: incorrect password") {
err = ErrIncorrectPassword
}
return config, err
}
if typedPassword {
Expand Down
Loading