better windows smb logic

This commit is contained in:
Travis Glenn Hansen 2022-05-12 14:42:27 -06:00
parent fc581fa6d0
commit 908685f073
2 changed files with 54 additions and 11 deletions

View File

@ -1300,6 +1300,7 @@ class CsiBaseDriver {
filesystem.covertUnixSeparatorToWindowsSeparator(device) filesystem.covertUnixSeparatorToWindowsSeparator(device)
); );
if (!result) { if (!result) {
// check for mount option cache=none and set -UseWriteThrough $true
await wutils.NewSmbGlobalMapping( await wutils.NewSmbGlobalMapping(
filesystem.covertUnixSeparatorToWindowsSeparator(device), filesystem.covertUnixSeparatorToWindowsSeparator(device),
`${volume_context.server}\\${username}`, `${volume_context.server}\\${username}`,
@ -2290,11 +2291,16 @@ class CsiBaseDriver {
switch (node_attach_driver) { switch (node_attach_driver) {
case "smb": case "smb":
// remove symlink *before* disconnecting
await removePath(win_normalized_staging_path);
let parts = target.split("\\"); let parts = target.split("\\");
await wutils.RemoveSmbGlobalMapping( // only remove global mapping if we certain there may not be other
`\\\\${parts[1]}\\${parts[2]}` // consumers of the mapping/share (ie: smb-client scenarios, etc)
); if (!parts[3]) {
await wutils.RemoveSmbGlobalMapping(
`\\\\${parts[1]}\\${parts[2]}`
);
}
break; break;
case "iscsi": case "iscsi":
// write volume cache // write volume cache

View File

@ -41,6 +41,20 @@ class Windows {
} }
} }
uncPathToShare(path) {
// UNC\<server>\<share>[\<path>\]
if (path.startsWith("UNC")) {
path = path.replace("UNC", "\\");
}
if (!path.startsWith("\\\\")) {
path = `\\\\${path}`;
}
let parts = path.split("\\");
return `\\\\${parts[2]}\\${parts[3]}`;
}
async GetRealTarget(path) { async GetRealTarget(path) {
let item; let item;
let target; let target;
@ -77,6 +91,9 @@ class Windows {
async GetSmbGlobalMapping(remotePath) { async GetSmbGlobalMapping(remotePath) {
let command; let command;
// cannot have trailing slash nor a path
// must be \\<server>\<share>
remotePath = this.uncPathToShare(remotePath);
command = command =
"Get-SmbGlobalMapping -RemotePath $Env:smbremotepath | ConvertTo-Json"; "Get-SmbGlobalMapping -RemotePath $Env:smbremotepath | ConvertTo-Json";
try { try {
@ -88,23 +105,41 @@ class Windows {
} catch (err) {} } catch (err) {}
} }
/**
* Global in this context is allowed access by all users
*
* @param {*} remotePath
* @param {*} username
* @param {*} password
*/
async NewSmbGlobalMapping(remotePath, username, password) { async NewSmbGlobalMapping(remotePath, username, password) {
let result;
let command; let command;
// -UseWriteThrough $true
// cannot have trailing slash nor a path
// must be \\<server>\<share>
remotePath = this.uncPathToShare(remotePath);
command = command =
"$PWord = ConvertTo-SecureString -String $Env:smbpassword -AsPlainText -Force;$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $Env:smbuser, $PWord;New-SmbGlobalMapping -RemotePath $Env:smbremotepath -Credential $Credential -RequirePrivacy $true"; "$PWord = ConvertTo-SecureString -String $Env:smbpassword -AsPlainText -Force;$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $Env:smbuser, $PWord;New-SmbGlobalMapping -RemotePath $Env:smbremotepath -Credential $Credential -RequirePrivacy $true";
await this.ps.exec(command, { result = await this.GetSmbGlobalMapping(remotePath);
env: { if (!result) {
smbuser: username, await this.ps.exec(command, {
smbpassword: password, env: {
smbremotepath: remotePath, smbuser: username,
}, smbpassword: password,
}); smbremotepath: remotePath,
},
});
}
} }
async RemoveSmbGlobalMapping(remotePath) { async RemoveSmbGlobalMapping(remotePath) {
let result; let result;
let command; let command;
// cannot have trailing slash nor a path
// must be \\<server>\<share>
remotePath = this.uncPathToShare(remotePath);
command = "Remove-SmbGlobalMapping -RemotePath $Env:smbremotepath -Force"; command = "Remove-SmbGlobalMapping -RemotePath $Env:smbremotepath -Force";
do { do {
@ -121,6 +156,8 @@ class Windows {
async NewSmbLink(remotePath, localPath) { async NewSmbLink(remotePath, localPath) {
let command; let command;
// trailing slash required
// may include subdirectories on the share if desired
if (!remotePath.endsWith("\\")) { if (!remotePath.endsWith("\\")) {
remotePath = `${remotePath}\\`; remotePath = `${remotePath}\\`;
} }