Skip to content

Commit 8ad46db

Browse files
authored
Merge pull request #2043 from FabianKramm/master
feat: add proxyCommands.gitCredentials
2 parents 581a768 + eae0359 commit 8ad46db

7 files changed

Lines changed: 85 additions & 2 deletions

File tree

helper/cmd/proxycommands/configure.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ package proxycommands
33
import (
44
"encoding/base64"
55
"fmt"
6+
"github.com/mitchellh/go-homedir"
67
"io/ioutil"
78
"os"
9+
"path/filepath"
810
"strings"
911

1012
"github.com/loft-sh/devspace/helper/util/stderrlog"
@@ -24,6 +26,8 @@ type ConfigureCmd struct {
2426
PrivateKey string
2527
WorkingDir string
2628

29+
GitCredentials bool
30+
2731
Commands []string
2832
}
2933

@@ -41,6 +45,7 @@ func NewConfigureCmd() *cobra.Command {
4145
configureCmd.Flags().StringVar(&cmd.PrivateKey, "private-key", "", "Private key to use")
4246
configureCmd.Flags().StringVar(&cmd.WorkingDir, "working-dir", "", "Working dir to use")
4347
configureCmd.Flags().StringSliceVar(&cmd.Commands, "commands", []string{}, "Commands to overwrite")
48+
configureCmd.Flags().BoolVar(&cmd.GitCredentials, "git-credentials", false, "If git credentials should get configured")
4449
return configureCmd
4550
}
4651

@@ -118,6 +123,24 @@ func (cmd *ConfigureCmd) Run(_ *cobra.Command, _ []string) error {
118123
}
119124
}
120125

126+
// now configure git credentials
127+
if cmd.GitCredentials {
128+
homeDir, err := homedir.Dir()
129+
if err != nil {
130+
return err
131+
}
132+
133+
gitConfigPath := filepath.Join(homeDir, ".gitconfig")
134+
out, err = ioutil.ReadFile(gitConfigPath)
135+
if err != nil || !strings.Contains(string(out), "helper = \"/tmp/devspacehelper proxy-commands git-credentials\"") {
136+
content := string(out) + "\n" + "[credential]" + "\n" + " helper = \"/tmp/devspacehelper proxy-commands git-credentials\"\n"
137+
err = ioutil.WriteFile(gitConfigPath, []byte(content), 0644)
138+
if err != nil {
139+
return errors.Wrap(err, "write git config")
140+
}
141+
}
142+
}
143+
121144
// print working dir to stdout
122145
fmt.Print(workingDir)
123146
return nil
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package proxycommands
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
)
6+
7+
// GitCredentials holds the cmd flags
8+
type GitCredentials struct{}
9+
10+
// NewGitCredentialsCmd creates a new ssh command
11+
func NewGitCredentialsCmd() *cobra.Command {
12+
cmd := &GitCredentials{}
13+
runCmd := &cobra.Command{
14+
Use: "git-credentials",
15+
Short: "Retrieves git credentials from local",
16+
DisableFlagParsing: true,
17+
RunE: cmd.Run,
18+
}
19+
return runCmd
20+
}
21+
22+
// Run runs the command logic
23+
func (cmd *GitCredentials) Run(_ *cobra.Command, args []string) error {
24+
if len(args) == 0 {
25+
return nil
26+
} else if args[0] != "get" {
27+
return nil
28+
}
29+
30+
return runProxyCommand([]string{"git-credentials", "credential", "fill"})
31+
}

helper/cmd/proxycommands/proxy_commands.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ func NewProxyCommands() *cobra.Command {
1414

1515
reverseCommandsCmd.AddCommand(NewConfigureCmd())
1616
reverseCommandsCmd.AddCommand(NewRunCmd())
17+
reverseCommandsCmd.AddCommand(NewGitCredentialsCmd())
1718
return reverseCommandsCmd
1819
}

