|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package software.amazon.awssdk.services; |
| 17 | + |
| 18 | +import java.util.Arrays; |
| 19 | +import java.util.Collections; |
| 20 | +import java.util.List; |
| 21 | +import java.util.stream.Stream; |
| 22 | +import org.junit.jupiter.api.Assertions; |
| 23 | +import org.junit.jupiter.params.ParameterizedTest; |
| 24 | +import org.junit.jupiter.params.provider.Arguments; |
| 25 | +import org.junit.jupiter.params.provider.MethodSource; |
| 26 | +import software.amazon.awssdk.http.auth.spi.scheme.AuthSchemeOption; |
| 27 | +import software.amazon.awssdk.regions.Region; |
| 28 | +import software.amazon.awssdk.services.multiauth.auth.scheme.MultiauthAuthSchemeParams; |
| 29 | +import software.amazon.awssdk.services.multiauth.auth.scheme.MultiauthAuthSchemeProvider; |
| 30 | + |
| 31 | +public class PreferredAuthSchemeProviderTest { |
| 32 | + |
| 33 | + private static final String OPERATION_SIGV4A_ONLY = "multiAuthWithOnlySigv4a"; |
| 34 | + private static final String OPERATION_SIGV4A_AND_SIGV4 = "multiAuthWithOnlySigv4aAndSigv4"; |
| 35 | + |
| 36 | + private static final String SIGV4 = "aws.auth#sigv4"; |
| 37 | + private static final String SIGV4A = "aws.auth#sigv4a"; |
| 38 | + private static final String BEARER = "aws.auth#bearer"; |
| 39 | + private static final String ANONYMOUS = "aws.auth#noauth"; |
| 40 | + |
| 41 | + @ParameterizedTest(name = "{3}") |
| 42 | + @MethodSource("authSchemeTestCases") |
| 43 | + void testAuthSchemePreference(List<String> preferredAuthSchemes, String operation, String expectedFirstScheme, String testName) { |
| 44 | + MultiauthAuthSchemeProvider provider = MultiauthAuthSchemeProvider |
| 45 | + .builder() |
| 46 | + .withPreferredAuthSchemes(preferredAuthSchemes) |
| 47 | + .build(); |
| 48 | + |
| 49 | + MultiauthAuthSchemeParams params = MultiauthAuthSchemeParams |
| 50 | + .builder() |
| 51 | + .region(Region.US_WEST_2) |
| 52 | + .operation(operation) |
| 53 | + .build(); |
| 54 | + |
| 55 | + List<AuthSchemeOption> authSchemes = provider.resolveAuthScheme(params); |
| 56 | + |
| 57 | + Assertions.assertFalse(authSchemes.isEmpty()); |
| 58 | + Assertions.assertEquals(expectedFirstScheme, authSchemes.get(0).schemeId()); |
| 59 | + } |
| 60 | + |
| 61 | + static Stream<Arguments> authSchemeTestCases() { |
| 62 | + return Stream.of( |
| 63 | + Arguments.of( |
| 64 | + Arrays.asList(BEARER, ANONYMOUS), |
| 65 | + OPERATION_SIGV4A_AND_SIGV4, |
| 66 | + SIGV4A, |
| 67 | + "Unsupported auth schemes only" |
| 68 | + ), |
| 69 | + |
| 70 | + Arguments.of( |
| 71 | + Arrays.asList(BEARER, SIGV4, ANONYMOUS), |
| 72 | + OPERATION_SIGV4A_AND_SIGV4, |
| 73 | + SIGV4, |
| 74 | + "Mix of supported and unsupported schemes" |
| 75 | + ), |
| 76 | + |
| 77 | + Arguments.of( |
| 78 | + Arrays.asList(SIGV4, SIGV4A), |
| 79 | + OPERATION_SIGV4A_AND_SIGV4, |
| 80 | + SIGV4, |
| 81 | + "All supported schemes in reverse order" |
| 82 | + ), |
| 83 | + |
| 84 | + Arguments.of( |
| 85 | + Arrays.asList(SIGV4, SIGV4A), |
| 86 | + OPERATION_SIGV4A_ONLY, |
| 87 | + SIGV4A, |
| 88 | + "Operation with only one supported scheme" |
| 89 | + ), |
| 90 | + |
| 91 | + Arguments.of( |
| 92 | + Collections.emptyList(), |
| 93 | + OPERATION_SIGV4A_AND_SIGV4, |
| 94 | + SIGV4A, |
| 95 | + "Empty preference list" |
| 96 | + ), |
| 97 | + |
| 98 | + Arguments.of( |
| 99 | + Arrays.asList(SIGV4A, SIGV4, BEARER), |
| 100 | + OPERATION_SIGV4A_AND_SIGV4, |
| 101 | + SIGV4A, |
| 102 | + "First preference is supported" |
| 103 | + ) |
| 104 | + ); |
| 105 | + } |
| 106 | +} |
0 commit comments