From 9162ec03c14fd35ecf7179875da6adacf00edfc5 Mon Sep 17 00:00:00 2001 From: Jonas Kaninda Date: Mon, 9 Dec 2024 12:27:50 +0100 Subject: [PATCH 1/4] chore: rename package name --- cmd/config/check.go | 4 ++-- cmd/config/init.go | 4 ++-- cmd/server.go | 4 ++-- go.sum | 1 + internal/check_config.go | 2 +- internal/config.go | 2 +- internal/cors_type.go | 2 +- internal/extra_config.go | 2 +- internal/gateway_type.go | 2 +- internal/handler.go | 2 +- internal/healthcheck.go | 2 +- internal/helpers.go | 2 +- internal/jwt.go | 2 +- internal/middleware.go | 2 +- internal/middleware_test.go | 2 +- internal/middleware_type.go | 2 +- internal/proxy.go | 2 +- internal/redis.go | 2 +- internal/route_config.go | 2 +- internal/route_type.go | 2 +- internal/routes.go | 2 +- internal/routes_test.go | 2 +- internal/server.go | 2 +- internal/server_test.go | 2 +- internal/tls.go | 2 +- internal/types.go | 2 +- internal/var.go | 2 +- 27 files changed, 30 insertions(+), 29 deletions(-) diff --git a/cmd/config/check.go b/cmd/config/check.go index 5073a1e..b7b2d3a 100644 --- a/cmd/config/check.go +++ b/cmd/config/check.go @@ -21,7 +21,7 @@ import ( "fmt" "os" - pkg "github.com/jkaninda/goma-gateway/internal" + "github.com/jkaninda/goma-gateway/internal" "github.com/spf13/cobra" ) @@ -34,7 +34,7 @@ var CheckConfigCmd = &cobra.Command{ fmt.Println("no config file specified") os.Exit(1) } - err := pkg.CheckConfig(configFile) + err := internal.CheckConfig(configFile) if err != nil { fmt.Printf(" Error checking config file: %s\n", err) os.Exit(1) diff --git a/cmd/config/init.go b/cmd/config/init.go index 04d39a4..6c38140 100644 --- a/cmd/config/init.go +++ b/cmd/config/init.go @@ -17,9 +17,9 @@ limitations under the License. */ import ( "fmt" + "github.com/jkaninda/goma-gateway/internal" "os" - pkg "github.com/jkaninda/goma-gateway/internal" "github.com/spf13/cobra" ) @@ -41,7 +41,7 @@ var InitConfigCmd = &cobra.Command{ os.Exit(1) } } - err := pkg.InitConfig(configFile) + err := internal.InitConfig(configFile) if err != nil { fmt.Println(err) os.Exit(1) diff --git a/cmd/server.go b/cmd/server.go index 7a731d0..c8fa2ef 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -33,10 +33,10 @@ var ServerCmd = &cobra.Command{ intro() configFile, _ := cmd.Flags().GetString("config") if configFile == "" { - configFile = pkg.GetConfigPaths() + configFile = internal.GetConfigPaths() } ctx := context.Background() - g := pkg.GatewayServer{} + g := internal.GatewayServer{} gs, err := g.Config(configFile, ctx) if err != nil { fmt.Printf("Could not load configuration: %v\n", err) diff --git a/go.sum b/go.sum index 67cd902..cc28cbb 100644 --- a/go.sum +++ b/go.sum @@ -28,6 +28,7 @@ github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2 github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/jedib0t/go-pretty/v6 v6.6.3 h1:nGqgS0tgIO1Hto47HSaaK4ac/I/Bu7usmdD3qvs0WvM= github.com/jedib0t/go-pretty/v6 v6.6.3/go.mod h1:zbn98qrYlh95FIhwwsbIip0LYpwSG8SUOScs+v9/t0E= +github.com/jinzhu/copier v0.4.0/go.mod h1:DfbEm0FYsaqBcKcFuvmOZb218JkPGtvSHsKg8S8hyyg= github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA= github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= diff --git a/internal/check_config.go b/internal/check_config.go index 50cf81d..867c73c 100644 --- a/internal/check_config.go +++ b/internal/check_config.go @@ -15,7 +15,7 @@ * */ -package pkg +package internal import ( "fmt" diff --git a/internal/config.go b/internal/config.go index 348824b..135a4ba 100644 --- a/internal/config.go +++ b/internal/config.go @@ -1,4 +1,4 @@ -package pkg +package internal /* Copyright 2024 Jonas Kaninda diff --git a/internal/cors_type.go b/internal/cors_type.go index b2c9d85..dbf184a 100644 --- a/internal/cors_type.go +++ b/internal/cors_type.go @@ -15,7 +15,7 @@ * */ -package pkg +package internal type Cors struct { // Cors Allowed origins, diff --git a/internal/extra_config.go b/internal/extra_config.go index 0903984..19395c9 100644 --- a/internal/extra_config.go +++ b/internal/extra_config.go @@ -15,7 +15,7 @@ * */ -package pkg +package internal import ( "fmt" diff --git a/internal/gateway_type.go b/internal/gateway_type.go index 5ad3e1f..112a341 100644 --- a/internal/gateway_type.go +++ b/internal/gateway_type.go @@ -15,7 +15,7 @@ * */ -package pkg +package internal // Gateway contains Goma Proxy Gateway's configs type Gateway struct { diff --git a/internal/handler.go b/internal/handler.go index 1016aa0..27bdbfc 100644 --- a/internal/handler.go +++ b/internal/handler.go @@ -1,4 +1,4 @@ -package pkg +package internal /* Copyright 2024 Jonas Kaninda diff --git a/internal/healthcheck.go b/internal/healthcheck.go index cc38baa..2e48651 100644 --- a/internal/healthcheck.go +++ b/internal/healthcheck.go @@ -1,4 +1,4 @@ -package pkg +package internal /* Copyright 2024 Jonas Kaninda diff --git a/internal/helpers.go b/internal/helpers.go index ed36dd0..52cc302 100644 --- a/internal/helpers.go +++ b/internal/helpers.go @@ -15,7 +15,7 @@ * */ -package pkg +package internal import ( "context" diff --git a/internal/jwt.go b/internal/jwt.go index a6d477d..4da94c5 100644 --- a/internal/jwt.go +++ b/internal/jwt.go @@ -15,7 +15,7 @@ * */ -package pkg +package internal import ( "github.com/golang-jwt/jwt" diff --git a/internal/middleware.go b/internal/middleware.go index a8c5c82..9d24bbf 100644 --- a/internal/middleware.go +++ b/internal/middleware.go @@ -1,4 +1,4 @@ -package pkg +package internal import ( "errors" diff --git a/internal/middleware_test.go b/internal/middleware_test.go index e7e8e73..b25038f 100644 --- a/internal/middleware_test.go +++ b/internal/middleware_test.go @@ -1,4 +1,4 @@ -package pkg +package internal /* Copyright 2024 Jonas Kaninda diff --git a/internal/middleware_type.go b/internal/middleware_type.go index 2087773..04838f6 100644 --- a/internal/middleware_type.go +++ b/internal/middleware_type.go @@ -15,7 +15,7 @@ * */ -package pkg +package internal // Middleware defined the route middlewares type Middleware struct { diff --git a/internal/proxy.go b/internal/proxy.go index 1f6a472..ac6b590 100644 --- a/internal/proxy.go +++ b/internal/proxy.go @@ -1,4 +1,4 @@ -package pkg +package internal /* Copyright 2024 Jonas Kaninda diff --git a/internal/redis.go b/internal/redis.go index 7e5d027..ef3027c 100644 --- a/internal/redis.go +++ b/internal/redis.go @@ -15,7 +15,7 @@ * */ -package pkg +package internal import ( "github.com/jkaninda/goma-gateway/internal/middlewares" diff --git a/internal/route_config.go b/internal/route_config.go index ba8307b..bb2bfb8 100644 --- a/internal/route_config.go +++ b/internal/route_config.go @@ -15,7 +15,7 @@ * */ -package pkg +package internal import ( "fmt" diff --git a/internal/route_type.go b/internal/route_type.go index d263c10..c0b88c6 100644 --- a/internal/route_type.go +++ b/internal/route_type.go @@ -15,7 +15,7 @@ * */ -package pkg +package internal // Route defines gateway route type Route struct { diff --git a/internal/routes.go b/internal/routes.go index 1ceb709..4c852ba 100644 --- a/internal/routes.go +++ b/internal/routes.go @@ -1,4 +1,4 @@ -package pkg +package internal /* Copyright 2024 Jonas Kaninda diff --git a/internal/routes_test.go b/internal/routes_test.go index 3917a01..d3f4691 100644 --- a/internal/routes_test.go +++ b/internal/routes_test.go @@ -15,7 +15,7 @@ * */ -package pkg +package internal import ( "fmt" diff --git a/internal/server.go b/internal/server.go index 404a43c..951c613 100644 --- a/internal/server.go +++ b/internal/server.go @@ -1,4 +1,4 @@ -package pkg +package internal /* Copyright 2024 Jonas Kaninda diff --git a/internal/server_test.go b/internal/server_test.go index 1bec5d9..6437ab1 100644 --- a/internal/server_test.go +++ b/internal/server_test.go @@ -1,4 +1,4 @@ -package pkg +package internal import ( "context" diff --git a/internal/tls.go b/internal/tls.go index 8f363b9..b980b93 100644 --- a/internal/tls.go +++ b/internal/tls.go @@ -15,7 +15,7 @@ * */ -package pkg +package internal import ( "crypto/tls" diff --git a/internal/types.go b/internal/types.go index d4a2c38..8f8dd80 100644 --- a/internal/types.go +++ b/internal/types.go @@ -15,7 +15,7 @@ * */ -package pkg +package internal import ( "context" diff --git a/internal/var.go b/internal/var.go index 854295f..eb412aa 100644 --- a/internal/var.go +++ b/internal/var.go @@ -1,4 +1,4 @@ -package pkg +package internal const ConfigDir = "/etc/goma/" // Default configuration file const ExtraDir = ConfigDir + "extra" From dc4cc35b9f2b564ad53ddb6dd6042819b27926bc Mon Sep 17 00:00:00 2001 From: Jonas Kaninda Date: Mon, 9 Dec 2024 12:30:49 +0100 Subject: [PATCH 2/4] refactor: clean up code --- internal/routes.go | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/routes.go b/internal/routes.go index 4c852ba..4047b26 100644 --- a/internal/routes.go +++ b/internal/routes.go @@ -254,7 +254,6 @@ func attachMiddlewares(rIndex int, route Route, gateway Gateway, router *mux.Rou logger.Error("Error: %v, middleware not applied", err.Error()) } if len(accessPolicy.SourceRanges) != 0 { - logger.Info("Ips: %v", accessPolicy.SourceRanges) access := middlewares.AccessPolicy{ SourceRanges: accessPolicy.SourceRanges, Action: accessPolicy.Action, From 9e3219bdddc9d6530e01f8d3f08a319b8c923af7 Mon Sep 17 00:00:00 2001 From: Jonas Kaninda Date: Mon, 9 Dec 2024 13:04:06 +0100 Subject: [PATCH 3/4] chore: improvement of logging --- internal/config.go | 1 - pkg/logger/logger.go | 77 +++++++++++++++++++++++++++----------------- 2 files changed, 47 insertions(+), 31 deletions(-) diff --git a/internal/config.go b/internal/config.go index 135a4ba..c143022 100644 --- a/internal/config.go +++ b/internal/config.go @@ -191,7 +191,6 @@ func initConfig(configFile string) error { Type: AccessMiddleware, Paths: []string{ "/swagger-ui/*", - "/v2/swagger-ui/*", "/api-docs/*", "/actuator/*", }, diff --git a/pkg/logger/logger.go b/pkg/logger/logger.go index 492bb6a..a6e4b11 100644 --- a/pkg/logger/logger.go +++ b/pkg/logger/logger.go @@ -26,47 +26,62 @@ import ( "github.com/jkaninda/goma-gateway/util" ) -// Info returns info log -func Info(msg string, args ...interface{}) { - log.SetOutput(getStd(util.GetStringEnv("GOMA_ACCESS_LOG", "/dev/stdout"))) - logWithCaller("INFO", msg, args...) - +// Generic logging function +func logMessage(level, defaultOutput, msg string, args ...interface{}) { + logLevel := getLogLevel() + if shouldLog(level, logLevel) { + log.SetOutput(getStd(util.GetStringEnv("GOMA_ACCESS_LOG", defaultOutput))) + logWithCaller(level, msg, args...) + } } -// Warn returns warning log -func Warn(msg string, args ...interface{}) { - log.SetOutput(getStd(util.GetStringEnv("GOMA_ACCESS_LOG", "/dev/stdout"))) - logWithCaller("WARN", msg, args...) +// Info logs informational messages +func Info(msg string, args ...interface{}) { + logMessage("INFO", "/dev/stdout", msg, args...) +} +// Warn logs warning messages +func Warn(msg string, args ...interface{}) { + logMessage("WARN", "/dev/stdout", msg, args...) } // Error logs error messages func Error(msg string, args ...interface{}) { - log.SetOutput(getStd(util.GetStringEnv("GOMA_ERROR_LOG", "/dev/stderr"))) - logWithCaller("ERROR", msg, args...) + logMessage("ERROR", "/dev/stderr", msg, args...) } +// Fatal logs fatal errors and exits the program func Fatal(msg string, args ...interface{}) { log.SetOutput(os.Stdout) logWithCaller("ERROR", msg, args...) os.Exit(1) } +// Debug logs debug messages func Debug(msg string, args ...interface{}) { - log.SetOutput(getStd(util.GetStringEnv("GOMA_ACCESS_LOG", "/dev/stdout"))) - logLevel := util.GetStringEnv("GOMA_LOG_LEVEL", "") - if strings.ToLower(logLevel) == traceLog || strings.ToLower(logLevel) == "debug" { - logWithCaller("DEBUG", msg, args...) - } - + logMessage("DEBUG", "/dev/stdout", msg, args...) } + +// Trace logs trace messages func Trace(msg string, args ...interface{}) { - log.SetOutput(getStd(util.GetStringEnv("GOMA_ACCESS_LOG", "/dev/stdout"))) - logLevel := util.GetStringEnv("GOMA_LOG_LEVEL", "") - if strings.ToLower(logLevel) == traceLog { - logWithCaller("DEBUG", msg, args...) + logMessage("TRACE", "/dev/stdout", msg, args...) +} + +// Determines whether the message should be logged based on log level +func shouldLog(level, currentLevel string) bool { + levelOrder := map[string]int{ + "trace": 1, + "debug": 2, + "info": 3, + "warn": 4, + "error": 5, + "off": 6, } + current := strings.ToLower(currentLevel) + target := strings.ToLower(level) + + return levelOrder[target] >= levelOrder[current] } // Helper function to format and log messages with file and line number @@ -83,17 +98,15 @@ func logWithCaller(level, msg string, args ...interface{}) { file = "unknown" line = 0 } - // Log message with caller information if GOMA_LOG_LEVEL is trace - logLevel := util.GetStringEnv("GOMA_LOG_LEVEL", "") - if strings.ToLower(logLevel) != "off" { - if strings.ToLower(logLevel) == traceLog { - log.Printf("%s: %s (File: %s, Line: %d)\n", level, formattedMessage, file, line) - } else { - log.Printf("%s: %s\n", level, formattedMessage) - } + + if getLogLevel() == traceLog { + log.Printf("%s: %s (File: %s, Line: %d)\n", level, formattedMessage, file, line) + } else { + log.Printf("%s: %s\n", level, formattedMessage) } } +// Determines the appropriate standard output based on the environment variable func getStd(out string) *os.File { switch out { case "/dev/stdout": @@ -104,6 +117,10 @@ func getStd(out string) *os.File { return os.Stdin default: return os.Stdout - } } + +// Retrieves the current log level from environment variables +func getLogLevel() string { + return strings.ToLower(util.GetStringEnv("GOMA_LOG_LEVEL", "")) +} From 10c2d3332d7f5cf6892ff84701c5845e3a2c9fa2 Mon Sep 17 00:00:00 2001 From: Jonas Kaninda Date: Mon, 9 Dec 2024 13:14:48 +0100 Subject: [PATCH 4/4] test: add IP validation test --- internal/helpers_test.go | 58 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 internal/helpers_test.go diff --git a/internal/helpers_test.go b/internal/helpers_test.go new file mode 100644 index 0000000..ce07c7b --- /dev/null +++ b/internal/helpers_test.go @@ -0,0 +1,58 @@ +/* + * 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. + * + */ + +package internal + +import ( + "fmt" + "testing" +) + +func TestValidateIPAddress(t *testing.T) { + tests := []string{ + "192.168.1.100", + "192.168.1.120", + } + for _, test := range tests { + if validateIPAddress(test) { + fmt.Println("Ip is valid") + } else { + fmt.Println("Ip is invalid") + } + } + +} +func TestValidateIPOrCIDR(t *testing.T) { + tests := []string{ + "192.168.1.100", + "192.168.1.100", + "192.168.1.100/32", + "invalid-input", + "192.168.1.100/33", + } + for _, test := range tests { + isIP, isCIDR := isIPOrCIDR(test) + if isIP { + fmt.Printf("%s is an IP address\n", test) + } else if isCIDR { + fmt.Printf("%s is a CIDR\n", test) + } else { + fmt.Printf("%s is neither an IP address nor a CIDR\n", test) + } + } + +}