Skip to content

Commit 34b72ad

Browse files
authored
Refactor git proxy implementation (#92)
1 parent 8f1b7ac commit 34b72ad

File tree

4 files changed

+50
-52
lines changed

4 files changed

+50
-52
lines changed

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ require (
1212
github.com/prometheus/client_golang v1.14.0
1313
github.com/spf13/afero v1.9.5
1414
github.com/stretchr/testify v1.8.2
15-
github.com/xenitab/pkg/gin v0.0.7
15+
github.com/xenitab/pkg/gin v0.0.9
1616
github.com/xenitab/pkg/kubernetes v0.0.3
1717
go.uber.org/zap v1.24.0
1818
golang.org/x/sync v0.1.0
@@ -65,6 +65,7 @@ require (
6565
github.com/prometheus/client_model v0.3.0 // indirect
6666
github.com/prometheus/common v0.42.0 // indirect
6767
github.com/prometheus/procfs v0.9.0 // indirect
68+
github.com/rogpeppe/go-internal v1.10.0 // indirect
6869
github.com/slok/go-http-metrics v0.10.0 // indirect
6970
github.com/spf13/pflag v1.0.5 // indirect
7071
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect

go.sum

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,8 @@ github.com/klauspost/cpuid/v2 v2.2.4/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8t
220220
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
221221
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
222222
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
223-
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
224223
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
224+
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
225225
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
226226
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
227227
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
@@ -262,6 +262,8 @@ github.com/prometheus/procfs v0.9.0 h1:wzCHvIvM5SxWqYvwgVL7yJY8Lz3PKn49KQtpgMYJf
262262
github.com/prometheus/procfs v0.9.0/go.mod h1:+pB4zwohETzFnmlpe6yd2lSc+0/46IYZRB/chUwxUZY=
263263
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
264264
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
265+
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
266+
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
265267
github.com/rwtodd/Go.Sed v0.0.0-20210816025313-55464686f9ef/go.mod h1:8AEUvGVi2uQ5b24BIhcr0GCcpd/RNAFWaN2CJFrWIIQ=
266268
github.com/slok/go-http-metrics v0.10.0 h1:rh0LaYEKza5eaYRGDXujKrOln57nHBi4TtVhmNEpbgM=
267269
github.com/slok/go-http-metrics v0.10.0/go.mod h1:lFqdaS4kWMfUKCSukjC47PdCeTk+hXDUVm8kLHRqJ38=
@@ -293,8 +295,8 @@ github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZ
293295
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
294296
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
295297
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
296-
github.com/xenitab/pkg/gin v0.0.7 h1:laVd20nzWXVQFGiWMOEbFxh+Uc6IHSGZ+2i7TdxuHPA=
297-
github.com/xenitab/pkg/gin v0.0.7/go.mod h1:pnHYJpj9OM+RXRecdH7mj2VoNfNpNQQpogeaOGY10SQ=
298+
github.com/xenitab/pkg/gin v0.0.9 h1:BGdxnKoXAJBkthQTwQdaRdN7jTiNO+/C8hIexBrasfU=
299+
github.com/xenitab/pkg/gin v0.0.9/go.mod h1:8rzqJ8X5KJOo31PBOD4/Wtlt2ac8hCjN1mpOf1YAFs4=
298300
github.com/xenitab/pkg/kubernetes v0.0.3 h1:u7qQC1I13QQ9rgRgxNfzXsjvf36fLUEYoRsmdup6PSI=
299301
github.com/xenitab/pkg/kubernetes v0.0.3/go.mod h1:yde/gLI1Pvnh0HKcXQikULlF330ANu0TI4p8rrbfDBI=
300302
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=

main.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,10 @@ func run(ctx context.Context, addr, metricsAddr, cfgPath, kubeconfigPath string)
8787
return nil
8888
})
8989

90-
srv := server.NewServer(ctx, addr, authz)
90+
gp := server.NewGitProxy(authz)
91+
proxySrv := gp.Server(ctx, addr)
9192
g.Go(func() error {
92-
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
93+
if err := proxySrv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
9394
return err
9495
}
9596
return nil
@@ -98,7 +99,7 @@ func run(ctx context.Context, addr, metricsAddr, cfgPath, kubeconfigPath string)
9899
<-ctx.Done()
99100
shutdownCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
100101
defer cancel()
101-
return srv.Shutdown(shutdownCtx)
102+
return proxySrv.Shutdown(shutdownCtx)
102103
})
103104

104105
logr.FromContextOrDiscard(ctx).Info("running git-auth-proxy")

pkg/server/server.go

Lines changed: 39 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,59 @@ import (
1414
"github.com/xenitab/git-auth-proxy/pkg/auth"
1515
)
1616

