Files
seaweedfs/weed/s3api/auth_credentials_upsert_test.go
T
Chris LuandClaude Opus 5.5 d6d5e99f9b s3api: limit service account keys to their stored actions (#13325)
* s3api: limit service account keys to their stored actions

A service account created with an action subset authenticated as its
parent and was authorized with the parent's full permissions. Map each
restricted service account key to a copy of the parent that carries the
subset as a boundary, and check it in the S3, bucket listing and IAM
authorization paths. An empty action list still inherits the parent.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* s3api: drop restricted service account keys with their removed parent

RemoveIdentity only dropped keys that mapped to the parent itself, so a
restricted service account key, which maps to its own bounded copy, kept
authenticating after the parent was deleted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* sts: keep restricted service account keys out of their parent's roles

A named role is admitted by its trust policy alone, so a service account
key limited to Read could assume any role trusting its parent and use the
role's permissions. Require the key's action subset to allow
sts:AssumeRole.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* s3api: let a service account's scoped upload listing grant pass its boundary

The upload and part listing routes are authorized as Read, and the
legacy path falls back to the listing grant. The boundary check now does
the same, so a service account limited to ListBucketMultipartUploads can
still list uploads without being allowed object reads.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* sts: refuse self sessions for restricted service account keys

AssumeRole without a RoleArn and GetFederationToken issue sessions that
carry the caller's own policies, which for a service account are its
parent's. A restricted key would shed its action subset that way, so
refuse both for it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* sts: give the restricted service account role test its own role

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* s3api: check a restricted key's parent grants apart from its boundary

The boundary is checked against the request first, so the legacy action
check only has to cover the parent's grants. Checking the boundary there
again without the request denied a Read parent's upload listing to a key
limited to ListBucketMultipartUploads.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-02 23:50:44 -08:00

574 lines
23 KiB
Go

package s3api
import (
"context"
"net/http"
"net/http/httptest"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/gorilla/mux"
"github.com/seaweedfs/seaweedfs/weed/credential"
"github.com/seaweedfs/seaweedfs/weed/pb/iam_pb"
"github.com/seaweedfs/seaweedfs/weed/s3api/policy_engine"
s3_constants "github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
"github.com/seaweedfs/seaweedfs/weed/s3api/s3err"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// PutIdentity pushes carry the persisted identity proto, which records the
// inline policy flattened into Actions but not its runtime policy name. The
// upsert must keep the identity on the attached-policy route; otherwise its
// coarse "Write:bucket" action authorizes deletes the policy never granted.
// The push must also install the policy document itself: the inline policy
// here is never loaded by a full reload, so only the push can supply it.
func TestUpsertIdentityKeepsInlinePolicyAuthorization(t *testing.T) {
s3a := newTestS3ApiServerWithMemoryIAM(t, []*iam_pb.Identity{{
Name: "writer",
Credentials: []*iam_pb.Credential{{AccessKey: "AKW", SecretKey: "secret"}},
Actions: []string{"Read:bucket", "Write:bucket", "List:bucket"},
}})
ctx := context.Background()
var doc policy_engine.PolicyDocument
require.NoError(t, doc.UnmarshalJSON([]byte(`{
"Version":"2012-10-17",
"Statement":[{
"Effect":"Allow",
"Action":["s3:GetObject","s3:PutObject","s3:ListBucket"],
"Resource":["arn:aws:s3:::bucket","arn:aws:s3:::bucket/*"]
}]
}`)))
require.NoError(t, s3a.iam.credentialManager.PutUserInlinePolicy(ctx, "writer", "writer", doc))
pushed, err := s3a.iam.credentialManager.GetUser(ctx, "writer")
require.NoError(t, err)
require.NotContains(t, pushed.PolicyNames, inlinePolicyRuntimeName("writer", "writer"),
"persisted protos do not carry runtime inline policy names")
require.NoError(t, s3a.iam.UpsertIdentity(pushed))
ident := s3a.iam.lookupByIdentityName("writer")
require.NotNil(t, ident)
require.Contains(t, ident.PolicyNames, inlinePolicyRuntimeName("writer", "writer"))
_, err = s3a.iam.GetPolicy(inlinePolicyRuntimeName("writer", "writer"))
require.NoError(t, err, "the push must install the inline policy document, not just its name")
req, err := http.NewRequest(http.MethodGet, "http://s3/bucket/obj", nil)
require.NoError(t, err)
assert.Equal(t, s3err.ErrNone,
s3a.iam.VerifyActionPermission(req, ident, s3_constants.ACTION_READ, "bucket", "obj"),
"an allowed request must succeed through the pushed inline policy")
req, err = http.NewRequest(http.MethodDelete, "http://s3/bucket/obj", nil)
require.NoError(t, err)
assert.Equal(t, s3err.ErrAccessDenied,
s3a.iam.VerifyActionPermission(req, ident, s3_constants.ACTION_WRITE, "bucket", "obj"),
"an identity pushed without its inline policy must not fall back to legacy Actions")
}
// A pushed identity must also refresh a cached inline policy document that has
// since been replaced in the store; otherwise the stale allow keeps
// authorizing requests the new policy revoked.
func TestUpsertIdentityRefreshesInlinePolicyDocument(t *testing.T) {
s3a := newTestS3ApiServerWithMemoryIAM(t, []*iam_pb.Identity{{
Name: "writer",
Credentials: []*iam_pb.Credential{{AccessKey: "AKW", SecretKey: "secret"}},
Actions: []string{"Read:bucket", "Write:bucket", "List:bucket"},
}})
ctx := context.Background()
var doc policy_engine.PolicyDocument
require.NoError(t, doc.UnmarshalJSON([]byte(`{
"Version":"2012-10-17",
"Statement":[{"Effect":"Allow","Action":"s3:GetObject","Resource":"*"}]
}`)))
require.NoError(t, s3a.iam.credentialManager.PutUserInlinePolicy(ctx, "writer", "writer", doc))
require.NoError(t, s3a.iam.LoadS3ApiConfigurationFromCredentialManager())
ident := s3a.iam.lookupByIdentityName("writer")
require.NotNil(t, ident)
req, err := http.NewRequest(http.MethodGet, "http://s3/bucket/obj", nil)
require.NoError(t, err)
require.Equal(t, s3err.ErrNone,
s3a.iam.VerifyActionPermission(req, ident, s3_constants.ACTION_READ, "bucket", "obj"))
require.NoError(t, doc.UnmarshalJSON([]byte(`{
"Version":"2012-10-17",
"Statement":[{"Effect":"Allow","Action":"s3:ListBucket","Resource":"*"}]
}`)))
require.NoError(t, s3a.iam.credentialManager.PutUserInlinePolicy(ctx, "writer", "writer", doc))
pushed, err := s3a.iam.credentialManager.GetUser(ctx, "writer")
require.NoError(t, err)
require.NoError(t, s3a.iam.UpsertIdentity(pushed))
ident = s3a.iam.lookupByIdentityName("writer")
require.NotNil(t, ident)
assert.Equal(t, s3err.ErrAccessDenied,
s3a.iam.VerifyActionPermission(req, ident, s3_constants.ACTION_READ, "bucket", "obj"),
"a push must replace the stale cached document, not keep the old allow")
}
// The same applies to PutGroup pushes: the persisted group proto lacks the
// group's runtime inline policy names, and the inline document is never
// loaded by a full reload here, so only the push can supply it.
func TestPutGroupKeepsInlinePolicyAuthorization(t *testing.T) {
s3a := newTestS3ApiServerWithMemoryIAM(t, []*iam_pb.Identity{{Name: "alice"}})
ctx := context.Background()
require.NoError(t, s3a.iam.credentialManager.CreateGroup(ctx,
&iam_pb.Group{Name: "devs", Members: []string{"alice"}}))
var doc policy_engine.PolicyDocument
require.NoError(t, doc.UnmarshalJSON([]byte(`{
"Version":"2012-10-17",
"Statement":[{"Effect":"Allow","Action":"s3:GetObject","Resource":"*"}]
}`)))
require.NoError(t, s3a.iam.credentialManager.PutGroupInlinePolicy(ctx, "devs", "p", doc))
pushed, err := s3a.iam.credentialManager.GetGroup(ctx, "devs")
require.NoError(t, err)
require.NoError(t, s3a.iam.PutGroup(pushed))
s3a.iam.m.RLock()
require.Contains(t, s3a.iam.groups["devs"].PolicyNames, inlineGroupPolicyRuntimeName("devs", "p"))
s3a.iam.m.RUnlock()
_, err = s3a.iam.GetPolicy(inlineGroupPolicyRuntimeName("devs", "p"))
require.NoError(t, err, "the push must install the group's inline policy document")
ident := s3a.iam.lookupByIdentityName("alice")
require.NotNil(t, ident)
req, err := http.NewRequest(http.MethodGet, "http://s3/bucket/obj", nil)
require.NoError(t, err)
assert.Equal(t, s3err.ErrNone,
s3a.iam.VerifyActionPermission(req, ident, s3_constants.ACTION_READ, "bucket", "obj"),
"a pushed group must keep its inline policy grant active")
}
// pausingInlineStore pauses only the first user and first group inline policy
// Get, after capturing the document, so a test can interleave a newer write
// between a push's store read and the install of what it read. Later calls
// proceed immediately.
type pausingInlineStore struct {
credential.CredentialStore
userGets int32
userPaused chan struct{}
userRelease chan struct{}
groupGets int32
groupPaused chan struct{}
groupRelease chan struct{}
}
func (s *pausingInlineStore) userPolicies() credential.InlinePolicyStore {
return s.CredentialStore.(credential.InlinePolicyStore)
}
func (s *pausingInlineStore) groupPolicies() credential.GroupInlinePolicyStore {
return s.CredentialStore.(credential.GroupInlinePolicyStore)
}
func (s *pausingInlineStore) PutUserInlinePolicy(ctx context.Context, userName, policyName string, doc policy_engine.PolicyDocument) error {
return s.userPolicies().PutUserInlinePolicy(ctx, userName, policyName, doc)
}
func (s *pausingInlineStore) GetUserInlinePolicy(ctx context.Context, userName, policyName string) (*policy_engine.PolicyDocument, error) {
doc, err := s.userPolicies().GetUserInlinePolicy(ctx, userName, policyName)
if err != nil {
return nil, err
}
if atomic.AddInt32(&s.userGets, 1) == 1 {
close(s.userPaused)
<-s.userRelease
}
return doc, nil
}
func (s *pausingInlineStore) DeleteUserInlinePolicy(ctx context.Context, userName, policyName string) error {
return s.userPolicies().DeleteUserInlinePolicy(ctx, userName, policyName)
}
func (s *pausingInlineStore) ListUserInlinePolicies(ctx context.Context, userName string) ([]string, error) {
return s.userPolicies().ListUserInlinePolicies(ctx, userName)
}
func (s *pausingInlineStore) PutGroupInlinePolicy(ctx context.Context, groupName, policyName string, doc policy_engine.PolicyDocument) error {
return s.groupPolicies().PutGroupInlinePolicy(ctx, groupName, policyName, doc)
}
func (s *pausingInlineStore) GetGroupInlinePolicy(ctx context.Context, groupName, policyName string) (*policy_engine.PolicyDocument, error) {
doc, err := s.groupPolicies().GetGroupInlinePolicy(ctx, groupName, policyName)
if err != nil {
return nil, err
}
if atomic.AddInt32(&s.groupGets, 1) == 1 {
close(s.groupPaused)
<-s.groupRelease
}
return doc, nil
}
func (s *pausingInlineStore) DeleteGroupInlinePolicy(ctx context.Context, groupName, policyName string) error {
return s.groupPolicies().DeleteGroupInlinePolicy(ctx, groupName, policyName)
}
func (s *pausingInlineStore) ListGroupInlinePolicies(ctx context.Context, groupName string) ([]string, error) {
return s.groupPolicies().ListGroupInlinePolicies(ctx, groupName)
}
// A push paused after reading a permissive inline policy must not overwrite a
// tighter document a later push already installed: once the pause lifts, the
// stale read may still land, but the newer push must be the state that wins.
func TestUpsertIdentityRaceKeepsNewerInlinePolicy(t *testing.T) {
s3a := newTestS3ApiServerWithMemoryIAM(t, []*iam_pb.Identity{{
Name: "writer",
Credentials: []*iam_pb.Credential{{AccessKey: "AKW", SecretKey: "secret"}},
}})
ctx := context.Background()
pauser := &pausingInlineStore{
CredentialStore: s3a.iam.credentialManager.GetStore(),
userPaused: make(chan struct{}),
userRelease: make(chan struct{}),
}
s3a.iam.credentialManager.Store = pauser
var docV1 policy_engine.PolicyDocument
require.NoError(t, docV1.UnmarshalJSON([]byte(
`{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":"s3:GetObject","Resource":"*"}]}`)))
require.NoError(t, s3a.iam.credentialManager.PutUserInlinePolicy(ctx, "writer", "p", docV1))
pushed, err := s3a.iam.credentialManager.GetUser(ctx, "writer")
require.NoError(t, err)
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
assert.NoError(t, s3a.iam.UpsertIdentity(pushed))
}()
select {
case <-pauser.userPaused:
case <-time.After(5 * time.Second):
t.Fatal("first push did not reach the inline policy read")
}
// The administrator tightens the policy; its push must be the surviving one.
var docV2 policy_engine.PolicyDocument
require.NoError(t, docV2.UnmarshalJSON([]byte(
`{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":"s3:ListBucket","Resource":"*"}]}`)))
require.NoError(t, s3a.iam.credentialManager.PutUserInlinePolicy(ctx, "writer", "p", docV2))
wg.Add(1)
go func() {
defer wg.Done()
assert.NoError(t, s3a.iam.UpsertIdentity(pushed))
}()
// Give the second push a moment to install the newer document; with the
// ordering fix it instead waits for the first push and this poll expires.
deadline := time.Now().Add(500 * time.Millisecond)
for time.Now().Before(deadline) {
if p, err := s3a.iam.GetPolicy(inlinePolicyRuntimeName("writer", "p")); err == nil && strings.Contains(p.Content, "s3:ListBucket") {
break
}
time.Sleep(time.Millisecond)
}
close(pauser.userRelease)
wg.Wait()
installed, err := s3a.iam.GetPolicy(inlinePolicyRuntimeName("writer", "p"))
require.NoError(t, err)
assert.JSONEq(t,
`{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":"s3:ListBucket","Resource":"*"}]}`,
installed.Content, "the newer pushed document must win over the stale read")
ident := s3a.iam.lookupByIdentityName("writer")
require.NotNil(t, ident)
req, err := http.NewRequest(http.MethodGet, "http://s3/bucket/obj", nil)
require.NoError(t, err)
assert.Equal(t, s3err.ErrAccessDenied,
s3a.iam.VerifyActionPermission(req, ident, s3_constants.ACTION_READ, "bucket", "obj"),
"a stale push must not restore a grant the newer policy revoked")
}
// Same ordering guarantee for group pushes: a paused PutGroup must not
// overwrite a tighter inline document installed by a later push.
func TestPutGroupRaceKeepsNewerInlinePolicy(t *testing.T) {
s3a := newTestS3ApiServerWithMemoryIAM(t, []*iam_pb.Identity{{Name: "alice"}})
ctx := context.Background()
pauser := &pausingInlineStore{
CredentialStore: s3a.iam.credentialManager.GetStore(),
groupPaused: make(chan struct{}),
groupRelease: make(chan struct{}),
}
s3a.iam.credentialManager.Store = pauser
require.NoError(t, s3a.iam.credentialManager.CreateGroup(ctx,
&iam_pb.Group{Name: "devs", Members: []string{"alice"}}))
var docV1 policy_engine.PolicyDocument
require.NoError(t, docV1.UnmarshalJSON([]byte(
`{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":"s3:GetObject","Resource":"*"}]}`)))
require.NoError(t, s3a.iam.credentialManager.PutGroupInlinePolicy(ctx, "devs", "p", docV1))
pushed, err := s3a.iam.credentialManager.GetGroup(ctx, "devs")
require.NoError(t, err)
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
assert.NoError(t, s3a.iam.PutGroup(pushed))
}()
select {
case <-pauser.groupPaused:
case <-time.After(5 * time.Second):
t.Fatal("first group push did not reach the inline policy read")
}
var docV2 policy_engine.PolicyDocument
require.NoError(t, docV2.UnmarshalJSON([]byte(
`{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":"s3:ListBucket","Resource":"*"}]}`)))
require.NoError(t, s3a.iam.credentialManager.PutGroupInlinePolicy(ctx, "devs", "p", docV2))
wg.Add(1)
go func() {
defer wg.Done()
assert.NoError(t, s3a.iam.PutGroup(pushed))
}()
deadline := time.Now().Add(500 * time.Millisecond)
for time.Now().Before(deadline) {
if p, err := s3a.iam.GetPolicy(inlineGroupPolicyRuntimeName("devs", "p")); err == nil && strings.Contains(p.Content, "s3:ListBucket") {
break
}
time.Sleep(time.Millisecond)
}
close(pauser.groupRelease)
wg.Wait()
installed, err := s3a.iam.GetPolicy(inlineGroupPolicyRuntimeName("devs", "p"))
require.NoError(t, err)
assert.JSONEq(t,
`{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":"s3:ListBucket","Resource":"*"}]}`,
installed.Content, "the newer pushed group document must win over the stale read")
ident := s3a.iam.lookupByIdentityName("alice")
require.NotNil(t, ident)
req, err := http.NewRequest(http.MethodGet, "http://s3/bucket/obj", nil)
require.NoError(t, err)
assert.Equal(t, s3err.ErrAccessDenied,
s3a.iam.VerifyActionPermission(req, ident, s3_constants.ACTION_READ, "bucket", "obj"),
"a stale group push must not restore a grant the newer policy revoked")
}
// Service account changes push the parent identity, whose proto carries only
// the user's own keys. The upsert must rebuild the parent's service account
// keys from the store instead of dropping them.
func TestUpsertIdentityKeepsServiceAccountKeys(t *testing.T) {
s3a := newTestS3ApiServerWithMemoryIAM(t, []*iam_pb.Identity{{
Name: "pacs",
Credentials: []*iam_pb.Credential{{AccessKey: "AKPACS", SecretKey: "secret"}},
Actions: []string{"Read:bucket"},
}})
cm := s3a.iam.credentialManager
ctx := context.Background()
expiration := time.Now().Add(time.Hour).Unix()
newServiceAccount := func(id, accessKey string) *iam_pb.ServiceAccount {
return &iam_pb.ServiceAccount{
Id: id,
ParentUser: "pacs",
Credential: &iam_pb.Credential{AccessKey: accessKey, SecretKey: "sa-secret", Status: "Active"},
Expiration: expiration,
}
}
first := newServiceAccount("sa:pacs:1", "AKSA1")
require.NoError(t, cm.CreateServiceAccount(ctx, first))
parent, err := cm.GetUser(ctx, "pacs")
require.NoError(t, err)
require.NoError(t, s3a.iam.ReplaceS3ApiConfiguration(&iam_pb.S3ApiConfiguration{
Identities: []*iam_pb.Identity{parent},
ServiceAccounts: []*iam_pb.ServiceAccount{first},
}))
second := newServiceAccount("sa:pacs:2", "AKSA2")
require.NoError(t, cm.CreateServiceAccount(ctx, second))
pushed, err := cm.GetUser(ctx, "pacs")
require.NoError(t, err)
require.NoError(t, s3a.iam.UpsertIdentity(pushed))
for _, accessKey := range []string{"AKPACS", "AKSA1", "AKSA2"} {
ident, cred, found := s3a.iam.LookupByAccessKey(accessKey)
require.True(t, found, "access key %s must survive the parent push", accessKey)
assert.Equal(t, "pacs", ident.Name)
if accessKey != "AKPACS" {
assert.Equal(t, "sa-secret", cred.SecretKey)
assert.Equal(t, "Active", cred.Status)
assert.Equal(t, expiration, cred.Expiration)
}
}
require.NoError(t, cm.DeleteServiceAccount(ctx, first.Id))
pushed, err = cm.GetUser(ctx, "pacs")
require.NoError(t, err)
require.NoError(t, s3a.iam.UpsertIdentity(pushed))
_, _, found := s3a.iam.LookupByAccessKey("AKSA1")
assert.False(t, found, "a deleted service account must stop authenticating")
_, _, found = s3a.iam.LookupByAccessKey("AKSA2")
assert.True(t, found, "a sibling service account must survive another one's deletion")
disabled := newServiceAccount(second.Id, "AKSA2")
disabled.Disabled = true
require.NoError(t, cm.UpdateServiceAccount(ctx, second.Id, disabled))
pushed, err = cm.GetUser(ctx, "pacs")
require.NoError(t, err)
require.NoError(t, s3a.iam.UpsertIdentity(pushed))
_, _, found = s3a.iam.LookupByAccessKey("AKSA1")
assert.False(t, found)
_, _, found = s3a.iam.LookupByAccessKey("AKSA2")
assert.False(t, found, "a disabled service account must stop authenticating")
_, _, found = s3a.iam.LookupByAccessKey("AKPACS")
assert.True(t, found)
}
func TestServiceAccountActionsLimitParent(t *testing.T) {
parent := &iam_pb.Identity{
Name: "parent",
Credentials: []*iam_pb.Credential{{AccessKey: "AKPARENT", SecretKey: "parent-secret"}},
Actions: []string{"Admin"},
}
serviceAccount := &iam_pb.ServiceAccount{
Id: "sa:parent:restricted",
ParentUser: "parent",
Actions: []string{"Read"},
Credential: &iam_pb.Credential{AccessKey: "AKSERVICE", SecretKey: "service-secret", Status: "Active"},
}
s3a := newTestS3ApiServerWithMemoryIAM(t, []*iam_pb.Identity{parent})
require.NoError(t, s3a.iam.ReplaceS3ApiConfiguration(&iam_pb.S3ApiConfiguration{
Identities: []*iam_pb.Identity{parent},
ServiceAccounts: []*iam_pb.ServiceAccount{serviceAccount},
}))
writeRequest, err := http.NewRequest(http.MethodPut, "http://s3/bucket/object", nil)
require.NoError(t, err)
readRequest, err := http.NewRequest(http.MethodGet, "http://s3/bucket/object", nil)
require.NoError(t, err)
iamRequest, err := http.NewRequest(http.MethodPost, "http://iam/", nil)
require.NoError(t, err)
assertPermissions := func() {
resolved, _, found := s3a.iam.LookupByAccessKey("AKSERVICE")
require.True(t, found)
assert.Equal(t, "parent", resolved.Name)
assert.Equal(t, s3err.ErrAccessDenied,
s3a.iam.VerifyActionPermission(writeRequest, resolved, s3_constants.ACTION_WRITE, "bucket", "object"))
assert.Equal(t, s3err.ErrNone,
s3a.iam.VerifyActionPermission(readRequest, resolved, s3_constants.ACTION_READ, "bucket", "object"))
assert.Equal(t, s3err.ErrAccessDenied,
s3a.iam.AuthorizeIamAction(iamRequest, resolved, "CreateAccessKey", "parent"))
}
assertPermissions()
withPolicy := &iam_pb.Identity{
Name: parent.Name,
Credentials: parent.Credentials,
Actions: parent.Actions,
PolicyNames: []string{"ListBucketsOnly"},
}
require.NoError(t, s3a.iam.ReplaceS3ApiConfiguration(&iam_pb.S3ApiConfiguration{
Identities: []*iam_pb.Identity{withPolicy},
ServiceAccounts: []*iam_pb.ServiceAccount{serviceAccount},
Policies: []*iam_pb.Policy{{
Name: "ListBucketsOnly",
Content: `{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":"s3:ListAllMyBuckets","Resource":"*"}]}`,
}},
}))
assertPermissions()
s3a.iam.SetIAMIntegration(NewS3IAMIntegration(newTestIAMManager(t), ""))
assertPermissions()
ctx := context.Background()
require.NoError(t, s3a.iam.credentialManager.CreateServiceAccount(ctx, serviceAccount))
pushed, err := s3a.iam.credentialManager.GetUser(ctx, "parent")
require.NoError(t, err)
require.NoError(t, s3a.iam.UpsertIdentity(pushed))
assertPermissions()
inherited := &iam_pb.ServiceAccount{
Id: serviceAccount.Id,
ParentUser: serviceAccount.ParentUser,
Credential: serviceAccount.Credential,
}
require.NoError(t, s3a.iam.ReplaceS3ApiConfiguration(&iam_pb.S3ApiConfiguration{
Identities: []*iam_pb.Identity{parent},
ServiceAccounts: []*iam_pb.ServiceAccount{inherited},
}))
resolved, _, found := s3a.iam.LookupByAccessKey("AKSERVICE")
require.True(t, found)
assert.Equal(t, s3err.ErrNone,
s3a.iam.VerifyActionPermission(writeRequest, resolved, s3_constants.ACTION_WRITE, "bucket", "object"))
require.NoError(t, s3a.iam.ReplaceS3ApiConfiguration(&iam_pb.S3ApiConfiguration{
Identities: []*iam_pb.Identity{parent},
ServiceAccounts: []*iam_pb.ServiceAccount{serviceAccount},
}))
require.NoError(t, s3a.iam.MergeS3ApiConfiguration(&iam_pb.S3ApiConfiguration{}, false, true))
_, _, found = s3a.iam.LookupByAccessKey("AKSERVICE")
assert.False(t, found)
require.NoError(t, s3a.iam.ReplaceS3ApiConfiguration(&iam_pb.S3ApiConfiguration{
Identities: []*iam_pb.Identity{parent},
ServiceAccounts: []*iam_pb.ServiceAccount{serviceAccount},
}))
s3a.iam.RemoveIdentity("parent")
_, _, found = s3a.iam.LookupByAccessKey("AKSERVICE")
assert.False(t, found)
}
func TestServiceAccountActionsAllowScopedUploadListing(t *testing.T) {
for _, parentAction := range []string{"Admin", "Read"} {
t.Run(parentAction, func(t *testing.T) {
testServiceAccountScopedUploadListing(t, parentAction)
})
}
}
func testServiceAccountScopedUploadListing(t *testing.T, parentAction string) {
parent := &iam_pb.Identity{
Name: "parent",
Credentials: []*iam_pb.Credential{{AccessKey: "AKPARENT", SecretKey: "parent-secret"}},
Actions: []string{parentAction},
}
s3a := newTestS3ApiServerWithMemoryIAM(t, []*iam_pb.Identity{parent})
require.NoError(t, s3a.iam.ReplaceS3ApiConfiguration(&iam_pb.S3ApiConfiguration{
Identities: []*iam_pb.Identity{parent},
ServiceAccounts: []*iam_pb.ServiceAccount{{
Id: "sa:parent:uploads",
ParentUser: "parent",
Actions: []string{"ListBucketMultipartUploads:demo"},
Credential: &iam_pb.Credential{AccessKey: "AKSERVICE", SecretKey: "service-secret"},
}},
}))
resolved, _, found := s3a.iam.LookupByAccessKey("AKSERVICE")
require.True(t, found)
uploads := mux.SetURLVars(httptest.NewRequest(http.MethodGet, "/demo?uploads", nil), map[string]string{"bucket": "demo"})
assert.Equal(t, s3err.ErrNone, s3a.iam.VerifyActionPermission(uploads, resolved, s3_constants.ACTION_READ, "demo", ""))
assert.Equal(t, s3err.ErrNone, s3a.iam.authorizeRequest(uploads, resolved, s3_constants.ACTION_READ))
get := mux.SetURLVars(httptest.NewRequest(http.MethodGet, "/demo/key", nil), map[string]string{"bucket": "demo", "object": "key"})
assert.Equal(t, s3err.ErrAccessDenied, s3a.iam.VerifyActionPermission(get, resolved, s3_constants.ACTION_READ, "demo", "key"))
assert.Equal(t, s3err.ErrAccessDenied, s3a.iam.authorizeRequest(get, resolved, s3_constants.ACTION_READ))
}