Skip to content

Commit

Permalink
feat: Add debug logs on key rewrite selection
Browse files Browse the repository at this point in the history
Related to #442
  • Loading branch information
oxyno-zeta committed Aug 18, 2024
1 parent 099c26a commit 72f47da
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
17 changes: 17 additions & 0 deletions pkg/s3-proxy/bucket/bucket-req-impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/oxyno-zeta/s3-proxy/pkg/s3-proxy/authx/models"
"github.com/oxyno-zeta/s3-proxy/pkg/s3-proxy/config"
"github.com/oxyno-zeta/s3-proxy/pkg/s3-proxy/log"
responsehandler "github.com/oxyno-zeta/s3-proxy/pkg/s3-proxy/response-handler"
"github.com/oxyno-zeta/s3-proxy/pkg/s3-proxy/s3client"
utils "github.com/oxyno-zeta/s3-proxy/pkg/s3-proxy/utils/generalutils"
Expand Down Expand Up @@ -42,15 +43,24 @@ func (bri *bucketReqImpl) generateStartKey(requestPath string) string {
func (bri *bucketReqImpl) manageKeyRewrite(ctx context.Context, key string) (string, error) {
// Check if key rewrite list exists
if bri.targetCfg.KeyRewriteList != nil {
// Get logger
logger := log.GetLoggerFromContext(ctx)

// Loop over key rewrite list
for _, kr := range bri.targetCfg.KeyRewriteList {
// Check if key is matching
if kr.SourceRegex.MatchString(key) {
// Debug log
logger.Debugf("Rewrite key : Key %s is matching the source rewrite %s", key, kr.SourceRegex.String())

// Find submatches
submatches := kr.SourceRegex.FindStringSubmatchIndex(key)

// Check if there isn't any submatch
if len(submatches) == 0 {
// Debug log
logger.Debugf("Rewrite key : Key %s have been changed to %s", key, kr.Target)

return kr.Target, nil
}

Expand All @@ -60,6 +70,10 @@ func (bri *bucketReqImpl) manageKeyRewrite(ctx context.Context, key string) (str
result := []byte{}
// Replace matches in target
result = kr.SourceRegex.ExpandString(result, kr.Target, key, submatches)

// Debug log
logger.Debugf("Rewrite key : Key %s have been changed to %s", key, string(result))

// Return result
return string(result), nil
}
Expand Down Expand Up @@ -113,6 +127,9 @@ func (bri *bucketReqImpl) manageKeyRewrite(ctx context.Context, key string) (str
// Trim spaces
str = strings.TrimSpace(str)

// Debug log
logger.Debugf("Rewrite key : Key %s have been changed to %s", key, str)

return str, nil
}
}
Expand Down
1 change: 1 addition & 0 deletions pkg/s3-proxy/bucket/bucket-req-impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2553,6 +2553,7 @@ func Test_requestContext_manageKeyRewrite(t *testing.T) {
ctx := context.TODO()
ctx = responsehandler.SetResponseHandlerInContext(ctx, resHandlerMock)
ctx = models.SetAuthenticatedUserInContext(ctx, tt.userMockResult)
ctx = log.SetLoggerInContext(ctx, log.NewLogger())

rctx := &bucketReqImpl{
targetCfg: tt.fields.targetCfg,
Expand Down

0 comments on commit 72f47da

Please sign in to comment.