2024-10-27 06:10:27 +01:00
|
|
|
package pkg
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
Copyright 2024 Jonas Kaninda
|
|
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
|
limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
import (
|
2024-11-07 09:45:09 +01:00
|
|
|
"context"
|
2024-10-27 06:10:27 +01:00
|
|
|
"encoding/json"
|
2024-11-15 07:56:37 +01:00
|
|
|
"fmt"
|
2024-10-27 06:10:27 +01:00
|
|
|
"github.com/gorilla/mux"
|
2024-11-04 08:48:38 +01:00
|
|
|
"github.com/jkaninda/goma-gateway/pkg/logger"
|
2024-10-27 06:10:27 +01:00
|
|
|
"net/http"
|
2024-10-31 07:02:51 +01:00
|
|
|
"sync"
|
2024-10-27 06:10:27 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// CORSHandler handles CORS headers for incoming requests
|
|
|
|
|
//
|
|
|
|
|
// Adds CORS headers to the response dynamically based on the provided headers map[string]string
|
|
|
|
|
func CORSHandler(cors Cors) mux.MiddlewareFunc {
|
|
|
|
|
return func(next http.Handler) http.Handler {
|
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
// Set CORS headers from the cors config
|
|
|
|
|
//Update Cors Headers
|
|
|
|
|
for k, v := range cors.Headers {
|
|
|
|
|
w.Header().Set(k, v)
|
|
|
|
|
}
|
|
|
|
|
//Update Origin Cors Headers
|
2024-10-29 10:35:31 +01:00
|
|
|
if allowedOrigin(cors.Origins, r.Header.Get("Origin")) {
|
|
|
|
|
// Handle preflight requests (OPTIONS)
|
|
|
|
|
if r.Method == "OPTIONS" {
|
|
|
|
|
w.Header().Set(accessControlAllowOrigin, r.Header.Get("Origin"))
|
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
|
return
|
|
|
|
|
} else {
|
|
|
|
|
w.Header().Set(accessControlAllowOrigin, r.Header.Get("Origin"))
|
2024-10-27 06:10:27 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Pass the request to the next handler
|
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ProxyErrorHandler catches backend errors and returns a custom response
|
|
|
|
|
func ProxyErrorHandler(w http.ResponseWriter, r *http.Request, err error) {
|
|
|
|
|
logger.Error("Proxy error: %v", err)
|
|
|
|
|
w.WriteHeader(http.StatusBadGateway)
|
2024-11-15 07:56:37 +01:00
|
|
|
_, err = w.Write([]byte(fmt.Sprintf("%d %s ", http.StatusBadGateway, http.StatusText(http.StatusBadGateway))))
|
2024-10-27 06:10:27 +01:00
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// HealthCheckHandler handles health check of routes
|
|
|
|
|
func (heathRoute HealthCheckRoute) HealthCheckHandler(w http.ResponseWriter, r *http.Request) {
|
2024-11-10 19:58:53 +01:00
|
|
|
logger.Debug("%s %s %s %s", r.Method, r.RemoteAddr, r.URL, r.UserAgent())
|
2024-11-15 08:19:22 +01:00
|
|
|
healthRoutes := healthCheckRoutes(heathRoute.Routes)
|
2024-10-31 07:02:51 +01:00
|
|
|
wg := sync.WaitGroup{}
|
2024-11-15 08:19:22 +01:00
|
|
|
wg.Add(len(healthRoutes))
|
2024-10-27 06:10:27 +01:00
|
|
|
var routes []HealthCheckRouteResponse
|
2024-11-15 08:19:22 +01:00
|
|
|
for _, health := range healthRoutes {
|
2024-10-31 07:02:51 +01:00
|
|
|
go func() {
|
2024-11-12 14:31:18 +01:00
|
|
|
err := health.Check()
|
|
|
|
|
if err != nil {
|
|
|
|
|
if heathRoute.DisableRouteHealthCheckError {
|
|
|
|
|
routes = append(routes, HealthCheckRouteResponse{Name: health.Name, Status: "unhealthy", Error: "Route healthcheck errors disabled"})
|
2024-10-27 06:10:27 +01:00
|
|
|
}
|
2024-11-12 14:31:18 +01:00
|
|
|
routes = append(routes, HealthCheckRouteResponse{Name: health.Name, Status: "unhealthy", Error: "Error: " + err.Error()})
|
2024-10-27 06:10:27 +01:00
|
|
|
} else {
|
2024-11-12 14:31:18 +01:00
|
|
|
logger.Debug("Route %s is healthy", health.Name)
|
|
|
|
|
routes = append(routes, HealthCheckRouteResponse{Name: health.Name, Status: "healthy", Error: ""})
|
2024-10-27 06:10:27 +01:00
|
|
|
}
|
2024-11-15 08:19:22 +01:00
|
|
|
defer wg.Done()
|
2024-11-12 14:31:18 +01:00
|
|
|
|
2024-10-31 07:02:51 +01:00
|
|
|
}()
|
2024-10-27 06:10:27 +01:00
|
|
|
|
|
|
|
|
}
|
2024-10-31 07:02:51 +01:00
|
|
|
wg.Wait() // Wait for all requests to complete
|
2024-10-27 06:10:27 +01:00
|
|
|
response := HealthCheckResponse{
|
2024-10-31 07:02:51 +01:00
|
|
|
Status: "healthy", //Goma proxy
|
|
|
|
|
Routes: routes, // Routes health check
|
2024-10-27 06:10:27 +01:00
|
|
|
}
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
err := json.NewEncoder(w).Encode(response)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-27 07:24:50 +01:00
|
|
|
func (heathRoute HealthCheckRoute) HealthReadyHandler(w http.ResponseWriter, r *http.Request) {
|
2024-11-02 13:05:43 +01:00
|
|
|
logger.Info("%s %s %s %s", r.Method, r.RemoteAddr, r.URL, r.UserAgent())
|
2024-10-27 07:24:50 +01:00
|
|
|
response := HealthCheckRouteResponse{
|
2024-11-05 10:34:47 +01:00
|
|
|
Name: "Service Gateway",
|
2024-10-27 07:24:50 +01:00
|
|
|
Status: "healthy",
|
|
|
|
|
Error: "",
|
|
|
|
|
}
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
err := json.NewEncoder(w).Encode(response)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-29 10:35:31 +01:00
|
|
|
func allowedOrigin(origins []string, origin string) bool {
|
|
|
|
|
for _, o := range origins {
|
|
|
|
|
if o == origin {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
|
|
}
|
2024-11-08 12:03:52 +01:00
|
|
|
|
|
|
|
|
// callbackHandler handles oauth callback
|
|
|
|
|
func (oauth *OauthRulerMiddleware) callbackHandler(w http.ResponseWriter, r *http.Request) {
|
2024-11-07 09:45:09 +01:00
|
|
|
oauthConfig := oauth2Config(oauth)
|
|
|
|
|
// Verify the state to protect against CSRF
|
|
|
|
|
if r.URL.Query().Get("state") != oauth.State {
|
|
|
|
|
http.Error(w, "Invalid state", http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
// Exchange the authorization code for an access token
|
|
|
|
|
code := r.URL.Query().Get("code")
|
|
|
|
|
token, err := oauthConfig.Exchange(context.Background(), code)
|
|
|
|
|
if err != nil {
|
2024-11-08 12:03:52 +01:00
|
|
|
logger.Error("Failed to exchange token: %v", err.Error())
|
|
|
|
|
http.Error(w, "Failed to exchange token", http.StatusInternalServerError)
|
2024-11-07 09:45:09 +01:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-08 12:03:52 +01:00
|
|
|
// Get user info from the token
|
|
|
|
|
userInfo, err := oauth.getUserInfo(token)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logger.Error("Error getting user info: %v", err)
|
|
|
|
|
http.Error(w, "Error getting user info: ", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
// Generate JWT with user's email
|
|
|
|
|
jwtToken, err := createJWT(userInfo.Email, oauth.JWTSecret)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logger.Error("Error creating JWT: %v", err)
|
|
|
|
|
http.Error(w, "Error creating JWT ", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
2024-11-07 09:45:09 +01:00
|
|
|
// Save token to a cookie for simplicity
|
|
|
|
|
http.SetCookie(w, &http.Cookie{
|
2024-11-08 18:19:26 +01:00
|
|
|
Name: "goma.oauth",
|
2024-11-08 12:03:52 +01:00
|
|
|
Value: jwtToken,
|
2024-11-07 09:45:09 +01:00
|
|
|
Path: oauth.CookiePath,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Redirect to the home page or another protected route
|
|
|
|
|
http.Redirect(w, r, oauth.RedirectPath, http.StatusSeeOther)
|
|
|
|
|
}
|