-
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
8 changed files
with
825 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* 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`. | ||
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...)) | ||
} |
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,114 @@ | ||
/* | ||
* 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 ( | ||
"context" | ||
"os/exec" | ||
"slices" | ||
"testing" | ||
|
||
"github.com/gravitational/trace" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestGitCloneCommand(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
cmd *gitCloneCommand | ||
verifyCommand func(*exec.Cmd) error | ||
checkError require.ErrorAssertionFunc | ||
}{ | ||
{ | ||
name: "success", | ||
cmd: &gitCloneCommand{ | ||
repository: "git@github.com:gravitational/teleport.git", | ||
}, | ||
verifyCommand: func(cmd *exec.Cmd) error { | ||
expect := []string{ | ||
"git", "clone", | ||
"--config", "core.sshcommand=tsh git ssh --github-org gravitational", | ||
"git@github.com:gravitational/teleport.git", | ||
} | ||
if !slices.Equal(expect, cmd.Args) { | ||
return trace.CompareFailed("expect %v but got %v", expect, cmd.Args) | ||
} | ||
return nil | ||
}, | ||
checkError: require.NoError, | ||
}, | ||
{ | ||
name: "success with target dir", | ||
cmd: &gitCloneCommand{ | ||
repository: "git@github.com:gravitational/teleport.git", | ||
directory: "target_dir", | ||
}, | ||
verifyCommand: func(cmd *exec.Cmd) error { | ||
expect := []string{ | ||
"git", "clone", | ||
"--config", "core.sshcommand=tsh git ssh --github-org gravitational", | ||
"git@github.com:gravitational/teleport.git", | ||
"target_dir", | ||
} | ||
if !slices.Equal(expect, cmd.Args) { | ||
return trace.CompareFailed("expect %v but got %v", expect, cmd.Args) | ||
} | ||
return nil | ||
}, | ||
checkError: require.NoError, | ||
}, | ||
{ | ||
name: "invalid URL", | ||
cmd: &gitCloneCommand{ | ||
repository: "not-a-git-ssh-url", | ||
}, | ||
checkError: require.Error, | ||
}, | ||
{ | ||
name: "unsupported Git service", | ||
cmd: &gitCloneCommand{ | ||
repository: "git@gitlab.com:group/project.git", | ||
}, | ||
checkError: require.Error, | ||
}, | ||
{ | ||
name: "git fails", | ||
cmd: &gitCloneCommand{ | ||
repository: "git@github.com:gravitational/teleport.git", | ||
}, | ||
verifyCommand: func(cmd *exec.Cmd) error { | ||
return trace.BadParameter("some git error") | ||
}, | ||
checkError: func(t require.TestingT, err error, i ...interface{}) { | ||
require.ErrorIs(t, err, trace.BadParameter("some git error")) | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
cf := &CLIConf{ | ||
Context: context.Background(), | ||
executablePath: "tsh", | ||
cmdRunner: tt.verifyCommand, | ||
} | ||
tt.checkError(t, tt.cmd.run(cf)) | ||
}) | ||
} | ||
} |
Oops, something went wrong.