generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 62
feat: Use WMI to implement SMB API to reduce PowerShell overhead (library version) #398
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
k8s-ci-robot
merged 1 commit into
kubernetes-csi:library-development
from
laozc:wmi-library-smb-api
Jul 28, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
//go:build windows | ||
// +build windows | ||
|
||
package cim | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/microsoft/wmi/pkg/base/query" | ||
cim "github.com/microsoft/wmi/pkg/wmiinstance" | ||
) | ||
|
||
// Refer to https://learn.microsoft.com/en-us/previous-versions/windows/desktop/smb/msft-smbmapping | ||
const ( | ||
SmbMappingStatusOK int32 = iota | ||
SmbMappingStatusPaused | ||
SmbMappingStatusDisconnected | ||
SmbMappingStatusNetworkError | ||
SmbMappingStatusConnecting | ||
SmbMappingStatusReconnecting | ||
SmbMappingStatusUnavailable | ||
|
||
credentialDelimiter = ":" | ||
) | ||
|
||
// escapeQueryParameter escapes a parameter for WMI Queries | ||
func escapeQueryParameter(s string) string { | ||
s = strings.ReplaceAll(s, "'", "''") | ||
s = strings.ReplaceAll(s, "\\", "\\\\") | ||
return s | ||
} | ||
|
||
func escapeUserName(userName string) string { | ||
// refer to https://github.com/PowerShell/PowerShell/blob/9303de597da55963a6e26a8fe164d0b256ca3d4d/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/cim/cimConverter.cs#L169-L170 | ||
userName = strings.ReplaceAll(userName, "\\", "\\\\") | ||
userName = strings.ReplaceAll(userName, credentialDelimiter, "\\"+credentialDelimiter) | ||
return userName | ||
} | ||
|
||
// QuerySmbGlobalMappingByRemotePath retrieves the SMB global mapping from its remote path. | ||
// | ||
// The equivalent WMI query is: | ||
// | ||
// SELECT [selectors] FROM MSFT_SmbGlobalMapping | ||
// | ||
// Refer to https://pkg.go.dev/github.com/microsoft/wmi/server2019/root/microsoft/windows/smb#MSFT_SmbGlobalMapping | ||
// for the WMI class definition. | ||
func QuerySmbGlobalMappingByRemotePath(remotePath string) (*cim.WmiInstance, error) { | ||
smbQuery := query.NewWmiQuery("MSFT_SmbGlobalMapping", "RemotePath", escapeQueryParameter(remotePath)) | ||
instances, err := QueryInstances(WMINamespaceSmb, smbQuery) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return instances[0], err | ||
} | ||
|
||
// GetSmbGlobalMappingStatus returns the status of an SMB global mapping. | ||
func GetSmbGlobalMappingStatus(inst *cim.WmiInstance) (int32, error) { | ||
statusProp, err := inst.GetProperty("Status") | ||
if err != nil { | ||
return SmbMappingStatusUnavailable, err | ||
} | ||
|
||
return statusProp.(int32), nil | ||
} | ||
|
||
// RemoveSmbGlobalMappingByRemotePath removes an SMB global mapping matching to the remote path. | ||
// | ||
// Refer to https://pkg.go.dev/github.com/microsoft/wmi/server2019/root/microsoft/windows/smb#MSFT_SmbGlobalMapping | ||
// for the WMI class definition. | ||
func RemoveSmbGlobalMappingByRemotePath(remotePath string) error { | ||
smbQuery := query.NewWmiQuery("MSFT_SmbGlobalMapping", "RemotePath", escapeQueryParameter(remotePath)) | ||
instances, err := QueryInstances(WMINamespaceSmb, smbQuery) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = instances[0].InvokeMethod("Remove", true) | ||
return err | ||
} | ||
|
||
// NewSmbGlobalMapping creates a new SMB global mapping to the remote path. | ||
// | ||
// Refer to https://pkg.go.dev/github.com/microsoft/wmi/server2019/root/microsoft/windows/smb#MSFT_SmbGlobalMapping | ||
// for the WMI class definition. | ||
func NewSmbGlobalMapping(remotePath, username, password string, requirePrivacy bool) (int, error) { | ||
params := map[string]interface{}{ | ||
"RemotePath": remotePath, | ||
"RequirePrivacy": requirePrivacy, | ||
} | ||
if username != "" { | ||
// refer to https://github.com/PowerShell/PowerShell/blob/9303de597da55963a6e26a8fe164d0b256ca3d4d/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/cim/cimConverter.cs#L166-L178 | ||
// on how SMB credential is handled in PowerShell | ||
params["Credential"] = escapeUserName(username) + credentialDelimiter + password | ||
} | ||
|
||
result, _, err := InvokeCimMethod(WMINamespaceSmb, "MSFT_SmbGlobalMapping", "Create", params) | ||
return result, err | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.