mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-11-20 03:13:53 +01:00
s3api: evaluate request condition keys for STS sessions (#12762)
* s3api: build request condition keys in one helper Split the keys ExtractConditionValuesFromRequest reads from the request itself into RequestConditionValues, keeping aws:SourceIp and the bucket-listing keys in the method. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3api: evaluate request condition keys for STS sessions The IAM integration context for STS and OIDC sessions carried only the source IP, legacy user agent/referer keys and claims, so role and session policy conditions on s3:x-amz-*, aws:SecureTransport, aws:CurrentTime, s3:RequestObjectTag/* and the other request keys never matched: a Deny on them never fired, and a negated Deny always did. Add the same request keys the static identity path evaluates. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3api: report SigV4 presigned requests as REST-QUERY-STRING s3:authType recognized only SigV2 query authentication, so a policy condition on REST-QUERY-STRING never matched a SigV4 presigned URL. 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
2089abecf3
commit
f88751c3ff
4 files changed
+181
-16
No files matched your search
@@ -416,10 +416,33 @@ func ExtractPrincipalVariables(principal string) map[string][]string {
|
||||
|
||||
// ExtractConditionValuesFromRequest extracts condition values from HTTP request
|
||||
func (engine *PolicyEngine) ExtractConditionValuesFromRequest(r *http.Request) map[string][]string {
|
||||
values := RequestConditionValues(r)
|
||||
values["aws:SourceIp"] = []string{engine.extractSourceIP(r)}
|
||||
|
||||
// S3 bucket-level conditions
|
||||
if delimiter := r.URL.Query().Get("delimiter"); delimiter != "" {
|
||||
values["s3:delimiter"] = []string{delimiter}
|
||||
}
|
||||
|
||||
if prefix := r.URL.Query().Get("prefix"); prefix != "" {
|
||||
values["s3:prefix"] = []string{prefix}
|
||||
}
|
||||
|
||||
// Multipart listings use max-uploads, so max-keys cannot constrain them.
|
||||
if maxKeys := r.URL.Query().Get("max-keys"); maxKeys != "" && !r.URL.Query().Has("uploads") {
|
||||
values["s3:max-keys"] = []string{maxKeys}
|
||||
}
|
||||
|
||||
return values
|
||||
}
|
||||
|
||||
// RequestConditionValues returns the condition keys read from the request
|
||||
// itself, leaving out aws:SourceIp and the bucket-listing keys, which depend
|
||||
// on the caller's proxy and listing handling.
|
||||
func RequestConditionValues(r *http.Request) map[string][]string {
|
||||
values := make(map[string][]string)
|
||||
|
||||
// AWS condition keys
|
||||
values["aws:SourceIp"] = []string{engine.extractSourceIP(r)}
|
||||
values["aws:SecureTransport"] = []string{fmt.Sprintf("%t", r.TLS != nil)}
|
||||
// Use AWS standard condition key for current time
|
||||
values["aws:CurrentTime"] = []string{time.Now().Format(time.RFC3339)}
|
||||
@@ -445,20 +468,6 @@ func (engine *PolicyEngine) ExtractConditionValuesFromRequest(r *http.Request) m
|
||||
values[RequestObjectTagKeys] = slices.Sorted(maps.Keys(tags))
|
||||
}
|
||||
|
||||
// S3 bucket-level conditions
|
||||
if delimiter := r.URL.Query().Get("delimiter"); delimiter != "" {
|
||||
values["s3:delimiter"] = []string{delimiter}
|
||||
}
|
||||
|
||||
if prefix := r.URL.Query().Get("prefix"); prefix != "" {
|
||||
values["s3:prefix"] = []string{prefix}
|
||||
}
|
||||
|
||||
// Multipart listings use max-uploads, so max-keys cannot constrain them.
|
||||
if maxKeys := r.URL.Query().Get("max-keys"); maxKeys != "" && !r.URL.Query().Has("uploads") {
|
||||
values["s3:max-keys"] = []string{maxKeys}
|
||||
}
|
||||
|
||||
// Authentication method
|
||||
if authHeader := r.Header.Get("Authorization"); authHeader != "" {
|
||||
if strings.HasPrefix(authHeader, "AWS4-HMAC-SHA256") {
|
||||
@@ -466,7 +475,7 @@ func (engine *PolicyEngine) ExtractConditionValuesFromRequest(r *http.Request) m
|
||||
} else if strings.HasPrefix(authHeader, "AWS ") {
|
||||
values["s3:authType"] = []string{"REST-HEADER"}
|
||||
}
|
||||
} else if r.URL.Query().Get("AWSAccessKeyId") != "" {
|
||||
} else if query := r.URL.Query(); query.Get("AWSAccessKeyId") != "" || query.Get("X-Amz-Credential") != "" {
|
||||
values["s3:authType"] = []string{"REST-QUERY-STRING"}
|
||||
}
|
||||
|
||||
|
||||
@@ -436,6 +436,27 @@ func TestExtractConditionValuesFromRequestMaxKeys(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractConditionValuesFromRequestAuthType(t *testing.T) {
|
||||
for _, tt := range []struct {
|
||||
query string
|
||||
header string
|
||||
want []string
|
||||
}{
|
||||
{"X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=key%2F20261119%2Fus-east-1%2Fs3%2Faws4_request", "", []string{"REST-QUERY-STRING"}},
|
||||
{"AWSAccessKeyId=key&Expires=1&Signature=sig", "", []string{"REST-QUERY-STRING"}},
|
||||
{"", "AWS4-HMAC-SHA256 Credential=key/20261119/us-east-1/s3/aws4_request", []string{"REST-HEADER"}},
|
||||
{"", "", nil},
|
||||
} {
|
||||
req := &http.Request{Method: "GET", URL: &url.URL{Path: "/test-bucket/key", RawQuery: tt.query}, Header: http.Header{}, RemoteAddr: "10.0.0.5:54321"}
|
||||
if tt.header != "" {
|
||||
req.Header.Set("Authorization", tt.header)
|
||||
}
|
||||
if got := NewPolicyEngine().ExtractConditionValuesFromRequest(req)["s3:authType"]; !slices.Equal(got, tt.want) {
|
||||
t.Errorf("%q %q: s3:authType = %v, want %v", tt.query, tt.header, got, tt.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractConditionValuesFromRequestSourceIPPrecedence(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
|
||||
@@ -419,6 +419,10 @@ func (s3iam *S3IAMIntegration) extractRequestContext(r *http.Request) map[string
|
||||
context["referer"] = referer
|
||||
}
|
||||
|
||||
for key, values := range policy_engine.RequestConditionValues(r) {
|
||||
context[key] = values
|
||||
}
|
||||
|
||||
for key, values := range vectorIAMConditions(r) {
|
||||
context[key] = values
|
||||
}
|
||||
|
||||
@@ -0,0 +1,131 @@
|
||||
package s3api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/seaweedfs/seaweedfs/weed/iam/integration"
|
||||
"github.com/seaweedfs/seaweedfs/weed/iam/policy"
|
||||
"github.com/seaweedfs/seaweedfs/weed/iam/sts"
|
||||
"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"
|
||||
)
|
||||
|
||||
func TestSTSSessionPolicyEvaluatesRequestConditions(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
iamManager := setupGroupsRolesIAMManager(t)
|
||||
s3iam := NewS3IAMIntegration(iamManager, "localhost:8888")
|
||||
|
||||
require.NoError(t, iamManager.CreatePolicy(ctx, "", "RequestConditionPolicy", &policy.PolicyDocument{
|
||||
Version: "2012-10-17",
|
||||
Statement: []policy.Statement{
|
||||
{
|
||||
Effect: "Allow",
|
||||
Action: []string{"s3:PutObject"},
|
||||
Resource: []string{"arn:aws:s3:::cond-bucket/*", "arn:aws:s3:::kms-bucket/*", "arn:aws:s3:::tls-bucket/*"},
|
||||
},
|
||||
{
|
||||
Effect: "Deny",
|
||||
Action: []string{"s3:PutObject"},
|
||||
Resource: []string{"arn:aws:s3:::cond-bucket/*"},
|
||||
Condition: map[string]map[string]interface{}{
|
||||
"StringEquals": {"s3:x-amz-object-lock-mode": []string{"GOVERNANCE", "COMPLIANCE"}},
|
||||
},
|
||||
},
|
||||
{
|
||||
Effect: "Deny",
|
||||
Action: []string{"s3:PutObject"},
|
||||
Resource: []string{"arn:aws:s3:::cond-bucket/*"},
|
||||
Condition: map[string]map[string]interface{}{
|
||||
"StringEquals": {"s3:RequestObjectTag/classification": "secret"},
|
||||
},
|
||||
},
|
||||
{
|
||||
Effect: "Deny",
|
||||
Action: []string{"s3:PutObject"},
|
||||
Resource: []string{"arn:aws:s3:::kms-bucket/*"},
|
||||
Condition: map[string]map[string]interface{}{
|
||||
"StringNotEquals": {"s3:x-amz-server-side-encryption": "aws:kms"},
|
||||
},
|
||||
},
|
||||
{
|
||||
Effect: "Deny",
|
||||
Action: []string{"s3:PutObject"},
|
||||
Resource: []string{"arn:aws:s3:::tls-bucket/*"},
|
||||
Condition: map[string]map[string]interface{}{
|
||||
"Bool": {"aws:SecureTransport": "false"},
|
||||
},
|
||||
},
|
||||
{
|
||||
Effect: "Deny",
|
||||
Action: []string{"s3:PutObject"},
|
||||
Resource: []string{"arn:aws:s3:::cond-bucket/*"},
|
||||
Condition: map[string]map[string]interface{}{
|
||||
"StringEquals": {"s3:authType": "REST-QUERY-STRING"},
|
||||
},
|
||||
},
|
||||
},
|
||||
}))
|
||||
require.NoError(t, iamManager.CreateRole(ctx, "", "RequestConditionRole", &integration.RoleDefinition{
|
||||
RoleName: "RequestConditionRole",
|
||||
TrustPolicy: &policy.PolicyDocument{
|
||||
Version: "2012-10-17",
|
||||
Statement: []policy.Statement{{
|
||||
Effect: "Allow",
|
||||
Principal: map[string]interface{}{"Federated": "test-oidc"},
|
||||
Action: []string{"sts:AssumeRoleWithWebIdentity"},
|
||||
}},
|
||||
},
|
||||
AttachedPolicies: []string{"RequestConditionPolicy"},
|
||||
}))
|
||||
response, err := iamManager.AssumeRoleWithWebIdentity(ctx, &sts.AssumeRoleWithWebIdentityRequest{
|
||||
RoleArn: "arn:aws:iam::role/RequestConditionRole",
|
||||
WebIdentityToken: groupsRolesJWT(t, "condition-user", nil, nil),
|
||||
RoleSessionName: "condition-session",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
identity, errCode := authenticateSessionToken(t, s3iam, response.Credentials.SessionToken)
|
||||
require.Equal(t, s3err.ErrNone, errCode)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
bucket string
|
||||
query string
|
||||
headers map[string]string
|
||||
secure bool
|
||||
want s3err.ErrorCode
|
||||
}{
|
||||
{name: "plain write", bucket: "cond-bucket", want: s3err.ErrNone},
|
||||
{name: "object lock mode", bucket: "cond-bucket", headers: map[string]string{"X-Amz-Object-Lock-Mode": "GOVERNANCE"}, want: s3err.ErrAccessDenied},
|
||||
{name: "presigned v4", bucket: "cond-bucket", query: "?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=key%2F20261119%2Fus-east-1%2Fs3%2Faws4_request", want: s3err.ErrAccessDenied},
|
||||
{name: "presigned v2", bucket: "cond-bucket", query: "?AWSAccessKeyId=key&Expires=1&Signature=sig", want: s3err.ErrAccessDenied},
|
||||
{name: "request object tag", bucket: "cond-bucket", headers: map[string]string{"X-Amz-Tagging": "classification=secret"}, want: s3err.ErrAccessDenied},
|
||||
{name: "other request object tag", bucket: "cond-bucket", headers: map[string]string{"X-Amz-Tagging": "classification=public"}, want: s3err.ErrNone},
|
||||
{name: "kms encryption", bucket: "kms-bucket", headers: map[string]string{"X-Amz-Server-Side-Encryption": "aws:kms"}, want: s3err.ErrNone},
|
||||
{name: "kms encryption any case", bucket: "kms-bucket", headers: map[string]string{"X-Amz-Server-Side-Encryption": "AWS:KMS"}, want: s3err.ErrNone},
|
||||
{name: "other encryption", bucket: "kms-bucket", headers: map[string]string{"X-Amz-Server-Side-Encryption": "AES256"}, want: s3err.ErrAccessDenied},
|
||||
{name: "no encryption", bucket: "kms-bucket", want: s3err.ErrAccessDenied},
|
||||
{name: "insecure transport", bucket: "tls-bucket", want: s3err.ErrAccessDenied},
|
||||
{name: "secure transport", bucket: "tls-bucket", secure: true, want: s3err.ErrNone},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
req := httptest.NewRequest(http.MethodPut, "/"+tt.bucket+"/file.txt"+tt.query, http.NoBody)
|
||||
if tt.query == "" {
|
||||
req.Header.Set("Authorization", "Bearer "+identity.SessionToken)
|
||||
}
|
||||
for k, v := range tt.headers {
|
||||
req.Header.Set(k, v)
|
||||
}
|
||||
if tt.secure {
|
||||
req.TLS = &tls.ConnectionState{}
|
||||
}
|
||||
assert.Equal(t, tt.want, s3iam.AuthorizeAction(ctx, identity, s3_constants.ACTION_WRITE, tt.bucket, "file.txt", req))
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user