Skip to content

Commit 012de41

Browse files
committed
fix: allow TES restart on port changes
1 parent 74c2796 commit 012de41

File tree

2 files changed

+54
-9
lines changed

2 files changed

+54
-9
lines changed

src/integrationtests/java/com/aws/greengrass/integrationtests/e2e/tes/TESTest.java

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
import com.aws.greengrass.logging.api.Logger;
2121
import com.aws.greengrass.logging.impl.LogManager;
2222
import com.aws.greengrass.tes.CredentialRequestHandler;
23+
import com.aws.greengrass.tes.HttpServerImpl;
2324
import com.aws.greengrass.tes.TokenExchangeService;
25+
import com.aws.greengrass.util.Coerce;
2426
import com.aws.greengrass.util.IamSdkClientFactory;
2527
import com.aws.greengrass.util.IotSdkClientFactory;
2628
import org.junit.jupiter.api.AfterAll;
@@ -43,25 +45,23 @@
4345
import java.nio.charset.StandardCharsets;
4446
import java.nio.file.Path;
4547
import java.util.Collections;
48+
import java.util.Objects;
4649
import java.util.UUID;
4750
import java.util.concurrent.CountDownLatch;
4851
import java.util.concurrent.TimeUnit;
52+
import java.util.concurrent.atomic.AtomicReference;
4953

5054
import static com.aws.greengrass.componentmanager.KernelConfigResolver.CONFIGURATION_CONFIG_KEY;
55+
import static com.aws.greengrass.deployment.DeviceConfiguration.IOT_ROLE_ALIAS_TOPIC;
5156
import static com.aws.greengrass.easysetup.DeviceProvisioningHelper.ThingInfo;
5257
import static com.aws.greengrass.integrationtests.e2e.BaseE2ETestCase.E2ETEST_ENV_STAGE;
5358
import static com.aws.greengrass.lifecyclemanager.GreengrassService.SERVICES_NAMESPACE_TOPIC;
5459
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.*;
5861
import static com.aws.greengrass.testcommons.testutilities.ExceptionLogProtector.ignoreExceptionUltimateCauseOfType;
5962
import static org.hamcrest.MatcherAssert.assertThat;
6063
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.*;
6565

6666
@Tag("E2E-INTRUSIVE")
6767
class TESTest extends BaseITCase {
@@ -131,6 +131,43 @@ static void tearDown() throws URISyntaxException {
131131
}
132132
}
133133

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+
134171
@Test
135172
void GIVEN_iot_role_alias_WHEN_tes_is_queried_THEN_valid_credentials_are_returned(ExtensionContext context)
136173
throws Exception {

src/main/java/com/aws/greengrass/tes/TokenExchangeService.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.aws.greengrass.authorization.exceptions.AuthorizationException;
1010
import com.aws.greengrass.config.Topic;
1111
import com.aws.greengrass.config.Topics;
12+
import com.aws.greengrass.config.WhatHappened;
1213
import com.aws.greengrass.dependency.ImplementsService;
1314
import com.aws.greengrass.dependency.State;
1415
import com.aws.greengrass.deployment.DeviceConfiguration;
@@ -56,9 +57,16 @@ public TokenExchangeService(Topics topics,
5657
CredentialRequestHandler credentialRequestHandler,
5758
AuthorizationHandler authZHandler, DeviceConfiguration deviceConfiguration) {
5859
super(topics);
59-
// Port change should not be allowed
6060
topics.lookup(CONFIGURATION_CONFIG_KEY, PORT_TOPIC).dflt(DEFAULT_PORT)
61-
.subscribe((why, newv) -> port = Coerce.toInt(newv));
61+
.subscribe((why, newv) -> {
62+
port = Coerce.toInt(newv);
63+
// this will also restart all components that have hard dependency on TES
64+
if (WhatHappened.changed.equals(why)) {
65+
logger.atInfo("tes-config-change").kv(PORT_TOPIC, port)
66+
.log("Restarting TES server due to port config change");
67+
requestRestart();
68+
}
69+
});
6270

6371
deviceConfiguration.getIotRoleAlias().subscribe((why, newv) -> {
6472
iotRoleAlias = Coerce.toString(newv);

0 commit comments

Comments
 (0)