|
20 | 20 | import com.aws.greengrass.logging.api.Logger; |
21 | 21 | import com.aws.greengrass.logging.impl.LogManager; |
22 | 22 | import com.aws.greengrass.tes.CredentialRequestHandler; |
| 23 | +import com.aws.greengrass.tes.HttpServerImpl; |
23 | 24 | import com.aws.greengrass.tes.TokenExchangeService; |
| 25 | +import com.aws.greengrass.util.Coerce; |
24 | 26 | import com.aws.greengrass.util.IamSdkClientFactory; |
25 | 27 | import com.aws.greengrass.util.IotSdkClientFactory; |
26 | 28 | import org.junit.jupiter.api.AfterAll; |
|
43 | 45 | import java.nio.charset.StandardCharsets; |
44 | 46 | import java.nio.file.Path; |
45 | 47 | import java.util.Collections; |
| 48 | +import java.util.Objects; |
46 | 49 | import java.util.UUID; |
47 | 50 | import java.util.concurrent.CountDownLatch; |
48 | 51 | import java.util.concurrent.TimeUnit; |
| 52 | +import java.util.concurrent.atomic.AtomicReference; |
49 | 53 |
|
50 | 54 | import static com.aws.greengrass.componentmanager.KernelConfigResolver.CONFIGURATION_CONFIG_KEY; |
| 55 | +import static com.aws.greengrass.deployment.DeviceConfiguration.IOT_ROLE_ALIAS_TOPIC; |
51 | 56 | import static com.aws.greengrass.easysetup.DeviceProvisioningHelper.ThingInfo; |
52 | 57 | import static com.aws.greengrass.integrationtests.e2e.BaseE2ETestCase.E2ETEST_ENV_STAGE; |
53 | 58 | import static com.aws.greengrass.lifecyclemanager.GreengrassService.SERVICES_NAMESPACE_TOPIC; |
54 | 59 | import static com.aws.greengrass.lifecyclemanager.GreengrassService.SETENV_CONFIG_NAMESPACE; |
55 | | -import static com.aws.greengrass.deployment.DeviceConfiguration.IOT_ROLE_ALIAS_TOPIC; |
56 | | -import static com.aws.greengrass.tes.TokenExchangeService.TES_URI_ENV_VARIABLE_NAME; |
57 | | -import static com.aws.greengrass.tes.TokenExchangeService.TOKEN_EXCHANGE_SERVICE_TOPICS; |
| 60 | +import static com.aws.greengrass.tes.TokenExchangeService.*; |
58 | 61 | import static com.aws.greengrass.testcommons.testutilities.ExceptionLogProtector.ignoreExceptionUltimateCauseOfType; |
59 | 62 | import static org.hamcrest.MatcherAssert.assertThat; |
60 | 63 | import static org.hamcrest.Matchers.matchesPattern; |
61 | | -import static org.junit.jupiter.api.Assertions.assertEquals; |
62 | | -import static org.junit.jupiter.api.Assertions.assertNotEquals; |
63 | | -import static org.junit.jupiter.api.Assertions.assertNotNull; |
64 | | -import static org.junit.jupiter.api.Assertions.assertTrue; |
| 64 | +import static org.junit.jupiter.api.Assertions.*; |
65 | 65 |
|
66 | 66 | @Tag("E2E-INTRUSIVE") |
67 | 67 | class TESTest extends BaseITCase { |
@@ -131,6 +131,43 @@ static void tearDown() throws URISyntaxException { |
131 | 131 | } |
132 | 132 | } |
133 | 133 |
|
| 134 | + @Test |
| 135 | + void GIVEN_iot_role_alias_WHEN_port_changes_THEN_valid_credentials_are_returned_from_new_port() throws Exception { |
| 136 | + // Get current port and calculate new port |
| 137 | + int currentPort = Coerce.toInt(kernel.getConfig().lookupTopics(SERVICES_NAMESPACE_TOPIC, TOKEN_EXCHANGE_SERVICE_TOPICS) |
| 138 | + .lookup(CONFIGURATION_CONFIG_KEY, PORT_TOPIC)); |
| 139 | + int newPort = currentPort + 1; |
| 140 | + String expectedUri = String.format("http://localhost:%d%s", newPort, HttpServerImpl.URL); |
| 141 | + |
| 142 | + // Setup server restart detection |
| 143 | + CountDownLatch serverRestarted = new CountDownLatch(1); |
| 144 | + AtomicReference<String> urlString = new AtomicReference<>(); |
| 145 | + kernel.getConfig().find(SETENV_CONFIG_NAMESPACE, TES_URI_ENV_VARIABLE_NAME) |
| 146 | + .subscribe((why, newv) -> { |
| 147 | + urlString.set(Coerce.toString(newv)); |
| 148 | + if (urlString.get().equals(expectedUri)) { |
| 149 | + serverRestarted.countDown(); |
| 150 | + } |
| 151 | + }); |
| 152 | + |
| 153 | + // Change port and wait for server restart |
| 154 | + kernel.getConfig().lookupTopics(SERVICES_NAMESPACE_TOPIC, TOKEN_EXCHANGE_SERVICE_TOPICS) |
| 155 | + .lookup(CONFIGURATION_CONFIG_KEY, PORT_TOPIC).withValue(newPort); |
| 156 | + assertTrue(serverRestarted.await(5, TimeUnit.SECONDS), "Server did not restart within 5 seconds"); |
| 157 | + assertEquals(expectedUri, urlString.get(), "New URL does not match expected URI"); |
| 158 | + |
| 159 | + // Get authentication token and make request |
| 160 | + String token = Objects.requireNonNull(kernel.getConfig() |
| 161 | + .findTopics(SERVICES_NAMESPACE_TOPIC, AuthenticationHandler.AUTHENTICATION_TOKEN_LOOKUP_KEY)) |
| 162 | + .iterator().next().getName(); |
| 163 | + |
| 164 | + String response = getResponseString(new URL(urlString.get()), token); |
| 165 | + |
| 166 | + // Verify response format |
| 167 | + assertThat(response, matchesPattern( |
| 168 | + "\\{\"AccessKeyId\":\".+\",\"SecretAccessKey\":\".+\",\"Expiration\":\".+\",\"Token\":\".+\"\\}")); |
| 169 | + } |
| 170 | + |
134 | 171 | @Test |
135 | 172 | void GIVEN_iot_role_alias_WHEN_tes_is_queried_THEN_valid_credentials_are_returned(ExtensionContext context) |
136 | 173 | throws Exception { |
|
0 commit comments