From 0dce99f5f49085e9a81a0aca9ea0ecab83f2d021 Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Mon, 17 Nov 2014 11:18:58 -0800 Subject: [PATCH] instructs Drone to ignore cert verification if self-signed --- plugin/remote/github/github.go | 34 +++++++++++++++++--------------- plugin/remote/github/helper.go | 12 ++++++++++- plugin/remote/github/register.go | 12 ++++++----- 3 files changed, 36 insertions(+), 22 deletions(-) diff --git a/plugin/remote/github/github.go b/plugin/remote/github/github.go index 6e7981273..31ff84e53 100644 --- a/plugin/remote/github/github.go +++ b/plugin/remote/github/github.go @@ -21,20 +21,22 @@ const ( ) type GitHub struct { - URL string - API string - Client string - Secret string - Private bool + URL string + API string + Client string + Secret string + Private bool + SkipVerify bool } -func New(url, api, client, secret string, private bool) *GitHub { +func New(url, api, client, secret string, private, skipVerify bool) *GitHub { var github = GitHub{ - URL: url, - API: api, - Client: client, - Secret: secret, - Private: private, + URL: url, + API: api, + Client: client, + Secret: secret, + Private: private, + SkipVerify: skipVerify, } // the API must have a trailing slash if !strings.HasSuffix(github.API, "/") { @@ -48,7 +50,7 @@ func New(url, api, client, secret string, private bool) *GitHub { } func NewDefault(client, secret string) *GitHub { - return New(DefaultURL, DefaultAPI, client, secret, false) + return New(DefaultURL, DefaultAPI, client, secret, false, false) } // Authorize handles GitHub API Authorization. @@ -84,7 +86,7 @@ func (r *GitHub) Authorize(res http.ResponseWriter, req *http.Request) (*model.L return nil, fmt.Errorf("Error exchanging token. %s", err) } - var client = NewClient(r.API, token.AccessToken) + var client = NewClient(r.API, token.AccessToken, r.SkipVerify) var useremail, errr = GetUserEmail(client) if errr != nil { return nil, fmt.Errorf("Error retrieving user or verified email. %s", errr) @@ -127,7 +129,7 @@ func (r *GitHub) IsEnterprise() bool { // user has access to in the remote system. func (r *GitHub) GetRepos(user *model.User) ([]*model.Repo, error) { var repos []*model.Repo - var client = NewClient(r.API, user.Access) + var client = NewClient(r.API, user.Access, r.SkipVerify) var list, err = GetAllRepos(client) if err != nil { return nil, err @@ -173,14 +175,14 @@ func (r *GitHub) GetRepos(user *model.User) ([]*model.Repo, error) { // GetScript fetches the build script (.drone.yml) from the remote // repository and returns in string format. func (r *GitHub) GetScript(user *model.User, repo *model.Repo, hook *model.Hook) ([]byte, error) { - var client = NewClient(r.API, user.Access) + var client = NewClient(r.API, user.Access, r.SkipVerify) return GetFile(client, repo.Owner, repo.Name, ".drone.yml", hook.Sha) } // Activate activates a repository by adding a Post-commit hook and // a Public Deploy key, if applicable. func (r *GitHub) Activate(user *model.User, repo *model.Repo, link string) error { - var client = NewClient(r.API, user.Access) + var client = NewClient(r.API, user.Access, r.SkipVerify) var title, err = GetKeyTitle(link) if err != nil { return err diff --git a/plugin/remote/github/helper.go b/plugin/remote/github/helper.go index b6ea885b8..b55895a89 100644 --- a/plugin/remote/github/helper.go +++ b/plugin/remote/github/helper.go @@ -1,6 +1,7 @@ package github import ( + "crypto/tls" "encoding/base32" "fmt" "io/ioutil" @@ -15,10 +16,19 @@ import ( // NewClient is a helper function that returns a new GitHub // client using the provided OAuth token. -func NewClient(uri, token string) *github.Client { +func NewClient(uri, token string, skipVerify bool) *github.Client { t := &oauth.Transport{ Token: &oauth.Token{AccessToken: token}, } + + // this is for GitHub enterprise users that are using + // self-signed certificates. + if skipVerify { + t.Transport = &http.Transport{ + TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, + } + } + c := github.NewClient(t.Client()) c.BaseURL, _ = url.Parse(uri) return c diff --git a/plugin/remote/github/register.go b/plugin/remote/github/register.go index ae4e1e946..eb71403f2 100644 --- a/plugin/remote/github/register.go +++ b/plugin/remote/github/register.go @@ -11,11 +11,12 @@ var ( githubSecret = config.String("github-secret", "") // GitHub Enterprise configuration details - githubEnterpriseURL = config.String("github-enterprise-url", "") - githubEnterpriseAPI = config.String("github-enterprise-api", "") - githubEnterpriseClient = config.String("github-enterprise-client", "") - githubEnterpriseSecret = config.String("github-enterprise-secret", "") - githubEnterprisePrivate = config.Bool("github-enterprise-private-mode", true) + githubEnterpriseURL = config.String("github-enterprise-url", "") + githubEnterpriseAPI = config.String("github-enterprise-api", "") + githubEnterpriseClient = config.String("github-enterprise-client", "") + githubEnterpriseSecret = config.String("github-enterprise-secret", "") + githubEnterprisePrivate = config.Bool("github-enterprise-private-mode", true) + githubEnterpriseSkipVerify = config.Bool("github-enterprise-skip-verify", false) ) // Registers the GitHub plugins using the default @@ -51,6 +52,7 @@ func registerGitHubEnterprise() { *githubEnterpriseClient, *githubEnterpriseSecret, *githubEnterprisePrivate, + *githubEnterpriseSkipVerify, ), ) }