summaryrefslogtreecommitdiff
path: root/go/internal/command/fallback/fallback.go
blob: 781eda11a903c62573f3edde27d0a5e887ee43b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package fallback

import (
	"errors"
	"fmt"
	"os"
	"path/filepath"
	"syscall"

	"gitlab.com/gitlab-org/gitlab-shell/go/internal/command/commandargs"
	"gitlab.com/gitlab-org/gitlab-shell/go/internal/executable"
)

type Command struct {
	Executable *executable.Executable
	RootDir    string
	Args       commandargs.CommandArgs
}

var (
	// execFunc is overridden in tests
	execFunc  = syscall.Exec
	whitelist = []string{
		executable.GitlabShell,
		executable.AuthorizedKeysCheck,
		executable.AuthorizedPrincipalsCheck,
	}
)

func (c *Command) Execute() error {
	if !c.isWhitelisted() {
		return errors.New("Failed to execute unknown executable")
	}

	rubyCmd := c.fallbackProgram()

	// Ensure rubyArgs[0] is the full path to gitlab-shell-ruby
	rubyArgs := append([]string{rubyCmd}, c.Args.GetArguments()...)

	return execFunc(rubyCmd, rubyArgs, os.Environ())
}

func (c *Command) isWhitelisted() bool {
	for _, item := range whitelist {
		if c.Executable.Name == item {
			return true
		}
	}

	return false
}

func (c *Command) fallbackProgram() string {
	fileName := fmt.Sprintf("%s-ruby", c.Executable.Name)

	return filepath.Join(c.RootDir, "bin", fileName)
}