Skip to content

Commit a7b7263

Browse files
authored
fix: reinitialize keystore if there are any loading problems (#24)
1 parent 4796a5b commit a7b7263

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

src/main/java/com/aws/greengrass/localdebugconsole/SimpleHttpServer.java

+5-8
Original file line numberDiff line numberDiff line change
@@ -245,14 +245,11 @@ boolean initializeHttps() {
245245
try (InputStream is = Files.newInputStream(keyStorePath)) {
246246
ks.load(is, passphrase);
247247
} catch (IOException e) {
248-
// If the password is wrong for whatever reason, delete the existing keystore and
249-
// reinitialize it
250-
if (e.getCause() instanceof UnrecoverableKeyException) {
251-
Files.deleteIfExists(keyStorePath);
252-
initializeKeyStore(ks, passphrase, keyStorePath);
253-
} else {
254-
throw e;
255-
}
248+
logger.warn(
249+
"Failed to load self-signed certificate keystore. Reinitializing keystore automatically",
250+
e);
251+
Files.deleteIfExists(keyStorePath);
252+
initializeKeyStore(ks, passphrase, keyStorePath);
256253
}
257254
} else {
258255
initializeKeyStore(ks, passphrase, keyStorePath);

src/test/java/com/aws/greengrass/localdebugconsole/SimpleHttpServerTest.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,19 @@
1414
import org.junit.jupiter.api.Assertions;
1515
import org.junit.jupiter.api.Test;
1616
import org.junit.jupiter.api.extension.ExtendWith;
17+
import org.junit.jupiter.api.extension.ExtensionContext;
1718
import org.junit.jupiter.api.io.TempDir;
1819

20+
import java.io.IOException;
21+
import java.nio.file.Files;
1922
import java.nio.file.Path;
2023
import java.time.Duration;
2124
import java.time.Instant;
2225
import java.util.Map;
2326

2427
import static com.aws.greengrass.localdebugconsole.SimpleHttpServer.DEBUG_PASSWORD_NAMESPACE;
2528
import static com.aws.greengrass.localdebugconsole.SimpleHttpServer.EXPIRATION_NAMESPACE;
29+
import static com.aws.greengrass.testcommons.testutilities.ExceptionLogProtector.ignoreExceptionOfType;
2630
import static org.junit.jupiter.api.Assertions.assertFalse;
2731
import static org.junit.jupiter.api.Assertions.assertNull;
2832
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -43,7 +47,8 @@ void after() {
4347
}
4448

4549
@Test
46-
void GIVEN_server_WHEN_authenticate_THEN_cleans_storage() {
50+
void GIVEN_server_WHEN_authenticate_THEN_cleans_storage(ExtensionContext context) throws IOException {
51+
ignoreExceptionOfType(context, IOException.class);
4752
kernel = new Kernel();
4853
kernel.parseArgs("-r", rootDir.toAbsolutePath().toString());
4954

@@ -66,6 +71,11 @@ void GIVEN_server_WHEN_authenticate_THEN_cleans_storage() {
6671
http.getRuntimeConfig().remove(); // remove runtime config so that the password is lost
6772
kernel.getContext().waitForPublishQueueToClear();
6873
assertTrue(http.initializeHttps());
74+
75+
// Verify that corrupting the keystore is recoverable
76+
Files.write(kernel.getNucleusPaths().workPath(SimpleHttpServer.AWS_GREENGRASS_DEBUG_SERVER)
77+
.resolve("keystore.jks"), new byte[1024]);
78+
assertTrue(http.initializeHttps());
6979
}
7080

7181
@Test

0 commit comments

Comments
 (0)