Skip to content

Commit 72f47da

Browse files
committed
feat: Add debug logs on key rewrite selection
Related to #442
1 parent 099c26a commit 72f47da

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

pkg/s3-proxy/bucket/bucket-req-impl.go

+17
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"github.com/oxyno-zeta/s3-proxy/pkg/s3-proxy/authx/models"
1414
"github.com/oxyno-zeta/s3-proxy/pkg/s3-proxy/config"
15+
"github.com/oxyno-zeta/s3-proxy/pkg/s3-proxy/log"
1516
responsehandler "github.com/oxyno-zeta/s3-proxy/pkg/s3-proxy/response-handler"
1617
"github.com/oxyno-zeta/s3-proxy/pkg/s3-proxy/s3client"
1718
utils "github.com/oxyno-zeta/s3-proxy/pkg/s3-proxy/utils/generalutils"
@@ -42,15 +43,24 @@ func (bri *bucketReqImpl) generateStartKey(requestPath string) string {
4243
func (bri *bucketReqImpl) manageKeyRewrite(ctx context.Context, key string) (string, error) {
4344
// Check if key rewrite list exists
4445
if bri.targetCfg.KeyRewriteList != nil {
46+
// Get logger
47+
logger := log.GetLoggerFromContext(ctx)
48+
4549
// Loop over key rewrite list
4650
for _, kr := range bri.targetCfg.KeyRewriteList {
4751
// Check if key is matching
4852
if kr.SourceRegex.MatchString(key) {
53+
// Debug log
54+
logger.Debugf("Rewrite key : Key %s is matching the source rewrite %s", key, kr.SourceRegex.String())
55+
4956
// Find submatches
5057
submatches := kr.SourceRegex.FindStringSubmatchIndex(key)
5158

5259
// Check if there isn't any submatch
5360
if len(submatches) == 0 {
61+
// Debug log
62+
logger.Debugf("Rewrite key : Key %s have been changed to %s", key, kr.Target)
63+
5464
return kr.Target, nil
5565
}
5666

@@ -60,6 +70,10 @@ func (bri *bucketReqImpl) manageKeyRewrite(ctx context.Context, key string) (str
6070
result := []byte{}
6171
// Replace matches in target
6272
result = kr.SourceRegex.ExpandString(result, kr.Target, key, submatches)
73+
74+
// Debug log
75+
logger.Debugf("Rewrite key : Key %s have been changed to %s", key, string(result))
76+
6377
// Return result
6478
return string(result), nil
6579
}
@@ -113,6 +127,9 @@ func (bri *bucketReqImpl) manageKeyRewrite(ctx context.Context, key string) (str
113127
// Trim spaces
114128
str = strings.TrimSpace(str)
115129

130+
// Debug log
131+
logger.Debugf("Rewrite key : Key %s have been changed to %s", key, str)
132+
116133
return str, nil
117134
}
118135
}

pkg/s3-proxy/bucket/bucket-req-impl_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -2553,6 +2553,7 @@ func Test_requestContext_manageKeyRewrite(t *testing.T) {
25532553
ctx := context.TODO()
25542554
ctx = responsehandler.SetResponseHandlerInContext(ctx, resHandlerMock)
25552555
ctx = models.SetAuthenticatedUserInContext(ctx, tt.userMockResult)
2556+
ctx = log.SetLoggerInContext(ctx, log.NewLogger())
25562557

25572558
rctx := &bucketReqImpl{
25582559
targetCfg: tt.fields.targetCfg,

0 commit comments

Comments
 (0)