mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 02:27:26 +01:00
iam: evaluate credential trust policies with the policy engine (#12895)
* iam: evaluate credential trust policies with the policy engine AssumeRoleWithCredentials and the Iceberg credential vending path checked a role's trust policy by scanning for a matching Allow statement only, so an explicit Deny never took effect. Evaluate the trust policy through EvaluateTrustPolicy, as AssumeRole and AssumeRoleWithWebIdentity do, so a matching Deny wins over an Allow. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * iam: keep the Federated provider match for credential trust Allows Matching Allow statements through the shared trust matcher let {"AWS":"*"}, "*" and empty principal objects admit credential-provider users, and made Allow conditions fail closed on keys this path does not supply, such as the vending duration. Keep the exact Federated provider check for Allow and use the policy engine only to find a matching Deny, with the requested session duration in the context. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
04050e781b
commit
8d4ec5252a
2 files changed
+114
No files matched your search
@@ -0,0 +1,93 @@
|
||||
package integration
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/seaweedfs/seaweedfs/weed/iam/policy"
|
||||
"github.com/seaweedfs/seaweedfs/weed/iam/providers"
|
||||
"github.com/seaweedfs/seaweedfs/weed/iam/sts"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestCredentialsTrustPolicyExplicitDeny(t *testing.T) {
|
||||
iamManager := setupIntegratedIAMSystem(t)
|
||||
ctx := context.Background()
|
||||
|
||||
statement := func(effect string, principal interface{}, condition map[string]map[string]interface{}) policy.Statement {
|
||||
return policy.Statement{
|
||||
Effect: effect,
|
||||
Principal: principal,
|
||||
Action: []string{"sts:AssumeRoleWithCredentials"},
|
||||
Condition: condition,
|
||||
}
|
||||
}
|
||||
federated := func(provider interface{}) map[string]interface{} {
|
||||
return map[string]interface{}{"Federated": provider}
|
||||
}
|
||||
allowLDAP := statement("Allow", federated("test-ldap"), nil)
|
||||
providerIs := func(provider string) map[string]map[string]interface{} {
|
||||
return map[string]map[string]interface{}{"StringEquals": {"aws:FederatedProvider": provider}}
|
||||
}
|
||||
longSession := map[string]map[string]interface{}{"NumericGreaterThan": {"sts:DurationSeconds": 3600}}
|
||||
|
||||
tests := []struct {
|
||||
role string
|
||||
statements []policy.Statement
|
||||
duration int64
|
||||
allow bool
|
||||
allowVend bool
|
||||
}{
|
||||
{"Allowed", []policy.Statement{allowLDAP}, 0, true, true},
|
||||
{"Revoked", []policy.Statement{allowLDAP, statement("Deny", federated("test-ldap"), nil)}, 0, false, false},
|
||||
{"RevokedByArray", []policy.Statement{allowLDAP, statement("Deny", federated([]interface{}{"other-ldap", "test-ldap"}), nil)}, 0, false, false},
|
||||
{"RevokedForEveryone", []policy.Statement{allowLDAP, statement("Deny", "*", nil)}, 0, false, false},
|
||||
{"RevokedByCondition", []policy.Statement{allowLDAP, statement("Deny", federated("*"), providerIs("test-ldap"))}, 0, false, false},
|
||||
{"OtherProviderRevoked", []policy.Statement{allowLDAP, statement("Deny", federated("*"), providerIs("other-ldap"))}, 0, true, true},
|
||||
{"LongSessionRevoked", []policy.Statement{allowLDAP, statement("Deny", federated("test-ldap"), longSession)}, 7200, false, true},
|
||||
{"ShortSessionKept", []policy.Statement{allowLDAP, statement("Deny", federated("test-ldap"), longSession)}, 1800, true, true},
|
||||
{"OtherProvider", []policy.Statement{statement("Allow", federated("other-ldap"), nil)}, 0, false, false},
|
||||
{"AWSWildcard", []policy.Statement{statement("Allow", map[string]interface{}{"AWS": "*"}, nil)}, 0, false, false},
|
||||
{"EmptyPrincipal", []policy.Statement{statement("Allow", map[string]interface{}{}, nil)}, 0, false, false},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.role, func(t *testing.T) {
|
||||
require.NoError(t, iamManager.CreateRole(ctx, "", tt.role, &RoleDefinition{
|
||||
RoleName: tt.role,
|
||||
TrustPolicy: &policy.PolicyDocument{Version: "2012-10-17", Statement: tt.statements},
|
||||
AttachedPolicies: []string{"LDAPUserPolicy"},
|
||||
}))
|
||||
roleArn := "arn:aws:iam::role/" + tt.role
|
||||
|
||||
request := &sts.AssumeRoleWithCredentialsRequest{
|
||||
RoleArn: roleArn,
|
||||
Username: "testuser",
|
||||
Password: "testpass",
|
||||
RoleSessionName: "ldap-session",
|
||||
ProviderName: "test-ldap",
|
||||
}
|
||||
if tt.duration > 0 {
|
||||
request.DurationSeconds = &tt.duration
|
||||
}
|
||||
response, err := iamManager.AssumeRoleWithCredentials(ctx, request)
|
||||
if tt.allow {
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, response)
|
||||
} else {
|
||||
assert.Error(t, err)
|
||||
assert.Nil(t, response)
|
||||
}
|
||||
|
||||
err = iamManager.ValidateTrustPolicyForCredentials(ctx, roleArn, &providers.ExternalIdentity{
|
||||
UserID: "testuser",
|
||||
Provider: "test-ldap",
|
||||
})
|
||||
if tt.allowVend {
|
||||
assert.NoError(t, err)
|
||||
} else {
|
||||
assert.Error(t, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -2022,6 +2022,27 @@ func (m *IAMManager) validateTrustPolicyForCredentials(ctx context.Context, role
|
||||
return fmt.Errorf("role has no trust policy")
|
||||
}
|
||||
|
||||
requestContext := map[string]interface{}{
|
||||
"aws:FederatedProvider": request.ProviderName,
|
||||
}
|
||||
if request.DurationSeconds != nil {
|
||||
requestContext["sts:DurationSeconds"] = *request.DurationSeconds
|
||||
}
|
||||
result, err := m.policyEngine.EvaluateTrustPolicy(ctx, roleDef.TrustPolicy, &policy.EvaluationContext{
|
||||
Principal: request.Username,
|
||||
Action: sts.ActionAssumeRoleWithCredentials,
|
||||
Resource: roleDef.RoleArn,
|
||||
RequestContext: requestContext,
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("trust policy evaluation failed: %w", err)
|
||||
}
|
||||
for _, match := range result.MatchingStatements {
|
||||
if match.Effect == policy.EffectDeny {
|
||||
return fmt.Errorf("trust policy denies credential assumption for provider: %s", request.ProviderName)
|
||||
}
|
||||
}
|
||||
|
||||
// Check if trust policy allows credential assumption for the specific provider
|
||||
for _, statement := range roleDef.TrustPolicy.Statement {
|
||||
if statement.Effect == "Allow" {
|
||||
|
||||
Reference in new issue
Block a user