|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0. |
| 4 | + */ |
| 5 | + |
| 6 | +package aws.sdk.kotlin.services.sts |
| 7 | + |
| 8 | +import aws.sdk.kotlin.runtime.auth.credentials.Credentials |
| 9 | +import aws.sdk.kotlin.runtime.auth.credentials.StaticCredentialsProvider |
| 10 | +import aws.smithy.kotlin.runtime.http.Headers |
| 11 | +import aws.smithy.kotlin.runtime.http.HttpBody |
| 12 | +import aws.smithy.kotlin.runtime.http.HttpStatusCode |
| 13 | +import aws.smithy.kotlin.runtime.http.engine.HttpClientEngineBase |
| 14 | +import aws.smithy.kotlin.runtime.http.engine.callContext |
| 15 | +import aws.smithy.kotlin.runtime.http.request.HttpRequest |
| 16 | +import aws.smithy.kotlin.runtime.http.response.HttpCall |
| 17 | +import aws.smithy.kotlin.runtime.http.response.HttpResponse |
| 18 | +import aws.smithy.kotlin.runtime.testing.runSuspendTest |
| 19 | +import aws.smithy.kotlin.runtime.time.Instant |
| 20 | +import kotlin.test.Test |
| 21 | +import kotlin.test.assertFalse |
| 22 | +import kotlin.test.assertNotNull |
| 23 | +import kotlin.test.assertTrue |
| 24 | + |
| 25 | +/** |
| 26 | + * Tests related to STS model and whether requests need to be signed or not |
| 27 | + */ |
| 28 | +class StsAuthTests { |
| 29 | + |
| 30 | + private val mockEngine = object : HttpClientEngineBase("mock-engine") { |
| 31 | + var capturedRequest: HttpRequest? = null |
| 32 | + |
| 33 | + override suspend fun roundTrip(request: HttpRequest): HttpCall { |
| 34 | + capturedRequest = request |
| 35 | + val callContext = callContext() |
| 36 | + val now = Instant.now() |
| 37 | + return HttpCall(request, HttpResponse(HttpStatusCode.OK, Headers.Empty, HttpBody.Empty), now, now, callContext) |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + private val credentials = Credentials("ANOTREAL", "notrealrnrELgWzOk3IFjzDKtFBhDby", "notarealsessiontoken") |
| 42 | + |
| 43 | + @Test |
| 44 | + fun testAssumeRoleIsSigned(): Unit = runSuspendTest { |
| 45 | + val client = StsClient { |
| 46 | + region = "us-east-2" |
| 47 | + credentialsProvider = StaticCredentialsProvider(credentials) |
| 48 | + httpClientEngine = mockEngine |
| 49 | + } |
| 50 | + |
| 51 | + runCatching { client.assumeRole { } } |
| 52 | + val request = assertNotNull(mockEngine.capturedRequest) |
| 53 | + assertTrue(request.headers.contains("AUTHORIZATION")) |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + fun testWebIdentityIsUnsigned(): Unit = runSuspendTest { |
| 58 | + val client = StsClient { |
| 59 | + region = "us-east-2" |
| 60 | + credentialsProvider = StaticCredentialsProvider(credentials) |
| 61 | + httpClientEngine = mockEngine |
| 62 | + } |
| 63 | + |
| 64 | + runCatching { client.assumeRoleWithWebIdentity { } } |
| 65 | + val request = assertNotNull(mockEngine.capturedRequest) |
| 66 | + assertFalse(request.headers.contains("AUTHORIZATION"), "assumeRoleWithWebIdentity should not require a signed request") |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + fun testAssumeRoleSamlIsUnsigned(): Unit = runSuspendTest { |
| 71 | + val client = StsClient { |
| 72 | + region = "us-east-2" |
| 73 | + credentialsProvider = StaticCredentialsProvider(credentials) |
| 74 | + httpClientEngine = mockEngine |
| 75 | + } |
| 76 | + |
| 77 | + runCatching { client.assumeRoleWithSaml { } } |
| 78 | + val request = assertNotNull(mockEngine.capturedRequest) |
| 79 | + assertFalse(request.headers.contains("AUTHORIZATION"), "assumeRoleWithSaml should not require a signed request") |
| 80 | + } |
| 81 | +} |
0 commit comments