From 8d4ec5252ad5deacecfe1bf68dd5ce8b2206a19a Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Sun, 22 Nov 2026 10:20:57 -0800 Subject: [PATCH] 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 * 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 --------- Co-authored-by: Claude Opus 5.5 --- .../credentials_trust_policy_test.go | 93 +++++++++++++++++++ weed/iam/integration/iam_manager.go | 21 +++++ 2 files changed, 114 insertions(+) create mode 100644 weed/iam/integration/credentials_trust_policy_test.go diff --git a/weed/iam/integration/credentials_trust_policy_test.go b/weed/iam/integration/credentials_trust_policy_test.go new file mode 100644 index 0000000000..cef755490e --- /dev/null +++ b/weed/iam/integration/credentials_trust_policy_test.go @@ -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) + } + }) + } +} diff --git a/weed/iam/integration/iam_manager.go b/weed/iam/integration/iam_manager.go index 0ea4a32b2c..31df18ce08 100644 --- a/weed/iam/integration/iam_manager.go +++ b/weed/iam/integration/iam_manager.go @@ -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" {