-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GitHub proxy part 6.5: tsh git ssh/clone/config (#50044)
* GitHub proxy part 6.5: tsh git ssh/clone/config * review comments * fix test * fix ut for lookpath * fix logger and update dependency version * go mod tidy for integrations
- Loading branch information
Showing
14 changed files
with
888 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,24 +19,128 @@ | |
package common | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"os/exec" | ||
"strings" | ||
|
||
"github.com/alecthomas/kingpin/v2" | ||
"github.com/go-git/go-git/v5/plumbing/transport" | ||
"github.com/gravitational/trace" | ||
|
||
"github.com/gravitational/teleport/api/types" | ||
) | ||
|
||
type gitCommands struct { | ||
list *gitListCommand | ||
login *gitLoginCommand | ||
list *gitListCommand | ||
login *gitLoginCommand | ||
ssh *gitSSHCommand | ||
config *gitConfigCommand | ||
clone *gitCloneCommand | ||
} | ||
|
||
func newGitCommands(app *kingpin.Application) gitCommands { | ||
git := app.Command("git", "Git server commands.") | ||
cmds := gitCommands{ | ||
login: newGitLoginCommand(git), | ||
list: newGitListCommand(git), | ||
login: newGitLoginCommand(git), | ||
list: newGitListCommand(git), | ||
ssh: newGitSSHCommand(git), | ||
config: newGitConfigCommand(git), | ||
clone: newGitCloneCommand(git), | ||
} | ||
|
||
// TODO(greedy52) hide the commands until all basic features are implemented. | ||
git.Hidden() | ||
cmds.login.Hidden() | ||
cmds.list.Hidden() | ||
cmds.config.Hidden() | ||
cmds.clone.Hidden() | ||
return cmds | ||
} | ||
|
||
type gitSSHURL transport.Endpoint | ||
|
||
func (g gitSSHURL) check() error { | ||
switch { | ||
case g.isGitHub(): | ||
if err := types.ValidateGitHubOrganizationName(g.owner()); err != nil { | ||
return trace.Wrap(err) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (g gitSSHURL) isGitHub() bool { | ||
return g.Host == "github.com" | ||
} | ||
|
||
// owner returns the first part of the path. If the path does not have an owner, | ||
// an empty string is returned. | ||
// | ||
// For GitHub, owner is either the user or the organization that owns the repo. | ||
// | ||
// For example, if the SSH url is [email protected]:gravitational/teleport.git, the | ||
// owner would be "gravitational". | ||
func (g gitSSHURL) owner() string { | ||
// g.Path may have a preceding "/" from url.Parse. | ||
owner, _, ok := strings.Cut(strings.TrimPrefix(g.Path, "/"), "/") | ||
if !ok { | ||
return "" | ||
} | ||
return owner | ||
} | ||
|
||
// parseGitSSHURL parse a Git SSH URL. | ||
// | ||
// Git URL Spec: | ||
// - spec: https://git-scm.com/docs/git-clone#_git_urls | ||
// - example: ssh://example.org/path/to/repo.git | ||
// | ||
// GitHub (SCP-like) URL: | ||
// - spec: https://docs.github.com/en/get-started/getting-started-with-git/about-remote-repositories | ||
// - example: [email protected]:gravitational/teleport.git | ||
func parseGitSSHURL(originalURL string) (*gitSSHURL, error) { | ||
endpoint, err := transport.NewEndpoint(originalURL) | ||
if err != nil { | ||
return nil, trace.Wrap(err) | ||
} | ||
if endpoint.Protocol != "ssh" { | ||
return nil, trace.BadParameter("unsupported git ssh URL %s", originalURL) | ||
} | ||
s := gitSSHURL(*endpoint) | ||
if err := s.check(); err != nil { | ||
return nil, trace.Wrap(err) | ||
} | ||
return &s, nil | ||
} | ||
|
||
func execGitAndCaptureStdout(cf *CLIConf, args ...string) (string, error) { | ||
var bufStd bytes.Buffer | ||
if err := execGitWithStdoutAndStderr(cf, &bufStd, cf.Stderr(), args...); err != nil { | ||
return "", trace.Wrap(err) | ||
} | ||
return strings.TrimSpace(bufStd.String()), nil | ||
} | ||
|
||
func execGit(cf *CLIConf, args ...string) error { | ||
return trace.Wrap(execGitWithStdoutAndStderr(cf, cf.Stdout(), cf.Stderr(), args...)) | ||
} | ||
|
||
func execGitWithStdoutAndStderr(cf *CLIConf, stdout, stderr io.Writer, args ...string) error { | ||
const gitExecutable = "git" | ||
gitPath, err := cf.LookPath(gitExecutable) | ||
if err != nil { | ||
return trace.NotFound(`could not locate the executable %q. The following error occurred: | ||
%s | ||
tsh requires that the %q executable to be installed. | ||
You can install it by following the instructions at https://git-scm.com/book/en/v2/Getting-Started-Installing-Git`, | ||
gitExecutable, err.Error(), gitExecutable) | ||
} | ||
logger.DebugContext(cf.Context, "Executing git command", "path", gitPath, "args", args) | ||
cmd := exec.CommandContext(cf.Context, gitPath, args...) | ||
cmd.Stdin = cf.Stdin() | ||
cmd.Stdout = stdout | ||
cmd.Stderr = stderr | ||
return trace.Wrap(cf.RunCommand(cmd)) | ||
} |
Oops, something went wrong.