do not send the github token to aws

[finishes #113566853]
This commit is contained in:
Chris Brown
2016-02-12 12:23:08 -08:00
parent 1703fe6c05
commit 638dbc214e
2 changed files with 73 additions and 9 deletions

View File

@@ -3,12 +3,14 @@ package resource
import ( import (
"errors" "errors"
"io" "io"
"net/http"
"net/url" "net/url"
"os" "os"
"golang.org/x/oauth2" "golang.org/x/oauth2"
"github.com/google/go-github/github" "github.com/google/go-github/github"
"github.com/xoebus/statham"
) )
//go:generate counterfeiter . GitHub //go:generate counterfeiter . GitHub
@@ -42,11 +44,11 @@ func NewGitHubClient(source Source) (*GitHubClient, error) {
if source.AccessToken == "" { if source.AccessToken == "" {
client = github.NewClient(nil) client = github.NewClient(nil)
} else { } else {
ts := oauth2.StaticTokenSource(&oauth2.Token{ var err error
AccessToken: source.AccessToken, client, err = oauthClient(source)
}) if err != nil {
return nil, err
client = github.NewClient(oauth2.NewClient(oauth2.NoContext, ts)) }
} }
if source.GitHubAPIURL != "" { if source.GitHubAPIURL != "" {
@@ -219,3 +221,45 @@ func (g *GitHubClient) GetZipballLink(tag string) (*url.URL, error) {
res.Body.Close() res.Body.Close()
return u, nil return u, nil
} }
func oauthClient(source Source) (*github.Client, error) {
ts := oauth2.StaticTokenSource(&oauth2.Token{
AccessToken: source.AccessToken,
})
oauthClient := oauth2.NewClient(oauth2.NoContext, ts)
apiHost := "api.github.com"
if source.GitHubAPIURL != "" {
uri, err := url.Parse(source.GitHubAPIURL)
if err != nil {
return nil, err
}
apiHost = uri.Host
}
uploadHost := "uploads.github.com"
if source.GitHubUploadsURL != "" {
uri, err := url.Parse(source.GitHubUploadsURL)
if err != nil {
return nil, err
}
uploadHost = uri.Host
}
// The google/go-github library uses the same http.Client to perform
// requests to both github.com and the S3 download API (for downloading
// release assets). We don't want it to user the same OAuth transport for
// both.
transport := statham.NewTransport(http.DefaultTransport, statham.Mapping{
apiHost: oauthClient.Transport,
uploadHost: oauthClient.Transport,
})
httpClient := &http.Client{
Transport: transport,
}
return github.NewClient(httpClient), nil
}

View File

@@ -32,6 +32,26 @@ var _ = Describe("GitHub Client", func() {
server.Close() server.Close()
}) })
Context("with bad URLs", func() {
BeforeEach(func() {
source.AccessToken = "hello?"
})
It("returns an error if the API URL is bad", func() {
source.GitHubAPIURL = ":"
_, err := NewGitHubClient(source)
Ω(err).Should(HaveOccurred())
})
It("returns an error if the API URL is bad", func() {
source.GitHubUploadsURL = ":"
_, err := NewGitHubClient(source)
Ω(err).Should(HaveOccurred())
})
})
Context("with an OAuth Token", func() { Context("with an OAuth Token", func() {
BeforeEach(func() { BeforeEach(func() {
source = Source{ source = Source{
@@ -92,9 +112,9 @@ var _ = Describe("GitHub Client", func() {
}` }`
rateLimitHeaders := http.Header(map[string][]string{ rateLimitHeaders := http.Header(map[string][]string{
"X-RateLimit-Limit": {"60"}, "X-RateLimit-Limit": {"60"},
"X-RateLimit-Remaining": {"0"}, "X-RateLimit-Remaining": {"0"},
"X-RateLimit-Reset": {"1377013266"}, "X-RateLimit-Reset": {"1377013266"},
}) })
server.AppendHandlers( server.AppendHandlers(
@@ -128,9 +148,9 @@ var _ = Describe("GitHub Client", func() {
}` }`
rateLimitHeaders := http.Header(map[string][]string{ rateLimitHeaders := http.Header(map[string][]string{
"X-RateLimit-Limit": {"60"}, "X-RateLimit-Limit": {"60"},
"X-RateLimit-Remaining": {"0"}, "X-RateLimit-Remaining": {"0"},
"X-RateLimit-Reset": {"1377013266"}, "X-RateLimit-Reset": {"1377013266"},
}) })
server.AppendHandlers( server.AppendHandlers(