s3: register account-less identities' synthesized account so ACL/owner ids resolve (#9971)

* s3: register account-less identities' synthesized account in the lookup

#9962 gave each account-less identity a distinct account id derived from
its name (instead of collapsing into admin), but never registered that
account in the id->account map. GetAccountNameById then returned empty
for such ids, so ACL grantee validation rejected canonical grants to the
caller's own account with InvalidRequest, and bucket/object owner display
was dropped as 'owner is invalid'.

This broke a canned PutObjectAcl by an account-less identity (e.g.
TestVersionedObjectAcl with the default 'some_admin_user' identity):
ValidateAndTransferGrants -> GetAccountNameById -> 'account id is not
exists' -> 400 InvalidRequest.

Register the synthesized account at config load so its id resolves to a
display name. Add a regression test.

* s3: reuse explicitly-configured account for account-less identity

Address review: if an account with the same id as an account-less
identity's synthesized account is explicitly configured (custom display
name/email), reuse it instead of the synthesized one. Add a test.
This commit is contained in:
Chris Lu 2026-06-14 21:42:23 -07:00 committed by GitHub
parent 1391a85a20
commit 14d247703a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 83 additions and 2 deletions

View File

@ -70,3 +70,62 @@ func TestCheckAccessByOwnershipDeniesNonOwner(t *testing.T) {
owner.Header.Set(s3_constants.AmzAccountId, AccountAdmin.Id)
assert.Equal(t, s3err.ErrNone, s3a.checkAccessByOwnership(owner, "b"), "the actual owner is still allowed")
}
// An account-less identity's synthesized account must be registered in the
// account lookup so its id resolves to a display name. Otherwise ACL grantee
// validation and owner display report the id as "not exists" — the regression
// where a canned PutObjectAcl granting to the caller's own account returned
// 400 InvalidRequest.
func TestUnscopedIdentityAccountResolvesByName(t *testing.T) {
resetMemoryStore()
config := `{
"identities": [
{"name": "alice", "credentials": [{"accessKey": "alice_ak", "secretKey": "alice_sk"}], "actions": ["Read", "Write"]}
]
}`
tmp, err := os.CreateTemp("", "s3-config-*.json")
require.NoError(t, err)
defer os.Remove(tmp.Name())
_, err = tmp.WriteString(config)
require.NoError(t, err)
require.NoError(t, tmp.Close())
iam := NewIdentityAccessManagementWithStore(&S3ApiServerOption{Config: tmp.Name()}, nil, "memory")
assert.Equal(t, "alice", iam.GetAccountNameById("alice"),
"account-less identity id must resolve to a display name for ACL/owner validation")
}
// When an account is explicitly configured with the same id an account-less
// identity would synthesize, the identity must reuse that configured account so
// its custom display name/email are preserved.
func TestUnscopedIdentityReusesConfiguredAccount(t *testing.T) {
resetMemoryStore()
config := `{
"accounts": [
{"id": "alice", "displayName": "Alice Smith", "emailAddress": "alice@example.com"}
],
"identities": [
{"name": "alice", "credentials": [{"accessKey": "alice_ak", "secretKey": "alice_sk"}], "actions": ["Read"]}
]
}`
tmp, err := os.CreateTemp("", "s3-config-*.json")
require.NoError(t, err)
defer os.Remove(tmp.Name())
_, err = tmp.WriteString(config)
require.NoError(t, err)
require.NoError(t, tmp.Close())
iam := NewIdentityAccessManagementWithStore(&S3ApiServerOption{Config: tmp.Name()}, nil, "memory")
assert.Equal(t, "Alice Smith", iam.GetAccountNameById("alice"),
"explicitly configured account display name must be preserved")
alice, _, found := iam.LookupByAccessKey("alice_ak")
require.True(t, found)
require.NotNil(t, alice.Account)
assert.Equal(t, "Alice Smith", alice.Account.DisplayName,
"identity must reuse the configured account, not the synthesized one")
}

View File

@ -627,7 +627,18 @@ func (iam *IdentityAccessManagement) ReplaceS3ApiConfiguration(config *iam_pb.S3
t.Account = &AccountAnonymous
identityAnonymous = t
case ident.Account == nil:
t.Account = accountForUnscopedIdentity(t.Name)
// Account-less identities own resources under a distinct id derived
// from their name. Reuse an explicitly-configured account with that
// id if one exists (preserving its display name/email); otherwise
// synthesize one and register it so the id resolves via
// GetAccountNameById (ACL grantee validation, owner display).
synthesized := accountForUnscopedIdentity(t.Name)
if existing, ok := accounts[synthesized.Id]; ok {
t.Account = existing
} else {
t.Account = synthesized
accounts[synthesized.Id] = synthesized
}
default:
if account, ok := accounts[ident.Account.Id]; ok {
t.Account = account
@ -846,7 +857,18 @@ func (iam *IdentityAccessManagement) MergeS3ApiConfiguration(config *iam_pb.S3Ap
t.Account = &AccountAnonymous
identityAnonymous = t
case ident.Account == nil:
t.Account = accountForUnscopedIdentity(t.Name)
// Account-less identities own resources under a distinct id derived
// from their name. Reuse an explicitly-configured account with that
// id if one exists (preserving its display name/email); otherwise
// synthesize one and register it so the id resolves via
// GetAccountNameById (ACL grantee validation, owner display).
synthesized := accountForUnscopedIdentity(t.Name)
if existing, ok := accounts[synthesized.Id]; ok {
t.Account = existing
} else {
t.Account = synthesized
accounts[synthesized.Id] = synthesized
}
default:
if account, ok := accounts[ident.Account.Id]; ok {
t.Account = account