s3api: hydrate runtime policies on propagated IAM updates (#11784)

* s3api: hydrate runtime policies on pushed identity upserts

PutIdentity propagation (used on every IAM write when the credential
store has a master client) carries the persisted identity proto. That
proto records inline policies only as flattened legacy Actions; their
runtime __inline_policy__ names live in the policy store and were
dropped by the merge. For the interval until the next full reload, an
identity whose policy denied e.g. DeleteObject routed to legacy
CanDo, where "Write:bucket" authorizes deletes.

Hydrate the pushed identity the same way a full load does, so the
merged state always carries its runtime policy names and documents.

* s3api: hydrate runtime policies on pushed group upserts

PutGroup propagation has the same gap as identity pushes: the wire
proto does not carry the group's runtime __inline_group_policy__ names,
so members' attached-policy check lost them until the next reload.

* s3api: attach only runtime inline policy names on pushes

Listing the pushed identity's or group's own inline policies is enough
to keep it on the attached-policy route; a whole-store hydration per
pushed proto is not. Pushes now never write iam.policies, so a stale
read can no longer overwrite a newer committed policy document.

* s3api: carry inline policy documents on pushed upserts

Attaching only runtime policy names left a window where a pushed identity or group referenced a document the local cache had never loaded, denying allowed requests until the next reload. Fetch the pushed entity own inline documents and publish them with the update; reads stay scoped to the pushed entity rather than the whole store, and inline runtime names are only ever populated from this store so a read cannot install a document older than the committed one. Resync the IAM manager only when a push actually installs documents.

* s3api: order pushed inline policy reads against their installs

A pushed UpsertIdentity or PutGroup read inline policy documents from
the credential store and installed them without holding the ordering
guard that full reloads already take, so a delayed push could overwrite
a newer committed document with its stale read and restore a revoked
grant until the next update or reload.

Pushes now hold reloadMu across the store read and the install, the same
snapshot-through-commit serialization the credential-manager load uses.
MergeS3ApiConfiguration also applies config.Policies to the live
iam.policies at commit instead of swapping in its earlier snapshot, so a
policy installed while a merge prepared (a pushed inline document or a
PutPolicy update) is no longer discarded.
This commit is contained in:
Chris Lu authored and GitHub committed 2026-10-17 15:25:00 -07:00
1 parent 74ed12d6b3
commit 44bb697495
2 files changed
+487 -26

No files matched your search

+118 -26
View File
@@ -113,8 +113,9 @@ type IdentityAccessManagement struct {
// reloadCh coalesces failed-reload retries handled by reloadRetryLoop
reloadCh chan struct{}
// reloadMu serializes configuration loads end-to-end (store snapshot
// through commit) so an older snapshot cannot overwrite a newer one
// reloadMu serializes configuration loads and pushes end-to-end (store
// snapshot through commit) so an older snapshot cannot overwrite a newer
// one
reloadMu sync.Mutex
}
@@ -1027,10 +1028,6 @@ func (iam *IdentityAccessManagement) MergeS3ApiConfiguration(config *iam_pb.S3Ap
for k, v := range iam.nameToIdentity {
nameToIdentity[k] = v
}
policies := make(map[string]*iam_pb.Policy)
for k, v := range iam.policies {
policies[k] = v
}
accounts := make(map[string]*Account)
for k, v := range iam.accounts {
accounts[k] = v
@@ -1246,22 +1243,6 @@ func (iam *IdentityAccessManagement) MergeS3ApiConfiguration(config *iam_pb.S3Ap
glog.V(3).Infof("Loaded service account %s for dynamic parent %s (expiration: %d)", sa.Id, sa.ParentUser, sa.Expiration)
}
for _, policy := range config.Policies {
policies[policy.Name] = policy
}
// full snapshot: drop dynamic policies the store no longer has
if isFullState {
presentPolicies := make(map[string]bool, len(config.Policies))
for _, policy := range config.Policies {
presentPolicies[policy.Name] = true
}
for name := range policies {
if !staticPolicies[name] && !presentPolicies[name] {
delete(policies, name)
}
}
}
iam.m.Lock()
// atomically switch
iam.identities = identities
@@ -1270,7 +1251,28 @@ func (iam *IdentityAccessManagement) MergeS3ApiConfiguration(config *iam_pb.S3Ap
iam.emailAccount = emailAccount
iam.nameToIdentity = nameToIdentity
iam.accessKeyIdent = accessKeyIdent
iam.policies = policies
// Apply policy changes to the live map rather than swapping in a snapshot
// taken before this merge prepared: a policy committed in the meantime (a
// pushed inline document or a PutPolicy update) must not be discarded.
if iam.policies == nil {
iam.policies = make(map[string]*iam_pb.Policy)
}
for _, policy := range config.Policies {
iam.policies[policy.Name] = policy
}
// full snapshot: drop dynamic policies the store no longer has
if isFullState {
presentPolicies := make(map[string]bool, len(config.Policies))
for _, policy := range config.Policies {
presentPolicies[policy.Name] = true
}
for name := range iam.policies {
if !staticPolicies[name] && !presentPolicies[name] {
delete(iam.policies, name)
}
}
}
// Groups: a full snapshot is authoritative even when empty (last group
// deleted); partial updates pass nil Groups and preserve existing state.
@@ -1366,9 +1368,51 @@ func (iam *IdentityAccessManagement) UpsertIdentity(ident *iam_pb.Identity) erro
return fmt.Errorf("upsert identity failed: empty identity name")
}
glog.V(1).Infof("IAM: upsert identity %s", ident.Name)
return iam.MergeS3ApiConfiguration(&iam_pb.S3ApiConfiguration{
// Serialize the store reads below with the merge that publishes them:
// reloadMu already gives full reloads the same snapshot-through-commit
// ordering, so a document read here cannot overwrite a newer copy that a
// concurrent push or reload committed in the meantime.
iam.reloadMu.Lock()
defer iam.reloadMu.Unlock()
config := &iam_pb.S3ApiConfiguration{
Identities: []*iam_pb.Identity{ident},
}, false, false)
}
// The wire proto lacks runtime inline policy names; merging it raw would
// drop them until the next reload and authorize by flattened Actions.
// Reattach the names and their documents, scoped to this identity: a name
// without its document would deny requests the policy allows until the
// next reload.
if iam.credentialManager != nil {
if store, ok := iam.credentialManager.GetStore().(credential.InlinePolicyStore); ok {
policyNames, err := store.ListUserInlinePolicies(context.Background(), ident.Name)
if err != nil {
return err
}
for _, policyName := range policyNames {
runtimeName := inlinePolicyRuntimeName(ident.Name, policyName)
ident.PolicyNames = appendUniquePolicyName(ident.PolicyNames, runtimeName)
doc, err := store.GetUserInlinePolicy(context.Background(), ident.Name, policyName)
if err != nil {
return err
}
if doc == nil {
continue
}
content, err := json.Marshal(doc)
if err != nil {
return fmt.Errorf("failed to marshal inline policy %q for user %q: %w", policyName, ident.Name, err)
}
config.Policies = append(config.Policies, &iam_pb.Policy{Name: runtimeName, Content: string(content)})
}
}
}
if err := iam.MergeS3ApiConfiguration(config, false, false); err != nil {
return err
}
if len(config.Policies) > 0 {
iam.resyncIAMManagerPolicies()
}
return nil
}
// isEnabled reports whether S3 auth should be enforced for this server.
@@ -3190,8 +3234,42 @@ func (iam *IdentityAccessManagement) PutGroup(group *iam_pb.Group) error {
}
glog.V(1).Infof("IAM: put group %s", group.Name)
// Serialize the store reads below with their installation under iam.m:
// the same reloadMu ordering that identity pushes and full reloads use,
// so a document read here cannot overwrite a newer committed copy.
iam.reloadMu.Lock()
defer iam.reloadMu.Unlock()
// As for identity pushes, the wire proto lacks the group's runtime
// inline policy names; reattach them with their documents, scoped to
// this group.
var inlinePolicies []*iam_pb.Policy
if iam.credentialManager != nil {
if store, ok := iam.credentialManager.GetStore().(credential.GroupInlinePolicyStore); ok {
policyNames, err := store.ListGroupInlinePolicies(context.Background(), group.Name)
if err != nil {
return err
}
for _, policyName := range policyNames {
runtimeName := inlineGroupPolicyRuntimeName(group.Name, policyName)
group.PolicyNames = appendUniquePolicyName(group.PolicyNames, runtimeName)
doc, err := store.GetGroupInlinePolicy(context.Background(), group.Name, policyName)
if err != nil {
return err
}
if doc == nil {
continue
}
content, err := json.Marshal(doc)
if err != nil {
return fmt.Errorf("failed to marshal inline policy %q for group %q: %w", policyName, group.Name, err)
}
inlinePolicies = append(inlinePolicies, &iam_pb.Policy{Name: runtimeName, Content: string(content)})
}
}
}
iam.m.Lock()
defer iam.m.Unlock()
// Remove old reverse index entries for this group
if old, ok := iam.groups[group.Name]; ok && !old.Disabled {
@@ -3209,6 +3287,20 @@ func (iam *IdentityAccessManagement) PutGroup(group *iam_pb.Group) error {
}
}
if len(inlinePolicies) > 0 {
if iam.policies == nil {
iam.policies = make(map[string]*iam_pb.Policy)
}
for _, policy := range inlinePolicies {
iam.policies[policy.Name] = policy
}
iam.rebuildIAMPolicyEngineLocked()
}
iam.m.Unlock()
if len(inlinePolicies) > 0 {
iam.resyncIAMManagerPolicies()
}
return nil
}