(sess ssh.Session)
| 124 | } |
| 125 | |
| 126 | func (s *Server) getCommand(sess ssh.Session) (*exec.Cmd, *types.ProxyCommand, error) { |
| 127 | var cmd *exec.Cmd |
| 128 | rawCommand := sess.RawCommand() |
| 129 | if len(rawCommand) == 0 { |
| 130 | return nil, nil, fmt.Errorf("command required") |
| 131 | } |
| 132 | |
| 133 | command := &types.ProxyCommand{} |
| 134 | err := json.Unmarshal([]byte(rawCommand), &command) |
| 135 | if err != nil { |
| 136 | return nil, nil, fmt.Errorf("parse command: %v", err) |
| 137 | } else if len(command.Args) == 0 { |
| 138 | return nil, nil, fmt.Errorf("command is empty") |
| 139 | } |
| 140 | |
| 141 | var reverseCommand *latest.ProxyCommand |
| 142 | for _, r := range s.commands { |
| 143 | if r.GitCredentials && command.Args[0] == "git-credentials" { |
| 144 | reverseCommand = r |
| 145 | break |
| 146 | } |
| 147 | if r.Command == command.Args[0] { |
| 148 | reverseCommand = r |
| 149 | break |
| 150 | } |
| 151 | } |
| 152 | if reverseCommand == nil { |
| 153 | return nil, nil, fmt.Errorf("command not allowed") |
| 154 | } |
| 155 | |
| 156 | c := reverseCommand.Command |
| 157 | if reverseCommand.LocalCommand != "" { |
| 158 | c = reverseCommand.LocalCommand |
| 159 | } |
| 160 | if reverseCommand.GitCredentials { |
| 161 | c = "git" |
| 162 | } |
| 163 | |
| 164 | args := []string{} |
| 165 | for _, arg := range command.Args[1:] { |
| 166 | splitted := strings.Split(arg, "=") |
| 167 | if len(splitted) == 1 { |
| 168 | args = append(args, s.transformPath(arg)) |
| 169 | continue |
| 170 | } |
| 171 | |
| 172 | args = append(args, splitted[0]+"="+s.transformPath(strings.Join(splitted[1:], "="))) |
| 173 | } |
| 174 | |
| 175 | cmd = exec.Command(c, args...) |
| 176 | cmd.Dir = s.transformPath(command.WorkingDir) |
| 177 | |
| 178 | // make sure working dir exists otherwise we get an error |
| 179 | _, err = os.Stat(cmd.Dir) |
| 180 | if err != nil { |
| 181 | s.log.Debugf("unknown working dir: %s", cmd.Dir) |
| 182 | cmd.Dir = os.TempDir() |
| 183 | } |
no test coverage detected