aboutsummaryrefslogtreecommitdiff
path: root/weed/s3api/s3api_bucket_handlers.go
diff options
context:
space:
mode:
authorChris Lu <chrislusf@users.noreply.github.com>2025-12-08 01:24:12 -0800
committerGitHub <noreply@github.com>2025-12-08 01:24:12 -0800
commitf5c0bcafa34639aaafb18e26d260bd6d02d639fb (patch)
tree2b5b2e25e7dcc66b1b00eb90a5d87e1ec47620bc /weed/s3api/s3api_bucket_handlers.go
parenta9b3be416bb9deae3eecdbabef0ca3d4f8a11feb (diff)
downloadseaweedfs-f5c0bcafa34639aaafb18e26d260bd6d02d639fb.tar.xz
seaweedfs-f5c0bcafa34639aaafb18e26d260bd6d02d639fb.zip
s3: fix ListBuckets not showing buckets created by authenticated users (#7648)
* s3: fix ListBuckets not showing buckets created by authenticated users Fixes #7647 ## Problem Users with proper Admin permissions could create buckets but couldn't list them. The issue occurred because ListBucketsHandler was not wrapped with the Auth middleware, so the authenticated identity was never set in the request context. ## Root Cause - PutBucketHandler uses iam.Auth() middleware which sets identity in context - ListBucketsHandler did NOT use iam.Auth() middleware - Without the middleware, GetIdentityNameFromContext() returned empty string - Bucket ownership checks failed because no identity was present ## Changes 1. Wrap ListBucketsHandler with iam.Auth() middleware (s3api_server.go) 2. Update ListBucketsHandler to get identity from context (s3api_bucket_handlers.go) 3. Add lookupByIdentityName() helper method (auth_credentials.go) 4. Add comprehensive test TestListBucketsIssue7647 (s3api_bucket_handlers_test.go) ## Testing - All existing tests pass (1348 tests in s3api package) - New test TestListBucketsIssue7647 validates the fix - Verified admin users can see their created buckets - Verified admin users can see all buckets - Verified backward compatibility maintained * s3: fix ListBuckets for JWT/Keycloak authentication The previous fix broke JWT/Keycloak authentication because JWT identities are created on-the-fly and not stored in the iam.identities list. The lookupByIdentityName() would return nil for JWT users. Solution: Store the full Identity object in the request context, not just the name. This allows ListBucketsHandler to retrieve the complete identity for all authentication types (SigV2, SigV4, JWT, Anonymous). Changes: - Add SetIdentityInContext/GetIdentityFromContext in s3_constants/header.go - Update Auth middleware to store full identity in context - Update ListBucketsHandler to retrieve identity from context first, with fallback to lookup for backward compatibility * s3: optimize lookupByIdentityName to O(1) using map Address code review feedback: Use a map for O(1) lookups instead of O(N) linear scan through identities list. Changes: - Add nameToIdentity map to IdentityAccessManagement struct - Populate map in loadS3ApiConfiguration (consistent with accessKeyIdent pattern) - Update lookupByIdentityName to use map lookup instead of loop This improves performance when many identities are configured and aligns with the existing pattern used for accessKeyIdent lookups. * s3: address code review feedback on nameToIdentity and logging Address two code review points: 1. Wire nameToIdentity into env-var fallback path - The AWS env-var fallback in NewIdentityAccessManagementWithStore now populates nameToIdentity map along with accessKeyIdent - Keeps all identity lookup maps in sync - Avoids potential issues if handlers rely on lookupByIdentityName 2. Improve access key lookup logging - Reduce log verbosity: V(1) -> V(2) for failed lookups - Truncate access keys in logs (show first 4 chars + ***) - Include key length for debugging - Prevents credential exposure in production logs - Reduces log noise from misconfigured clients * fmt * s3: refactor truncation logic and improve error handling Address additional code review feedback: 1. DRY principle: Extract key truncation logic into local function - Define truncate() helper at function start - Reuse throughout lookupByAccessKey - Eliminates code duplication 2. Enhanced security: Mask very short access keys - Keys <= 4 chars now show as '***' instead of full key - Prevents any credential exposure even for short keys - Consistent masking across all log statements 3. Improved robustness: Add warning log for type assertion failure - Log unexpected type when identity context object is wrong type - Helps debug potential middleware or context issues - Better production diagnostics 4. Documentation: Add comment about future optimization opportunity - Note potential for lightweight identity view in context - Suggests credential-free view for better data minimization - Documents design decision for future maintainers
Diffstat (limited to 'weed/s3api/s3api_bucket_handlers.go')
-rw-r--r--weed/s3api/s3api_bucket_handlers.go31
1 files changed, 20 insertions, 11 deletions
diff --git a/weed/s3api/s3api_bucket_handlers.go b/weed/s3api/s3api_bucket_handlers.go
index a810dfd37..09bea9aa8 100644
--- a/weed/s3api/s3api_bucket_handlers.go
+++ b/weed/s3api/s3api_bucket_handlers.go
@@ -38,15 +38,28 @@ func (s3a *S3ApiServer) ListBucketsHandler(w http.ResponseWriter, r *http.Reques
glog.V(3).Infof("ListBucketsHandler")
+ // Get authenticated identity from context (set by Auth middleware)
+ // For unauthenticated requests, this returns empty string
+ identityId := s3_constants.GetIdentityNameFromContext(r)
+
+ // Get the full identity object for permission and ownership checks
+ // This is especially important for JWT users whose identity is not in the identities list
+ // Note: We store the full Identity object in context for simplicity. Future optimization
+ // could use a lightweight, credential-free view (name, account, actions, principal ARN)
+ // for better data minimization.
var identity *Identity
- var s3Err s3err.ErrorCode
if s3a.iam.isEnabled() {
- // Use authRequest instead of authUser for consistency with other endpoints
- // This ensures the same authentication flow and any fixes (like prefix handling) are applied
- identity, s3Err = s3a.iam.authRequest(r, s3_constants.ACTION_LIST)
- if s3Err != s3err.ErrNone {
- s3err.WriteErrorResponse(w, r, s3Err)
- return
+ // Try to get the full identity from context first (works for all auth types including JWT)
+ if identityObj := s3_constants.GetIdentityFromContext(r); identityObj != nil {
+ if id, ok := identityObj.(*Identity); ok {
+ identity = id
+ } else {
+ glog.Warningf("ListBucketsHandler: identity object in context has unexpected type: %T", identityObj)
+ }
+ }
+ // Fallback to looking up by name if not in context (backward compatibility)
+ if identity == nil && identityId != "" {
+ identity = s3a.iam.lookupByIdentityName(identityId)
}
}
@@ -59,10 +72,6 @@ func (s3a *S3ApiServer) ListBucketsHandler(w http.ResponseWriter, r *http.Reques
return
}
- // Get authenticated identity from context (secure, cannot be spoofed)
- // For unauthenticated requests, this returns empty string
- identityId := s3_constants.GetIdentityNameFromContext(r)
-
var listBuckets ListAllMyBucketsList
for _, entry := range entries {
if entry.IsDirectory {