Skip to content

Commit

Permalink
feat: add support for GIT_SSL_NO_VERIFY when cloning repos
Browse files Browse the repository at this point in the history
addresses #243
  • Loading branch information
patrickdevivo committed Feb 2, 2022
1 parent e29a72d commit 564f2c5
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 16 deletions.
1 change: 1 addition & 0 deletions cmd/root.go
Expand Up @@ -19,6 +19,7 @@ var presetQuery string // named / preset query fl
var dbPath string // path to sqlite db file on disk to mount on
var repo string // path to repo on disk
var cloneDir string // path to directory to clone repos in
var gitSSLNoVerify = os.Getenv("GIT_SSL_NO_VERIFY") // if set to anything, will not verify SSL when cloning
var githubToken = os.Getenv("GITHUB_TOKEN") // GitHub auth token for GitHub tables
var sourcegraphToken = os.Getenv("SOURCEGRAPH_TOKEN") // Sourcegraph auth token for Sourcegraph queries
var verbose bool // whether or not to print logs to stderr
Expand Down
3 changes: 2 additions & 1 deletion cmd/setup.go
Expand Up @@ -16,7 +16,8 @@ import (

func registerExt() {
multiLocOpt := &locator.MultiLocatorOptions{
CloneDir: cloneDir,
CloneDir: cloneDir,
InsecureSkipTLS: gitSSLNoVerify != "",
}
if githubToken != "" {
multiLocOpt.HTTPAuth = &http.BasicAuth{Username: githubToken}
Expand Down
21 changes: 11 additions & 10 deletions pkg/locator/locator.go
Expand Up @@ -97,7 +97,7 @@ func determineCloneDir(path, baseCloneDir string) (string, bool, error) {
// http repositories on-demand into temporary storage. It is recommended
// that you club it with something like CachedLocator to improve performance
// and remove the need to clone a single repository multiple times.
func HttpLocator(cloneDir string) func() services.RepoLocator {
func HttpLocator(o *MultiLocatorOptions) func() services.RepoLocator {
return func() services.RepoLocator {
return options.RepoLocatorFn(func(ctx context.Context, path string) (*git.Repository, error) {
var err error
Expand All @@ -107,11 +107,11 @@ func HttpLocator(cloneDir string) func() services.RepoLocator {

var cd string
var isTmp bool
if cd, isTmp, err = determineCloneDir(path, cloneDir); err != nil {
if cd, isTmp, err = determineCloneDir(path, o.CloneDir); err != nil {
return nil, errors.Wrap(err, "could not determine clone directory")
}

return git.PlainCloneContext(ctx, cd, isTmp, &git.CloneOptions{URL: path})
return git.PlainCloneContext(ctx, cd, isTmp, &git.CloneOptions{URL: path, InsecureSkipTLS: o.InsecureSkipTLS})
})
}
}
Expand Down Expand Up @@ -145,7 +145,7 @@ func httpLocatorWithAuth(user, pass string, rl services.RepoLocator) func() serv
// ssh repositories on-demand into temporary storage. It is recommended
// that you club it with something like CachedLocator to improve performance
// and remove the need to clone a single repository multiple times.
func SSHLocator(cloneDir string) func() services.RepoLocator {
func SSHLocator(o *MultiLocatorOptions) func() services.RepoLocator {
return func() services.RepoLocator {
return options.RepoLocatorFn(func(ctx context.Context, path string) (*git.Repository, error) {
path = strings.TrimPrefix(path, "ssh://")
Expand All @@ -166,7 +166,7 @@ func SSHLocator(cloneDir string) func() services.RepoLocator {
var cd string
var isTmp bool
var err error
if cd, isTmp, err = determineCloneDir(path, cloneDir); err != nil {
if cd, isTmp, err = determineCloneDir(path, o.CloneDir); err != nil {
return nil, errors.Wrap(err, "could not determine clone directory")
}

Expand All @@ -175,14 +175,15 @@ func SSHLocator(cloneDir string) func() services.RepoLocator {
return nil, errors.Wrap(err, "failed to create an SSH authentication method")
}

return git.PlainCloneContext(ctx, cd, isTmp, &git.CloneOptions{URL: path, Auth: auth})
return git.PlainCloneContext(ctx, cd, isTmp, &git.CloneOptions{URL: path, Auth: auth, InsecureSkipTLS: o.InsecureSkipTLS})
})
}
}

type MultiLocatorOptions struct {
HTTPAuth *http.BasicAuth
CloneDir string
HTTPAuth *http.BasicAuth
CloneDir string
InsecureSkipTLS bool
}

// MultiLocator returns a locator service that work with multiple git protocols
Expand All @@ -192,8 +193,8 @@ func MultiLocator(o *MultiLocatorOptions) services.RepoLocator {
o = &MultiLocatorOptions{}
}
var locators = map[string]func() services.RepoLocator{
"http": HttpLocator(o.CloneDir),
"ssh": SSHLocator(o.CloneDir),
"http": HttpLocator(o),
"ssh": SSHLocator(o),
"file": DiskLocator,
}

Expand Down
11 changes: 6 additions & 5 deletions shared.go
Expand Up @@ -16,17 +16,18 @@ import (
)

func init() {
multiLocOpt := &locator.MultiLocatorOptions{
InsecureSkipTLS: os.Getenv("GIT_SSL_NO_VERIFY") != "",
}

githubToken := os.Getenv("GITHUB_TOKEN")
var multiLocOpt locator.MultiLocatorOptions
if githubToken != "" {
multiLocOpt = locator.MultiLocatorOptions{
HTTPAuth: &http.BasicAuth{Username: githubToken},
}
multiLocOpt.HTTPAuth = &http.BasicAuth{Username: githubToken}
}

sqlite.Register(extensions.RegisterFn(
options.WithExtraFunctions(),
options.WithRepoLocator(locator.CachedLocator(locator.MultiLocator(&multiLocOpt))),
options.WithRepoLocator(locator.CachedLocator(locator.MultiLocator(multiLocOpt))),
options.WithGitHub(),
options.WithContextValue("githubToken", githubToken),
options.WithContextValue("githubPerPage", os.Getenv("GITHUB_PER_PAGE")),
Expand Down

0 comments on commit 564f2c5

Please sign in to comment.