Skip to content

Commit 2d89a92

Browse files
authored
Merge pull request #184 from mauriciopoppe/working-dir-flag
Add the --working-dir repeated flag
2 parents e2ace04 + 4cb6bf9 commit 2d89a92

File tree

5 files changed

+39
-11
lines changed

5 files changed

+39
-11
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ On successful execution of `make build`, the output binary `csi-proxy.exe` will
5050

5151
csi-proxy.exe can be installed and run as binary or run as a Windows service on each Windows node. See the following as an example to run CSI Proxy as a web service.
5252
```
53-
$flags = "-windows-service -log_file=\etc\kubernetes\logs\csi-proxy.log -logtostderr=false"
54-
sc.exe create csiproxy binPath= "\etc\kubernetes\node\bin\csi-proxy.exe $flags"
53+
$flags = "-windows-service -log_file=C:\etc\kubernetes\logs\csi-proxy.log -logtostderr=false"
54+
sc.exe create csiproxy binPath= "C:\etc\kubernetes\node\bin\csi-proxy.exe $flags"
5555
sc.exe failure csiproxy reset= 0 actions= restart/10000
5656
sc.exe start csiproxy
5757
```
@@ -62,6 +62,7 @@ If you are using kube-up to start a Windows cluster, node startup script will au
6262
### Command line options
6363

6464
* `--kubelet-path`: This is the prefix path of the kubelet path directory in the host file system (`C:\var\lib\kubelet` is used by default).
65+
* `--working-dir` (repeated flag): Prefix path where CSI Proxy is allowed to make privileged operations in the host file system (no value by default).
6566

6667
### Setup for CSI Driver Deployment
6768

@@ -108,6 +109,7 @@ spec:
108109
- name: registration-dir
109110
mountPath: C:\registration
110111
- name: csi-driver
112+
# placeholder, use your CSI driver
111113
image: org/csi-driver:win-v1
112114
args:
113115
- "--v=5"

cmd/csi-proxy/main.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,33 @@ import (
2222
"k8s.io/klog/v2"
2323
)
2424

25+
type workingDirFlags []string
26+
27+
func (i *workingDirFlags) String() string {
28+
return "Not implemented"
29+
}
30+
31+
func (i *workingDirFlags) Set(value string) error {
32+
*i = append(*i, value)
33+
return nil
34+
}
35+
2536
var (
2637
kubeletPath = flag.String("kubelet-path", `C:\var\lib\kubelet`, "Prefix path of the kubelet directory in the host file system")
2738
windowsSvc = flag.Bool("windows-service", false, "Configure as a Windows Service")
2839
service *handler
40+
workingDirs workingDirFlags
2941
)
3042

3143
type handler struct {
3244
tosvc chan bool
3345
fromsvc chan error
3446
}
3547

48+
func init() {
49+
flag.Var(&workingDirs, "working-dir", "Prefix path of the csi-proxy working directory in the host file system")
50+
}
51+
3652
func main() {
3753
defer klog.Flush()
3854
klog.InitFlags(nil)
@@ -60,7 +76,7 @@ func main() {
6076

6177
// apiGroups returns the list of enabled API groups.
6278
func apiGroups() ([]srvtypes.APIGroup, error) {
63-
fssrv, err := filesystemsrv.NewServer(*kubeletPath, filesystemapi.New())
79+
fssrv, err := filesystemsrv.NewServer(*kubeletPath, workingDirs, filesystemapi.New())
6480
if err != nil {
6581
return []srvtypes.APIGroup{}, err
6682
}

pkg/server/filesystem/server.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515

1616
type Server struct {
1717
kubeletPath string
18+
workingDirs []string
1819
hostAPI filesystem.API
1920
}
2021

@@ -24,9 +25,10 @@ var _ internal.ServerInterface = &Server{}
2425
var invalidPathCharsRegexWindows = regexp.MustCompile(`["/\:\?\*|]`)
2526
var absPathRegexWindows = regexp.MustCompile(`^[a-zA-Z]:\\`)
2627

27-
func NewServer(kubeletPath string, hostAPI filesystem.API) (*Server, error) {
28+
func NewServer(kubeletPath string, workingDirs []string, hostAPI filesystem.API) (*Server, error) {
2829
return &Server{
2930
kubeletPath: kubeletPath,
31+
workingDirs: workingDirs,
3032
hostAPI: hostAPI,
3133
}, nil
3234
}
@@ -69,8 +71,6 @@ func (s *Server) ValidatePluginPath(path string) error {
6971
}
7072

7173
func (s *Server) validatePathWindows(path string) error {
72-
prefix := s.kubeletPath
73-
7474
pathlen := len(path)
7575

7676
if pathlen > utils.MaxPathLengthWindows {
@@ -93,8 +93,18 @@ func (s *Server) validatePathWindows(path string) error {
9393
return fmt.Errorf("not an absolute Windows path: %s", path)
9494
}
9595

96-
if !strings.HasPrefix(strings.ToLower(path), strings.ToLower(prefix)) {
97-
return fmt.Errorf("path: %s is not within context path: %s", path, prefix)
96+
valid := false
97+
if strings.HasPrefix(strings.ToLower(path), strings.ToLower(s.kubeletPath)) {
98+
valid = true
99+
}
100+
for _, workingDir := range s.workingDirs {
101+
if strings.HasPrefix(strings.ToLower(path), strings.ToLower(workingDir)) {
102+
valid = true
103+
}
104+
}
105+
106+
if !valid {
107+
return fmt.Errorf("path: %s is not within context path: %s or %v", path, s.kubeletPath, s.workingDirs)
98108
}
99109

100110
return nil

pkg/server/filesystem/server_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func TestMkdirWindows(t *testing.T) {
117117
expectError: true,
118118
},
119119
}
120-
srv, err := NewServer(`C:\var\lib\kubelet`, &fakeFileSystemAPI{})
120+
srv, err := NewServer(`C:\var\lib\kubelet`, []string{}, &fakeFileSystemAPI{})
121121
if err != nil {
122122
t.Fatalf("FileSystem Server could not be initialized for testing: %v", err)
123123
}
@@ -221,7 +221,7 @@ func TestRmdirWindows(t *testing.T) {
221221
expectError: true,
222222
},
223223
}
224-
srv, err := NewServer(`C:\var\lib\kubelet`, &fakeFileSystemAPI{})
224+
srv, err := NewServer(`C:\var\lib\kubelet`, []string{}, &fakeFileSystemAPI{})
225225
if err != nil {
226226
t.Fatalf("FileSystem Server could not be initialized for testing: %v", err)
227227
}

pkg/server/smb/server_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func TestNewSmbGlobalMapping(t *testing.T) {
8383
expectError: false,
8484
},
8585
}
86-
fsSrv, err := fsserver.NewServer(`C:\var\lib\kubelet`, &fakeFileSystemAPI{})
86+
fsSrv, err := fsserver.NewServer(`C:\var\lib\kubelet`, []string{}, &fakeFileSystemAPI{})
8787
if err != nil {
8888
t.Fatalf("FileSystem Server could not be initialized for testing: %v", err)
8989
}

0 commit comments

Comments
 (0)