Use environment varaibles for parameters in Powershell

As a defense in depth, pass parameters to powershell via environment variables.

Signed-off-by: James Sturtevant <jstur@microsoft.com>
This commit is contained in:
James Sturtevant
2023-07-17 14:24:02 -07:00
committed by Rita Zhang
parent fb785f1f42
commit 2bede058f9
3 changed files with 25 additions and 13 deletions

View File

@@ -709,11 +709,15 @@ func HasMountRefs(mountPath string, mountRefs []string) bool {
func WriteVolumeCache(deviceMountPath string, exec utilexec.Interface) error {
// If runtime os is windows, execute Write-VolumeCache powershell command on the disk
if runtime.GOOS == "windows" {
cmd := fmt.Sprintf("Get-Volume -FilePath %s | Write-Volumecache", deviceMountPath)
output, err := exec.Command("powershell", "/c", cmd).CombinedOutput()
klog.Infof("command (%q) execeuted: %v, output: %q", cmd, err, string(output))
cmdString := "Get-Volume -FilePath $env:mountpath | Write-Volumecache"
cmd := exec.Command("powershell", "/c", cmdString)
env := append(os.Environ(), fmt.Sprintf("mountpath=%s", deviceMountPath))
cmd.SetEnv(env)
klog.V(8).Infof("Executing command: %q", cmdString)
output, err := cmd.CombinedOutput()
klog.Infof("command (%q) execeuted: %v, output: %q", cmdString, err, string(output))
if err != nil {
return fmt.Errorf("command (%q) failed: %v, output: %q", cmd, err, string(output))
return fmt.Errorf("command (%q) failed: %v, output: %q", cmdString, err, string(output))
}
}
// For linux runtime, it skips because unmount will automatically flush disk data