Skip to content

feat: migrate smb API group to library model #245

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
merged 2 commits into from
Oct 13, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
175 changes: 63 additions & 112 deletions integrationtests/smb_test.go
Original file line number Diff line number Diff line change
@@ -1,138 +1,89 @@
package integrationtests

import (
"context"
"fmt"
"io/ioutil"
"math/rand"
"os"
"os/exec"
"strings"
"time"

"testing"
)

const letterset = "abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

var seededRand = rand.New(rand.NewSource(time.Now().UnixNano()))
fs "github.com/kubernetes-csi/csi-proxy/pkg/filesystem"
fsapi "github.com/kubernetes-csi/csi-proxy/pkg/filesystem/api"
"github.com/kubernetes-csi/csi-proxy/pkg/smb"
smbapi "github.com/kubernetes-csi/csi-proxy/pkg/smb/api"
)

func stringWithCharset(length int, charset string) string {
b := make([]byte, length)
for i := range b {
b[i] = charset[seededRand.Intn(len(charset))]
}
return string(b)
func TestSmbAPIGroup(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change to SMB tracked in #246

t.Run("v1alpha1SmbTests", func(t *testing.T) {
v1alpha1SmbTests(t)
})
t.Run("v1beta1SmbTests", func(t *testing.T) {
v1beta1SmbTests(t)
})
t.Run("v1beta2SmbTests", func(t *testing.T) {
v1beta2SmbTests(t)
})
t.Run("v1SmbTests", func(t *testing.T) {
v1SmbTests(t)
})
}

// RandomString generates a random string with specified length
func randomString(length int) string {
return stringWithCharset(length, letterset)
}
func TestSmb(t *testing.T) {
fsClient, err := fs.New(fsapi.New())
require.Nil(t, err)
client, err := smb.New(smbapi.New(), fsClient)
require.Nil(t, err)

func setupUser(username, password string) error {
cmdLine := fmt.Sprintf(`$PWord = ConvertTo-SecureString $Env:password -AsPlainText -Force` +
`;New-Localuser -name $Env:username -accountneverexpires -password $PWord`)
cmd := exec.Command("powershell", "/c", cmdLine)
cmd.Env = append(os.Environ(),
fmt.Sprintf("username=%s", username),
fmt.Sprintf("password=%s", password))
if output, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("setupUser failed: %v, output: %q", err, string(output))
}
return nil
}
username := randomString(5)
password := randomString(10) + "!"
sharePath := fmt.Sprintf("C:\\smbshare%s", randomString(5))
smbShare := randomString(6)

func removeUser(t *testing.T, username string) {
cmdLine := fmt.Sprintf(`Remove-Localuser -name $Env:username`)
cmd := exec.Command("powershell", "/c", cmdLine)
cmd.Env = append(os.Environ(),
fmt.Sprintf("username=%s", username))
if output, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("setupUser failed: %v, output: %q", err, string(output))
}
}
localPath := fmt.Sprintf("C:\\localpath%s", randomString(5))

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)
if err = setupUser(username, password); err != nil {
t.Fatalf("TestSmbAPIGroup %v", err)
}
cmdLine := fmt.Sprintf(`New-SMBShare -Name $Env:sharename -Path $Env:path -fullaccess $Env:username`)
cmd := exec.Command("powershell", "/c", cmdLine)
cmd.Env = append(os.Environ(),
fmt.Sprintf("sharename=%s", shareName),
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 nil
}
defer removeUser(t, username)

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))
if err = setupSmbShare(smbShare, sharePath, username); err != nil {
t.Fatalf("TestSmbAPIGroup %v", err)
}
return
}
defer removeSmbShare(t, smbShare)

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`)
hostname, err := os.Hostname()
assert.Nil(t, err)

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))
username = "domain\\" + username
remotePath := "\\\\" + hostname + "\\" + smbShare
// simulate Mount SMB operations around staging a volume on a node
mountSmbShareReq := &smb.NewSmbGlobalMappingRequest{
RemotePath: remotePath,
Username: username,
Password: password,
}
if !strings.Contains(string(output), "OK") {
return fmt.Errorf("Get-SmbGlobalMapping return status %q instead of OK", string(output))
}
return nil
}

func writeReadFile(path string) error {
fileName := path + "\\hello.txt"
f, err := os.Create(fileName)
_, err = client.NewSmbGlobalMapping(context.Background(), mountSmbShareReq)
if err != nil {
return fmt.Errorf("create file %q failed: %v", fileName, err)
}
defer f.Close()
fileContent := "Hello World"
if _, err = f.WriteString(fileContent); err != nil {
return fmt.Errorf("write to file %q failed: %v", fileName, err)
t.Fatalf("TestSmbAPIGroup %v", err)
}
if err = f.Sync(); err != nil {
return fmt.Errorf("sync file %q failed: %v", fileName, err)

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

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

unmountSmbShareReq := &smb.RemoveSmbGlobalMappingRequest{
RemotePath: remotePath,
}
dat, err := ioutil.ReadFile(fileName)
_, err = client.RemoveSmbGlobalMapping(context.Background(), unmountSmbShareReq)
if err != nil {
return fmt.Errorf("read file %q failed: %v", fileName, err)
}
if fileContent != string(dat) {
return fmt.Errorf("read content of file %q failed: expected %q, got %q", fileName, fileContent, string(dat))
t.Fatalf("TestSmbAPIGroup %v", err)
}
return nil
}

func TestSmbAPIGroup(t *testing.T) {
t.Run("v1alpha1SmbTests", func(t *testing.T) {
v1alpha1SmbTests(t)
})
t.Run("v1beta1SmbTests", func(t *testing.T) {
v1beta1SmbTests(t)
})
t.Run("v1beta2SmbTests", func(t *testing.T) {
v1beta2SmbTests(t)
})
t.Run("v1SmbTests", func(t *testing.T) {
v1SmbTests(t)
})
err = getSmbGlobalMapping(remotePath)
assert.NotNil(t, err)
err = writeReadFile(localPath)
assert.NotNil(t, err)
}
110 changes: 110 additions & 0 deletions integrationtests/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,3 +346,113 @@ func volumeInit(volumeClient volume.Interface, t *testing.T) (*VirtualHardDisk,
}
return vhd, volumeID, vhdCleanup
}

const letterset = "abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

var seededRand = rand.New(rand.NewSource(time.Now().UnixNano()))

func stringWithCharset(length int, charset string) string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are utils not used by any other integration test, I'd limit the scope of these functions to be inside the SMB test

I think volumeInit was used in many API Groups so I guess that one is ok

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved back in latest commit :)

b := make([]byte, length)
for i := range b {
b[i] = charset[seededRand.Intn(len(charset))]
}
return string(b)
}

// RandomString generates a random string with specified length
func randomString(length int) string {
return stringWithCharset(length, letterset)
}

func setupUser(username, password string) error {
cmdLine := fmt.Sprintf(`$PWord = ConvertTo-SecureString $Env:password -AsPlainText -Force` +
`;New-Localuser -name $Env:username -accountneverexpires -password $PWord`)
cmd := exec.Command("powershell", "/c", cmdLine)
cmd.Env = append(os.Environ(),
fmt.Sprintf("username=%s", username),
fmt.Sprintf("password=%s", password))
if output, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("setupUser failed: %v, output: %q", err, string(output))
}
return nil
}

func removeUser(t *testing.T, username string) {
cmdLine := fmt.Sprintf(`Remove-Localuser -name $Env:username`)
cmd := exec.Command("powershell", "/c", cmdLine)
cmd.Env = append(os.Environ(),
fmt.Sprintf("username=%s", username))
if output, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("setupUser failed: %v, output: %q", err, string(output))
}
}

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)
}
cmdLine := fmt.Sprintf(`New-SMBShare -Name $Env:sharename -Path $Env:path -fullaccess $Env:username`)
cmd := exec.Command("powershell", "/c", cmdLine)
cmd.Env = append(os.Environ(),
fmt.Sprintf("sharename=%s", shareName),
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 nil
}

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))
}
return
}

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`)

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))
}
if !strings.Contains(string(output), "OK") {
return fmt.Errorf("Get-SmbGlobalMapping return status %q instead of OK", string(output))
}
return nil
}

