Skip to content

Commit 74d7bc6

Browse files
committed
GitHub Gateway Working
1 parent 9905388 commit 74d7bc6

File tree

6 files changed

+75
-41
lines changed

6 files changed

+75
-41
lines changed

api/api.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77

88
jwt "github.com/dgrijalva/jwt-go"
99
"github.com/go-chi/chi"
10-
"github.com/netlify/git-gateway/api/gateways"
1110
"github.com/netlify/git-gateway/conf"
1211
"github.com/netlify/git-gateway/storage"
1312
"github.com/netlify/git-gateway/storage/dial"
@@ -72,11 +71,12 @@ func NewAPIWithVersion(ctx context.Context, globalConfig *conf.GlobalConfigurati
7271
r.Get("/health", api.HealthCheck)
7372

7473
r.Route("/", func(r *router) {
74+
7575
if globalConfig.MultiInstanceMode {
7676
r.Use(api.loadJWSSignatureHeader)
7777
r.Use(api.loadInstanceConfig)
7878
}
79-
r.Mount("/github", &gateways.GitHubGateway{})
79+
r.With(api.requireAuthentication).Mount("/github", NewGitHubGateway())
8080
})
8181

8282
if globalConfig.MultiInstanceMode {
@@ -122,6 +122,7 @@ func NewAPIFromConfigFile(filename string, version string) (*API, error) {
122122
return nil, err
123123
}
124124

125+
logrus.Infof("Config is: %v", config)
125126
ctx, err := WithInstanceConfig(context.Background(), config, "")
126127
if err != nil {
127128
logrus.Fatalf("Error loading instance config: %+v", err)
@@ -144,13 +145,3 @@ func WithInstanceConfig(ctx context.Context, config *conf.Configuration, instanc
144145

145146
return ctx, nil
146147
}
147-
148-
func (a *API) getConfig(ctx context.Context) *conf.Configuration {
149-
obj := ctx.Value(configKey)
150-
if obj == nil {
151-
return nil
152-
}
153-
154-
config := obj.(*conf.Configuration)
155-
return config
156-
}

api/auth.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func (a *API) extractBearerToken(w http.ResponseWriter, r *http.Request) (string
3333

3434
func (a *API) parseJWTClaims(bearer string, r *http.Request) (context.Context, error) {
3535
ctx := r.Context()
36-
config := a.getConfig(ctx)
36+
config := getConfig(ctx)
3737

3838
p := jwt.Parser{ValidMethods: []string{jwt.SigningMethodHS256.Name}}
3939
token, err := p.ParseWithClaims(bearer, &GatewayClaims{}, func(token *jwt.Token) (interface{}, error) {

api/context.go

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,33 @@ package api
22

33
import (
44
"context"
5+
"net/url"
56

67
jwt "github.com/dgrijalva/jwt-go"
78
"github.com/netlify/git-gateway/conf"
89
"github.com/netlify/git-gateway/models"
910
)
1011

12+
type Role struct {
13+
Name string
14+
}
15+
1116
type contextKey string
1217

1318
func (c contextKey) String() string {
1419
return "git-gateway api context key " + string(c)
1520
}
1621

1722
const (
18-
tokenKey = contextKey("jwt")
19-
requestIDKey = contextKey("request_id")
20-
configKey = contextKey("config")
21-
instanceIDKey = contextKey("instance_id")
22-
instanceKey = contextKey("instance")
23-
signatureKey = contextKey("signature")
24-
netlifyIDKey = contextKey("netlify_id")
23+
accessTokenKey = contextKey("access_token")
24+
tokenKey = contextKey("jwt")
25+
requestIDKey = contextKey("request_id")
26+
configKey = contextKey("config")
27+
instanceIDKey = contextKey("instance_id")
28+
instanceKey = contextKey("instance")
29+
proxyTargetKey = contextKey("target")
30+
signatureKey = contextKey("signature")
31+
netlifyIDKey = contextKey("netlify_id")
2532
)
2633

2734
// withToken adds the JWT token to the context.
@@ -47,6 +54,11 @@ func getClaims(ctx context.Context) *GatewayClaims {
4754
return token.Claims.(*GatewayClaims)
4855
}
4956

57+
// TODO: actually get this from the config rather than hardcoding
58+
func getRoles(ctx context.Context) []Role {
59+
return []Role{Role{Name: "admin"}, Role{Name: "cms"}}
60+
}
61+
5062
func withRequestID(ctx context.Context, id string) context.Context {
5163
return context.WithValue(ctx, requestIDKey, id)
5264
}
@@ -60,6 +72,16 @@ func getRequestID(ctx context.Context) string {
6072
return obj.(string)
6173
}
6274

75+
func getConfig(ctx context.Context) *conf.Configuration {
76+
obj := ctx.Value(configKey)
77+
if obj == nil {
78+
return nil
79+
}
80+
81+
config := obj.(*conf.Configuration)
82+
return config
83+
}
84+
6385
func withConfig(ctx context.Context, config *conf.Configuration) context.Context {
6486
return context.WithValue(ctx, configKey, config)
6587
}
@@ -68,6 +90,30 @@ func withInstanceID(ctx context.Context, id string) context.Context {
6890
return context.WithValue(ctx, instanceIDKey, id)
6991
}
7092

93+
func withProxyTarget(ctx context.Context, target *url.URL) context.Context {
94+
return context.WithValue(ctx, proxyTargetKey, target)
95+
}
96+
97+
func getProxyTarget(ctx context.Context) *url.URL {
98+
obj := ctx.Value(proxyTargetKey)
99+
if obj == nil {
100+
return nil
101+
}
102+
return obj.(*url.URL)
103+
}
104+
105+
func withAccessToken(ctx context.Context, token string) context.Context {
106+
return context.WithValue(ctx, accessTokenKey, token)
107+
}
108+
109+
func getAccessToken(ctx context.Context) string {
110+
obj := ctx.Value(accessTokenKey)
111+
if obj == nil {
112+
return ""
113+
}
114+
return obj.(string)
115+
}
116+
71117
func getInstanceID(ctx context.Context) string {
72118
obj := ctx.Value(instanceIDKey)
73119
if obj == nil {

api/gateways/github.go

Lines changed: 0 additions & 11 deletions
This file was deleted.

api/helpers.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"fmt"
77
"net/http"
8+
"strings"
89

910
"github.com/pborman/uuid"
1011
"github.com/pkg/errors"
@@ -27,3 +28,16 @@ func sendJSON(w http.ResponseWriter, status int, obj interface{}) error {
2728
_, err = w.Write(b)
2829
return err
2930
}
31+
32+
// From https://golang.org/src/net/http/httputil/reverseproxy.go?s=2298:2359#L72
33+
func singleJoiningSlash(a, b string) string {
34+
aslash := strings.HasSuffix(a, "/")
35+
bslash := strings.HasPrefix(b, "/")
36+
switch {
37+
case aslash && bslash:
38+
return a + b[1:]
39+
case !aslash && !bslash:
40+
return a + "/" + b
41+
}
42+
return a + b
43+
}

conf/configuration.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,10 @@ import (
88
"github.com/netlify/netlify-commons/nconf"
99
)
1010

11-
type Role struct {
12-
Name string `json:"name"`
13-
// TODO: fine grained permissions
14-
}
15-
1611
type GitHubConfig struct {
17-
AccessToken string `json:"access_token"`
18-
Endpoint string `json:"endpoint"`
19-
Repo string `json:"repo"` // Should be "owner/repo" format
20-
Roles []Role `json:"roles"`
12+
AccessToken string `envconfig:"ACCESS_TOKEN" json:"access_token"`
13+
Endpoint string `envconfig:"ENDPOINT" json:"endpoint"`
14+
Repo string `envconfig:"REPO" json:"repo"` // Should be "owner/repo" format
2115
}
2216

2317
// DBConfiguration holds all the database related configuration.
@@ -50,7 +44,7 @@ type GlobalConfiguration struct {
5044
// Configuration holds all the per-instance configuration.
5145
type Configuration struct {
5246
JWT JWTConfiguration `json:"jwt"`
53-
GitHub GitHubConfig `json:"github,omitempty"`
47+
GitHub GitHubConfig `envconfig:"GITHUB" json:"github"`
5448
}
5549

5650
func loadEnvironment(filename string) error {

0 commit comments

Comments
 (0)