|
| 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.signin.auth; |
| 17 | + |
| 18 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 19 | +import static org.junit.jupiter.api.Assertions.assertInstanceOf; |
| 20 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 21 | +import static software.amazon.awssdk.services.signin.auth.internal.DpopTestUtils.VALID_TEST_PEM; |
| 22 | + |
| 23 | +import java.nio.file.Path; |
| 24 | +import java.time.Instant; |
| 25 | +import java.util.Optional; |
| 26 | +import org.junit.jupiter.api.AfterEach; |
| 27 | +import org.junit.jupiter.api.BeforeEach; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | +import org.junit.jupiter.api.io.TempDir; |
| 30 | +import software.amazon.awssdk.auth.credentials.AwsCredentials; |
| 31 | +import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider; |
| 32 | +import software.amazon.awssdk.auth.credentials.AwsSessionCredentials; |
| 33 | +import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider; |
| 34 | +import software.amazon.awssdk.auth.credentials.ProfileProviderCredentialsContext; |
| 35 | +import software.amazon.awssdk.auth.credentials.internal.ProfileCredentialsUtils; |
| 36 | +import software.amazon.awssdk.core.useragent.BusinessMetricFeatureId; |
| 37 | +import software.amazon.awssdk.profiles.ProfileFile; |
| 38 | +import software.amazon.awssdk.services.signin.internal.AccessTokenManager; |
| 39 | +import software.amazon.awssdk.services.signin.internal.LoginAccessToken; |
| 40 | +import software.amazon.awssdk.services.signin.internal.LoginCacheDirectorySystemSetting; |
| 41 | +import software.amazon.awssdk.services.signin.internal.OnDiskTokenManager; |
| 42 | +import software.amazon.awssdk.utils.StringInputStream; |
| 43 | + |
| 44 | +public class LoginProfileCredentialsProviderFactoryTest { |
| 45 | + private static final String LOGIN_SESSION_ID = "loginSessionId"; |
| 46 | + |
| 47 | + @TempDir |
| 48 | + Path tempDir; |
| 49 | + |
| 50 | + private AccessTokenManager tokenManager; |
| 51 | + |
| 52 | + @BeforeEach |
| 53 | + public void setup() { |
| 54 | + System.setProperty(new LoginCacheDirectorySystemSetting().property(), tempDir.toString()); |
| 55 | + tokenManager = OnDiskTokenManager.create(tempDir, LOGIN_SESSION_ID); |
| 56 | + } |
| 57 | + |
| 58 | + @AfterEach |
| 59 | + public void teardown() { |
| 60 | + System.clearProperty(new LoginCacheDirectorySystemSetting().property()); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void create_returnsLoginCredentialsFromProfile() { |
| 65 | + AwsSessionCredentials creds = buildCredentials( Instant.now().plusSeconds(600)); |
| 66 | + LoginAccessToken token = buildAccessToken(creds); |
| 67 | + tokenManager.storeToken(token); |
| 68 | + |
| 69 | + ProfileFile profileFile = configFile("[profile foo]\n" + |
| 70 | + "login_session=" + LOGIN_SESSION_ID + "\n"); |
| 71 | + |
| 72 | + AwsCredentialsProvider provider = new LoginProfileCredentialsProviderFactory().create( |
| 73 | + ProfileProviderCredentialsContext |
| 74 | + .builder() |
| 75 | + .profile(profileFile.profile("foo").get()) |
| 76 | + .profileFile(profileFile) |
| 77 | + .build() |
| 78 | + ); |
| 79 | + |
| 80 | + // validate the creds are from cached token file stored above |
| 81 | + AwsCredentials credentials = provider.resolveCredentials(); |
| 82 | + assertInstanceOf(AwsSessionCredentials.class, credentials); |
| 83 | + AwsSessionCredentials resolvedCredentials = (AwsSessionCredentials) credentials; |
| 84 | + assertEquals(creds.accessKeyId(), resolvedCredentials.accessKeyId()); |
| 85 | + assertEquals(creds.secretAccessKey(), resolvedCredentials.secretAccessKey()); |
| 86 | + assertEquals(creds.sessionToken(), resolvedCredentials.sessionToken()); |
| 87 | + assertEquals(creds.accountId(), resolvedCredentials.accountId()); |
| 88 | + assertEquals(BusinessMetricFeatureId.CREDENTIALS_LOGIN.value(), resolvedCredentials.providerName().get()); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * This test validates that the {@link ProfileCredentialsUtils} used in the {@link ProfileCredentialsProvider} |
| 93 | + * correctly loads and configures login session credentials using the factory. |
| 94 | + * This test exists in this module because it depends on having the `signin` module available. |
| 95 | + */ |
| 96 | + @Test |
| 97 | + public void profileCredentialsUtils_returnsLoginCredentialsFromProfile() { |
| 98 | + AwsSessionCredentials creds = buildCredentials( Instant.now().plusSeconds(600)); |
| 99 | + LoginAccessToken token = buildAccessToken(creds); |
| 100 | + tokenManager.storeToken(token); |
| 101 | + |
| 102 | + ProfileFile profileFile = configFile("[profile foo]\n" + |
| 103 | + "login_session=" + LOGIN_SESSION_ID + "\n"); |
| 104 | + |
| 105 | + ProfileCredentialsUtils profileCredentialsUtils = new ProfileCredentialsUtils( |
| 106 | + profileFile, profileFile.profile("foo").get(), profileFile::profile |
| 107 | + ); |
| 108 | + |
| 109 | + Optional<AwsCredentialsProvider> resolvedProvider = profileCredentialsUtils.credentialsProvider(); |
| 110 | + assertTrue(resolvedProvider.isPresent()); |
| 111 | + |
| 112 | + // validate the creds are from cached token file stored above |
| 113 | + AwsCredentials credentials = resolvedProvider.get().resolveCredentials(); |
| 114 | + assertInstanceOf(AwsSessionCredentials.class, credentials); |
| 115 | + AwsSessionCredentials resolvedCredentials = (AwsSessionCredentials) credentials; |
| 116 | + assertEquals(creds.accessKeyId(), resolvedCredentials.accessKeyId()); |
| 117 | + assertEquals(creds.secretAccessKey(), resolvedCredentials.secretAccessKey()); |
| 118 | + assertEquals(creds.sessionToken(), resolvedCredentials.sessionToken()); |
| 119 | + assertEquals(creds.accountId(), resolvedCredentials.accountId()); |
| 120 | + String expectedCredSource = BusinessMetricFeatureId.CREDENTIALS_PROFILE_LOGIN.value() + "," |
| 121 | + + BusinessMetricFeatureId.CREDENTIALS_LOGIN.value(); |
| 122 | + assertEquals(expectedCredSource, resolvedCredentials.providerName().get()); |
| 123 | + } |
| 124 | + |
| 125 | + private static ProfileFile configFile(String configFile) { |
| 126 | + return ProfileFile.builder() |
| 127 | + .content(new StringInputStream(configFile)) |
| 128 | + .type(ProfileFile.Type.CONFIGURATION) |
| 129 | + .build(); |
| 130 | + } |
| 131 | + |
| 132 | + |
| 133 | + |
| 134 | + private AwsSessionCredentials buildCredentials(Instant expirationTime) { |
| 135 | + return AwsSessionCredentials.builder() |
| 136 | + .accessKeyId("akid") |
| 137 | + .secretAccessKey("skid") |
| 138 | + .sessionToken("sessionToken") |
| 139 | + .accountId("123456789012") |
| 140 | + .expirationTime(expirationTime) |
| 141 | + .build(); |
| 142 | + } |
| 143 | + |
| 144 | + private LoginAccessToken buildAccessToken(AwsSessionCredentials credentials) { |
| 145 | + return LoginAccessToken.builder() |
| 146 | + .accessToken(credentials) |
| 147 | + .clientId("client-123") |
| 148 | + .dpopKey(VALID_TEST_PEM) |
| 149 | + .refreshToken("refresh-token") |
| 150 | + .tokenType("aws_sigv4") |
| 151 | + .identityToken("id-token") |
| 152 | + .build(); |
| 153 | + } |
| 154 | +} |
0 commit comments