Skip to content
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

Rename Smb to SMB #255

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 22 additions & 22 deletions integrationtests/smb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
smbapi "github.com/kubernetes-csi/csi-proxy/pkg/smb/api"
)

func TestSmb(t *testing.T) {
func TestSMB(t *testing.T) {
fsClient, err := fs.New(fsapi.New())
require.Nil(t, err)
client, err := smb.New(smbapi.New(), fsClient)
Expand All @@ -34,45 +34,45 @@ func TestSmb(t *testing.T) {
localPath := fmt.Sprintf("C:\\localpath%s", randomString(5))

if err = setupUser(username, password); err != nil {
t.Fatalf("TestSmbAPIGroup %v", err)
t.Fatalf("TestSMBAPIGroup %v", err)
}
defer removeUser(t, username)

if err = setupSmbShare(smbShare, sharePath, username); err != nil {
t.Fatalf("TestSmbAPIGroup %v", err)
if err = setupSMBShare(smbShare, sharePath, username); err != nil {
t.Fatalf("TestSMBAPIGroup %v", err)
}
defer removeSmbShare(t, smbShare)
defer removeSMBShare(t, smbShare)

hostname, err := os.Hostname()
assert.Nil(t, err)

username = "domain\\" + username
remotePath := "\\\\" + hostname + "\\" + smbShare
// simulate Mount SMB operations around staging a volume on a node
mountSmbShareReq := &smb.NewSmbGlobalMappingRequest{
mountSMBShareReq := &smb.NewSMBGlobalMappingRequest{
RemotePath: remotePath,
Username: username,
Password: password,
}
_, err = client.NewSmbGlobalMapping(context.Background(), mountSmbShareReq)
_, err = client.NewSMBGlobalMapping(context.Background(), mountSMBShareReq)
if err != nil {
t.Fatalf("TestSmbAPIGroup %v", err)
t.Fatalf("TestSMBAPIGroup %v", err)
}

err = getSmbGlobalMapping(remotePath)
err = getSMBGlobalMapping(remotePath)
assert.Nil(t, err)

err = writeReadFile(remotePath)
assert.Nil(t, err)

unmountSmbShareReq := &smb.RemoveSmbGlobalMappingRequest{
unmountSMBShareReq := &smb.RemoveSMBGlobalMappingRequest{
RemotePath: remotePath,
}
_, err = client.RemoveSmbGlobalMapping(context.Background(), unmountSmbShareReq)
_, err = client.RemoveSMBGlobalMapping(context.Background(), unmountSMBShareReq)
if err != nil {
t.Fatalf("TestSmbAPIGroup %v", err)
t.Fatalf("TestSMBAPIGroup %v", err)
}
err = getSmbGlobalMapping(remotePath)
err = getSMBGlobalMapping(remotePath)
assert.NotNil(t, err)
err = writeReadFile(localPath)
assert.NotNil(t, err)
Expand Down Expand Up @@ -118,9 +118,9 @@ func removeUser(t *testing.T, username string) {
}
}

func setupSmbShare(shareName, localPath, username string) error {
func setupSMBShare(shareName, localPath, username string) error {
if err := os.MkdirAll(localPath, 0755); err != nil {
return fmt.Errorf("setupSmbShare failed to create local path %q: %v", localPath, err)
return fmt.Errorf("setupSMBShare failed to create local path %q: %v", localPath, err)
}
cmdLine := fmt.Sprintf(`New-SMBShare -Name $Env:sharename -Path $Env:path -fullaccess $Env:username`)
cmd := exec.Command("powershell", "/c", cmdLine)
Expand All @@ -129,37 +129,37 @@ func setupSmbShare(shareName, localPath, username string) error {
fmt.Sprintf("path=%s", localPath),
fmt.Sprintf("username=%s", username))
if output, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("setupSmbShare failed: %v, output: %q", err, string(output))
return fmt.Errorf("setupSMBShare failed: %v, output: %q", err, string(output))
}

return nil
}

func removeSmbShare(t *testing.T, shareName string) {
func removeSMBShare(t *testing.T, shareName string) {
cmdLine := fmt.Sprintf(`Remove-SMBShare -Name $Env:sharename -Force`)
cmd := exec.Command("powershell", "/c", cmdLine)
cmd.Env = append(os.Environ(),
fmt.Sprintf("sharename=%s", shareName))
if output, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("setupSmbShare failed: %v, output: %q", err, string(output))
t.Fatalf("setupSMBShare failed: %v, output: %q", err, string(output))
}
return
}

func getSmbGlobalMapping(remotePath string) error {
func getSMBGlobalMapping(remotePath string) error {
// use PowerShell Environment Variables to store user input string to prevent command line injection
// https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_environment_variables?view=powershell-5.1
cmdLine := fmt.Sprintf(`(Get-SmbGlobalMapping -RemotePath $Env:smbremotepath).Status`)
cmdLine := fmt.Sprintf(`(Get-SMBGlobalMapping -RemotePath $Env:smbremotepath).Status`)

cmd := exec.Command("powershell", "/c", cmdLine)
cmd.Env = append(os.Environ(),
fmt.Sprintf("smbremotepath=%s", remotePath))
output, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("Get-SmbGlobalMapping failed: %v, output: %q", err, string(output))
return fmt.Errorf("Get-SMBGlobalMapping failed: %v, output: %q", err, string(output))
}
if !strings.Contains(string(output), "OK") {
return fmt.Errorf("Get-SmbGlobalMapping return status %q instead of OK", string(output))
return fmt.Errorf("Get-SMBGlobalMapping return status %q instead of OK", string(output))
}
return nil
}
Expand Down
15 changes: 15 additions & 0 deletions pkg/disk/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,28 @@ type Interface interface {
DiskStats(context.Context, *DiskStatsRequest) (*DiskStatsResponse, error)
GetAttachState(context.Context, *GetAttachStateRequest) (*GetAttachStateResponse, error)
GetDiskNumberByName(context.Context, *GetDiskNumberByNameRequest) (*GetDiskNumberByNameResponse, error)
// GetDiskState gets the offline/online state of a disk.
GetDiskState(context.Context, *GetDiskStateRequest) (*GetDiskStateResponse, error)

// GetDiskStats returns the stats of a disk (currently it returns the disk size).
GetDiskStats(context.Context, *GetDiskStatsRequest) (*GetDiskStatsResponse, error)

// ListDiskIDs returns a map of DiskID objects where the key is the disk number.
ListDiskIDs(context.Context, *ListDiskIDsRequest) (*ListDiskIDsResponse, error)

// ListDiskLocations returns locations <Adapter, Bus, Target, LUN ID> of all
// disk devices enumerated by the host.
ListDiskLocations(context.Context, *ListDiskLocationsRequest) (*ListDiskLocationsResponse, error)

// PartitionDisk initializes and partitions a disk device with the GPT partition style
// (if the disk has not been partitioned already) and returns the resulting volume device ID.
PartitionDisk(context.Context, *PartitionDiskRequest) (*PartitionDiskResponse, error)

// Rescan refreshes the host's storage cache.
Rescan(context.Context, *RescanRequest) (*RescanResponse, error)
SetAttachState(context.Context, *SetAttachStateRequest) (*SetAttachStateResponse, error)

// SetDiskState sets the offline/online state of a disk.
SetDiskState(context.Context, *SetDiskStateRequest) (*SetDiskStateResponse, error)
}

Expand Down
18 changes: 17 additions & 1 deletion pkg/filesystem/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,31 @@ type Filesystem struct {
}

type Interface interface {
// CreateSymlink creates a symbolic link called target_path that points to source_path
// in the host filesystem (target_path is the name of the symbolic link created,
// source_path is the existing path).
CreateSymlink(context.Context, *CreateSymlinkRequest) (*CreateSymlinkResponse, error)
IsMountPoint(context.Context, *IsMountPointRequest) (*IsMountPointResponse, error)

// IsSymlink checks if a given path is a symlink.
IsSymlink(context.Context, *IsSymlinkRequest) (*IsSymlinkResponse, error)
LinkPath(context.Context, *LinkPathRequest) (*LinkPathResponse, error)

// Mkdir creates a directory at the requested path in the host filesystem.
Mkdir(context.Context, *MkdirRequest) (*MkdirResponse, error)

// PathExists checks if the requested path exists in the host filesystem.
PathExists(context.Context, *PathExistsRequest) (*PathExistsResponse, error)

// PathValid checks if the given path is accessible.
PathValid(context.Context, *PathValidRequest) (*PathValidResponse, error)

// Rmdir removes the directory at the requested path in the host filesystem.
// This may be used for unlinking a symlink created through CreateSymlink.
Rmdir(context.Context, *RmdirRequest) (*RmdirResponse, error)

// RmdirContents removes the contents of a directory in the host filesystem.
// Unlike Rmdir it won't delete the requested path, it'll only delete its contents.
RmdirContents(context.Context, *RmdirContentsRequest) (*RmdirContentsResponse, error)
}

Expand Down Expand Up @@ -50,7 +67,6 @@ func (f *Filesystem) PathExists(ctx context.Context, request *PathExistsRequest)
}, err
}

// PathValid checks if the given path is accessible.
func (f *Filesystem) PathValid(ctx context.Context, request *PathValidRequest) (*PathValidResponse, error) {
klog.V(2).Infof("Request: PathValid with path %q", request.Path)
valid, err := f.hostAPI.PathValid(request.Path)
Expand Down
26 changes: 26 additions & 0 deletions pkg/iscsi/iscsi.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,39 @@ type IsCSI struct {
}

type Interface interface {
// AddTargetPortal registers an iSCSI target network address for later
// discovery.
// AddTargetPortal currently does not support selecting different NICs or
// a different iSCSI initiator (e.g a hardware initiator). This means that
// Windows will select the initiator NIC and instance on its own.
AddTargetPortal(context.Context, *AddTargetPortalRequest) (*AddTargetPortalResponse, error)

// ConnectTarget connects to an iSCSI Target
ConnectTarget(context.Context, *ConnectTargetRequest) (*ConnectTargetResponse, error)

// DisconnectTarget disconnects from an iSCSI Target
DisconnectTarget(context.Context, *DisconnectTargetRequest) (*DisconnectTargetResponse, error)

// DiscoverTargetPortal initiates discovery on an iSCSI target network address
// and returns discovered IQNs.
DiscoverTargetPortal(context.Context, *DiscoverTargetPortalRequest) (*DiscoverTargetPortalResponse, error)

// GetTargetDisks returns the disk addresses that correspond to an iSCSI
// target
GetTargetDisks(context.Context, *GetTargetDisksRequest) (*GetTargetDisksResponse, error)

// ListTargetPortal lists all currently registered iSCSI target network
// addresses.
ListTargetPortals(context.Context, *ListTargetPortalsRequest) (*ListTargetPortalsResponse, error)

// RemoveTargetPortal removes an iSCSI target network address registration.
RemoveTargetPortal(context.Context, *RemoveTargetPortalRequest) (*RemoveTargetPortalResponse, error)

// SetMutualChapSecret sets the default CHAP secret that all initiators on
// this machine (node) use to authenticate the target on mutual CHAP
// authentication.
// NOTE: This method affects global node state and should only be used
// with consideration to other CSI drivers that run concurrently.
SetMutualChapSecret(context.Context, *SetMutualChapSecretRequest) (*SetMutualChapSecretResponse, error)
}

Expand Down
24 changes: 12 additions & 12 deletions pkg/smb/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
)

type API interface {
IsSmbMapped(remotePath string) (bool, error)
NewSmbLink(remotePath, localPath string) error
NewSmbGlobalMapping(remotePath, username, password string) error
RemoveSmbGlobalMapping(remotePath string) error
IsSMBMapped(remotePath string) (bool, error)
NewSMBLink(remotePath, localPath string) error
NewSMBGlobalMapping(remotePath, username, password string) error
RemoveSMBGlobalMapping(remotePath string) error
}

type smbAPI struct{}
Expand All @@ -22,12 +22,12 @@ func New() API {
return smbAPI{}
}

func (smbAPI) IsSmbMapped(remotePath string) (bool, error) {
func (smbAPI) IsSMBMapped(remotePath string) (bool, error) {
cmdLine := `$(Get-SmbGlobalMapping -RemotePath $Env:smbremotepath -ErrorAction Stop).Status `
cmdEnv := fmt.Sprintf("smbremotepath=%s", remotePath)
out, err := utils.RunPowershellCmd(cmdLine, cmdEnv)
if err != nil {
return false, fmt.Errorf("error checking smb mapping. cmd %s, output: %s, err: %v", remotePath, string(out), err)
return false, fmt.Errorf("error checking SMB mapping. cmd %s, output: %s, err: %v", remotePath, string(out), err)
}

if len(out) == 0 || !strings.EqualFold(strings.TrimSpace(string(out)), "OK") {
Expand All @@ -36,14 +36,14 @@ func (smbAPI) IsSmbMapped(remotePath string) (bool, error) {
return true, nil
}

// NewSmbLink - creates a directory symbolic link to the remote share.
// NewSMBLink - creates a directory symbolic link to the remote share.
// The os.Symlink was having issue for cases where the destination was an SMB share - the container
// runtime would complain stating "Access Denied". Because of this, we had to perform
// this operation with powershell commandlet creating an directory softlink.
// Since os.Symlink is currently being used in working code paths, no attempt is made in
// alpha to merge the paths.
// TODO (for beta release): Merge the link paths - os.Symlink and Powershell link path.
func (smbAPI) NewSmbLink(remotePath, localPath string) error {
func (smbAPI) NewSMBLink(remotePath, localPath string) error {
if !strings.HasSuffix(remotePath, "\\") {
// Golang has issues resolving paths mapped to file shares if they do not end in a trailing \
// so add one if needed.
Expand All @@ -59,7 +59,7 @@ func (smbAPI) NewSmbLink(remotePath, localPath string) error {
return nil
}

func (smbAPI) NewSmbGlobalMapping(remotePath, username, password string) error {
func (smbAPI) NewSMBGlobalMapping(remotePath, username, password string) error {
// use PowerShell Environment Variables to store user input string to prevent command line injection
// https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_environment_variables?view=powershell-5.1
cmdLine := fmt.Sprintf(`$PWord = ConvertTo-SecureString -String $Env:smbpassword -AsPlainText -Force` +
Expand All @@ -69,15 +69,15 @@ func (smbAPI) NewSmbGlobalMapping(remotePath, username, password string) error {
if output, err := utils.RunPowershellCmd(cmdLine, fmt.Sprintf("smbuser=%s", username),
fmt.Sprintf("smbpassword=%s", password),
fmt.Sprintf("smbremotepath=%s", remotePath)); err != nil {
return fmt.Errorf("NewSmbGlobalMapping failed. output: %q, err: %v", string(output), err)
return fmt.Errorf("NewSMBGlobalMapping failed. output: %q, err: %v", string(output), err)
}
return nil
}

func (smbAPI) RemoveSmbGlobalMapping(remotePath string) error {
func (smbAPI) RemoveSMBGlobalMapping(remotePath string) error {
cmd := `Remove-SmbGlobalMapping -RemotePath $Env:smbremotepath -Force`
if output, err := utils.RunPowershellCmd(cmd, fmt.Sprintf("smbremotepath=%s", remotePath)); err != nil {
return fmt.Errorf("UnmountSmbShare failed. output: %q, err: %v", string(output), err)
return fmt.Errorf("UnmountSMBShare failed. output: %q, err: %v", string(output), err)
}
return nil
}
Loading