mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-11-15 00:09:00 +01:00
* fix(s3api/iam): fail config snapshot on empty or malformed IAM files A full IAM reload reads every identity/policy/service-account/group file from the filer. When an external secrets tool rewrites a file, a reload that reads it mid-rewrite sees empty or partially-written content. The identity, policy and service-account loaders silently skipped such files (``continue``), so the snapshot was missing entries that still existed on disk. The atomic swap then installed an incomplete identity set while ``isAuthEnabled`` stayed on, denying unrelated clients mid-reload (#11259). The group loader and the read-error paths already fail the snapshot in this situation (a skipped entry reads as deleted). Apply the same behavior to empty content and unmarshal failures across the identity, policy, service-account and group loaders, so a transient mid-rewrite fails the reload (preserving the last known-good state) instead of silently dropping entries. * fix(s3api/iam): coalesce burst IAM config reloads through the reload queue onIamConfigChange did a full synchronous reload for every identity/policy file change event. When several independently-refreshing credentials rewrite their files within the same second, that produced a burst of dozens of back-to-back full reloads, each reading the whole store and widening the window where a mid-rewrite file is observed (#11259). Route every IAM config change through the existing coalescing reload queue (scheduleReload/reloadRetryLoop) instead. A burst of N events now collapses into a single reload (plus one tail reload for events that arrived while one was in flight). scheduleReload gains a reason argument for the existing log line; the reloadRetryLoop already retries failed reloads, so the per-event failure handoff is no longer needed. Tests that asserted on the synchronous reload now wire up the queue (centralized in newTestS3ApiServerWithMemoryIAM) and poll via waitForIdentity/waitForIdentityGone. Adds TestOnIamConfigChangeCoalescesBurstReloads showing 50 events coalesce into <=3 reloads. * fix(s3api/iam): skip non-JSON auxiliary files before failing IAM snapshot Per review: the multi-file loaders unmarshal every entry in an IAM directory, so a non-JSON auxiliary file (README, .DS_Store, a migration backup such as identity.json.old) would hit the new empty/malformed errors and reject the whole snapshot, blocking all later IAM reloads. Only *.json files are IAM objects (SeaweedFS writes identities, policies, service accounts and groups as <name>.json, and other call sites already gate on the .json suffix). Skip non-.json entries at the top of each loader loop, before reading content, so auxiliary files are ignored while empty/malformed .json files still fail the snapshot. Adds TestLoadConfigurationIgnoresNonJsonAuxiliaryFiles. * fix(s3api/iam): reject IAM files with empty identifiers and skip aux in listing Per review: - ListPolicyNames listed every regular entry in the policies directory as a policy name, including non-JSON auxiliary files, but GetPolicy cannot retrieve them. Apply the same .json suffix filter used by the loader so the list only exposes retrievable policies. - json.Unmarshal accepts `{}` and unknown fields. The identity and group loaders merge by the decoded Name (not the file name), so a `{}` file could install an empty-key record and displace a real one; the service-account loader accepted an empty Id. Validate Identity.Name, Group.Name and ServiceAccount.Id (via validateServiceAccountId) after unmarshal and fail the snapshot on empty identifiers. Adds TestFilerEtcStoreListPolicyNamesSkipsNonJsonAuxiliary and empty-identifier regression tests for identity, group and service-account files.
212 lines
6.1 KiB
Go
212 lines
6.1 KiB
Go
package filer_etc
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/credential"
|
|
"github.com/seaweedfs/seaweedfs/weed/filer"
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/iam_pb"
|
|
)
|
|
|
|
func validateServiceAccountId(id string) error {
|
|
return credential.ValidateServiceAccountId(id)
|
|
}
|
|
|
|
func (store *FilerEtcStore) loadServiceAccountsFromMultiFile(ctx context.Context, s3cfg *iam_pb.S3ApiConfiguration) error {
|
|
return store.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
|
|
dir := filer.IamConfigDirectory + "/" + IamServiceAccountsDirectory
|
|
entries, err := listEntries(ctx, client, dir)
|
|
if err != nil {
|
|
if err == filer_pb.ErrNotFound {
|
|
return nil
|
|
}
|
|
return err
|
|
}
|
|
|
|
for _, entry := range entries {
|
|
if entry.IsDirectory {
|
|
continue
|
|
}
|
|
if !strings.HasSuffix(entry.Name, ".json") {
|
|
continue
|
|
}
|
|
|
|
var content []byte
|
|
if len(entry.Content) > 0 {
|
|
content = entry.Content
|
|
} else {
|
|
c, err := filer.ReadInsideFiler(ctx, client, dir, entry.Name)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to read service account file %s: %w", entry.Name, err)
|
|
}
|
|
content = c
|
|
}
|
|
|
|
if len(content) == 0 {
|
|
return fmt.Errorf("service account file %s is empty", entry.Name)
|
|
}
|
|
sa := &iam_pb.ServiceAccount{}
|
|
if err := json.Unmarshal(content, sa); err != nil {
|
|
return fmt.Errorf("failed to unmarshal service account %s: %w", entry.Name, err)
|
|
}
|
|
if err := validateServiceAccountId(sa.Id); err != nil {
|
|
return fmt.Errorf("service account file %s: %w", entry.Name, err)
|
|
}
|
|
s3cfg.ServiceAccounts = append(s3cfg.ServiceAccounts, sa)
|
|
}
|
|
return nil
|
|
})
|
|
}
|
|
|
|
func (store *FilerEtcStore) saveServiceAccount(ctx context.Context, sa *iam_pb.ServiceAccount) error {
|
|
if sa == nil {
|
|
return fmt.Errorf("service account is nil")
|
|
}
|
|
if err := validateServiceAccountId(sa.Id); err != nil {
|
|
return err
|
|
}
|
|
return store.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
|
|
data, err := json.MarshalIndent(sa, "", " ")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return filer.SaveInsideFiler(context.Background(), client, filer.IamConfigDirectory+"/"+IamServiceAccountsDirectory, sa.Id+".json", data)
|
|
})
|
|
}
|
|
|
|
func (store *FilerEtcStore) deleteServiceAccount(ctx context.Context, saId string) error {
|
|
if err := validateServiceAccountId(saId); err != nil {
|
|
return err
|
|
}
|
|
return store.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
|
|
resp, err := client.DeleteEntry(ctx, &filer_pb.DeleteEntryRequest{
|
|
Directory: filer.IamConfigDirectory + "/" + IamServiceAccountsDirectory,
|
|
Name: saId + ".json",
|
|
})
|
|
if err != nil {
|
|
if strings.Contains(err.Error(), filer_pb.ErrNotFound.Error()) {
|
|
return credential.ErrServiceAccountNotFound
|
|
}
|
|
return err
|
|
}
|
|
if resp != nil && resp.Error != "" {
|
|
if strings.Contains(resp.Error, filer_pb.ErrNotFound.Error()) {
|
|
return credential.ErrServiceAccountNotFound
|
|
}
|
|
return fmt.Errorf("delete service account %s: %s", saId, resp.Error)
|
|
}
|
|
return nil
|
|
})
|
|
}
|
|
|
|
func (store *FilerEtcStore) CreateServiceAccount(ctx context.Context, sa *iam_pb.ServiceAccount) error {
|
|
existing, err := store.GetServiceAccount(ctx, sa.Id)
|
|
if err != nil {
|
|
if !errors.Is(err, credential.ErrServiceAccountNotFound) {
|
|
return err
|
|
}
|
|
} else if existing != nil {
|
|
return fmt.Errorf("service account %s already exists", sa.Id)
|
|
}
|
|
return store.saveServiceAccount(ctx, sa)
|
|
}
|
|
|
|
func (store *FilerEtcStore) UpdateServiceAccount(ctx context.Context, id string, sa *iam_pb.ServiceAccount) error {
|
|
if sa.Id != id {
|
|
return fmt.Errorf("service account ID mismatch")
|
|
}
|
|
_, err := store.GetServiceAccount(ctx, id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return store.saveServiceAccount(ctx, sa)
|
|
}
|
|
|
|
func (store *FilerEtcStore) DeleteServiceAccount(ctx context.Context, id string) error {
|
|
return store.deleteServiceAccount(ctx, id)
|
|
}
|
|
|
|
func (store *FilerEtcStore) GetServiceAccount(ctx context.Context, id string) (*iam_pb.ServiceAccount, error) {
|
|
if err := validateServiceAccountId(id); err != nil {
|
|
return nil, err
|
|
}
|
|
var sa *iam_pb.ServiceAccount
|
|
err := store.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
|
|
data, err := filer.ReadInsideFiler(ctx, client, filer.IamConfigDirectory+"/"+IamServiceAccountsDirectory, id+".json")
|
|
if err != nil {
|
|
if err == filer_pb.ErrNotFound {
|
|
return credential.ErrServiceAccountNotFound
|
|
}
|
|
return err
|
|
}
|
|
if len(data) == 0 {
|
|
return credential.ErrServiceAccountNotFound
|
|
}
|
|
sa = &iam_pb.ServiceAccount{}
|
|
return json.Unmarshal(data, sa)
|
|
})
|
|
return sa, err
|
|
}
|
|
|
|
func (store *FilerEtcStore) ListServiceAccounts(ctx context.Context) ([]*iam_pb.ServiceAccount, error) {
|
|
var accounts []*iam_pb.ServiceAccount
|
|
err := store.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
|
|
dir := filer.IamConfigDirectory + "/" + IamServiceAccountsDirectory
|
|
entries, err := listEntries(ctx, client, dir)
|
|
if err != nil {
|
|
if err == filer_pb.ErrNotFound {
|
|
return nil
|
|
}
|
|
return err
|
|
}
|
|
|
|
for _, entry := range entries {
|
|
if entry.IsDirectory {
|
|
continue
|
|
}
|
|
|
|
var content []byte
|
|
if len(entry.Content) > 0 {
|
|
content = entry.Content
|
|
} else {
|
|
c, err := filer.ReadInsideFiler(ctx, client, dir, entry.Name)
|
|
if err != nil {
|
|
glog.Warningf("Failed to read service account file %s: %v", entry.Name, err)
|
|
continue
|
|
}
|
|
content = c
|
|
}
|
|
|
|
if len(content) > 0 {
|
|
sa := &iam_pb.ServiceAccount{}
|
|
if err := json.Unmarshal(content, sa); err != nil {
|
|
glog.Warningf("Failed to unmarshal service account %s: %v", entry.Name, err)
|
|
continue
|
|
}
|
|
accounts = append(accounts, sa)
|
|
}
|
|
}
|
|
return nil
|
|
})
|
|
return accounts, err
|
|
}
|
|
|
|
func (store *FilerEtcStore) GetServiceAccountByAccessKey(ctx context.Context, accessKey string) (*iam_pb.ServiceAccount, error) {
|
|
accounts, err := store.ListServiceAccounts(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for _, sa := range accounts {
|
|
if sa.Credential != nil && sa.Credential.AccessKey == accessKey {
|
|
return sa, nil
|
|
}
|
|
}
|
|
return nil, credential.ErrAccessKeyNotFound
|
|
}
|