Skip to content

Commit

Permalink
Merge pull request #546 from s4u/offline-key-logs
Browse files Browse the repository at this point in the history
Provide a path for not existing key in exception in offline mode
  • Loading branch information
slawekjaranowski authored May 26, 2024
2 parents df5de59 + 8d81a87 commit 281d7df
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 39 deletions.
1 change: 1 addition & 0 deletions src/main/java/org/simplify4u/plugins/AbstractPGPMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ protected void setupMojo() throws MojoFailureException {
.keyServers(pgpKeyServer)
.loadBalance(pgpKeyServerLoadBalance)
.notFoundRefreshHours(keyNotFoundRefreshHour)
.offLine(session.isOffline())
.build();

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,6 @@ public class KeyCacheSettings {
boolean loadBalance;

int notFoundRefreshHours;

boolean offLine;
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,4 @@ public Optional<Proxy> getProxy() {
.filter(proxy -> proxyName.equalsIgnoreCase(proxy.getId()))
.findFirst();
}

public boolean isOffline() {
return mavenSession.isOffline();
}
}
11 changes: 10 additions & 1 deletion src/main/java/org/simplify4u/plugins/keyserver/PGPKeysCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public class PGPKeysCache {
private File cachePath;
private int notFoundRefreshHours;
private KeyServerList keyServerList;
private boolean offLine;

PGPKeysCache() {
}
Expand All @@ -89,6 +90,7 @@ void init(KeyCacheSettings cacheSettings, List<PGPKeysServerClient> 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);

Expand Down Expand Up @@ -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
Expand All @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -69,6 +67,7 @@ class PGPKeysCacheTest {

public static final KeyId KEY_ID_1 = KeyId.from(1L);

@TempDir
private Path cachePath;

@Spy
Expand Down Expand Up @@ -108,16 +107,6 @@ private List<PGPKeysServerClient> 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 {

Expand Down Expand Up @@ -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<PGPKeysServerClient> keysServerClients = prepareKeyServerClientWithNotFound();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit 281d7df

Please sign in to comment.