mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-08-01 21:06:33 +00:00
A config-file reload (SIGHUP) routed through MergeS3ApiConfiguration, which skips identities marked static so dynamic admin/filer updates can't clobber them. That also blocked the config file itself from updating its own identities, so editing a secretKey and reloading had no effect. Thread a fromStaticFile flag from the file-load path into the merge: the authoritative file overwrites its static identities (and reapplies service accounts under them), while dynamic updates still leave them immutable. Mark the rebuilt identities static in the merge so a concurrent RemoveIdentity never observes them as removable mid-reload.
184 lines
7.9 KiB
Go
184 lines
7.9 KiB
Go
package s3api
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
_ "github.com/seaweedfs/seaweedfs/weed/credential/memory"
|
|
"github.com/seaweedfs/seaweedfs/weed/filer"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/iam_pb"
|
|
)
|
|
|
|
// An advanced -iam.config file (STS/OIDC/roles) carries no inline identities, so the
|
|
// server must not enter static-config mode. Otherwise it freezes live reloads and
|
|
// filer-backed identities created at runtime (e.g. by the operator's IAM CRDs) never
|
|
// take effect.
|
|
func TestIamConfigWithoutIdentitiesIsNotStatic(t *testing.T) {
|
|
s3a := newTestS3ApiServerWithMemoryIAM(t, []*iam_pb.Identity{})
|
|
|
|
path := writeTempIamConfig(t, `{"sts":{"signingKey":"dGVzdC1zaWduaW5nLWtleQ=="}}`)
|
|
if err := s3a.iam.loadS3ApiConfigurationFromFile(path); err != nil {
|
|
t.Fatalf("failed to load advanced iam config: %v", err)
|
|
}
|
|
|
|
if s3a.iam.IsStaticConfig() {
|
|
t.Fatalf("advanced iam config without identities must not be treated as static")
|
|
}
|
|
|
|
// A filer change (operator creating a user) must still reload at runtime.
|
|
if err := s3a.iam.credentialManager.CreateUser(context.Background(), &iam_pb.Identity{Name: "alice"}); err != nil {
|
|
t.Fatalf("failed to create alice: %v", err)
|
|
}
|
|
if err := s3a.onIamConfigChange(filer.IamConfigDirectory+"/identities", nil, &filer_pb.Entry{Name: "alice.json"}); err != nil {
|
|
t.Fatalf("onIamConfigChange returned error: %v", err)
|
|
}
|
|
if !hasIdentity(s3a.iam, "alice") {
|
|
t.Fatalf("expected alice to load after filer change with -iam.config-only setup")
|
|
}
|
|
}
|
|
|
|
// A -config identity file marks its identities static, protecting them and keeping the
|
|
// established behavior of not live-reloading those from the filer.
|
|
func TestConfigWithIdentitiesIsStatic(t *testing.T) {
|
|
s3a := newTestS3ApiServerWithMemoryIAM(t, []*iam_pb.Identity{})
|
|
|
|
path := writeTempIamConfig(t, `{"identities":[{"name":"static-admin","credentials":[{"accessKey":"AKIAITEST","secretKey":"c2VjcmV0"}],"actions":["Admin"]}]}`)
|
|
if err := s3a.iam.loadS3ApiConfigurationFromFile(path); err != nil {
|
|
t.Fatalf("failed to load identity config: %v", err)
|
|
}
|
|
|
|
if !s3a.iam.IsStaticConfig() {
|
|
t.Fatalf("config file with inline identities must be treated as static")
|
|
}
|
|
|
|
s3a.iam.m.RLock()
|
|
id := s3a.iam.nameToIdentity["static-admin"]
|
|
s3a.iam.m.RUnlock()
|
|
if id == nil || !id.IsStatic {
|
|
t.Fatalf("expected static-admin to be marked static")
|
|
}
|
|
|
|
// A static identity file does not live-reload dynamic identities from the filer.
|
|
if err := s3a.iam.credentialManager.CreateUser(context.Background(), &iam_pb.Identity{Name: "alice"}); err != nil {
|
|
t.Fatalf("failed to create alice: %v", err)
|
|
}
|
|
if err := s3a.onIamConfigChange(filer.IamConfigDirectory+"/identities", nil, &filer_pb.Entry{Name: "alice.json"}); err != nil {
|
|
t.Fatalf("onIamConfigChange returned error: %v", err)
|
|
}
|
|
if hasIdentity(s3a.iam, "alice") {
|
|
t.Fatalf("did not expect alice to load while running off a static identity file")
|
|
}
|
|
}
|
|
|
|
// Reloading the static config file (grace.OnReload) must mark newly added
|
|
// identities as static so dynamic filer updates can't overwrite them, while
|
|
// leaving already-loaded dynamic (filer-managed) identities untouched.
|
|
func TestReloadStaticConfigMarksNewIdentitiesWithoutFreezingDynamic(t *testing.T) {
|
|
s3a := newTestS3ApiServerWithMemoryIAM(t, []*iam_pb.Identity{})
|
|
|
|
p1 := writeTempIamConfig(t, `{"identities":[{"name":"static-admin","credentials":[{"accessKey":"AKADMIN0","secretKey":"c2VjcmV0"}],"actions":["Admin"]}]}`)
|
|
if err := s3a.iam.loadS3ApiConfigurationFromFile(p1); err != nil {
|
|
t.Fatalf("failed to load initial config: %v", err)
|
|
}
|
|
|
|
// A dynamic identity arrives from the filer; merge mode keeps it dynamic.
|
|
if err := s3a.iam.credentialManager.CreateUser(context.Background(), &iam_pb.Identity{Name: "alice"}); err != nil {
|
|
t.Fatalf("failed to create alice: %v", err)
|
|
}
|
|
if err := s3a.iam.LoadS3ApiConfigurationFromCredentialManager(); err != nil {
|
|
t.Fatalf("failed to load from credential manager: %v", err)
|
|
}
|
|
if !hasIdentity(s3a.iam, "alice") {
|
|
t.Fatalf("expected alice to load dynamically")
|
|
}
|
|
|
|
// Reload the static file with a new identity bob.
|
|
p2 := writeTempIamConfig(t, `{"identities":[{"name":"static-admin","credentials":[{"accessKey":"AKADMIN0","secretKey":"c2VjcmV0"}],"actions":["Admin"]},{"name":"bob","credentials":[{"accessKey":"AKBOB000","secretKey":"c2VjcmV0"}],"actions":["Read"]}]}`)
|
|
if err := s3a.iam.loadS3ApiConfigurationFromFile(p2); err != nil {
|
|
t.Fatalf("failed to reload config: %v", err)
|
|
}
|
|
|
|
if !isStaticName(s3a.iam, "bob") {
|
|
t.Fatalf("expected reloaded identity bob to be marked static")
|
|
}
|
|
if isStaticName(s3a.iam, "alice") {
|
|
t.Fatalf("dynamic identity alice must not be frozen as static by a config reload")
|
|
}
|
|
}
|
|
|
|
// A config-file reload must apply an edited secretKey to its static identity.
|
|
func TestReloadStaticConfigUpdatesExistingSecretKey(t *testing.T) {
|
|
s3a := newTestS3ApiServerWithMemoryIAM(t, []*iam_pb.Identity{})
|
|
|
|
p1 := writeTempIamConfig(t, `{"identities":[{"name":"static-admin","credentials":[{"accessKey":"AKADMIN0","secretKey":"b2xkc2VjcmV0"}],"actions":["Admin"]}]}`)
|
|
if err := s3a.iam.loadS3ApiConfigurationFromFile(p1); err != nil {
|
|
t.Fatalf("failed to load initial config: %v", err)
|
|
}
|
|
|
|
_, cred, found := s3a.iam.lookupByAccessKey("AKADMIN0")
|
|
if !found || cred.SecretKey != "b2xkc2VjcmV0" {
|
|
t.Fatalf("expected initial secretKey to load, got found=%v cred=%+v", found, cred)
|
|
}
|
|
|
|
// Rotate the secretKey in the file and reload.
|
|
p2 := writeTempIamConfig(t, `{"identities":[{"name":"static-admin","credentials":[{"accessKey":"AKADMIN0","secretKey":"bmV3c2VjcmV0"}],"actions":["Admin"]}]}`)
|
|
if err := s3a.iam.loadS3ApiConfigurationFromFile(p2); err != nil {
|
|
t.Fatalf("failed to reload config: %v", err)
|
|
}
|
|
|
|
_, cred, found = s3a.iam.lookupByAccessKey("AKADMIN0")
|
|
if !found {
|
|
t.Fatalf("static-admin access key disappeared after reload")
|
|
}
|
|
if cred.SecretKey != "bmV3c2VjcmV0" {
|
|
t.Fatalf("expected reloaded secretKey bmV3c2VjcmV0, got %q", cred.SecretKey)
|
|
}
|
|
if !isStaticName(s3a.iam, "static-admin") {
|
|
t.Fatalf("static-admin must stay marked static after reload")
|
|
}
|
|
}
|
|
|
|
// A reload must also reapply a service-account credential under a static parent.
|
|
func TestReloadStaticConfigUpdatesServiceAccountSecret(t *testing.T) {
|
|
s3a := newTestS3ApiServerWithMemoryIAM(t, []*iam_pb.Identity{})
|
|
|
|
p1 := writeTempIamConfig(t, `{"identities":[{"name":"static-admin","credentials":[{"accessKey":"AKADMIN0","secretKey":"YWRtaW4="}],"actions":["Admin"]}],"serviceAccounts":[{"id":"sa-1","parentUser":"static-admin","credential":{"accessKey":"AKSA0001","secretKey":"b2xkc2E="}}]}`)
|
|
if err := s3a.iam.loadS3ApiConfigurationFromFile(p1); err != nil {
|
|
t.Fatalf("failed to load initial config: %v", err)
|
|
}
|
|
if _, cred, found := s3a.iam.lookupByAccessKey("AKSA0001"); !found || cred.SecretKey != "b2xkc2E=" {
|
|
t.Fatalf("expected service account secret to load, got found=%v cred=%+v", found, cred)
|
|
}
|
|
|
|
// Rotate the service account secret in the file and reload.
|
|
p2 := writeTempIamConfig(t, `{"identities":[{"name":"static-admin","credentials":[{"accessKey":"AKADMIN0","secretKey":"YWRtaW4="}],"actions":["Admin"]}],"serviceAccounts":[{"id":"sa-1","parentUser":"static-admin","credential":{"accessKey":"AKSA0001","secretKey":"bmV3c2E="}}]}`)
|
|
if err := s3a.iam.loadS3ApiConfigurationFromFile(p2); err != nil {
|
|
t.Fatalf("failed to reload config: %v", err)
|
|
}
|
|
_, cred, found := s3a.iam.lookupByAccessKey("AKSA0001")
|
|
if !found {
|
|
t.Fatalf("service account access key disappeared after reload")
|
|
}
|
|
if cred.SecretKey != "bmV3c2E=" {
|
|
t.Fatalf("expected reloaded service account secret bmV3c2E=, got %q", cred.SecretKey)
|
|
}
|
|
}
|
|
|
|
func isStaticName(iam *IdentityAccessManagement, name string) bool {
|
|
iam.m.RLock()
|
|
defer iam.m.RUnlock()
|
|
return iam.staticIdentityNames[name]
|
|
}
|
|
|
|
func writeTempIamConfig(t *testing.T, content string) string {
|
|
t.Helper()
|
|
path := filepath.Join(t.TempDir(), "iam.json")
|
|
if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
|
|
t.Fatalf("failed to write temp config: %v", err)
|
|
}
|
|
return path
|
|
}
|