From 8d81a870869297365a42c42243d7f0da1b312b03 Mon Sep 17 00:00:00 2001 From: Slawomir Jaranowski Date: Sun, 26 May 2024 09:00:02 +0200 Subject: [PATCH] Provide a path for not existing key in exception in offline mode --- .../simplify4u/plugins/AbstractPGPMojo.java | 1 + .../plugins/keyserver/KeyCacheSettings.java | 2 + .../keyserver/KeyServerClientSettings.java | 4 -- .../plugins/keyserver/PGPKeysCache.java | 11 ++++- .../keyserver/PGPKeysServerClient.java | 3 -- .../plugins/keyserver/PGPKeysCacheTest.java | 40 +++++++++++++------ .../keyserver/PGPKeysServerClientTest.java | 18 --------- 7 files changed, 40 insertions(+), 39 deletions(-) diff --git a/src/main/java/org/simplify4u/plugins/AbstractPGPMojo.java b/src/main/java/org/simplify4u/plugins/AbstractPGPMojo.java index 8559a0ce..a69f8dfe 100644 --- a/src/main/java/org/simplify4u/plugins/AbstractPGPMojo.java +++ b/src/main/java/org/simplify4u/plugins/AbstractPGPMojo.java @@ -155,6 +155,7 @@ protected void setupMojo() throws MojoFailureException { .keyServers(pgpKeyServer) .loadBalance(pgpKeyServerLoadBalance) .notFoundRefreshHours(keyNotFoundRefreshHour) + .offLine(session.isOffline()) .build(); try { diff --git a/src/main/java/org/simplify4u/plugins/keyserver/KeyCacheSettings.java b/src/main/java/org/simplify4u/plugins/keyserver/KeyCacheSettings.java index 2fe57774..92269e26 100644 --- a/src/main/java/org/simplify4u/plugins/keyserver/KeyCacheSettings.java +++ b/src/main/java/org/simplify4u/plugins/keyserver/KeyCacheSettings.java @@ -34,4 +34,6 @@ public class KeyCacheSettings { boolean loadBalance; int notFoundRefreshHours; + + boolean offLine; } diff --git a/src/main/java/org/simplify4u/plugins/keyserver/KeyServerClientSettings.java b/src/main/java/org/simplify4u/plugins/keyserver/KeyServerClientSettings.java index e5247a2d..c92aff43 100644 --- a/src/main/java/org/simplify4u/plugins/keyserver/KeyServerClientSettings.java +++ b/src/main/java/org/simplify4u/plugins/keyserver/KeyServerClientSettings.java @@ -60,8 +60,4 @@ public Optional getProxy() { .filter(proxy -> proxyName.equalsIgnoreCase(proxy.getId())) .findFirst(); } - - public boolean isOffline() { - return mavenSession.isOffline(); - } } diff --git a/src/main/java/org/simplify4u/plugins/keyserver/PGPKeysCache.java b/src/main/java/org/simplify4u/plugins/keyserver/PGPKeysCache.java index d0b96edf..4d9628b5 100644 --- a/src/main/java/org/simplify4u/plugins/keyserver/PGPKeysCache.java +++ b/src/main/java/org/simplify4u/plugins/keyserver/PGPKeysCache.java @@ -66,6 +66,7 @@ public class PGPKeysCache { private File cachePath; private int notFoundRefreshHours; private KeyServerList keyServerList; + private boolean offLine; PGPKeysCache() { } @@ -89,6 +90,7 @@ void init(KeyCacheSettings cacheSettings, List pgpKeysServe this.cachePath = cacheSettings.getCachePath(); this.notFoundRefreshHours = cacheSettings.getNotFoundRefreshHours(); this.keyServerList = createKeyServerList(pgpKeysServerClients, cacheSettings.isLoadBalance()); + this.offLine = cacheSettings.isOffLine(); LOGGER.info("Key server(s) - {}", keyServerList); @@ -167,7 +169,9 @@ public PGPPublicKeyRing getKeyRing(KeyId keyID) throws IOException { synchronized (LOCK) { - checkNotFoundCache(path); + if (!offLine) { + checkNotFoundCache(path); + } if (keyFile.exists()) { // load from cache @@ -177,6 +181,11 @@ public PGPPublicKeyRing getKeyRing(KeyId keyID) throws IOException { } } + if (offLine) { + throw new IOException("Key " + keyID + " not exits in cache under path: " + keyFile + + " it is not possible to download in offline mode"); + } + // key not exists in cache or something wrong with cache, so receive from servers return Try.of(() -> keyServerList.execute(keysServerClient -> receiveKey(keyFile, keyID, keysServerClient))) .onFailure(PGPKeyNotFound.class, e -> writeNotFoundCache(path)) diff --git a/src/main/java/org/simplify4u/plugins/keyserver/PGPKeysServerClient.java b/src/main/java/org/simplify4u/plugins/keyserver/PGPKeysServerClient.java index e90401db..b55fcd25 100644 --- a/src/main/java/org/simplify4u/plugins/keyserver/PGPKeysServerClient.java +++ b/src/main/java/org/simplify4u/plugins/keyserver/PGPKeysServerClient.java @@ -193,9 +193,6 @@ void copyKeyToOutputStream(KeyId keyId, OutputStream outputStream, OnRetryConsum throws IOException { final URI keyUri = getUriForGetKey(keyId); - if (keyServerClientSettings.isOffline()) { - throw new IOException("Not possible to download key: " + keyUri + " in offline mode."); - } final HttpUriRequest request = new HttpGet(keyUri); diff --git a/src/test/java/org/simplify4u/plugins/keyserver/PGPKeysCacheTest.java b/src/test/java/org/simplify4u/plugins/keyserver/PGPKeysCacheTest.java index e53db8ec..9559214c 100644 --- a/src/test/java/org/simplify4u/plugins/keyserver/PGPKeysCacheTest.java +++ b/src/test/java/org/simplify4u/plugins/keyserver/PGPKeysCacheTest.java @@ -44,11 +44,9 @@ import com.google.common.io.ByteStreams; import com.google.common.io.MoreFiles; -import com.google.common.io.RecursiveDeleteOption; import org.bouncycastle.openpgp.PGPPublicKeyRing; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.api.io.TempDir; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; import org.mockito.InjectMocks; @@ -69,6 +67,7 @@ class PGPKeysCacheTest { public static final KeyId KEY_ID_1 = KeyId.from(1L); + @TempDir private Path cachePath; @Spy @@ -108,16 +107,6 @@ private List prepareKeyServerClientWithNotFound() throws IO return Collections.singletonList(keysServerClient); } - @BeforeEach - void setup() throws IOException { - cachePath = Files.createTempDirectory("cache-path-test"); - } - - @AfterEach - void cleanup() throws IOException { - MoreFiles.deleteRecursively(cachePath, RecursiveDeleteOption.ALLOW_INSECURE); - } - @Test void emptyCacheDirShouldBeCreated() throws IOException { @@ -218,6 +207,31 @@ void notFoundKeyFromCache() throws IOException { verify(keysServerClients.get(0)).copyKeyToOutputStream(eq(keyId), any(), any()); } + @Test + void notFoundKeyFromCacheInOfflineMode() throws IOException { + + KeyCacheSettings cacheSettings = KeyCacheSettings.builder() + .cachePath(cachePath.toFile()) + .offLine(true) + .build(); + + pgpKeysCache.init(cacheSettings, Collections.singletonList(keysServerClient)); + + KeyId keyId = KeyId.from(0x1234567890L); + + assertThatCode(() -> pgpKeysCache.getKeyRing(keyId)) + .isExactlyInstanceOf(IOException.class) + .hasMessageContainingAll("not exits in cache under path", "it is not possible to download in offline mode"); + + + File notFoundCache = new File(cachePath.toFile(), keyId.getHashPath() + ".404"); + + assertThat(notFoundCache).doesNotExist(); + + // client was not call at all + verifyNoInteractions(keysServerClient); + } + @Test void notFoundKeyCacheShouldBeEvicted() throws IOException { List keysServerClients = prepareKeyServerClientWithNotFound(); diff --git a/src/test/java/org/simplify4u/plugins/keyserver/PGPKeysServerClientTest.java b/src/test/java/org/simplify4u/plugins/keyserver/PGPKeysServerClientTest.java index 22f0229a..b331ad4f 100644 --- a/src/test/java/org/simplify4u/plugins/keyserver/PGPKeysServerClientTest.java +++ b/src/test/java/org/simplify4u/plugins/keyserver/PGPKeysServerClientTest.java @@ -116,24 +116,6 @@ private void runProxyConfig(Proxy proxy) { verify(clientBuilder).build(); } - @Test - void offLineModeShouldThrowIOException() throws URISyntaxException { - - URI uri = new URI("https://localhost/"); - - when(mavenSession.isOffline()).thenReturn(true); - - KeyServerClientSettings clientSettings = KeyServerClientSettings.builder() - .mavenSession(mavenSession) - .build(); - - PGPKeysServerClient pgpKeysServerClient = new PGPKeysServerClient(uri, clientSettings); - - assertThatThrownBy(() -> pgpKeysServerClient.copyKeyToOutputStream(KeyId.from(0x0123456789ABCDEFL), null, null)) - .isExactlyInstanceOf(IOException.class) - .hasMessage("Not possible to download key: https://localhost/pks/lookup?op=get&options=mr&search=0x0123456789ABCDEF in offline mode."); - } - @Test void unsupportedProtocolShouldThrowIOException() throws IOException { assertThatThrownBy(() -> PGPKeysServerClient.getClient("abc://loclahost", null))