-
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
- Loading branch information
Showing
10 changed files
with
850 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,24 +19,119 @@ | |
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 struct { | ||
*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. | ||
func (g gitSSHURL) owner() string { | ||
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: 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 { | ||
log.Debugf("Executing 'git' with args: %v", args) | ||
cmd := exec.CommandContext(cf.Context, "git", args...) | ||
cmd.Stdin = cf.Stdin() | ||
cmd.Stdout = stdout | ||
cmd.Stderr = stderr | ||
return trace.Wrap(cf.RunCommand(cmd)) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Teleport | ||
* Copyright (C) 2024 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 common | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/alecthomas/kingpin/v2" | ||
"github.com/gravitational/trace" | ||
) | ||
|
||
// gitCloneCommand implements `tsh git clone`. | ||
// | ||
// This command internally executes `git clone` while setting `core.sshcommand`. | ||
// You can generally assume the user has `git` binary installed (otherwise there | ||
// is no point using the `git` proxy feature). An alternative is to use the | ||
// `go-git` library. | ||
type gitCloneCommand struct { | ||
*kingpin.CmdClause | ||
|
||
repository string | ||
directory string | ||
} | ||
|
||
func newGitCloneCommand(parent *kingpin.CmdClause) *gitCloneCommand { | ||
cmd := &gitCloneCommand{ | ||
CmdClause: parent.Command("clone", "Clone a Git repository."), | ||
} | ||
|
||
cmd.Arg("repository", "Git URL of the repository to clone.").Required().StringVar(&cmd.repository) | ||
cmd.Arg("directory", "The name of a new directory to clone into.").StringVar(&cmd.directory) | ||
// TODO(greedy52) support passing extra args to git like --branch/--depth. | ||
return cmd | ||
} | ||
|
||
func (c *gitCloneCommand) run(cf *CLIConf) error { | ||
u, err := parseGitSSHURL(c.repository) | ||
if err != nil { | ||
return trace.Wrap(err) | ||
} | ||
if !u.isGitHub() { | ||
return trace.BadParameter("not a GitHub repository") | ||
} | ||
|
||
sshCommand := makeGitCoreSSHCommand(cf.executablePath, u.owner()) | ||
args := []string{ | ||
"clone", | ||
"--config", fmt.Sprintf("%s=%s", gitCoreSSHCommand, sshCommand), | ||
c.repository, | ||
} | ||
if c.directory != "" { | ||
args = append(args, c.directory) | ||
} | ||
return trace.Wrap(execGit(cf, args...)) | ||
} |
Oops, something went wrong.