Selaa lähdekoodia

Merge pull request #43597 from shoeffner/43596-mask-cifs-passwords

volume: mask password in cifs mount error messages
Sebastiaan van Stijn 3 vuotta sitten
vanhempi
commit
32f7551e61
3 muutettua tiedostoa jossa 36 lisäystä ja 0 poistoa
  1. 12 0
      volume/local/local.go
  2. 19 0
      volume/local/local_test.go
  3. 5 0
      volume/local/local_unix.go

+ 12 - 0
volume/local/local.go

@@ -361,3 +361,15 @@ func getAddress(opts string) string {
 	}
 	}
 	return ""
 	return ""
 }
 }
+
+// getPassword finds out a password from options
+func getPassword(opts string) string {
+	optsList := strings.Split(opts, ",")
+	for i := 0; i < len(optsList); i++ {
+		if strings.HasPrefix(optsList[i], "password=") {
+			passwd := strings.SplitN(optsList[i], "=", 2)[1]
+			return passwd
+		}
+	}
+	return ""
+}

+ 19 - 0
volume/local/local_test.go

@@ -29,6 +29,25 @@ func TestGetAddress(t *testing.T) {
 
 
 }
 }
 
 
+func TestGetPassword(t *testing.T) {
+	cases := map[string]string{
+		"password=secret":                       "secret",
+		" ":                                     "",
+		"password=":                             "",
+		"password=Tr0ub4dor&3":                  "Tr0ub4dor&3",
+		"password=correcthorsebatterystaple":    "correcthorsebatterystaple",
+		"username=moby,password=secret":         "secret",
+		"username=moby,password=secret,addr=11": "secret",
+		"username=moby,addr=11":                 "",
+	}
+	for optsstring, success := range cases {
+		v := getPassword(optsstring)
+		if v != success {
+			t.Errorf("Test case failed for %s actual: %s expected : %s", optsstring, v, success)
+		}
+	}
+}
+
 func TestRemove(t *testing.T) {
 func TestRemove(t *testing.T) {
 	skip.If(t, runtime.GOOS == "windows", "FIXME: investigate why this test fails on CI")
 	skip.If(t, runtime.GOOS == "windows", "FIXME: investigate why this test fails on CI")
 	rootDir, err := os.MkdirTemp("", "local-volume-test")
 	rootDir, err := os.MkdirTemp("", "local-volume-test")

+ 5 - 0
volume/local/local_unix.go

@@ -124,6 +124,11 @@ func (v *localVolume) mount() error {
 		}
 		}
 	}
 	}
 	err := mount.Mount(v.opts.MountDevice, v.path, v.opts.MountType, mountOpts)
 	err := mount.Mount(v.opts.MountDevice, v.path, v.opts.MountType, mountOpts)
+	if err != nil {
+		if password := getPassword(v.opts.MountOpts); password != "" {
+			err = errors.New(strings.Replace(err.Error(), "password="+password, "password=********", 1))
+		}
+	}
 	return errors.Wrap(err, "failed to mount local volume")
 	return errors.Wrap(err, "failed to mount local volume")
 }
 }