mirror of
https://github.com/steveiliop56/tinyauth.git
synced 2026-05-11 06:48:11 +00:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 134befe72f | |||
| fdcb253072 | |||
| 13bb3ca682 | |||
| 81cb695d0b |
+2
-2
@@ -37,7 +37,7 @@
|
|||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@eslint/js": "^10.0.1",
|
"@eslint/js": "^10.0.1",
|
||||||
"@tanstack/eslint-plugin-query": "^5.91.4",
|
"@tanstack/eslint-plugin-query": "^5.91.4",
|
||||||
"@types/node": "^25.5.0",
|
"@types/node": "^25.4.0",
|
||||||
"@types/react": "^19.2.14",
|
"@types/react": "^19.2.14",
|
||||||
"@types/react-dom": "^19.2.3",
|
"@types/react-dom": "^19.2.3",
|
||||||
"@vitejs/plugin-react": "^5.1.4",
|
"@vitejs/plugin-react": "^5.1.4",
|
||||||
@@ -417,7 +417,7 @@
|
|||||||
|
|
||||||
"@types/ms": ["@types/ms@2.1.0", "", {}, "sha512-GsCCIZDE/p3i96vtEqx+7dBUGXrc7zeSK3wwPHIaRThS+9OhWIXRqzs4d6k1SVU8g91DrNRWxWUGhp5KXQb2VA=="],
|
"@types/ms": ["@types/ms@2.1.0", "", {}, "sha512-GsCCIZDE/p3i96vtEqx+7dBUGXrc7zeSK3wwPHIaRThS+9OhWIXRqzs4d6k1SVU8g91DrNRWxWUGhp5KXQb2VA=="],
|
||||||
|
|
||||||
"@types/node": ["@types/node@25.5.0", "", { "dependencies": { "undici-types": "~7.18.0" } }, "sha512-jp2P3tQMSxWugkCUKLRPVUpGaL5MVFwF8RDuSRztfwgN1wmqJeMSbKlnEtQqU8UrhTmzEmZdu2I6v2dpp7XIxw=="],
|
"@types/node": ["@types/node@25.4.0", "", { "dependencies": { "undici-types": "~7.18.0" } }, "sha512-9wLpoeWuBlcbBpOY3XmzSTG3oscB6xjBEEtn+pYXTfhyXhIxC5FsBer2KTopBlvKEiW9l13po9fq+SJY/5lkhw=="],
|
||||||
|
|
||||||
"@types/react": ["@types/react@19.2.14", "", { "dependencies": { "csstype": "^3.2.2" } }, "sha512-ilcTH/UniCkMdtexkoCN0bI7pMcJDvmQFPvuPvmEaYA/NSfFTAgdUSLAoVjaRJm7+6PvcM+q1zYOwS4wTYMF9w=="],
|
"@types/react": ["@types/react@19.2.14", "", { "dependencies": { "csstype": "^3.2.2" } }, "sha512-ilcTH/UniCkMdtexkoCN0bI7pMcJDvmQFPvuPvmEaYA/NSfFTAgdUSLAoVjaRJm7+6PvcM+q1zYOwS4wTYMF9w=="],
|
||||||
|
|
||||||
|
|||||||
@@ -43,7 +43,7 @@
|
|||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@eslint/js": "^10.0.1",
|
"@eslint/js": "^10.0.1",
|
||||||
"@tanstack/eslint-plugin-query": "^5.91.4",
|
"@tanstack/eslint-plugin-query": "^5.91.4",
|
||||||
"@types/node": "^25.5.0",
|
"@types/node": "^25.4.0",
|
||||||
"@types/react": "^19.2.14",
|
"@types/react": "^19.2.14",
|
||||||
"@types/react-dom": "^19.2.3",
|
"@types/react-dom": "^19.2.3",
|
||||||
"@vitejs/plugin-react": "^5.1.4",
|
"@vitejs/plugin-react": "^5.1.4",
|
||||||
|
|||||||
@@ -27,6 +27,8 @@ const (
|
|||||||
|
|
||||||
var BrowserUserAgentRegex = regexp.MustCompile("Chrome|Gecko|AppleWebKit|Opera|Edge")
|
var BrowserUserAgentRegex = regexp.MustCompile("Chrome|Gecko|AppleWebKit|Opera|Edge")
|
||||||
|
|
||||||
|
var SupportedProxies = []string{"nginx", "traefik", "caddy", "envoy"}
|
||||||
|
|
||||||
type Proxy struct {
|
type Proxy struct {
|
||||||
Proxy string `uri:"proxy" binding:"required"`
|
Proxy string `uri:"proxy" binding:"required"`
|
||||||
}
|
}
|
||||||
@@ -364,17 +366,7 @@ func (controller *ProxyController) getAuthRequestContext(c *gin.Context) (ProxyC
|
|||||||
}
|
}
|
||||||
|
|
||||||
host := url.Host
|
host := url.Host
|
||||||
|
|
||||||
if strings.TrimSpace(host) == "" {
|
|
||||||
return ProxyContext{}, errors.New("host not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
proto := url.Scheme
|
proto := url.Scheme
|
||||||
|
|
||||||
if strings.TrimSpace(proto) == "" {
|
|
||||||
return ProxyContext{}, errors.New("proto not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
path := url.Path
|
path := url.Path
|
||||||
method := c.Request.Method
|
method := c.Request.Method
|
||||||
|
|
||||||
@@ -396,11 +388,7 @@ func (controller *ProxyController) getExtAuthzContext(c *gin.Context) (ProxyCont
|
|||||||
}
|
}
|
||||||
|
|
||||||
// It sets the host to the original host, not the forwarded host
|
// It sets the host to the original host, not the forwarded host
|
||||||
host := c.Request.Host
|
host := c.Request.URL.Host
|
||||||
|
|
||||||
if strings.TrimSpace(host) == "" {
|
|
||||||
return ProxyContext{}, errors.New("host not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
// We get the path from the query string
|
// We get the path from the query string
|
||||||
path := c.Query("path")
|
path := c.Query("path")
|
||||||
@@ -419,14 +407,14 @@ func (controller *ProxyController) getExtAuthzContext(c *gin.Context) (ProxyCont
|
|||||||
|
|
||||||
func (controller *ProxyController) determineAuthModules(proxy string) []AuthModuleType {
|
func (controller *ProxyController) determineAuthModules(proxy string) []AuthModuleType {
|
||||||
switch proxy {
|
switch proxy {
|
||||||
case "traefik", "caddy":
|
case "traefik":
|
||||||
return []AuthModuleType{ForwardAuth}
|
return []AuthModuleType{ForwardAuth}
|
||||||
case "envoy":
|
case "envoy":
|
||||||
return []AuthModuleType{ExtAuthz, ForwardAuth}
|
return []AuthModuleType{ExtAuthz, ForwardAuth}
|
||||||
case "nginx":
|
case "nginx":
|
||||||
return []AuthModuleType{AuthRequest, ForwardAuth}
|
return []AuthModuleType{AuthRequest, ForwardAuth}
|
||||||
default:
|
default:
|
||||||
return []AuthModuleType{}
|
return []AuthModuleType{ForwardAuth}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -464,11 +452,9 @@ func (controller *ProxyController) getProxyContext(c *gin.Context) (ProxyContext
|
|||||||
|
|
||||||
tlog.App.Debug().Msgf("Proxy: %v", req.Proxy)
|
tlog.App.Debug().Msgf("Proxy: %v", req.Proxy)
|
||||||
|
|
||||||
authModules := controller.determineAuthModules(req.Proxy)
|
tlog.App.Trace().Interface("headers", c.Request.Header).Msg("Request headers")
|
||||||
|
|
||||||
if len(authModules) == 0 {
|
authModules := controller.determineAuthModules(req.Proxy)
|
||||||
return ProxyContext{}, fmt.Errorf("no auth modules supported for proxy: %v", req.Proxy)
|
|
||||||
}
|
|
||||||
|
|
||||||
var ctx ProxyContext
|
var ctx ProxyContext
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package controller_test
|
package controller_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@@ -10,26 +9,21 @@ import (
|
|||||||
"github.com/steveiliop56/tinyauth/internal/controller"
|
"github.com/steveiliop56/tinyauth/internal/controller"
|
||||||
"github.com/steveiliop56/tinyauth/internal/repository"
|
"github.com/steveiliop56/tinyauth/internal/repository"
|
||||||
"github.com/steveiliop56/tinyauth/internal/service"
|
"github.com/steveiliop56/tinyauth/internal/service"
|
||||||
|
"github.com/steveiliop56/tinyauth/internal/utils/tlog"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"gotest.tools/v3/assert"
|
"gotest.tools/v3/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
var loggedInCtx = config.UserContext{
|
func setupProxyController(t *testing.T, middlewares *[]gin.HandlerFunc) (*gin.Engine, *httptest.ResponseRecorder, *service.AuthService) {
|
||||||
Username: "test",
|
tlog.NewSimpleLogger().Init()
|
||||||
Name: "Test",
|
|
||||||
Email: "test@example.com",
|
|
||||||
IsLoggedIn: true,
|
|
||||||
Provider: "local",
|
|
||||||
}
|
|
||||||
|
|
||||||
func setupProxyController(t *testing.T, middlewares []gin.HandlerFunc) (*gin.Engine, *httptest.ResponseRecorder) {
|
|
||||||
// Setup
|
// Setup
|
||||||
gin.SetMode(gin.TestMode)
|
gin.SetMode(gin.TestMode)
|
||||||
router := gin.Default()
|
router := gin.Default()
|
||||||
|
|
||||||
if len(middlewares) > 0 {
|
if middlewares != nil {
|
||||||
for _, m := range middlewares {
|
for _, m := range *middlewares {
|
||||||
router.Use(m)
|
router.Use(m)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -54,13 +48,7 @@ func setupProxyController(t *testing.T, middlewares []gin.HandlerFunc) (*gin.Eng
|
|||||||
assert.NilError(t, dockerService.Init())
|
assert.NilError(t, dockerService.Init())
|
||||||
|
|
||||||
// Access controls
|
// Access controls
|
||||||
accessControlsService := service.NewAccessControlsService(dockerService, map[string]config.App{
|
accessControlsService := service.NewAccessControlsService(dockerService, map[string]config.App{})
|
||||||
"whoami": {
|
|
||||||
Path: config.AppPath{
|
|
||||||
Allow: "/allow",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
assert.NilError(t, accessControlsService.Init())
|
assert.NilError(t, accessControlsService.Init())
|
||||||
|
|
||||||
@@ -89,214 +77,107 @@ func setupProxyController(t *testing.T, middlewares []gin.HandlerFunc) (*gin.Eng
|
|||||||
|
|
||||||
// Controller
|
// Controller
|
||||||
ctrl := controller.NewProxyController(controller.ProxyControllerConfig{
|
ctrl := controller.NewProxyController(controller.ProxyControllerConfig{
|
||||||
AppURL: "http://tinyauth.example.com",
|
AppURL: "http://localhost:8080",
|
||||||
}, group, accessControlsService, authService)
|
}, group, accessControlsService, authService)
|
||||||
ctrl.SetupRoutes()
|
ctrl.SetupRoutes()
|
||||||
|
|
||||||
return router, recorder
|
return router, recorder, authService
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Needs tests for context middleware
|
// TODO: Needs tests for context middleware
|
||||||
|
|
||||||
func TestProxyHandler(t *testing.T) {
|
func TestProxyHandler(t *testing.T) {
|
||||||
// Test logged out user traefik/caddy (forward_auth)
|
// Setup
|
||||||
router, recorder := setupProxyController(t, nil)
|
router, recorder, _ := setupProxyController(t, nil)
|
||||||
|
|
||||||
req, err := http.NewRequest("GET", "/api/auth/traefik", nil)
|
|
||||||
assert.NilError(t, err)
|
|
||||||
|
|
||||||
req.Header.Set("x-forwarded-host", "whoami.example.com")
|
|
||||||
req.Header.Set("x-forwarded-proto", "http")
|
|
||||||
req.Header.Set("x-forwarded-uri", "/")
|
|
||||||
|
|
||||||
|
// Test invalid proxy
|
||||||
|
req := httptest.NewRequest("GET", "/api/auth/invalidproxy", nil)
|
||||||
router.ServeHTTP(recorder, req)
|
router.ServeHTTP(recorder, req)
|
||||||
assert.Equal(t, recorder.Code, http.StatusUnauthorized)
|
|
||||||
|
|
||||||
// Test logged out user nginx (auth_request)
|
assert.Equal(t, 400, recorder.Code)
|
||||||
router, recorder = setupProxyController(t, nil)
|
|
||||||
|
|
||||||
req, err = http.NewRequest("GET", "/api/auth/nginx", nil)
|
|
||||||
assert.NilError(t, err)
|
|
||||||
|
|
||||||
req.Header.Set("x-original-url", "http://whoami.example.com/")
|
|
||||||
|
|
||||||
|
// Test invalid method for non-envoy proxy
|
||||||
|
recorder = httptest.NewRecorder()
|
||||||
|
req = httptest.NewRequest("POST", "/api/auth/traefik", nil)
|
||||||
router.ServeHTTP(recorder, req)
|
router.ServeHTTP(recorder, req)
|
||||||
assert.Equal(t, recorder.Code, http.StatusUnauthorized)
|
|
||||||
|
|
||||||
// Test logged out user envoy (ext_authz)
|
assert.Equal(t, 405, recorder.Code)
|
||||||
router, recorder = setupProxyController(t, nil)
|
assert.Equal(t, "GET", recorder.Header().Get("Allow"))
|
||||||
|
|
||||||
req, err = http.NewRequest("GET", "/api/auth/envoy?path=/", nil)
|
|
||||||
assert.NilError(t, err)
|
|
||||||
|
|
||||||
req.Host = "whoami.example.com"
|
|
||||||
req.Header.Set("x-forwarded-proto", "http")
|
|
||||||
|
|
||||||
|
// Test logged out user (traefik/caddy)
|
||||||
|
recorder = httptest.NewRecorder()
|
||||||
|
req = httptest.NewRequest("GET", "/api/auth/traefik", nil)
|
||||||
|
req.Header.Set("X-Forwarded-Proto", "https")
|
||||||
|
req.Header.Set("X-Forwarded-Host", "example.com")
|
||||||
|
req.Header.Set("X-Forwarded-Uri", "/somepath")
|
||||||
|
req.Header.Set("Accept", "text/html")
|
||||||
router.ServeHTTP(recorder, req)
|
router.ServeHTTP(recorder, req)
|
||||||
assert.Equal(t, recorder.Code, http.StatusUnauthorized)
|
|
||||||
|
|
||||||
// Test logged in user traefik/caddy (forward_auth)
|
assert.Equal(t, 307, recorder.Code)
|
||||||
router, recorder = setupProxyController(t, []gin.HandlerFunc{
|
assert.Equal(t, "http://localhost:8080/login?redirect_uri=https%3A%2F%2Fexample.com%2Fsomepath", recorder.Header().Get("Location"))
|
||||||
|
|
||||||
|
// Test logged out user (envoy - POST method)
|
||||||
|
recorder = httptest.NewRecorder()
|
||||||
|
req = httptest.NewRequest("POST", "/api/auth/envoy", nil)
|
||||||
|
req.Header.Set("X-Forwarded-Proto", "https")
|
||||||
|
req.Header.Set("X-Forwarded-Host", "example.com")
|
||||||
|
req.Header.Set("X-Forwarded-Uri", "/somepath")
|
||||||
|
req.Header.Set("Accept", "text/html")
|
||||||
|
router.ServeHTTP(recorder, req)
|
||||||
|
|
||||||
|
assert.Equal(t, 307, recorder.Code)
|
||||||
|
assert.Equal(t, "http://localhost:8080/login?redirect_uri=https%3A%2F%2Fexample.com%2Fsomepath", recorder.Header().Get("Location"))
|
||||||
|
|
||||||
|
// Test logged out user (envoy - DELETE method)
|
||||||
|
recorder = httptest.NewRecorder()
|
||||||
|
req = httptest.NewRequest("DELETE", "/api/auth/envoy", nil)
|
||||||
|
req.Header.Set("X-Forwarded-Proto", "https")
|
||||||
|
req.Header.Set("X-Forwarded-Host", "example.com")
|
||||||
|
req.Header.Set("X-Forwarded-Uri", "/somepath")
|
||||||
|
req.Header.Set("Accept", "text/html")
|
||||||
|
router.ServeHTTP(recorder, req)
|
||||||
|
|
||||||
|
assert.Equal(t, 307, recorder.Code)
|
||||||
|
assert.Equal(t, "http://localhost:8080/login?redirect_uri=https%3A%2F%2Fexample.com%2Fsomepath", recorder.Header().Get("Location"))
|
||||||
|
|
||||||
|
// Test logged out user (nginx)
|
||||||
|
recorder = httptest.NewRecorder()
|
||||||
|
req = httptest.NewRequest("GET", "/api/auth/nginx", nil)
|
||||||
|
req.Header.Set("X-Forwarded-Proto", "https")
|
||||||
|
req.Header.Set("X-Forwarded-Host", "example.com")
|
||||||
|
// we won't set X-Forwarded-Uri to test that the controller can work without it
|
||||||
|
router.ServeHTTP(recorder, req)
|
||||||
|
|
||||||
|
assert.Equal(t, 401, recorder.Code)
|
||||||
|
|
||||||
|
// Test logged in user
|
||||||
|
router, recorder, _ = setupProxyController(t, &[]gin.HandlerFunc{
|
||||||
func(c *gin.Context) {
|
func(c *gin.Context) {
|
||||||
c.Set("context", &loggedInCtx)
|
c.Set("context", &config.UserContext{
|
||||||
|
Username: "testuser",
|
||||||
|
Name: "testuser",
|
||||||
|
Email: "testuser@example.com",
|
||||||
|
IsLoggedIn: true,
|
||||||
|
OAuth: false,
|
||||||
|
Provider: "local",
|
||||||
|
TotpPending: false,
|
||||||
|
OAuthGroups: "",
|
||||||
|
TotpEnabled: false,
|
||||||
|
})
|
||||||
c.Next()
|
c.Next()
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
req, err = http.NewRequest("GET", "/api/auth/traefik", nil)
|
req = httptest.NewRequest("GET", "/api/auth/traefik", nil)
|
||||||
assert.NilError(t, err)
|
req.Header.Set("X-Forwarded-Proto", "https")
|
||||||
|
req.Header.Set("X-Forwarded-Host", "example.com")
|
||||||
req.Header.Set("x-forwarded-host", "whoami.example.com")
|
req.Header.Set("X-Original-Uri", "/somepath") // Test with original URI for kubernetes ingress
|
||||||
req.Header.Set("x-forwarded-proto", "http")
|
req.Header.Set("Accept", "text/html")
|
||||||
req.Header.Set("x-forwarded-uri", "/")
|
|
||||||
|
|
||||||
router.ServeHTTP(recorder, req)
|
router.ServeHTTP(recorder, req)
|
||||||
assert.Equal(t, recorder.Code, http.StatusOK)
|
assert.Equal(t, 200, recorder.Code)
|
||||||
|
|
||||||
// Test logged in user nginx (auth_request)
|
assert.Equal(t, "testuser", recorder.Header().Get("Remote-User"))
|
||||||
router, recorder = setupProxyController(t, []gin.HandlerFunc{
|
assert.Equal(t, "testuser", recorder.Header().Get("Remote-Name"))
|
||||||
func(c *gin.Context) {
|
assert.Equal(t, "testuser@example.com", recorder.Header().Get("Remote-Email"))
|
||||||
c.Set("context", &loggedInCtx)
|
|
||||||
c.Next()
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
req, err = http.NewRequest("GET", "/api/auth/nginx", nil)
|
|
||||||
assert.NilError(t, err)
|
|
||||||
|
|
||||||
req.Header.Set("x-original-url", "http://whoami.example.com/")
|
|
||||||
|
|
||||||
router.ServeHTTP(recorder, req)
|
|
||||||
assert.Equal(t, recorder.Code, http.StatusOK)
|
|
||||||
|
|
||||||
// Test logged in user envoy (ext_authz)
|
|
||||||
router, recorder = setupProxyController(t, []gin.HandlerFunc{
|
|
||||||
func(c *gin.Context) {
|
|
||||||
c.Set("context", &loggedInCtx)
|
|
||||||
c.Next()
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
req, err = http.NewRequest("GET", "/api/auth/envoy?path=/", nil)
|
|
||||||
assert.NilError(t, err)
|
|
||||||
|
|
||||||
req.Host = "whoami.example.com"
|
|
||||||
req.Header.Set("x-forwarded-proto", "http")
|
|
||||||
|
|
||||||
router.ServeHTTP(recorder, req)
|
|
||||||
assert.Equal(t, recorder.Code, http.StatusOK)
|
|
||||||
|
|
||||||
// Test ACL allow caddy/traefik (forward_auth)
|
|
||||||
router, recorder = setupProxyController(t, nil)
|
|
||||||
|
|
||||||
req, err = http.NewRequest("GET", "/api/auth/traefik", nil)
|
|
||||||
assert.NilError(t, err)
|
|
||||||
|
|
||||||
req.Header.Set("x-forwarded-host", "whoami.example.com")
|
|
||||||
req.Header.Set("x-forwarded-proto", "http")
|
|
||||||
req.Header.Set("x-forwarded-uri", "/allow")
|
|
||||||
|
|
||||||
router.ServeHTTP(recorder, req)
|
|
||||||
assert.Equal(t, recorder.Code, http.StatusOK)
|
|
||||||
|
|
||||||
// Test ACL allow nginx
|
|
||||||
router, recorder = setupProxyController(t, nil)
|
|
||||||
|
|
||||||
req, err = http.NewRequest("GET", "/api/auth/nginx", nil)
|
|
||||||
assert.NilError(t, err)
|
|
||||||
|
|
||||||
req.Header.Set("x-original-url", "http://whoami.example.com/allow")
|
|
||||||
|
|
||||||
router.ServeHTTP(recorder, req)
|
|
||||||
assert.Equal(t, recorder.Code, http.StatusOK)
|
|
||||||
|
|
||||||
// Test ACL allow envoy
|
|
||||||
router, recorder = setupProxyController(t, nil)
|
|
||||||
|
|
||||||
req, err = http.NewRequest("GET", "/api/auth/envoy?path=/allow", nil)
|
|
||||||
assert.NilError(t, err)
|
|
||||||
|
|
||||||
req.Host = "whoami.example.com"
|
|
||||||
req.Header.Set("x-forwarded-proto", "http")
|
|
||||||
|
|
||||||
router.ServeHTTP(recorder, req)
|
|
||||||
assert.Equal(t, recorder.Code, http.StatusOK)
|
|
||||||
|
|
||||||
// Test traefik/caddy (forward_auth) without required headers
|
|
||||||
router, recorder = setupProxyController(t, nil)
|
|
||||||
|
|
||||||
req, err = http.NewRequest("GET", "/api/auth/traefik", nil)
|
|
||||||
assert.NilError(t, err)
|
|
||||||
|
|
||||||
router.ServeHTTP(recorder, req)
|
|
||||||
assert.Equal(t, recorder.Code, http.StatusBadRequest)
|
|
||||||
|
|
||||||
// Test nginx (forward_auth) without required headers
|
|
||||||
router, recorder = setupProxyController(t, nil)
|
|
||||||
|
|
||||||
req, err = http.NewRequest("GET", "/api/auth/nginx", nil)
|
|
||||||
assert.NilError(t, err)
|
|
||||||
|
|
||||||
router.ServeHTTP(recorder, req)
|
|
||||||
assert.Equal(t, recorder.Code, http.StatusBadRequest)
|
|
||||||
|
|
||||||
// Test envoy (forward_auth) without required headers
|
|
||||||
router, recorder = setupProxyController(t, nil)
|
|
||||||
|
|
||||||
req, err = http.NewRequest("GET", "/api/auth/envoy", nil)
|
|
||||||
assert.NilError(t, err)
|
|
||||||
|
|
||||||
router.ServeHTTP(recorder, req)
|
|
||||||
assert.Equal(t, recorder.Code, http.StatusBadRequest)
|
|
||||||
|
|
||||||
// Test nginx (auth_request) with forward_auth fallback with ACLs
|
|
||||||
router, recorder = setupProxyController(t, nil)
|
|
||||||
|
|
||||||
req, err = http.NewRequest("GET", "/api/auth/nginx", nil)
|
|
||||||
assert.NilError(t, err)
|
|
||||||
|
|
||||||
req.Header.Set("x-forwarded-host", "whoami.example.com")
|
|
||||||
req.Header.Set("x-forwarded-proto", "http")
|
|
||||||
req.Header.Set("x-forwarded-uri", "/allow")
|
|
||||||
|
|
||||||
router.ServeHTTP(recorder, req)
|
|
||||||
assert.Equal(t, recorder.Code, http.StatusOK)
|
|
||||||
|
|
||||||
// Test envoy (ext_authz) with forward_auth fallback with ACLs
|
|
||||||
router, recorder = setupProxyController(t, nil)
|
|
||||||
|
|
||||||
req, err = http.NewRequest("GET", "/api/auth/envoy", nil)
|
|
||||||
assert.NilError(t, err)
|
|
||||||
|
|
||||||
req.Header.Set("x-forwarded-host", "whoami.example.com")
|
|
||||||
req.Header.Set("x-forwarded-proto", "http")
|
|
||||||
req.Header.Set("x-forwarded-uri", "/allow")
|
|
||||||
|
|
||||||
router.ServeHTTP(recorder, req)
|
|
||||||
assert.Equal(t, recorder.Code, http.StatusOK)
|
|
||||||
|
|
||||||
// Test envoy (ext_authz) with empty path
|
|
||||||
router, recorder = setupProxyController(t, nil)
|
|
||||||
|
|
||||||
req, err = http.NewRequest("GET", "/api/auth/envoy", nil)
|
|
||||||
assert.NilError(t, err)
|
|
||||||
|
|
||||||
req.Host = "whoami.example.com"
|
|
||||||
req.Header.Set("x-forwarded-proto", "http")
|
|
||||||
|
|
||||||
router.ServeHTTP(recorder, req)
|
|
||||||
assert.Equal(t, recorder.Code, http.StatusUnauthorized)
|
|
||||||
|
|
||||||
// Ensure forward_auth fallback works with path (should ignore)
|
|
||||||
router, recorder = setupProxyController(t, nil)
|
|
||||||
|
|
||||||
req, err = http.NewRequest("GET", "/api/auth/traefik?path=/allow", nil)
|
|
||||||
assert.NilError(t, err)
|
|
||||||
|
|
||||||
req.Header.Set("x-forwarded-proto", "http")
|
|
||||||
req.Header.Set("x-forwarded-host", "whoami.example.com")
|
|
||||||
req.Header.Set("x-forwarded-uri", "/allow")
|
|
||||||
|
|
||||||
router.ServeHTTP(recorder, req)
|
|
||||||
assert.Equal(t, recorder.Code, http.StatusOK)
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user