Skip to content

Commit

Permalink
git-init don't assume the image is running as root
Browse files Browse the repository at this point in the history
We check what is our real home for our compared to what we have with $HOME and
symlink it so GIT+SSH works.

We use the go-homedir lib because user.Current doesn't work when cross
compiling (i.e: from osx with ko)

Fixes #724
  • Loading branch information
chmouel authored and tekton-robot committed Apr 11, 2019
1 parent 40c9b4a commit 41dfd05
Show file tree
Hide file tree
Showing 4 changed files with 214 additions and 7 deletions.
9 changes: 9 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 17 additions & 7 deletions cmd/git-init/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"os/exec"

"github.com/knative/pkg/logging"
homedir "github.com/mitchellh/go-homedir"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -59,15 +60,24 @@ func main() {
logger, _ := logging.NewLogger("", "git-init")
defer logger.Sync()

// HACK HACK HACK
// Git seems to ignore $HOME/.ssh and look in /root/.ssh for unknown reasons.
// As a workaround, symlink /root/.ssh to where we expect the $HOME to land.
// This means SSH auth only works for our built-in git support, and not
// custom steps.
err := os.Symlink("/builder/home/.ssh", "/root/.ssh")
// HACK: This is to get git+ssh to work since ssh doesn't respect the HOME
// env variable.
homepath, err := homedir.Dir()
if err != nil {
logger.Fatalf("Unexpected error creating symlink: %v", err)
logger.Fatalf("Unexpected error: getting the user home directory: %v", err)
}
homeenv := os.Getenv("HOME")
if homeenv != "" && homeenv != homepath {
if _, err := os.Stat(homepath + "/.ssh"); os.IsNotExist(err) {
err = os.Symlink(homeenv+"/.ssh", homepath+"/.ssh")
if 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)
}
}
}

if *revision == "" {
*revision = "master"
}
Expand Down
21 changes: 21 additions & 0 deletions vendor/github.com/mitchellh/go-homedir/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

167 changes: 167 additions & 0 deletions vendor/github.com/mitchellh/go-homedir/homedir.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 41dfd05

Please sign in to comment.