Skip to content

Commit

Permalink
Use utils.command to replace exec.command (#3686)
Browse files Browse the repository at this point in the history
* Use simpleCommand to enhance validation, To #53506158

Signed-off-by: cheyang <[email protected]>

Remove unused method, To #53506158

Signed-off-by: cheyang <[email protected]>

* Remove unused method, To #53506158

Signed-off-by: cheyang <[email protected]>

* Remove unused method, To #53506158

Signed-off-by: cheyang <[email protected]>

---------

Signed-off-by: cheyang <[email protected]>
  • Loading branch information
cheyang committed Jan 19, 2024
1 parent 5a1bb89 commit 5e13f2c
Show file tree
Hide file tree
Showing 11 changed files with 368 additions and 169 deletions.
10 changes: 8 additions & 2 deletions pkg/csi/plugins/nodeserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,10 @@ func (ns *nodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublis
} else {
args = append(args, mountPath, targetPath)
}
command := exec.Command("mount", args...)
command, err := utils.SimpleCommand("mount", args...)
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}

glog.V(3).Infof("NodePublishVolume: exec command %v", command)
stdoutStderr, err := command.CombinedOutput()
Expand Down Expand Up @@ -508,7 +511,10 @@ func checkMountInUse(volumeName string) (bool, error) {

// TODO: refer to https://github.com/kubernetes-sigs/alibaba-cloud-csi-driver/blob/4fcb743220371de82d556ab0b67b08440b04a218/pkg/oss/utils.go#L72
// for a better implementation
command := exec.Command("/usr/local/bin/check_bind_mounts.sh", volumeName)
command, err := utils.SimpleCommand("/usr/local/bin/check_bind_mounts.sh", volumeName)
if err != nil {
return inUse, err
}
glog.Infoln(command)

stdoutStderr, err := command.CombinedOutput()
Expand Down
86 changes: 86 additions & 0 deletions pkg/utils/exec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
Copyright 2024 The Fluid Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package utils

import (
"fmt"
"os/exec"
"path/filepath"
"strings"
)

func init() {
allowPathlist = buildPathList(allowPathlist)
}

// allowPathlist of safe commands
var allowPathlist = map[string]bool{
// "helm": true,
"kubectl": true,
"ddc-helm": true,
// add other commands as needed
}

// illegalChars to check
// var illegalChars = []string{"&", "|", ";", "$", "'", "`", "(", ")", ">>"}
var illegalChars = []rune{'&', '|', ';', '$', '\'', '`', '(', ')', '>'}

// buildPathList is a function that builds a map of paths for the given pathList.
func buildPathList(pathList map[string]bool) (targetPath map[string]bool) {
targetPath = make(map[string]bool)
for name, enabled := range pathList {
if filepath.Base(name) == name {
path, err := exec.LookPath(name)
if err != nil {
log.Info("Failed to find path %s due to %v", path, err)

} else {
targetPath[path] = enabled
}
}
targetPath[name] = enabled
}
return
}

// SimpleCommand checks the args before creating *exec.Cmd
func SimpleCommand(name string, arg ...string) (cmd *exec.Cmd, err error) {
if allowPathlist[name] {
cmd = exec.Command(name, arg...)
} else {
err = checkCommandArgs(arg...)
if err != nil {
return
}
cmd = exec.Command(name, arg...)
}

return
}

// CheckCommandArgs is check string is valid in args
func checkCommandArgs(arg ...string) (err error) {
for _, value := range arg {
for _, illegalChar := range illegalChars {
if strings.ContainsRune(value, illegalChar) {
return fmt.Errorf("args %s has illegal access with illegalChar %c", value, illegalChar)
}
}
}

return
}
188 changes: 188 additions & 0 deletions pkg/utils/exec_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
/*
Copyright 2024 The Fluid Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package utils

import (
"fmt"
"os/exec"
"reflect"
"testing"

"github.com/brahma-adshonor/gohook"
)

func TestCheckCommandArgs(t *testing.T) {
type args struct {
arg []string
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Test with illegal arguments",
args: args{
arg: []string{"ls", "|", "grep go"},
},
wantErr: true,
},
{
name: "Test with legal arguments",
args: args{
arg: []string{"ls"},
},
wantErr: false,
}, {
name: "Test with legal arguments2",
args: args{
arg: []string{"echo test > /dev/null"},
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := checkCommandArgs(tt.args.arg...); (err != nil) != tt.wantErr {
t.Errorf("checkCommandArgs() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

func TestSimpleCommand(t *testing.T) {
type args struct {
name string
arg []string
}
tests := []struct {
name string
args args
wantCmd *exec.Cmd
wantErr bool
}{
{
name: "Allow path list",
args: args{
name: "kubectl",
arg: []string{"Hello", "World"},
},
wantCmd: &exec.Cmd{
Path: "echo",
Args: []string{"Hello", "World"},
},
wantErr: false,
}, {
name: "Valid command arguments",
args: args{
name: "echo",
arg: []string{"Hello", "World"},
},
wantCmd: &exec.Cmd{
Path: "echo",
Args: []string{"Hello", "World"},
},
wantErr: false,
},
{
name: "Invalid command arguments",
args: args{
name: "echo",
arg: []string{"Hello", "World&"},
},
wantCmd: nil,
wantErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cmd, err := SimpleCommand(tt.args.name, tt.args.arg...)
if (err != nil) != tt.wantErr {
t.Errorf("SimpleCommand() error = %v, wantErr %v", err, tt.wantErr)
return
}
if err != nil {
return
}
tt.wantCmd = exec.Command(tt.args.name, tt.args.arg...)
if !reflect.DeepEqual(tt.wantCmd.Args, cmd.Args) {
t.Errorf("SimpleCommand() = %v, want %v", tt.args.arg, cmd.Args)
}
if !reflect.DeepEqual(tt.wantCmd.Path, cmd.Path) {
t.Errorf("SimpleCommand() = %v, want %v", tt.args.arg, cmd.Args)
}
})
}
}

func Test_buildPathList(t *testing.T) {
type args struct {
pathList map[string]bool
}
tests := []struct {
name string
args args
mockLookpathFunc func(file string) (string, error)
want map[string]bool
}{
{
name: "Test with command 'kubectl'",
args: args{
pathList: map[string]bool{"kubectl": true},
},
mockLookpathFunc: func(file string) (string, error) {
return "/path/to/" + file, nil // Mocked path
},
want: map[string]bool{"kubectl": true, "/path/to/kubectl": true}, // assuming '/path/to/kubectl' is the path of the 'kubectl' command
},
{
name: "Test with nonexistent command",
args: args{
pathList: map[string]bool{"nonexistent": true},
}, mockLookpathFunc: func(file string) (string, error) {
return "", fmt.Errorf("Failed to find path")
},
want: map[string]bool{"nonexistent": true}, // as 'nonexistent' command does not exist, so the result should be same as initial
},
{
name: "Test with full path command",
args: args{
pathList: map[string]bool{"/usr/local/bin/kubectl": true},
},
mockLookpathFunc: func(file string) (string, error) {
return "/path/to/" + file, nil // Mocked path
},
want: map[string]bool{"/usr/local/bin/kubectl": true}, // since '/usr/local/bin/kubectl' command already has full path, so the result should be same as initial
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := gohook.Hook(exec.LookPath, tt.mockLookpathFunc, nil)
if err != nil {
t.Fatalf("failed to hook function: %v", err)
}
got := buildPathList(tt.args.pathList)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("buildPathList() = %v, want %v", got, tt.want)
}
_ = gohook.UnHook(tt.mockLookpathFunc)

})
}
}
58 changes: 0 additions & 58 deletions pkg/utils/helm/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,64 +54,6 @@ func GenerateValueFile(values interface{}) (valueFileName string, err error) {
return valueFileName, err
}

// GenerateHelmTemplate generates helm template without tiller: helm template -f values.yaml chart_name
// Exec /usr/local/bin/helm, [template -f /tmp/values313606961 --namespace default --name hj /charts/tf-horovod]
// returns generated template file: templateFileName
func GenerateHelmTemplate(name string, namespace string, valueFileName string, chartName string, options ...string) (templateFileName string, err error) {
tempName := fmt.Sprintf("%s.yaml", name)
templateFile, err := os.CreateTemp("", tempName)
if err != nil {
return templateFileName, err
}
templateFileName = templateFile.Name()

binary, err := exec.LookPath(helmCmd[0])
if err != nil {
return templateFileName, err
}

// 3. check if the chart file exists
// if strings.HasPrefix(chartName, "/") {
if _, err = os.Stat(chartName); err != nil {
return templateFileName, err
}
// }

// 4. prepare the arguments
args := []string{binary, "template", "-f", valueFileName,
"--namespace", namespace,
"--name", name,
}
if len(options) != 0 {
args = append(args, options...)
}

args = append(args, []string{chartName, ">", templateFileName}...)

log.V(1).Info("Exec bash -c ", "cmd", args)

// return syscall.Exec(cmd, args, env)
// 5. execute the command
log.V(1).Info("Generating template", "args", args)
cmd := exec.Command("bash", "-c", strings.Join(args, " "))
// cmd.Env = env
out, err := cmd.CombinedOutput()
fmt.Printf("%s", string(out))
if err != nil {
return templateFileName, fmt.Errorf("failed to execute %s, %v with %v", binary, args, err)
}

// // 6. clean up the value file if needed
// if log.GetLevel() != log.DebugLevel {
// err = os.Remove(valueFileName)
// if err != nil {
// log.Warnf("Failed to delete %s due to %v", valueFileName, err)
// }
// }

return templateFileName, nil
}

// GetChartVersion checks the chart version by given the chart directory
// helm inspect chart /charts/tf-horovod
func GetChartVersion(chart string) (version string, err error) {
Expand Down
Loading

0 comments on commit 5e13f2c

Please sign in to comment.