Files
seaweedFS/weed/s3api/s3api_sts_get_caller_identity_test.go
Chris Lu 7c59b639c9 STS: add GetCallerIdentity support (#8893)
* STS: add GetCallerIdentity support

Implement the AWS STS GetCallerIdentity action, which returns the
ARN, account ID, and user ID of the caller based on SigV4 authentication.
This is commonly used by AWS SDKs and CLI tools (e.g. `aws sts get-caller-identity`)
to verify credentials and determine the authenticated identity.

* test: remove trivial GetCallerIdentity tests

Remove the XML unmarshal test (we don't consume this response as input)
and the routing constant test (just asserts a literal equals itself).

* fix: route GetCallerIdentity through STS in UnifiedPostHandler and use stable UserId

- UnifiedPostHandler only dispatched actions starting with "AssumeRole" to STS,
  so GetCallerIdentity in a POST body would fall through to the IAM path and
  get AccessDenied for non-admin users. Add explicit check for GetCallerIdentity.
- Use identity.Name as UserId instead of credential.AccessKey, which is a
  transient value and incorrect for STS assumed-role callers.
2026-04-02 15:59:09 -07:00

34 lines
1022 B
Go

package s3api
import (
"encoding/xml"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestGetCallerIdentityResponse_XMLMarshal(t *testing.T) {
response := &GetCallerIdentityResponse{
Result: GetCallerIdentityResult{
Arn: fmt.Sprintf("arn:aws:iam::%s:user/alice", defaultAccountID),
UserId: "alice",
Account: defaultAccountID,
},
}
response.ResponseMetadata.RequestId = "test-request-id"
data, err := xml.MarshalIndent(response, "", " ")
require.NoError(t, err)
xmlStr := string(data)
assert.Contains(t, xmlStr, "GetCallerIdentityResponse")
assert.Contains(t, xmlStr, "GetCallerIdentityResult")
assert.Contains(t, xmlStr, "<Arn>arn:aws:iam::000000000000:user/alice</Arn>")
assert.Contains(t, xmlStr, "<UserId>alice</UserId>")
assert.Contains(t, xmlStr, "<Account>000000000000</Account>")
assert.Contains(t, xmlStr, "<RequestId>test-request-id</RequestId>")
assert.Contains(t, xmlStr, "https://sts.amazonaws.com/doc/2011-06-15/")
}