Files
s3/weed/s3api/auth_credentials_policy_sync_test.go
zooqueen dd74d3e6d6 Replace AGPL MinIO fork with Apache-2.0 SeaweedFS fork; rebrand CLI weed->s3
hanzoai/s3 was an AGPL-3.0 MinIO fork whose network-copyleft blocks embedding
and reselling as a managed service. Replace it wholesale with a fork of
SeaweedFS (github.com/seaweedfs/seaweedfs, Apache-2.0, Go) which ships a
built-in S3 gateway + Filer.

- License: AGPL-3.0 -> Apache-2.0 (upstream LICENSE kept verbatim, incl.
  "Copyright 2025 Chris Lu"); NOTICE added crediting SeaweedFS. No AGPL/BUSL/
  SSPL/GPL or enterprise ("ee") files remain.
- Module: go.mod path set to github.com/hanzoai/s3 with a self-replace
  (replace github.com/seaweedfs/seaweedfs => ./) so the ~1800 internal Go files
  keep their upstream import paths with zero churn. Internal identifiers and
  import paths are intentionally NOT renamed.
- CLI rebrand: the binary is built as `s3` (was `weed`); root usage, per-command
  help/usage/flag strings, shell autocomplete program name, and the product name
  in CLI output now read "Hanzo S3" / `s3`. Users never see `weed`.
  (e.g. `s3 server`, `s3 filer`, `s3 master`, `s3 volume`, `s3 s3`, `s3 mount`.)
- Build: `make` (or `go build -o s3 ./weed`) builds the `s3` binary green;
  `cmd/weed-db` and `cmd/weed-sql` also build; `go vet ./weed/command/` clean.
- README/LLM.md rewritten for Hanzo S3; AGENTS.md/CLAUDE.md symlinks preserved.
- Dropped a 33MB prebuilt Mach-O `stress` binary upstream had committed; `.gitignore`
  updated to ignore the `s3` build output.
2026-06-18 23:41:25 -07:00

159 lines
5.8 KiB
Go

