Skip to content

Commit

Permalink
Fix git-init failure with git ssh creds 🔑
Browse files Browse the repository at this point in the history
In case of a private git resources that uses `ssh` keys, `git`
wouldn't find the correct `.ssh/known_hosts`. This only happens in
case of the user of the container is `root`.

We now treat use `root` as a special case. It should now work for any
user — being root or not.

Signed-off-by: Vincent Demeester <vdemeest@redhat.com>
  • Loading branch information
vdemeester committed Apr 26, 2019
1 parent 639b300 commit 10bacd3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
7 changes: 6 additions & 1 deletion pkg/credentials/gitcreds/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (

corev1 "k8s.io/api/core/v1"

"github.com/mitchellh/go-homedir"
"github.com/tektoncd/pipeline/pkg/credentials"
)

Expand Down Expand Up @@ -76,7 +77,11 @@ func (dc *sshGitConfig) Set(value string) error {
}

func (dc *sshGitConfig) Write() error {
sshDir := filepath.Join(os.Getenv("HOME"), ".ssh")
homepath, err := homedir.Dir()
if err != nil {
return err
}
sshDir := filepath.Join(homepath, ".ssh")
if err := os.MkdirAll(sshDir, os.ModePerm); err != nil {
return err
}
Expand Down
16 changes: 13 additions & 3 deletions pkg/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"os"
"os/exec"
"os/user"
"strings"

homedir "github.com/mitchellh/go-homedir"
Expand Down Expand Up @@ -48,10 +49,19 @@ func Fetch(logger *zap.SugaredLogger, revision, path, url string) error {
return err
}
homeenv := os.Getenv("HOME")
if homeenv != "" && homeenv != homepath {
u, err := user.Current()
if err != nil {
return err
}
if u.Name == "root" {
if err := os.Symlink(homeenv+"/.ssh", "/root/.ssh"); err != nil {
// Only do a warning, in case we don't have a real home
// directory writable in our image
logger.Warnf("Unexpected error: creating symlink: %v", err)
}
} else if homeenv != "" && homeenv != homepath {
if _, err := os.Stat(homepath + "/.ssh"); os.IsNotExist(err) {
err = os.Symlink(homeenv+"/.ssh", homepath+"/.ssh")
if err != nil {
if err := os.Symlink(homeenv+"/.ssh", homepath+"/.ssh"); err != nil {
// Only do a warning, in case we don't have a real home
// directory writable in our image
logger.Warnf("Unexpected error: creating symlink: %v", err)
Expand Down

0 comments on commit 10bacd3

Please sign in to comment.