Skip to content

Agent Mesh

feat: support BasePath for OIDC redirect URIs and cookie pathing, add 302 redirect for exact /console path

Antonio Ojeab69e3d04d4c8
diff --git a/.github/k8s/sam-console-template.yaml b/.github/k8s/sam-console-template.yaml
index f612aed5..25cb5e93 100644
--- a/.github/k8s/sam-console-template.yaml
+++ b/.github/k8s/sam-console-template.yaml
@@ -36,6 +36,7 @@ spec:
- "--bind-addr=:8081"
- "--static-dir=/app/public"
- "--admin-token=$(SAM_ADMIN_TOKEN)"
+ - "--base-path=/console"
env:
- name: SAM_ADMIN_TOKEN
valueFrom:
diff --git a/.github/k8s/sam-control-plane-template.yaml b/.github/k8s/sam-control-plane-template.yaml
index 8551c59a..53700c1b 100644
--- a/.github/k8s/sam-control-plane-template.yaml
+++ b/.github/k8s/sam-control-plane-template.yaml
@@ -260,6 +260,17 @@ spec:
backendRefs:
- name: sam-control-plane-${ENV_NAME}
port: 8080
+ - matches:
+ - path:
+ type: Exact
+ value: /console
+ filters:
+ - type: RequestRedirect
+ requestRedirect:
+ path:
+ type: ReplaceFullPath
+ replaceFullPath: /console/
+ statusCode: 302
- matches:
- path:
type: PathPrefix
diff --git a/cmd/sam-console/main.go b/cmd/sam-console/main.go
index 406fce17..be3e77c0 100644
--- a/cmd/sam-console/main.go
+++ b/cmd/sam-console/main.go
@@ -19,6 +19,7 @@ func main() {
adminToken = flag.String("admin-token", "", "Admin token for control plane authentication")
bindAddr = flag.String("bind-addr", ":8081", "Address to bind the console server")
staticDir = flag.String("static-dir", "public", "Directory containing static frontend files")
+ basePath = flag.String("base-path", "", "Base path prefix for the console (e.g. /console)")
)
flag.Parse()
@@ -30,6 +31,7 @@ func main() {
HubURL: *hubURL,
AdminToken: *adminToken,
StaticDir: *staticDir,
+ BasePath: *basePath,
})
if err != nil {
log.Fatalf("Failed to initialize console server: %v", err)
diff --git a/internal/console/server.go b/internal/console/server.go
index 75d9b0b4..4e399144 100644
--- a/internal/console/server.go
+++ b/internal/console/server.go
@@ -25,6 +25,7 @@ type Config struct {
HubURL string
AdminToken string
StaticDir string
+ BasePath string
}
type Server struct {
@@ -134,10 +135,10 @@ func (s *Server) HandleLogout(w http.ResponseWriter, r *http.Request) {
http.SetCookie(w, &http.Cookie{
Name: "sam_session",
Value: "",
- Path: "/",
+ Path: s.cfg.BasePath + "/",
MaxAge: -1,
})
- http.Redirect(w, r, "/", http.StatusFound)
+ http.Redirect(w, r, s.cfg.BasePath+"/", http.StatusFound)
}
func (s *Server) HandleLogin(w http.ResponseWriter, r *http.Request) {
@@ -145,7 +146,7 @@ func (s *Server) HandleLogin(w http.ResponseWriter, r *http.Request) {
if r.TLS != nil || r.Header.Get("X-Forwarded-Proto") == "https" {
scheme = "https"
}
- redirectURI := fmt.Sprintf("%s://%s/auth/callback", scheme, r.Host)
+ redirectURI := fmt.Sprintf("%s://%s%s/auth/callback", scheme, r.Host, s.cfg.BasePath)
verifier, challenge, err := generatePKCE()
if err != nil {
@@ -163,7 +164,7 @@ func (s *Server) HandleLogin(w http.ResponseWriter, r *http.Request) {
http.SetCookie(w, &http.Cookie{
Name: "sam_oidc_state",
Value: state,
- Path: "/",
+ Path: s.cfg.BasePath + "/",
MaxAge: 300,
HttpOnly: true,
Secure: scheme == "https",
@@ -173,7 +174,7 @@ func (s *Server) HandleLogin(w http.ResponseWriter, r *http.Request) {
http.SetCookie(w, &http.Cookie{
Name: "sam_oidc_verifier",
Value: verifier,
- Path: "/",
+ Path: s.cfg.BasePath + "/",
MaxAge: 300,
HttpOnly: true,
Secure: scheme == "https",
@@ -208,8 +209,8 @@ func (s *Server) HandleCallback(w http.ResponseWriter, r *http.Request) {
return
}
- http.SetCookie(w, &http.Cookie{Name: "sam_oidc_state", Value: "", Path: "/", MaxAge: -1})
- http.SetCookie(w, &http.Cookie{Name: "sam_oidc_verifier", Value: "", Path: "/", MaxAge: -1})
+ http.SetCookie(w, &http.Cookie{Name: "sam_oidc_state", Value: "", Path: s.cfg.BasePath + "/", MaxAge: -1})
+ http.SetCookie(w, &http.Cookie{Name: "sam_oidc_verifier", Value: "", Path: s.cfg.BasePath + "/", MaxAge: -1})
code := r.URL.Query().Get("code")
if code == "" {
@@ -221,7 +222,7 @@ func (s *Server) HandleCallback(w http.ResponseWriter, r *http.Request) {
if r.TLS != nil || r.Header.Get("X-Forwarded-Proto") == "https" {
scheme = "https"
}
- redirectURI := fmt.Sprintf("%s://%s/auth/callback", scheme, r.Host)
+ redirectURI := fmt.Sprintf("%s://%s%s/auth/callback", scheme, r.Host, s.cfg.BasePath)
oauth2Config := &oauth2.Config{
ClientID: s.clientID,
@@ -247,14 +248,14 @@ func (s *Server) HandleCallback(w http.ResponseWriter, r *http.Request) {
http.SetCookie(w, &http.Cookie{
Name: "sam_session",
Value: rawIDToken,
- Path: "/",
+ Path: s.cfg.BasePath + "/",
MaxAge: 24 * 3600,
HttpOnly: true,
Secure: scheme == "https",
SameSite: http.SameSiteLaxMode,
})
- http.Redirect(w, r, "/", http.StatusFound)
+ http.Redirect(w, r, s.cfg.BasePath+"/", http.StatusFound)
}
func (s *Server) HandleSession(w http.ResponseWriter, r *http.Request) {