helper/cmd/proxycommands/run.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ func NewRunCmd() *cobra.Command {
3030

3131
// Run runs the command logic
3232
func (cmd *RunCmd) Run(_ *cobra.Command, args []string) error {
33+
return runProxyCommand(args)
34+
}
35+
36+
func runProxyCommand(args []string) error {
3337
key, err := ioutil.ReadFile(sshPrivateKeyPath)
3438
if err != nil {
3539
return errors.Wrap(err, "read private key")

pkg/devspace/config/versions/latest/schema.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,9 @@ type RestartHelper struct {
916916
}
917917

918918
type ProxyCommand struct {
919+
// GitCredentials configures a git credentials helper inside the container that proxies local git credentials
920+
GitCredentials bool `yaml:"gitCredentials,omitempty" json:"gitCredentials,omitempty"`
921+
919922
// Command is the name of the command that should be available in the remote container. DevSpace
920923
// will create a small script for that inside the container that redirect command execution to
921924
// the local computer.

pkg/devspace/services/proxycommands/commands.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,11 @@ func startLocalSSH(ctx devspacecontext.Context, selector targetselector.TargetSe
9797

9898
// gather all commands that should get replaced in the container
9999
commandsToReplace := []string{}
100+
gitCredentials := false
100101
for _, r := range reverseCommands {
102+
if r.GitCredentials {
103+
gitCredentials = true
104+
}
101105
if r.Command == "" {
102106
continue
103107
}
@@ -106,7 +110,14 @@ func startLocalSSH(ctx devspacecontext.Context, selector targetselector.TargetSe
106110
}
107111

108112
// execute configure command in container
109-
command := []string{inject.DevSpaceHelperContainerPath, "proxy-commands", "configure", "--public-key", base64.StdEncoding.EncodeToString([]byte(publicKey)), "--private-key", base64.StdEncoding.EncodeToString([]byte(privateKey)), "--commands", strings.Join(commandsToReplace, ",")}
113+
command := []string{inject.DevSpaceHelperContainerPath, "proxy-commands", "configure", "--public-key", base64.StdEncoding.EncodeToString([]byte(publicKey)), "--private-key", base64.StdEncoding.EncodeToString([]byte(privateKey))}
114+
if len(commandsToReplace) > 0 {
115+
command = append(command, "--commands", strings.Join(commandsToReplace, ","))
116+
}
117+
if gitCredentials {
118+
command = append(command, "--git-credentials")
119+
}
120+
110121
stdout, stderr, err := ctx.KubeClient().ExecBuffered(ctx.Context(), container.Pod, container.Container.Name, command, nil)
111122
if err != nil {
112123
return fmt.Errorf("error setting up proxy commands in container: %s %s %v", string(stdout), string(stderr), err)

pkg/devspace/services/proxycommands/server.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ func (s *Server) getCommand(sess ssh.Session) (*exec.Cmd, *types.ProxyCommand, e
140140

141141
var reverseCommand *latest.ProxyCommand
142142
for _, r := range s.commands {
143+
if r.GitCredentials && command.Args[0] == "git-credentials" {
144+
reverseCommand = r
145+
break
146+
}
143147
if r.Command == command.Args[0] {
144148
reverseCommand = r
145149
break
@@ -153,6 +157,9 @@ func (s *Server) getCommand(sess ssh.Session) (*exec.Cmd, *types.ProxyCommand, e
153157
if reverseCommand.LocalCommand != "" {
154158
c = reverseCommand.LocalCommand
155159
}
160+
if reverseCommand.GitCredentials {
161+
c = "git"
162+
}
156163

157164
args := []string{}
158165
for _, arg := range command.Args[1:] {
@@ -176,13 +183,16 @@ func (s *Server) getCommand(sess ssh.Session) (*exec.Cmd, *types.ProxyCommand, e
176183
}
177184

178185
s.log.Debugf("run command '%s %s' locally", c, strings.Join(args, " "))
179-
if !reverseCommand.SkipContainerEnv {
186+
if !reverseCommand.SkipContainerEnv && !reverseCommand.GitCredentials {
180187
cmd.Env = append(cmd.Env, command.Env...)
181188
}
182189
cmd.Env = append(cmd.Env, os.Environ()...)
183190
for k, v := range reverseCommand.Env {
184191
cmd.Env = append(cmd.Env, k+"="+v)
185192
}
193+
if reverseCommand.GitCredentials {
194+
cmd.Env = append(cmd.Env, "GIT_ASKPASS=true")
195+
}
186196
return cmd, command, nil
187197
}
188198

0 commit comments

Comments
 (0)