|
5 | 5 |
|
6 | 6 | package aws.sdk.kotlin.runtime.auth.credentials |
7 | 7 |
|
| 8 | +import aws.sdk.kotlin.runtime.client.AwsClientOption |
8 | 9 | import aws.smithy.kotlin.runtime.auth.awscredentials.Credentials |
9 | 10 | import aws.smithy.kotlin.runtime.httptest.TestConnection |
10 | 11 | import aws.smithy.kotlin.runtime.httptest.buildTestConnection |
11 | 12 | import aws.smithy.kotlin.runtime.net.Host |
12 | 13 | import aws.smithy.kotlin.runtime.util.TestPlatformProvider |
| 14 | +import aws.smithy.kotlin.runtime.util.attributesOf |
13 | 15 | import kotlinx.coroutines.ExperimentalCoroutinesApi |
14 | 16 | import kotlinx.coroutines.test.runTest |
15 | 17 | import kotlin.test.Test |
@@ -137,4 +139,160 @@ class ProfileCredentialsProviderTest { |
137 | 139 | // region is overridden from the environment which should take precedence |
138 | 140 | assertEquals(Host.Domain("sts.us-west-2.amazonaws.com"), req.actual.url.host) |
139 | 141 | } |
| 142 | + |
| 143 | + @Test |
| 144 | + fun testExplicitRegion() = runTest { |
| 145 | + val testArn = "arn:aws:iam:1234567/test-role" |
| 146 | + val testProvider = TestPlatformProvider( |
| 147 | + env = mapOf( |
| 148 | + "AWS_CONFIG_FILE" to "config", |
| 149 | + "AWS_REGION" to "eu-west-3", |
| 150 | + ), |
| 151 | + fs = mapOf( |
| 152 | + "config" to """ |
| 153 | + [default] |
| 154 | + role_arn = $testArn |
| 155 | + source_profile = B |
| 156 | +
|
| 157 | + [profile B] |
| 158 | + aws_access_key_id = AKID-Profile |
| 159 | + aws_secret_access_key = Profile-Secret |
| 160 | + region = af-south-1 |
| 161 | + """.trimIndent(), |
| 162 | + ), |
| 163 | + ) |
| 164 | + |
| 165 | + val testEngine = buildTestConnection { |
| 166 | + expect(StsTestUtils.stsResponse(testArn)) |
| 167 | + } |
| 168 | + |
| 169 | + ProfileCredentialsProvider( |
| 170 | + platformProvider = testProvider, |
| 171 | + httpClient = testEngine, |
| 172 | + region = "us-west-2", |
| 173 | + ).resolve( |
| 174 | + attributesOf { |
| 175 | + AwsClientOption.Region to "cn-north-1" |
| 176 | + }, |
| 177 | + ) |
| 178 | + |
| 179 | + testEngine.assertRequests() |
| 180 | + val requests = testEngine.requests().first() |
| 181 | + assertEquals(Host.Domain("sts.us-west-2.amazonaws.com"), requests.actual.url.host) |
| 182 | + } |
| 183 | + |
| 184 | + @Test |
| 185 | + fun testProfileRegion() = runTest { |
| 186 | + val testArn = "arn:aws:iam:1234567/test-role" |
| 187 | + val testProvider = TestPlatformProvider( |
| 188 | + env = mapOf( |
| 189 | + "AWS_CONFIG_FILE" to "config", |
| 190 | + "AWS_REGION" to "eu-west-3", |
| 191 | + ), |
| 192 | + fs = mapOf( |
| 193 | + "config" to """ |
| 194 | + [default] |
| 195 | + role_arn = $testArn |
| 196 | + region = us-west-2 |
| 197 | + source_profile = B |
| 198 | +
|
| 199 | + [profile B] |
| 200 | + aws_access_key_id = AKID-Profile |
| 201 | + aws_secret_access_key = Profile-Secret |
| 202 | + """.trimIndent(), |
| 203 | + ), |
| 204 | + ) |
| 205 | + |
| 206 | + val testEngine = buildTestConnection { |
| 207 | + expect(StsTestUtils.stsResponse(testArn)) |
| 208 | + } |
| 209 | + |
| 210 | + ProfileCredentialsProvider( |
| 211 | + platformProvider = testProvider, |
| 212 | + httpClient = testEngine, |
| 213 | + profileName = "default", |
| 214 | + ).resolve( |
| 215 | + attributesOf { |
| 216 | + AwsClientOption.Region to "cn-north-1" |
| 217 | + }, |
| 218 | + ) |
| 219 | + |
| 220 | + testEngine.assertRequests() |
| 221 | + val requests = testEngine.requests().first() |
| 222 | + assertEquals(Host.Domain("sts.us-west-2.amazonaws.com"), requests.actual.url.host) |
| 223 | + } |
| 224 | + |
| 225 | + @Test |
| 226 | + fun testAttributeRegion() = runTest { |
| 227 | + val testArn = "arn:aws:iam:1234567/test-role" |
| 228 | + val testProvider = TestPlatformProvider( |
| 229 | + env = mapOf( |
| 230 | + "AWS_CONFIG_FILE" to "config", |
| 231 | + "AWS_REGION" to "eu-west-3", |
| 232 | + ), |
| 233 | + fs = mapOf( |
| 234 | + "config" to """ |
| 235 | + [default] |
| 236 | + role_arn = $testArn |
| 237 | + source_profile = B |
| 238 | +
|
| 239 | + [profile B] |
| 240 | + aws_access_key_id = AKID-Profile |
| 241 | + aws_secret_access_key = Profile-Secret |
| 242 | + """.trimIndent(), |
| 243 | + ), |
| 244 | + ) |
| 245 | + |
| 246 | + val testEngine = buildTestConnection { |
| 247 | + expect(StsTestUtils.stsResponse(testArn)) |
| 248 | + } |
| 249 | + |
| 250 | + ProfileCredentialsProvider( |
| 251 | + platformProvider = testProvider, |
| 252 | + httpClient = testEngine, |
| 253 | + ).resolve( |
| 254 | + attributesOf { |
| 255 | + AwsClientOption.Region to "us-west-2" |
| 256 | + }, |
| 257 | + ) |
| 258 | + |
| 259 | + testEngine.assertRequests() |
| 260 | + val requests = testEngine.requests().first() |
| 261 | + assertEquals(Host.Domain("sts.us-west-2.amazonaws.com"), requests.actual.url.host) |
| 262 | + } |
| 263 | + |
| 264 | + @Test |
| 265 | + fun testPlatformRegion() = runTest { |
| 266 | + val testArn = "arn:aws:iam:1234567/test-role" |
| 267 | + val testProvider = TestPlatformProvider( |
| 268 | + env = mapOf( |
| 269 | + "AWS_CONFIG_FILE" to "config", |
| 270 | + "AWS_REGION" to "us-west-2", |
| 271 | + ), |
| 272 | + fs = mapOf( |
| 273 | + "config" to """ |
| 274 | + [default] |
| 275 | + role_arn = $testArn |
| 276 | + source_profile = B |
| 277 | +
|
| 278 | + [profile B] |
| 279 | + aws_access_key_id = AKID-Profile |
| 280 | + aws_secret_access_key = Profile-Secret |
| 281 | + """.trimIndent(), |
| 282 | + ), |
| 283 | + ) |
| 284 | + |
| 285 | + val testEngine = buildTestConnection { |
| 286 | + expect(StsTestUtils.stsResponse(testArn)) |
| 287 | + } |
| 288 | + |
| 289 | + ProfileCredentialsProvider( |
| 290 | + platformProvider = testProvider, |
| 291 | + httpClient = testEngine, |
| 292 | + ).resolve() |
| 293 | + |
| 294 | + testEngine.assertRequests() |
| 295 | + val requests = testEngine.requests().first() |
| 296 | + assertEquals(Host.Domain("sts.us-west-2.amazonaws.com"), requests.actual.url.host) |
| 297 | + } |
140 | 298 | } |
0 commit comments