func writeReadFile(path string) error {
fileName := path + "\\hello.txt"
f, err := os.Create(fileName)
if err != nil {
return fmt.Errorf("create file %q failed: %v", fileName, err)
}
defer f.Close()
fileContent := "Hello World"
if _, err = f.WriteString(fileContent); err != nil {
return fmt.Errorf("write to file %q failed: %v", fileName, err)
}
if err = f.Sync(); err != nil {
return fmt.Errorf("sync file %q failed: %v", fileName, err)
}
dat, err := ioutil.ReadFile(fileName)
if err != nil {
return fmt.Errorf("read file %q failed: %v", fileName, err)
}
if fileContent != string(dat) {
return fmt.Errorf("read content of file %q failed: expected %q, got %q", fileName, fileContent, string(dat))
}
return nil
}
83 changes: 83 additions & 0 deletions pkg/smb/api/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package api

import (
"fmt"
"strings"

"github.com/kubernetes-csi/csi-proxy/pkg/utils"
)

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

type smbAPI struct{}

var _ API = &smbAPI{}

func New() API {
return smbAPI{}
}

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)
}

if len(out) == 0 || !strings.EqualFold(strings.TrimSpace(string(out)), "OK") {
return false, nil
}
return true, nil
}

// 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 {
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.
remotePath = remotePath + "\\"
}

cmdLine := `New-Item -ItemType SymbolicLink $Env:smblocalPath -Target $Env:smbremotepath`
output, err := utils.RunPowershellCmd(cmdLine, fmt.Sprintf("smbremotepath=%s", remotePath), fmt.Sprintf("smblocalpath=%s", localPath))
if err != nil {
return fmt.Errorf("error linking %s to %s. output: %s, err: %v", remotePath, localPath, string(output), err)
}

return nil
}

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` +
`;$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $Env:smbuser, $PWord` +
`;New-SmbGlobalMapping -RemotePath $Env:smbremotepath -Credential $Credential -RequirePrivacy $true`)

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 nil
}

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 nil
}
Loading