Skip to content

Commit

Permalink
respect maven offline mode for keys downloading
Browse files Browse the repository at this point in the history
fix: #245
  • Loading branch information
slawekjaranowski committed Mar 18, 2021
1 parent a6f93d1 commit 911d315
Show file tree
Hide file tree
Showing 12 changed files with 304 additions and 219 deletions.
13 changes: 10 additions & 3 deletions src/main/java/org/simplify4u/plugins/AbstractPGPMojo.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020 Slawomir Jaranowski
* Copyright 2021 Slawomir Jaranowski
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -30,6 +30,7 @@
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugins.annotations.Parameter;
import org.simplify4u.plugins.keyserver.KeyServerClientSettings;
import org.simplify4u.plugins.keyserver.PGPKeysCache;
import org.simplify4u.plugins.utils.PGPSignatureUtils;
import org.slf4j.Logger;
Expand Down Expand Up @@ -77,7 +78,7 @@ public abstract class AbstractPGPMojo extends AbstractMojo {

/**
* Choose which proxy to use (id from settings.xml in maven config). Uses no proxy if the proxy was not found. If it
* is not set, it will take the first active proxy if any or no proxy, if no active proxy was found)
* is not set, it will take the first active proxy if any or no proxy, if no active proxy was found.
*
* @since 1.8.0
*/
Expand Down Expand Up @@ -124,7 +125,13 @@ public final Log getLog() {
protected abstract void executeConfiguredMojo() throws MojoExecutionException, MojoFailureException;

private void initPgpKeysCache() throws IOException {
pgpKeysCache.init(pgpKeysCachePath, pgpKeyServer, pgpKeyServerLoadBalance, proxyName);

KeyServerClientSettings clientSettings = KeyServerClientSettings.builder()
.mavenSession(session)
.proxyName(proxyName)
.build();

pgpKeysCache.init(pgpKeysCachePath, pgpKeyServer, pgpKeyServerLoadBalance, clientSettings);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2021 Slawomir Jaranowski
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.simplify4u.plugins.keyserver;

import java.util.Collections;
import java.util.Optional;

import lombok.Builder;
import lombok.Getter;
import lombok.NonNull;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.settings.Proxy;

/**
* Provide settings for key server client connection.
*/
@Builder
@Getter
public class KeyServerClientSettings {

private static final int DEFAULT_CONNECT_TIMEOUT = 5000;
private static final int DEFAULT_READ_TIMEOUT = 20000;
public static final int DEFAULT_MAX_RETRIES = 10;

@NonNull
MavenSession mavenSession;

String proxyName;

@Builder.Default
int connectTimeout = DEFAULT_CONNECT_TIMEOUT;

@Builder.Default
int readTimeout = DEFAULT_READ_TIMEOUT;

@Builder.Default
int maxRetries = DEFAULT_MAX_RETRIES;

public Optional<Proxy> getProxy() {

if (proxyName == null) {
return Optional.ofNullable(mavenSession.getSettings().getActiveProxy());
}

return Optional.ofNullable(mavenSession.getSettings().getProxies()).orElse(Collections.emptyList())
.stream()
.filter(proxy -> proxyName.equalsIgnoreCase(proxy.getId()))
.findFirst();
}

public boolean isOffline() {
return mavenSession.isOffline();
}
}
38 changes: 22 additions & 16 deletions src/main/java/org/simplify4u/plugins/keyserver/PGPKeysCache.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020 Slawomir Jaranowski
* Copyright 2021 Slawomir Jaranowski
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -36,7 +36,6 @@
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import javax.inject.Inject;
import javax.inject.Named;

import static com.google.common.util.concurrent.Uninterruptibles.sleepUninterruptibly;
Expand All @@ -45,7 +44,6 @@
import io.vavr.control.Try;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.simplify4u.plugins.utils.MavenProxy;
import org.simplify4u.plugins.utils.PGPKeyId;
import org.simplify4u.plugins.utils.PublicKeyUtils;
import org.slf4j.Logger;
Expand All @@ -61,25 +59,27 @@ public class PGPKeysCache {

private static final Logger LOGGER = LoggerFactory.getLogger(PGPKeysCache.class);
private static final String NL = System.lineSeparator();
private static final Object LOCK = new Object();

private static final Pattern KEY_SERVERS_SPLIT_PATTERN = Pattern.compile("[;,\\s]");

private final MavenProxy mavenProxy;

private File cachePath;
private KeyServerList keyServerList;


private static final Object LOCK = new Object();

@Inject
PGPKeysCache(MavenProxy mavenProxy) {
this.mavenProxy = mavenProxy;
PGPKeysCache() {
}

public void init(File cachePath, String keyServers, boolean loadBalance, String proxyName)
/**
* Init Keys cache.
* @param cachePath a path where cache will be stored
* @param keyServers a list of key servers addresses
* @param loadBalance if use key servers list in balance mode
* @param clientSettings a kay server client settings
* @throws IOException in case of problems
*/
public void init(File cachePath, String keyServers, boolean loadBalance, KeyServerClientSettings clientSettings)
throws IOException {
init(cachePath, prepareClients(keyServers, proxyName), loadBalance);
init(cachePath, prepareClients(keyServers, clientSettings), loadBalance);
}

// used by test
Expand All @@ -105,7 +105,7 @@ void init(File cachePath, List<PGPKeysServerClient> pgpKeysServerClients, boolea
}
}

List<PGPKeysServerClient> prepareClients(String keyServers, String proxyName) {
List<PGPKeysServerClient> prepareClients(String keyServers, KeyServerClientSettings clientSettings) {

List<String> keyServersList = Arrays.stream(KEY_SERVERS_SPLIT_PATTERN.split(keyServers))
.map(String::trim)
Expand All @@ -114,7 +114,7 @@ List<PGPKeysServerClient> prepareClients(String keyServers, String proxyName) {

return keyServersList.stream()
.map(keyserver -> Try.of(() ->
PGPKeysServerClient.getClient(keyserver, mavenProxy.getProxyByName(proxyName))).get())
PGPKeysServerClient.getClient(keyserver, clientSettings)).get())
.collect(Collectors.toList());
}

Expand Down Expand Up @@ -149,9 +149,15 @@ public String getUrlForShowKey(PGPKeyId keyID) {
return keyServerList.getUriForShowKey(keyID).toString();
}

/**
* Return Public Key Ring from local cache or from key server.
*
* @param keyID a keyId for lookup
* @return Public Key Ring for given key
* @throws IOException in case of problems
*/
public PGPPublicKeyRing getKeyRing(PGPKeyId keyID) throws IOException {


String path = keyID.getHashPath();
File keyFile = new File(cachePath, path);

Expand Down
Loading

0 comments on commit 911d315

Please sign in to comment.