Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Evaluate settings.xml later, and per id #1712

Merged
merged 6 commits into from
May 15, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -103,18 +103,17 @@ public void execute() throws MojoExecutionException, MojoFailureException {
MavenHelpfulSuggestionsBuilder mavenHelpfulSuggestionsBuilder =
new MavenHelpfulSuggestionsBuilder(HELPFUL_SUGGESTIONS_PREFIX, this);

DecryptedMavenSettings decryptedSettings =
DecryptedMavenSettings.from(getSession().getSettings(), getSettingsDecrypter());

PluginConfigurationProcessor pluginConfigurationProcessor =
PluginConfigurationProcessor.processCommonConfigurationForDockerDaemonImage(
mavenRawConfiguration,
new MavenSettingsServerCredentials(decryptedSettings),
new MavenSettingsServerCredentials(
getSession().getSettings(), getSettingsDecrypter()),
projectProperties,
dockerExecutable,
getDockerClientEnvironment(),
mavenHelpfulSuggestionsBuilder.build());
ProxyProvider.init(decryptedSettings);
ProxyProvider.populateSystemProxyProperties(
getSession().getSettings(), getSettingsDecrypter());

ImageReference targetImageReference = pluginConfigurationProcessor.getTargetImageReference();
HelpfulSuggestions helpfulSuggestions =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,14 @@ public void execute() throws MojoExecutionException, MojoFailureException {
EventDispatcher eventDispatcher =
new DefaultEventDispatcher(projectProperties.getEventHandlers());

DecryptedMavenSettings decryptedSettings =
DecryptedMavenSettings.from(getSession().getSettings(), getSettingsDecrypter());

PluginConfigurationProcessor pluginConfigurationProcessor =
PluginConfigurationProcessor.processCommonConfigurationForRegistryImage(
mavenRawConfiguration,
new MavenSettingsServerCredentials(decryptedSettings),
new MavenSettingsServerCredentials(
getSession().getSettings(), getSettingsDecrypter()),
projectProperties);
ProxyProvider.init(decryptedSettings);
ProxyProvider.populateSystemProxyProperties(
getSession().getSettings(), getSettingsDecrypter());

ImageReference targetImageReference = pluginConfigurationProcessor.getTargetImageReference();
HelpfulSuggestions helpfulSuggestions =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,18 @@ public void execute() throws MojoExecutionException, MojoFailureException {
MavenHelpfulSuggestionsBuilder mavenHelpfulSuggestionsBuilder =
new MavenHelpfulSuggestionsBuilder(HELPFUL_SUGGESTIONS_PREFIX, this);

DecryptedMavenSettings decryptedSettings =
DecryptedMavenSettings.from(getSession().getSettings(), getSettingsDecrypter());

Path buildOutput = Paths.get(getProject().getBuild().getDirectory());
Path tarOutputPath = buildOutput.resolve("jib-image.tar");
PluginConfigurationProcessor pluginConfigurationProcessor =
PluginConfigurationProcessor.processCommonConfigurationForTarImage(
mavenRawConfiguration,
new MavenSettingsServerCredentials(decryptedSettings),
new MavenSettingsServerCredentials(
getSession().getSettings(), getSettingsDecrypter()),
projectProperties,
tarOutputPath,
mavenHelpfulSuggestionsBuilder.build());
ProxyProvider.init(decryptedSettings);
ProxyProvider.populateSystemProxyProperties(
getSession().getSettings(), getSettingsDecrypter());

HelpfulSuggestions helpfulSuggestions =
mavenHelpfulSuggestionsBuilder
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,36 @@
package com.google.cloud.tools.jib.maven;

import com.google.cloud.tools.jib.plugins.common.AuthProperty;
import com.google.cloud.tools.jib.plugins.common.InferredAuthException;
import com.google.cloud.tools.jib.plugins.common.InferredAuthProvider;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;
import org.apache.maven.settings.Server;
import org.apache.maven.settings.Settings;
import org.apache.maven.settings.building.SettingsProblem;
import org.apache.maven.settings.crypto.DefaultSettingsDecryptionRequest;
import org.apache.maven.settings.crypto.SettingsDecrypter;
import org.apache.maven.settings.crypto.SettingsDecryptionRequest;
import org.apache.maven.settings.crypto.SettingsDecryptionResult;

/**
* Retrieves credentials for servers defined in <a
* href="https://maven.apache.org/settings.html">Maven settings</a>.
*/
class MavenSettingsServerCredentials implements Function<String, Optional<AuthProperty>> {
class MavenSettingsServerCredentials implements InferredAuthProvider {

static final String CREDENTIAL_SOURCE = "Maven settings";

private final DecryptedMavenSettings settings;
private final Settings settings;
private final SettingsDecrypter decrypter;

/**
* Create new instance.
*
* @param settings decrypted Maven settings
*/
MavenSettingsServerCredentials(DecryptedMavenSettings settings) {
MavenSettingsServerCredentials(Settings settings, SettingsDecrypter decrypter) {
this.settings = settings;
this.decrypter = decrypter;
}

/**
Expand All @@ -48,15 +56,28 @@ class MavenSettingsServerCredentials implements Function<String, Optional<AuthPr
* @return the auth info for the registry, or {@link Optional#empty} if none could be retrieved
*/
@Override
public Optional<AuthProperty> apply(String registry) {
Predicate<Server> idMatches = server -> registry.equals(server.getId());
Optional<Server> server = settings.getServers().stream().filter(idMatches).findFirst();
if (!server.isPresent()) {
public Optional<AuthProperty> inferredAuth(String registry) throws InferredAuthException {

Server server = settings.getServer(registry);
if (server == null) {
return Optional.empty();
}

String username = server.get().getUsername();
String password = server.get().getPassword();
SettingsDecryptionRequest request = new DefaultSettingsDecryptionRequest(server);
SettingsDecryptionResult result = decrypter.decrypt(request);
// Un-encrypted passwords are passed through, so a problem indicates a real issue.
// If there are any ERROR or FATAL problems reported, then decryption failed.
for (SettingsProblem problem : result.getProblems()) {
if (problem.getSeverity() == SettingsProblem.Severity.ERROR
|| problem.getSeverity() == SettingsProblem.Severity.FATAL) {
throw new InferredAuthException(
"Unable to decrypt server(" + registry + ") info from settings.xml: " + problem);
}
}
Server resultServer = result.getServer();

String username = resultServer.getUsername();
String password = resultServer.getPassword();

return Optional.of(
new AuthProperty() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,64 @@

import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.settings.Proxy;
import org.apache.maven.settings.Settings;
import org.apache.maven.settings.building.SettingsProblem;
import org.apache.maven.settings.crypto.DefaultSettingsDecryptionRequest;
import org.apache.maven.settings.crypto.SettingsDecrypter;
import org.apache.maven.settings.crypto.SettingsDecryptionRequest;
import org.apache.maven.settings.crypto.SettingsDecryptionResult;

/** Propagates proxy configuration from Maven settings to system properties. */
class ProxyProvider {

private static final ImmutableList<String> PROXY_PROPERTIES =
ImmutableList.of("proxyHost", "proxyPort", "proxyUser", "proxyPassword");

private static final ImmutableList<String> PROXY_PROTOCOLS = ImmutableList.of("https", "http");
/**
* Initializes proxy settings based on Maven settings.
*
* @param settings Maven settings
*/
static void init(DecryptedMavenSettings settings) {
configureProxy(settings, "https");
configureProxy(settings, "http");
}
static void populateSystemProxyProperties(Settings settings, SettingsDecrypter decrypter)
throws MojoExecutionException {
List<Proxy> proxies =
loosebazooka marked this conversation as resolved.
Show resolved Hide resolved
PROXY_PROTOCOLS
loosebazooka marked this conversation as resolved.
Show resolved Hide resolved
.stream()
.map(
protocol ->
settings
.getProxies()
.stream()
.filter(Proxy::isActive)
.filter(proxy -> protocol.equals(proxy.getProtocol()))
.findFirst())
.filter(Optional::isPresent)
.map(Optional::get)
.collect(Collectors.toList());

private static void configureProxy(DecryptedMavenSettings settings, String protocol) {
settings
.getProxies()
.stream()
.filter(Proxy::isActive)
.filter(proxy -> protocol.equals(proxy.getProtocol()))
.findFirst()
.ifPresent(ProxyProvider::setProxyProperties);
if (proxies.size() == 0) {
return;
}

SettingsDecryptionRequest request = new DefaultSettingsDecryptionRequest().setProxies(proxies);
chanseokoh marked this conversation as resolved.
Show resolved Hide resolved
SettingsDecryptionResult result = decrypter.decrypt(request);

for (SettingsProblem problem : result.getProblems()) {
if (problem.getSeverity() == SettingsProblem.Severity.ERROR
|| problem.getSeverity() == SettingsProblem.Severity.FATAL) {
throw new MojoExecutionException(
"Unable to decrypt proxy info from settings.xml: " + problem);
}
}

result.getProxies().forEach(ProxyProvider::setProxyProperties);
chanseokoh marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down

This file was deleted.

Loading