Skip to content

Commit

Permalink
Mask password in error returned when mounting CIFS share fails
Browse files Browse the repository at this point in the history
this adds a new error type MaskedSecretError to the errors package
  • Loading branch information
anjannath committed Oct 11, 2022
1 parent b4cfc0e commit 846d772
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
16 changes: 16 additions & 0 deletions pkg/crc/errors/maskederror.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package errors

import (
"strings"
)

const mask = "*****"

type MaskedSecretError struct {
Err error
Secret string
}

func (err *MaskedSecretError) Error() string {
return strings.ReplaceAll(err.Err.Error(), err.Secret, mask)
}
16 changes: 16 additions & 0 deletions pkg/crc/errors/maskederror_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package errors

import (
"errors"
"fmt"
"testing"

"github.com/stretchr/testify/assert"
)

func TestMaskedError(t *testing.T) {
err := errors.New("The password is: pass@122")
maskedErr := &MaskedSecretError{err, "pass@122"}
expectedErrMsg := fmt.Sprintf("The password is: %s", mask)
assert.EqualError(t, maskedErr, expectedErrMsg)
}
4 changes: 4 additions & 0 deletions pkg/crc/machine/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ func configureSharedDirs(vm *virtualMachine, sshRunner *crcssh.Runner) error {
case "cifs":
smbUncPath := fmt.Sprintf("//%s/%s", hostVirtualIP, mount.Tag)
if _, _, err := sshRunner.RunPrivate("sudo", "mount", "-o", fmt.Sprintf("rw,uid=core,gid=core,username='%s',password='%s'", mount.Username, mount.Password), "-t", mount.Type, smbUncPath, mount.Target); err != nil {
err = &crcerrors.MaskedSecretError{
Err: err,
Secret: mount.Password,
}
return fmt.Errorf("Failed to mount CIFS/SMB share '%s' please make sure configured password is correct: %w", mount.Tag, err)
}
default:
Expand Down

0 comments on commit 846d772

Please sign in to comment.