17-
type Server struct {
18-
srv *http.Server
17+
type GitProxy struct {
18+
authz *auth.Authorizer
1919
}
2020

21-
func NewServer(ctx context.Context, addr string, authz *auth.Authorizer) *Server {
21+
func NewGitProxy(authz *auth.Authorizer) *GitProxy {
22+
return &GitProxy{
23+
authz: authz,
24+
}
25+
}
26+
27+
func (g *GitProxy) Server(ctx context.Context, addr string) *http.Server {
2228
cfg := pkggin.DefaultConfig()
2329
cfg.LogConfig.Logger = logr.FromContextOrDiscard(ctx)
2430
cfg.MetricsConfig.HandlerID = "proxy"
2531
router := pkggin.NewEngine(cfg)
2632
router.GET("/readyz", readinessHandler)
2733
router.GET("/healthz", livenessHandler)
28-
router.NoRoute(proxyHandler(authz))
34+
router.NoRoute(g.proxyHandler)
2935
// The ReadTimeout is set to 5 min make sure that strange requests don't live forever
3036
// But in general the external request should set a good timeout value for it's request.
3137
srv := &http.Server{ReadTimeout: 5 * time.Minute, Addr: addr, Handler: router}
32-
return &Server{
33-
srv: srv,
34-
}
38+
return srv
3539
}
3640

37-
func (s *Server) ListenAndServe() error {
38-
return s.srv.ListenAndServe()
39-
}
41+
func (g *GitProxy) proxyHandler(c *gin.Context) {
42+
// Get the token from the request
43+
token, err := getTokenFromRequest(c.Request)
44+
if err != nil {
45+
c.Header("WWW-Authenticate", "Basic realm=\"Restricted\"")
46+
c.String(http.StatusUnauthorized, "Missing basic authentication")
47+
return
48+
}
49+
// Check basic auth with local auth configuration
50+
err = g.authz.IsPermitted(c.Request.URL.EscapedPath(), token)
51+
if err != nil {
52+
//nolint: errcheck //ignore
53+
c.Error(fmt.Errorf("Received unauthorized request: %w", err))
54+
c.String(http.StatusForbidden, "User not permitted")
55+
return
56+
}
57+
// Authenticate the request with the proper token
58+
req, url, err := g.authz.UpdateRequest(c.Request.Context(), c.Request, token)
59+
if err != nil {
60+
//nolint: errcheck //ignore
61+
c.Error(fmt.Errorf("Could not authenticate request: %w", err))
62+
c.String(http.StatusInternalServerError, "Internal server error")
63+
return
64+
}
4065

41-
func (s *Server) Shutdown(ctx context.Context) error {
42-
return s.srv.Shutdown(ctx)
66+
// TODO (Philip): Add caching of the proxy
67+
// Forward the request to the correct proxy
68+
proxy := httputil.NewSingleHostReverseProxy(url)
69+
proxy.ServeHTTP(c.Writer, req)
4370
}
4471

4572
func readinessHandler(c *gin.Context) {
@@ -49,36 +76,3 @@ func readinessHandler(c *gin.Context) {
4976
func livenessHandler(c *gin.Context) {
5077
c.Status(http.StatusOK)
5178
}
52-
53-
func proxyHandler(authz *auth.Authorizer) gin.HandlerFunc {
54-
return func(c *gin.Context) {
55-
// Get the token from the request
56-
token, err := getTokenFromRequest(c.Request)
57-
if err != nil {
58-
c.Header("WWW-Authenticate", "Basic realm=\"Restricted\"")
59-
c.String(http.StatusUnauthorized, "Missing basic authentication")
60-
return
61-
}
62-
// Check basic auth with local auth configuration
63-
err = authz.IsPermitted(c.Request.URL.EscapedPath(), token)
64-
if err != nil {
65-
//nolint: errcheck //ignore
66-
c.Error(fmt.Errorf("Received unauthorized request: %w", err))
67-
c.String(http.StatusForbidden, "User not permitted")
68-
return
69-
}
70-
// Authenticate the request with the proper token
71-
req, url, err := authz.UpdateRequest(c.Request.Context(), c.Request, token)
72-
if err != nil {
73-
//nolint: errcheck //ignore
74-
c.Error(fmt.Errorf("Could not authenticate request: %w", err))
75-
c.String(http.StatusInternalServerError, "Internal server error")
76-
return
77-
}
78-
79-
// TODO (Philip): Add caching of the proxy
80-
// Forward the request to the correct proxy
81-
proxy := httputil.NewSingleHostReverseProxy(url)
82-
proxy.ServeHTTP(c.Writer, req)
83-
}
84-
}

0 commit comments

Comments
 (0)