Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle session tokens in generatePresignedPost #716

Merged
merged 3 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Sources/Soto/Extensions/S3/S3+presignedPost.swift
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@ extension S3 {
fields["x-amz-date"] = longDate
fields["x-amz-credential"] = presignedPostCredential

if let sessionToken = clientCredentials.sessionToken {
conditions.append(.match("x-amz-security-token", sessionToken))
fields["x-amz-security-token"] = sessionToken
}

// Create the policy and add to fields
let policy = PostPolicy(expiration: date.addingTimeInterval(expiresIn), conditions: conditions)
let stringToSign = try policy.stringToSign()
Expand Down
50 changes: 50 additions & 0 deletions Tests/SotoTests/Services/S3/S3ExtensionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -498,4 +498,54 @@ extension S3Tests {

XCTAssertEqual(credential, expectedCredential)
}

func testGeneratePresignedPostWithSessionToken() async throws {
let client = AWSClient(
credentialProvider: .static(
accessKeyId: "AKIAIOSFODNN7EXAMPLE",
secretAccessKey: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
sessionToken: "EXAMPLESESSIONTOKEN"
)
)

let s3 = S3(client: client, region: .useast1)

defer { try? client.syncShutdown() }

let fields = [
"acl": "public-read",
"success_action_redirect": "http://sigv4examplebucket.s3.amazonaws.com/successful_upload.html",
"x-amz-meta-uuid": "14365123651274",
"x-amz-server-side-encryption": "AES256",
]

let conditions: [S3.PostPolicyCondition] = [
.match("acl", "public-read"),
.match("success_action_redirect", "http://sigv4examplebucket.s3.amazonaws.com/successful_upload.html"),
.match("x-amz-meta-uuid", "14365123651274"),
.match("x-amz-server-side-encryption", "AES256"),
.rule("starts-with", "$Content-Type", "image/"),
.rule("starts-with", "$x-amz-meta-tag", "")
]

let expiresIn = 36.0 * 60.0 * 60.0
var dateComponents = DateComponents()
dateComponents.year = 2015
dateComponents.month = 12
dateComponents.day = 29
dateComponents.timeZone = TimeZone(secondsFromGMT: 0)!

let date = Calendar(identifier: .gregorian).date(from: dateComponents)!

let presignedPost = try await s3.generatePresignedPost(
key: "user/user1/${filename}",
bucket: "sigv4examplebucket",
fields: fields,
conditions: conditions,
expiresIn: expiresIn,
date: date
)

XCTAssertEqual(presignedPost.fields["x-amz-security-token"], "EXAMPLESESSIONTOKEN")
}
}