Skip to content

Allows use of private repositories #119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.0.0
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
golang.org/x/tools v0.0.0-20200714190737-9048b464a08d
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543
k8s.io/api v0.18.4
Expand Down
93 changes: 91 additions & 2 deletions pkg/patterns/addon/pkg/loaders/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ import (
"context"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"

"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/transport"
gitssh "github.com/go-git/go-git/v5/plumbing/transport/ssh"
"golang.org/x/crypto/ssh"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/yaml"
)
Expand Down Expand Up @@ -88,13 +92,29 @@ func (r *GitRepository) readURL(url string) ([]byte, error) {
repoDir := "/tmp/repo"
filePath := filepath.Join(repoDir, url)
fmt.Println(r.baseURL)
_, err := git.PlainClone(repoDir, false, &git.CloneOptions{
URL: r.baseURL,

auth, err := getAuthMethod()
if err != nil {
return nil, err
}

_, err = git.PlainClone(repoDir, false, &git.CloneOptions{
URL: r.baseURL,
RecurseSubmodules: git.DefaultSubmoduleRecursionDepth,
Auth: auth,
})

if err != nil && err != git.ErrRepositoryAlreadyExists {
return nil, err
}

if err == git.ErrRepositoryAlreadyExists {
err := handleExistingRepo(repoDir, auth)
if err != nil {
return nil, err
}
}

b, err := ioutil.ReadFile(filePath)
if err != nil {
return nil, err
Expand Down Expand Up @@ -122,3 +142,72 @@ func parseGitURL(url string) GitRepository {
subDir: subdir,
}
}

func handleExistingRepo(path string, auth transport.AuthMethod) error {
gitRepo, err := git.PlainOpen(path)
if err != nil {
return err
}

remote, err := gitRepo.Remote("origin")
if err != nil {
return err
}

err = remote.Fetch(&git.FetchOptions{
Force: true,
Auth: auth,
})
if err != nil && err != git.NoErrAlreadyUpToDate {
return err
}

w, err := gitRepo.Worktree()
if err != nil {
return err
}

err = w.Checkout(&git.CheckoutOptions{
Branch: "refs/heads/master",
})
if err != nil {
return err
}

err = w.Reset(&git.ResetOptions{
Mode: git.HardReset,
})
if err != nil {
return err
}

return nil
}

func getAuthMethod() (transport.AuthMethod, error) {
sshFile := fmt.Sprintf("%s/.ssh/id_rsa", os.Getenv("HOME"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the future, we could also support setting this directly in an env var (e.g. GIT_SSH_KEY or something like that). But this is good

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds great! I will do so in a follow-up PR.

if _, err := os.Stat(sshFile); os.IsNotExist(err) {
return nil, nil
}
sshBytes, err := ioutil.ReadFile(sshFile)
if err != nil {
return nil, err
}

sshPassphrase := os.Getenv("SSH_PASSPHRASE")
var signer ssh.Signer
if sshPassphrase != "" {
signer, err = ssh.ParsePrivateKeyWithPassphrase(sshBytes, []byte(sshPassphrase))
} else {
signer, err = ssh.ParsePrivateKey(sshBytes)
}
if err != nil {
return nil, err
}

auth := &gitssh.PublicKeys{
Signer: signer,
User: "git",
}
return auth, nil
}