|
| 1 | +package smb |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + |
| 8 | + fs "github.com/kubernetes-csi/csi-proxy/pkg/filesystem" |
| 9 | + smbapi "github.com/kubernetes-csi/csi-proxy/pkg/smb/api" |
| 10 | + "k8s.io/klog/v2" |
| 11 | +) |
| 12 | + |
| 13 | +type Smb struct { |
| 14 | + hostAPI smbapi.API |
| 15 | + fs fs.Interface |
| 16 | +} |
| 17 | + |
| 18 | +type Interface interface { |
| 19 | + NewSmbGlobalMapping(context.Context, *NewSmbGlobalMappingRequest) (*NewSmbGlobalMappingResponse, error) |
| 20 | + RemoveSmbGlobalMapping(context.Context, *RemoveSmbGlobalMappingRequest) (*RemoveSmbGlobalMappingResponse, error) |
| 21 | +} |
| 22 | + |
| 23 | +// check that Smb implements the Interface |
| 24 | +var _ Interface = &Smb{} |
| 25 | + |
| 26 | +func normalizeWindowsPath(path string) string { |
| 27 | + normalizedPath := strings.Replace(path, "/", "\\", -1) |
| 28 | + return normalizedPath |
| 29 | +} |
| 30 | + |
| 31 | +func getRootMappingPath(path string) (string, error) { |
| 32 | + items := strings.Split(path, "\\") |
| 33 | + parts := []string{} |
| 34 | + for _, s := range items { |
| 35 | + if len(s) > 0 { |
| 36 | + parts = append(parts, s) |
| 37 | + if len(parts) == 2 { |
| 38 | + break |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + if len(parts) != 2 { |
| 43 | + klog.Errorf("remote path (%s) is invalid", path) |
| 44 | + return "", fmt.Errorf("remote path (%s) is invalid", path) |
| 45 | + } |
| 46 | + // parts[0] is a smb host name |
| 47 | + // parts[1] is a smb share name |
| 48 | + return strings.ToLower("\\\\" + parts[0] + "\\" + parts[1]), nil |
| 49 | +} |
| 50 | + |
| 51 | +func New(hostAPI smbapi.API, fsClient fs.Interface) (*Smb, error) { |
| 52 | + return &Smb{ |
| 53 | + hostAPI: hostAPI, |
| 54 | + fs: fsClient, |
| 55 | + }, nil |
| 56 | +} |
| 57 | + |
| 58 | +func (s *Smb) NewSmbGlobalMapping(context context.Context, request *NewSmbGlobalMappingRequest) (*NewSmbGlobalMappingResponse, error) { |
| 59 | + klog.V(2).Infof("calling NewSmbGlobalMapping with remote path %q", request.RemotePath) |
| 60 | + response := &NewSmbGlobalMappingResponse{} |
| 61 | + remotePath := normalizeWindowsPath(request.RemotePath) |
| 62 | + localPath := request.LocalPath |
| 63 | + |
| 64 | + if remotePath == "" { |
| 65 | + klog.Errorf("remote path is empty") |
| 66 | + return response, fmt.Errorf("remote path is empty") |
| 67 | + } |
| 68 | + |
| 69 | + mappingPath, err := getRootMappingPath(remotePath) |
| 70 | + if err != nil { |
| 71 | + return response, err |
| 72 | + } |
| 73 | + |
| 74 | + isMapped, err := s.hostAPI.IsSmbMapped(mappingPath) |
| 75 | + if err != nil { |
| 76 | + isMapped = false |
| 77 | + } |
| 78 | + |
| 79 | + if isMapped { |
| 80 | + klog.V(4).Infof("Remote %s already mapped. Validating...", mappingPath) |
| 81 | + |
| 82 | + validResp, err := s.fs.PathValid(context, &fs.PathValidRequest{Path: mappingPath}) |
| 83 | + if err != nil { |
| 84 | + klog.Warningf("PathValid(%s) failed with %v, ignore error", mappingPath, err) |
| 85 | + } |
| 86 | + |
| 87 | + if !validResp.Valid { |
| 88 | + klog.V(4).Infof("RemotePath %s is not valid, removing now", mappingPath) |
| 89 | + err := s.hostAPI.RemoveSmbGlobalMapping(mappingPath) |
| 90 | + if err != nil { |
| 91 | + klog.Errorf("RemoveSmbGlobalMapping(%s) failed with %v", mappingPath, err) |
| 92 | + return response, err |
| 93 | + } |
| 94 | + isMapped = false |
| 95 | + } else { |
| 96 | + klog.V(4).Infof("RemotePath %s is valid", mappingPath) |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + if !isMapped { |
| 101 | + klog.V(4).Infof("Remote %s not mapped. Mapping now!", mappingPath) |
| 102 | + err = s.hostAPI.NewSmbGlobalMapping(mappingPath, request.Username, request.Password) |
| 103 | + if err != nil { |
| 104 | + klog.Errorf("failed NewSmbGlobalMapping %v", err) |
| 105 | + return response, err |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + if len(localPath) != 0 { |
| 110 | + klog.V(4).Infof("ValidatePathWindows: '%s'", localPath) |
| 111 | + err = fs.ValidatePathWindows(localPath) |
| 112 | + if err != nil { |
| 113 | + klog.Errorf("failed validate plugin path %v", err) |
| 114 | + return response, err |
| 115 | + } |
| 116 | + err = s.hostAPI.NewSmbLink(remotePath, localPath) |
| 117 | + if err != nil { |
| 118 | + klog.Errorf("failed NewSmbLink %v", err) |
| 119 | + return response, fmt.Errorf("creating link %s to %s failed with error: %v", localPath, remotePath, err) |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + klog.V(2).Infof("NewSmbGlobalMapping on remote path %q is completed", request.RemotePath) |
| 124 | + return response, nil |
| 125 | +} |
| 126 | + |
| 127 | +func (s *Smb) RemoveSmbGlobalMapping(context context.Context, request *RemoveSmbGlobalMappingRequest) (*RemoveSmbGlobalMappingResponse, error) { |
| 128 | + klog.V(2).Infof("calling RemoveSmbGlobalMapping with remote path %q", request.RemotePath) |
| 129 | + response := &RemoveSmbGlobalMappingResponse{} |
| 130 | + remotePath := normalizeWindowsPath(request.RemotePath) |
| 131 | + |
| 132 | + if remotePath == "" { |
| 133 | + klog.Errorf("remote path is empty") |
| 134 | + return response, fmt.Errorf("remote path is empty") |
| 135 | + } |
| 136 | + |
| 137 | + mappingPath, err := getRootMappingPath(remotePath) |
| 138 | + if err != nil { |
| 139 | + return response, err |
| 140 | + } |
| 141 | + |
| 142 | + err = s.hostAPI.RemoveSmbGlobalMapping(mappingPath) |
| 143 | + if err != nil { |
| 144 | + klog.Errorf("failed RemoveSmbGlobalMapping %v", err) |
| 145 | + return response, err |
| 146 | + } |
| 147 | + |
| 148 | + klog.V(2).Infof("RemoveSmbGlobalMapping on remote path %q is completed", request.RemotePath) |
| 149 | + return response, nil |
| 150 | +} |
0 commit comments