From 9db5d1e4f4c0b466ac1e33f35acb35caa8689acf Mon Sep 17 00:00:00 2001 From: Daniel Valdivia <18384552+dvaldivia@users.noreply.github.com> Date: Mon, 11 Dec 2023 09:57:02 -0800 Subject: [PATCH] Tests for pkg/utils (#3155) Signed-off-by: Daniel Valdivia <18384552+dvaldivia@users.noreply.github.com> --- pkg/logger/message/audit/entry.go | 4 +++- pkg/utils/utils.go | 11 --------- pkg/utils/utils_test.go | 38 ++++++++++++++++++++++++++++++- restapi/configure_console.go | 8 +++---- 4 files changed, 43 insertions(+), 18 deletions(-) diff --git a/pkg/logger/message/audit/entry.go b/pkg/logger/message/audit/entry.go index 43148375f..2848eccd7 100644 --- a/pkg/logger/message/audit/entry.go +++ b/pkg/logger/message/audit/entry.go @@ -22,6 +22,8 @@ import ( "strings" "time" + "github.com/google/uuid" + "github.com/golang-jwt/jwt/v4" "github.com/minio/console/pkg/utils" @@ -100,7 +102,7 @@ func ToEntry(w http.ResponseWriter, r *http.Request, reqClaims map[string]interf var requestID interface{} requestID = r.Context().Value(utils.ContextRequestID) if requestID == nil { - requestID, _ = utils.NewUUID() + requestID = uuid.NewString() } entry.RequestID = requestID.(string) diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 6e42b3465..d7b3e828e 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -19,19 +19,8 @@ package utils import ( "context" "encoding/base64" - - "github.com/google/uuid" ) -// NewUUID - get a random UUID. -func NewUUID() (string, error) { - u, err := uuid.NewRandom() - if err != nil { - return "", err - } - return u.String(), nil -} - // DecodeBase64 : decoded base64 input into utf-8 text func DecodeBase64(s string) (string, error) { decodedInput, err := base64.StdEncoding.DecodeString(s) diff --git a/pkg/utils/utils_test.go b/pkg/utils/utils_test.go index fa5a20943..75dce5acf 100644 --- a/pkg/utils/utils_test.go +++ b/pkg/utils/utils_test.go @@ -16,7 +16,10 @@ package utils -import "testing" +import ( + "context" + "testing" +) func TestDecodeInput(t *testing.T) { type args struct { @@ -90,3 +93,36 @@ func TestDecodeInput(t *testing.T) { }) } } + +func TestClientIPFromContext(t *testing.T) { + type args struct { + ctx context.Context + } + tests := []struct { + name string + args args + want string + }{ + { + name: "working return", + args: args{ + ctx: context.Background(), + }, + want: "127.0.0.1", + }, + { + name: "context contains ip", + args: args{ + ctx: context.WithValue(context.Background(), ContextClientIP, "10.0.0.1"), + }, + want: "10.0.0.1", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := ClientIPFromContext(tt.args.ctx); got != tt.want { + t.Errorf("ClientIPFromContext() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/restapi/configure_console.go b/restapi/configure_console.go index 6d074a6d2..acdcc9b13 100644 --- a/restapi/configure_console.go +++ b/restapi/configure_console.go @@ -35,6 +35,8 @@ import ( "sync" "time" + "github.com/google/uuid" + "github.com/minio/console/pkg/logger" "github.com/minio/console/pkg/utils" "github.com/minio/minio-go/v7/pkg/credentials" @@ -192,11 +194,7 @@ func setupMiddlewares(handler http.Handler) http.Handler { func ContextMiddleware(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - requestID, err := utils.NewUUID() - if err != nil && err != auth.ErrNoAuthToken { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } + requestID := uuid.NewString() ctx := context.WithValue(r.Context(), utils.ContextRequestID, requestID) ctx = context.WithValue(ctx, utils.ContextRequestUserAgent, r.UserAgent()) ctx = context.WithValue(ctx, utils.ContextRequestHost, r.Host)