Skip to content

Commit 12fab56

Browse files
committed
Add support for Github token auth
* Bump GH client version * Add support for GH token auth * Raise fatal error if initial config load fails, instead of silently serving with no release data. This resolves an issue where the server would come up but serve no data because the error from the initial load was silently ignored. The most frequent cause of this was rate limiting due to lack of auth support: > `Failed to load config from [https://raw.githubusercontent.com/k3s-io/k3s/master/channel.yaml]: GET https://api.github.com/repos/k3s-io/k3s/releases: 403 API rate limit exceeded for CLIENT-IP. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)` Signed-off-by: Brad Davidson <brad.davidson@rancher.com>
1 parent 2f1e0ae commit 12fab56

5 files changed

Lines changed: 35 additions & 24 deletions

File tree

go.mod

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ replace k8s.io/client-go => k8s.io/client-go v0.20.0
88

99
require (
1010
github.com/blang/semver v3.5.1+incompatible
11-
github.com/google/go-github/v29 v29.0.3
11+
github.com/google/go-github/v67 v67.0.0
1212
github.com/gorilla/handlers v1.4.2
1313
github.com/gorilla/mux v1.8.1
1414
github.com/pkg/errors v0.9.1
@@ -28,7 +28,7 @@ require (
2828
github.com/ghodss/yaml v1.0.0 // indirect
2929
github.com/go-logr/logr v1.4.2 // indirect
3030
github.com/gogo/protobuf v1.3.2 // indirect
31-
github.com/google/go-querystring v1.0.0 // indirect
31+
github.com/google/go-querystring v1.1.0 // indirect
3232
github.com/google/gofuzz v1.2.0 // indirect
3333
github.com/gorilla/websocket v1.5.1 // indirect
3434
github.com/json-iterator/go v1.1.12 // indirect
@@ -42,7 +42,6 @@ require (
4242
github.com/russross/blackfriday/v2 v2.1.0 // indirect
4343
github.com/spf13/pflag v1.0.5 // indirect
4444
github.com/x448/float16 v0.8.4 // indirect
45-
golang.org/x/crypto v0.26.0 // indirect
4645
golang.org/x/net v0.28.0 // indirect
4746
golang.org/x/sys v0.23.0 // indirect
4847
golang.org/x/text v0.17.0 // indirect

go.sum

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@ github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
2323
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
2424
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
2525
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
26-
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
2726
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
2827
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
28+
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
2929
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
3030
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
3131
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
32-
github.com/google/go-github/v29 v29.0.3 h1:IktKCTwU//aFHnpA+2SLIi7Oo9uhAzgsdZNbcAqhgdc=
33-
github.com/google/go-github/v29 v29.0.3/go.mod h1:CHKiKKPHJ0REzfwc14QMklvtHwCveD0PxlMjLlzAM5E=
34-
github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk=
35-
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
32+
github.com/google/go-github/v67 v67.0.0 h1:g11NDAmfaBaCO8qYdI9fsmbaRipHNWRIU/2YGvlh4rg=
33+
github.com/google/go-github/v67 v67.0.0/go.mod h1:zH3K7BxjFndr9QSeFibx4lTKkYS3K9nDanoI1NjaOtY=
34+
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
35+
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
3636
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
3737
github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0=
3838
github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
@@ -96,18 +96,14 @@ github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9dec
9696
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
9797
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
9898
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
99-
golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw=
100-
golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54=
10199
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
102100
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
103-
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
104101
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
105102
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
106103
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
107104
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
108105
golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE=
109106
golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg=
110-
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
111107
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
112108
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
113109
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
@@ -129,7 +125,6 @@ golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8T
129125
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
130126
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
131127
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
132-
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
133128
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
134129
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
135130
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

main.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ var (
2323
ChannelServerVersion string
2424
PathPrefix cli.StringSlice
2525
AppName string
26+
GithubToken string
2627
)
2728

2829
func main() {
@@ -70,6 +71,11 @@ func main() {
7071
Value: cli.NewStringSlice("v1-release"),
7172
Destination: &PathPrefix,
7273
},
74+
&cli.StringFlag{
75+
Name: "github-token",
76+
EnvVars: []string{"GITHUB_TOKEN"},
77+
Destination: &GithubToken,
78+
},
7379
}
7480
app.Action = run
7581

@@ -100,9 +106,9 @@ func run(c *cli.Context) error {
100106
}
101107
for index, subkey := range SubKeys.Value() {
102108
prefix := PathPrefix.Value()[index]
103-
config := config.NewConfig(ctx, subkey, &config.DurationWait{Duration: intval}, ChannelServerVersion, AppName, sources)
109+
config := config.NewConfig(ctx, subkey, &config.DurationWait{Duration: intval}, ChannelServerVersion, AppName, GithubToken, sources)
104110
configs[prefix] = config
105-
logrus.Infof("Serving channels with subkey %q at /%s", subkey, prefix)
111+
logrus.Infof("Serving channels from %v with subkey %q at /%s", sources, subkey, prefix)
106112
}
107113
return server.ListenAndServe(ctx, ListenAddress, configs)
108114
}

pkg/config/config.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"sync"
88
"time"
99

10-
"github.com/google/go-github/v29/github"
10+
"github.com/google/go-github/v67/github"
1111
"github.com/rancher/channelserver/pkg/model"
1212
"github.com/sirupsen/logrus"
1313
)
@@ -16,6 +16,7 @@ type Config struct {
1616
sync.Mutex
1717

1818
url string
19+
ghToken string
1920
redirect *url.URL
2021
gh *github.Client
2122
channelsConfig *model.ChannelsConfig
@@ -50,22 +51,28 @@ func (s StringSource) URL() string {
5051
return string(s)
5152
}
5253

53-
func NewConfig(ctx context.Context, subKey string, wait Wait, channelServerVersion string, appName string, urls []Source) *Config {
54+
func NewConfig(ctx context.Context, subKey string, wait Wait, channelServerVersion string, appName string, ghToken string, urls []Source) *Config {
5455
c := &Config{
56+
ghToken: ghToken,
5557
channelsConfig: &model.ChannelsConfig{},
5658
releasesConfig: &model.ReleasesConfig{},
5759
appDefaultsConfig: &model.AppDefaultsConfig{},
5860
}
5961

60-
_, _ = c.loadConfig(ctx, subKey, channelServerVersion, appName, urls...)
62+
logrus.Infof("Loading configuration from %v", urls)
63+
if index, err := c.loadConfig(ctx, subKey, channelServerVersion, appName, urls...); err != nil {
64+
logrus.Fatalf("Failed to load initial config from %s: %v", urls[index].URL(), err)
65+
} else {
66+
logrus.Infof("Loaded initial configuration from %s in %v", urls[index].URL(), urls)
67+
}
6168

6269
go func() {
6370
for wait.Wait(ctx) {
6471
if index, err := c.loadConfig(ctx, subKey, channelServerVersion, appName, urls...); err != nil {
65-
logrus.Errorf("failed to reload configuration from %s: %v", urls[index].URL(), err)
72+
logrus.Errorf("Failed to reload configuration from %s: %v", urls[index].URL(), err)
6673
} else {
6774
urls = urls[:index+1]
68-
logrus.Infof("Loaded configuration from %s in %v", urls[index].URL(), urls)
75+
logrus.Infof("Reloaded configuration from %s in %v", urls[index].URL(), urls)
6976
}
7077
}
7178
}()
@@ -103,10 +110,14 @@ func (c *Config) ghClient(config *model.ChannelsConfig) (*github.Client, error)
103110
}
104111

105112
if c.gh == nil || c.url != config.GitHub.APIURL {
106-
if config.GitHub.APIURL == "" {
107-
return github.NewClient(nil), nil
113+
client := github.NewClient(nil)
114+
if c.ghToken != "" {
115+
client = client.WithAuthToken(c.ghToken)
116+
}
117+
if config.GitHub.APIURL != "" {
118+
return client.WithEnterpriseURLs(config.GitHub.APIURL, config.GitHub.APIURL)
108119
}
109-
return github.NewEnterpriseClient(config.GitHub.APIURL, config.GitHub.APIURL, nil)
120+
return client, nil
110121
}
111122
return c.gh, nil
112123
}

pkg/config/get.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"time"
1010

1111
"github.com/blang/semver"
12-
"github.com/google/go-github/v29/github"
12+
"github.com/google/go-github/v67/github"
1313
"github.com/rancher/channelserver/pkg/model"
1414
"github.com/rancher/wrangler/v3/pkg/data/convert"
1515
"sigs.k8s.io/yaml"

0 commit comments

Comments
 (0)