mirror of
https://github.com/gravitational/teleport.git
synced 2026-09-24 16:17:11 +08:00
* GitHub proxy part 6: proxing Git using SSH transport * better command parsing and update suite * refactor * revert unnecearrty files * address review comments * ut fix * revert localsite_test.go * change special suffix to teleport-github-org for routing * fix routing ut * minor typo edit * fix ut after sshca change * add UT to sshutils * minor review comments * fix api ut because of special suffix change * GitServerReadOnlyClient * downgrade error to warning * run go mod tidy. not sure why it's needed * rename mock.go to mock_test.go
71 lines
2.1 KiB
Go
71 lines
2.1 KiB
Go
/*
|
|
* Teleport
|
|
* Copyright (C) 2025 Gravitational, Inc.
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU Affero General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
package sshutils
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"log/slog"
|
|
"syscall"
|
|
|
|
"github.com/gravitational/teleport"
|
|
)
|
|
|
|
// errorWithExitStatus defines an interface that provides an ExitStatus
|
|
// function to get the exit code of the process execution.
|
|
//
|
|
// This interface is introduced so ssh.ExitError can be mocked in unit test.
|
|
type errorWithExitStatus interface {
|
|
ExitStatus() int
|
|
}
|
|
|
|
// execExitError defines an interface that provides a Sys function to get exit
|
|
// status from the process execution.
|
|
//
|
|
// This interface is introduced so exec.ExitError can be mocked in unit test.
|
|
type execExitError interface {
|
|
Sys() any
|
|
}
|
|
|
|
// ExitCodeFromExecError extracts and returns the exit code from the
|
|
// error.
|
|
func ExitCodeFromExecError(err error) int {
|
|
// If no error occurred, return 0 (success).
|
|
if err == nil {
|
|
return teleport.RemoteCommandSuccess
|
|
}
|
|
|
|
var execExitErr execExitError
|
|
var exitErr errorWithExitStatus
|
|
switch {
|
|
case errors.As(err, &execExitErr):
|
|
waitStatus, ok := execExitErr.Sys().(syscall.WaitStatus)
|
|
if !ok {
|
|
return teleport.RemoteCommandFailure
|
|
}
|
|
return waitStatus.ExitStatus()
|
|
case errors.As(err, &exitErr):
|
|
return exitErr.ExitStatus()
|
|
// An error occurred, but the type is unknown, return a generic 255 code.
|
|
default:
|
|
slog.DebugContext(context.Background(), "Unknown error returned when executing command", "error", err)
|
|
return teleport.RemoteCommandFailure
|
|
}
|
|
}
|