Files
seaweedfs/weed/s3api/s3api_sts_stale_role_test.go
T
Chris LuandClaude Opus 5.5 04b2698db2 iam: end a role session's bucket-policy access with its role (#12968)
A bucket-policy Allow skips IsActionAllowed and only asks
IsPrincipalActionExplicitlyDenied for an overriding Deny. That check
validated the session token and its revocation but not the role ID the
session was issued for, so a session kept a bucket policy's grant after
its role was deleted or replaced by a role of the same name.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 12:49:48 -08:00

51 lines
2.0 KiB
Go

package s3api
import (
"context"
"net/http"
"testing"
"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
"github.com/seaweedfs/seaweedfs/weed/s3api/s3err"
)
// A bucket policy naming a role session grants it only while the session's
// role exists: once the role is deleted, or replaced by a role of the same
// name, the Allow must not keep the old session working.
func TestBucketPolicyAllowEndsWithTheSessionsRole(t *testing.T) {
ctx := context.Background()
manager, iam := newSharedRoleIAM(t)
iam.policyEngine = NewBucketPolicyEngine()
result := assumeSharedRole(t, manager, iam, sessionUserAccessKey, sessionUserSecretKey, "", "alice")
creds := result.Credentials
require.NoError(t, iam.policyEngine.engine.SetBucketPolicy("home", `{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Principal":{"AWS":"`+result.AssumedRoleUser.Arn+`"},"Action":"s3:GetObject","Resource":"arn:aws:s3:::home/*"}]}`))
get := func() s3err.ErrorCode {
t.Helper()
r, err := newTestRequest(http.MethodGet, "http://s3.seaweedfs.test/home/notes.txt", 0, nil)
require.NoError(t, err)
r.Header.Set("X-Amz-Security-Token", creds.SessionToken)
require.NoError(t, signRequestV4(r, creds.AccessKeyId, creds.SecretAccessKey))
r = mux.SetURLVars(r, map[string]string{"bucket": "home", "object": "notes.txt"})
_, errCode := iam.authRequest(r, s3_constants.ACTION_READ)
return errCode
}
require.Equal(t, s3err.ErrNone, get(), "precondition: the bucket policy grants the session")
original, err := manager.GetRole(ctx, "SharedRole")
require.NoError(t, err)
require.NoError(t, manager.DeleteRole(ctx, "SharedRole"))
assert.Equal(t, s3err.ErrAccessDenied, get(), "the session outlived its role's deletion")
recreated := *original
recreated.RoleId = ""
require.NoError(t, manager.CreateRole(ctx, "", "SharedRole", &recreated))
assert.Equal(t, s3err.ErrAccessDenied, get(), "a session of the deleted role works again under a new role of the same name")
}