package s3api
import (
"context"
"encoding/json"
"testing"
"time"
"github.com/seaweedfs/seaweedfs/weed/iam/integration"
"github.com/seaweedfs/seaweedfs/weed/iam/policy"
"github.com/seaweedfs/seaweedfs/weed/iam/sts"
"github.com/seaweedfs/seaweedfs/weed/pb/iam_pb"
"github.com/stretchr/testify/require"
)
func newTestIAMManager(t *testing.T) *integration.IAMManager {
t.Helper()
mgr := integration.NewIAMManager()
require.NoError(t, mgr.Initialize(&integration.IAMConfig{
STS: &sts.STSConfig{
TokenDuration: sts.FlexibleDuration{Duration: time.Hour},
MaxSessionLength: sts.FlexibleDuration{Duration: 12 * time.Hour},
Issuer: "test",
SigningKey: []byte("test-signing-key-32-characters!!"),
AccountId: "111122223333",
},
Policy: &policy.PolicyEngineConfig{DefaultEffect: "Allow", StoreType: "memory"},
Roles: &integration.RoleStoreConfig{StoreType: "memory"},
}, func() string { return "localhost:8888" }))
return mgr
}
func TestPutPolicy_SyncsToIAMManager(t *testing.T) {
mgr := newTestIAMManager(t)
iam := &IdentityAccessManagement{}
iam.SetIAMIntegration(NewS3IAMIntegration(mgr, ""))
policyDoc, _ := json.Marshal(map[string]interface{}{
"Version": "2012-10-17",
"Statement": []map[string]interface{}{
{"Effect": "Allow", "Action": "s3:*", "Resource": "arn:aws:s3:::backup/*"},
},
})
require.NoError(t, iam.PutPolicy("BackupAll", string(policyDoc)))
allowed, err := mgr.IsActionAllowed(context.Background(), &integration.ActionRequest{
Principal: "arn:aws:iam::111122223333:user/test",
Action: "s3:PutObject",
Resource: "arn:aws:s3:::backup/file.txt",
PolicyNames: []string{"BackupAll"},
})
require.NoError(t, err)
require.True(t, allowed, "PutPolicy should sync to IAM Manager policy engine")
}
func TestDeletePolicy_SyncsToIAMManager(t *testing.T) {
mgr := newTestIAMManager(t)
iam := &IdentityAccessManagement{}
iam.SetIAMIntegration(NewS3IAMIntegration(mgr, ""))
policyDoc, _ := json.Marshal(map[string]interface{}{
"Version": "2012-10-17",
"Statement": []map[string]interface{}{
{"Effect": "Allow", "Action": "s3:*", "Resource": "arn:aws:s3:::backup/*"},
},
})
require.NoError(t, iam.PutPolicy("BackupAll", string(policyDoc)))
require.NoError(t, iam.DeletePolicy("BackupAll"))
allowed, err := mgr.IsActionAllowed(context.Background(), &integration.ActionRequest{
Principal: "arn:aws:iam::111122223333:user/test",
Action: "s3:PutObject",
Resource: "arn:aws:s3:::backup/file.txt",
PolicyNames: []string{"BackupAll"},
})
require.NoError(t, err)
require.False(t, allowed, "DeletePolicy should remove from IAM Manager policy engine")
}
// TestSetIAMIntegration_FlushesLoadedPolicies reproduces the startup race: a
// policy is loaded into the IAM cache before the integration is attached (so the
// sync was skipped), then SetIAMIntegration must flush it into the manager's
// engine. Without the flush, policy_names identities get AccessDenied until an
// external IAM change triggers a reload.
func TestSetIAMIntegration_FlushesLoadedPolicies(t *testing.T) {
iam := &IdentityAccessManagement{}
policyDoc, _ := json.Marshal(map[string]interface{}{
"Version": "2012-10-17",
"Statement": []map[string]interface{}{
{"Effect": "Allow", "Action": "s3:*", "Resource": "arn:aws:s3:::backup/*"},
},
})
// Integration not attached yet, so this only lands in the legacy engine.
require.NoError(t, iam.PutPolicy("BackupAll", string(policyDoc)))
mgr := newTestIAMManager(t)
iam.SetIAMIntegration(NewS3IAMIntegration(mgr, ""))
allowed, err := mgr.IsActionAllowed(context.Background(), &integration.ActionRequest{
Principal: "arn:aws:iam::111122223333:user/test",
Action: "s3:PutObject",
Resource: "arn:aws:s3:::backup/file.txt",
PolicyNames: []string{"BackupAll"},
})
require.NoError(t, err)
require.True(t, allowed, "SetIAMIntegration should flush already-loaded policies into the IAM Manager")
}
// TestResyncIAMManager_ReflectsCurrentPolicies pins the property that prevents
// the stale-snapshot race: SyncRuntimePolicies replaces the full desired set, so
// the resync must derive that set from the live iam.policies map at apply time,
// not from a view captured earlier. Here the map is mutated out from under an
// already-synced policy, and the next resync must converge the manager onto the
// new map rather than reinstating the old state.
func TestResyncIAMManager_ReflectsCurrentPolicies(t *testing.T) {
mgr := newTestIAMManager(t)
iam := &IdentityAccessManagement{}
iam.SetIAMIntegration(NewS3IAMIntegration(mgr, ""))
doc := func(name string) string {
b, _ := json.Marshal(map[string]interface{}{
"Version": "2012-10-17",
"Statement": []map[string]interface{}{
{"Effect": "Allow", "Action": "s3:*", "Resource": "arn:aws:s3:::" + name + "/*"},
},
})
return string(b)
}
allows := func(name string) bool {
allowed, err := mgr.IsActionAllowed(context.Background(), &integration.ActionRequest{
Principal: "arn:aws:iam::111122223333:user/test",
Action: "s3:PutObject",
Resource: "arn:aws:s3:::" + name + "/file.txt",
PolicyNames: []string{name},
})
require.NoError(t, err)
return allowed
}
require.NoError(t, iam.PutPolicy("alpha", doc("alpha")))
require.True(t, allows("alpha"))
// Swap the map contents without going through PutPolicy/DeletePolicy, then
// resync. A snapshot-based sync would push a pre-swap view; the fresh-read
// resync must mirror the current map: alpha gone, beta present.
iam.m.Lock()
delete(iam.policies, "alpha")
iam.policies["beta"] = &iam_pb.Policy{Name: "beta", Content: doc("beta")}
iam.m.Unlock()
iam.resyncIAMManagerPolicies()
require.False(t, allows("alpha"), "resync should drop a policy no longer in the map")
require.True(t, allows("beta"), "resync should add a policy newly in the map")
}