From fc9035516dd6dd4dfe6c5ba30ac748328444f2da Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Thu, 9 Jan 2025 22:48:36 +0100 Subject: [PATCH] Avoid direct usage of Plexus SecDispatcher and use the proper Maven 3 API --- .../ConfigurableMavenWorkingSessionImpl.java | 3 +- .../resolver/impl/maven/SettingsManager.java | 11 +- .../bootstrap/MavenRepositorySystem.java | 8 +- .../maven/bootstrap/MavenSettingsBuilder.java | 10 +- .../internal/decrypt/MavenPlexusCipher.java | 122 ----------- .../decrypt/MavenSecurityDispatcher.java | 190 ------------------ .../decrypt/MavenSettingsDecrypter.java | 95 --------- .../MavenSettingsDecryptionResult.java | 72 ------- .../DefaultSettingsXmlLocationTestCase.java | 13 +- .../MavenSettingsBuilderUnitTestCase.java | 19 +- .../MavenPlexusCipherTestCase.java | 127 ------------ 11 files changed, 51 insertions(+), 619 deletions(-) delete mode 100644 maven/impl-maven/src/main/java/org/jboss/shrinkwrap/resolver/impl/maven/internal/decrypt/MavenPlexusCipher.java delete mode 100644 maven/impl-maven/src/main/java/org/jboss/shrinkwrap/resolver/impl/maven/internal/decrypt/MavenSecurityDispatcher.java delete mode 100644 maven/impl-maven/src/main/java/org/jboss/shrinkwrap/resolver/impl/maven/internal/decrypt/MavenSettingsDecrypter.java delete mode 100644 maven/impl-maven/src/main/java/org/jboss/shrinkwrap/resolver/impl/maven/internal/decrypt/MavenSettingsDecryptionResult.java delete mode 100644 maven/impl-maven/src/test/java/org/jboss/shrinkwrap/resolver/impl/maven/integration/MavenPlexusCipherTestCase.java diff --git a/maven/impl-maven/src/main/java/org/jboss/shrinkwrap/resolver/impl/maven/ConfigurableMavenWorkingSessionImpl.java b/maven/impl-maven/src/main/java/org/jboss/shrinkwrap/resolver/impl/maven/ConfigurableMavenWorkingSessionImpl.java index c49993858..7f2bfa603 100644 --- a/maven/impl-maven/src/main/java/org/jboss/shrinkwrap/resolver/impl/maven/ConfigurableMavenWorkingSessionImpl.java +++ b/maven/impl-maven/src/main/java/org/jboss/shrinkwrap/resolver/impl/maven/ConfigurableMavenWorkingSessionImpl.java @@ -21,6 +21,7 @@ import java.util.logging.Logger; import org.apache.maven.settings.Settings; +import org.apache.maven.settings.crypto.SettingsDecrypter; import org.eclipse.aether.DefaultRepositorySystemSession; import org.jboss.shrinkwrap.resolver.api.InvalidConfigurationFileException; import org.jboss.shrinkwrap.resolver.api.maven.MavenWorkingSession; @@ -57,7 +58,7 @@ public abstract class ConfigurableMavenWorkingSessionImpl implements MavenWorkin public ConfigurableMavenWorkingSessionImpl() { this.system = new MavenRepositorySystem(); - this.settingsManager = new SettingsManager(); + this.settingsManager = new SettingsManager(system.getContext().lookup().lookup(SettingsDecrypter.class).get()); } @Override diff --git a/maven/impl-maven/src/main/java/org/jboss/shrinkwrap/resolver/impl/maven/SettingsManager.java b/maven/impl-maven/src/main/java/org/jboss/shrinkwrap/resolver/impl/maven/SettingsManager.java index 9d1f533b7..3969e5f50 100644 --- a/maven/impl-maven/src/main/java/org/jboss/shrinkwrap/resolver/impl/maven/SettingsManager.java +++ b/maven/impl-maven/src/main/java/org/jboss/shrinkwrap/resolver/impl/maven/SettingsManager.java @@ -21,6 +21,7 @@ import org.apache.maven.settings.Settings; import org.apache.maven.settings.building.DefaultSettingsBuildingRequest; import org.apache.maven.settings.building.SettingsBuildingRequest; +import org.apache.maven.settings.crypto.SettingsDecrypter; import org.jboss.shrinkwrap.resolver.api.InvalidConfigurationFileException; import org.jboss.shrinkwrap.resolver.impl.maven.bootstrap.MavenSettingsBuilder; @@ -34,11 +35,17 @@ */ public class SettingsManager { + private final SettingsDecrypter settingsDecrypter; + private Settings settings; // make sure that programmatic call to offline method is always preserved private Boolean programmaticOffline; + public SettingsManager(SettingsDecrypter settingsDecrypter) { + this.settingsDecrypter = settingsDecrypter; + } + /** * Crates an instance of {@link Settings} and configures it from the given file. * @@ -58,7 +65,7 @@ public void configureSettingsFromFile(File globalSettings, File userSettings) } request.setSystemProperties(SecurityActions.getProperties()); - MavenSettingsBuilder builder = new MavenSettingsBuilder(); + MavenSettingsBuilder builder = new MavenSettingsBuilder(settingsDecrypter); this.settings = builder.buildSettings(request); // ensure we keep offline(boolean) if previously set @@ -72,7 +79,7 @@ public void configureSettingsFromFile(File globalSettings, File userSettings) */ protected Settings getSettings() { if (this.settings == null) { - this.settings = new MavenSettingsBuilder().buildDefaultSettings(); + this.settings = new MavenSettingsBuilder(settingsDecrypter).buildDefaultSettings(); // ensure we keep offline(boolean) if previously set propagateProgrammaticOfflineIntoSettings(); } diff --git a/maven/impl-maven/src/main/java/org/jboss/shrinkwrap/resolver/impl/maven/bootstrap/MavenRepositorySystem.java b/maven/impl-maven/src/main/java/org/jboss/shrinkwrap/resolver/impl/maven/bootstrap/MavenRepositorySystem.java index 70c2069c7..93c80b0bc 100644 --- a/maven/impl-maven/src/main/java/org/jboss/shrinkwrap/resolver/impl/maven/bootstrap/MavenRepositorySystem.java +++ b/maven/impl-maven/src/main/java/org/jboss/shrinkwrap/resolver/impl/maven/bootstrap/MavenRepositorySystem.java @@ -61,7 +61,7 @@ public class MavenRepositorySystem { * Creates a Maven repository system */ public MavenRepositorySystem() { - this.context = getContext(); + this.context = doGetContext(); } @@ -159,10 +159,14 @@ public VersionRangeResult resolveVersionRange(final RepositorySystemSession sess * * @return Context */ - private Context getContext() throws UnsupportedOperationException { + private Context doGetContext() throws UnsupportedOperationException { eu.maveniverse.maven.mima.context.Runtime runtime = Runtimes.INSTANCE.getRuntime(); return runtime.create(ContextOverrides.create().checksumPolicy(ContextOverrides.ChecksumPolicy.WARN).build()); } + + public Context getContext() { + return context; + } } class MavenResolutionFilterWrap implements org.eclipse.aether.graph.DependencyFilter { diff --git a/maven/impl-maven/src/main/java/org/jboss/shrinkwrap/resolver/impl/maven/bootstrap/MavenSettingsBuilder.java b/maven/impl-maven/src/main/java/org/jboss/shrinkwrap/resolver/impl/maven/bootstrap/MavenSettingsBuilder.java index 2285e848d..04976defb 100644 --- a/maven/impl-maven/src/main/java/org/jboss/shrinkwrap/resolver/impl/maven/bootstrap/MavenSettingsBuilder.java +++ b/maven/impl-maven/src/main/java/org/jboss/shrinkwrap/resolver/impl/maven/bootstrap/MavenSettingsBuilder.java @@ -40,7 +40,6 @@ import org.apache.maven.settings.crypto.SettingsDecryptionRequest; import org.apache.maven.settings.crypto.SettingsDecryptionResult; import org.jboss.shrinkwrap.resolver.api.InvalidConfigurationFileException; -import org.jboss.shrinkwrap.resolver.impl.maven.internal.decrypt.MavenSettingsDecrypter; /** * Builds Maven settings from arbitrary settings.xml file @@ -105,6 +104,12 @@ public class MavenSettingsBuilder { } + private final SettingsDecrypter settingsDecrypter; + + public MavenSettingsBuilder(SettingsDecrypter settingsDecrypter) { + this.settingsDecrypter = settingsDecrypter; + } + static String getFirstNotNull(String... values) { for(String value : values) { if(value != null) { @@ -225,9 +230,8 @@ private Settings decryptPasswords(Settings settings) { securitySettings = new File(altSecuritySettings); } - SettingsDecrypter decrypter = new MavenSettingsDecrypter(securitySettings); SettingsDecryptionRequest request = new DefaultSettingsDecryptionRequest(settings); - SettingsDecryptionResult result = decrypter.decrypt(request); + SettingsDecryptionResult result = settingsDecrypter.decrypt(request); if (!result.getProblems().isEmpty() && !Boolean.getBoolean("org.jboss.shrinkwrap.resolver.maven.ignoreDecryptionProblems")) { diff --git a/maven/impl-maven/src/main/java/org/jboss/shrinkwrap/resolver/impl/maven/internal/decrypt/MavenPlexusCipher.java b/maven/impl-maven/src/main/java/org/jboss/shrinkwrap/resolver/impl/maven/internal/decrypt/MavenPlexusCipher.java deleted file mode 100644 index a405939e5..000000000 --- a/maven/impl-maven/src/main/java/org/jboss/shrinkwrap/resolver/impl/maven/internal/decrypt/MavenPlexusCipher.java +++ /dev/null @@ -1,122 +0,0 @@ -/* - * JBoss, Home of Professional Open Source - * Copyright 2013, Red Hat Middleware LLC, and individual contributors - * by the @authors tag. See the copyright.txt in the distribution for a - * full listing of individual contributors. - * - * 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.jboss.shrinkwrap.resolver.impl.maven.internal.decrypt; - -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import org.sonatype.plexus.components.cipher.PBECipher; -import org.sonatype.plexus.components.cipher.PlexusCipher; -import org.sonatype.plexus.components.cipher.PlexusCipherException; - -/** - * Inspired by DefaultPlexusCipher from Sonatype - * - * @author Karel Piwko - * @author Oleg Gusakov - * @author Matous Jobanek - */ -public class MavenPlexusCipher implements PlexusCipher { - - private static final Pattern ENCRYPTED_PATTERN_WITH_PRECEDING_STRING = - Pattern.compile("[^\\\\$]\\{(.*?[^\\\\])\\}.*", Pattern.DOTALL); - - private static final Pattern ENCRYPTED_PATTERN_WITHOUT_PRECEDING_STRING = - Pattern.compile("^\\{(.*?[^\\\\])\\}.*", Pattern.MULTILINE + Pattern.DOTALL); - - private final PBECipher cipher; - - public MavenPlexusCipher() throws IllegalStateException { - try { - cipher = new PBECipher(); - } catch (RuntimeException e) { - throw new IllegalStateException("Unable to instantiate Cipher to decrypt Maven passwords"); - } - } - - @Override - public String encrypt(final String str, final String passPhrase) throws PlexusCipherException { - if (str == null || str.isEmpty()) { - return str; - } - - return cipher.encrypt64(str, passPhrase); - } - - @Override - public String encryptAndDecorate(final String str, final String passPhrase) throws PlexusCipherException { - return decorate(encrypt(str, passPhrase)); - } - - @Override - public String decrypt(final String str, final String passPhrase) throws PlexusCipherException { - if (str == null || str.isEmpty()) { - return str; - } - - return cipher.decrypt64(str, passPhrase); - } - - @Override - public String decryptDecorated(final String str, final String passPhrase) throws PlexusCipherException { - if (str == null || str.isEmpty()) { - return str; - } - - if (isEncryptedString(str)) { - return decrypt(unDecorate(str), passPhrase); - } - - return decrypt(str, passPhrase); - } - - @Override - public boolean isEncryptedString(final String str) { - if (str == null || str.isEmpty()) { - return false; - } - - Matcher matcherWithPrecString = ENCRYPTED_PATTERN_WITH_PRECEDING_STRING.matcher(str); - Matcher matcherWithoutPrecString = ENCRYPTED_PATTERN_WITHOUT_PRECEDING_STRING.matcher(str); - - return matcherWithoutPrecString.matches() || matcherWithoutPrecString.find() - || matcherWithPrecString.matches() || matcherWithPrecString.find(); - } - - @Override - public String unDecorate(final String str) { - - Matcher matcherWithoutPrecString = ENCRYPTED_PATTERN_WITHOUT_PRECEDING_STRING.matcher(str); - if (matcherWithoutPrecString.matches() || matcherWithoutPrecString.find()) { - return matcherWithoutPrecString.group(1); - } - - Matcher matcherWithPrecString = ENCRYPTED_PATTERN_WITH_PRECEDING_STRING.matcher(str); - if (matcherWithPrecString.matches() || matcherWithPrecString.find()) { - return matcherWithPrecString.group(1); - } else { - throw new IllegalStateException("Unable to undecorate decrypted string " + str); - } - } - - @Override - public String decorate(final String str) { - return ENCRYPTED_STRING_DECORATION_START + (str == null ? "" : str) + ENCRYPTED_STRING_DECORATION_STOP; - } - -} diff --git a/maven/impl-maven/src/main/java/org/jboss/shrinkwrap/resolver/impl/maven/internal/decrypt/MavenSecurityDispatcher.java b/maven/impl-maven/src/main/java/org/jboss/shrinkwrap/resolver/impl/maven/internal/decrypt/MavenSecurityDispatcher.java deleted file mode 100644 index 6123a62f3..000000000 --- a/maven/impl-maven/src/main/java/org/jboss/shrinkwrap/resolver/impl/maven/internal/decrypt/MavenSecurityDispatcher.java +++ /dev/null @@ -1,190 +0,0 @@ -/* - * JBoss, Home of Professional Open Source - * Copyright 2013, Red Hat Middleware LLC, and individual contributors - * by the @authors tag. See the copyright.txt in the distribution for a - * full listing of individual contributors. - * - * 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.jboss.shrinkwrap.resolver.impl.maven.internal.decrypt; - -import java.io.File; -import java.util.HashMap; -import java.util.Map; -import java.util.StringTokenizer; -import java.util.logging.Level; -import java.util.logging.Logger; - -import org.jboss.shrinkwrap.resolver.api.InvalidConfigurationFileException; -import org.jboss.shrinkwrap.resolver.impl.maven.bootstrap.MavenSettingsBuilder; -import org.jboss.shrinkwrap.resolver.impl.maven.util.Validate; -import org.sonatype.plexus.components.cipher.PlexusCipher; -import org.sonatype.plexus.components.cipher.PlexusCipherException; -import org.sonatype.plexus.components.sec.dispatcher.SecDispatcher; -import org.sonatype.plexus.components.sec.dispatcher.SecDispatcherException; -import org.sonatype.plexus.components.sec.dispatcher.SecUtil; -import org.sonatype.plexus.components.sec.dispatcher.model.SettingsSecurity; - -/** - * Inspired by DefaultSecDispatched from Sonatype - * - * @author Karel Piwko - * @author Oleg Gusakov - * - */ -class MavenSecurityDispatcher implements SecDispatcher { - private static final Logger log = Logger.getLogger(MavenSecurityDispatcher.class.getName()); - - private static final String DEFAULT_PASSPHRASE = "settings.security"; - - private static final String TYPE_ATTR = "type"; - - private static final char ATTR_START = '['; - - private static final char ATTR_STOP = ']'; - - private final PlexusCipher cipher; - - private SettingsSecurity securitySettings; - - private final File securitySettingsPath; - - MavenSecurityDispatcher(File securitySettings) throws InvalidConfigurationFileException { - this.cipher = new MavenPlexusCipher(); - this.securitySettingsPath = securitySettings; - // settings-security is loaded only if it exists - // error is raised later only in case that it is missing but needed - if (Validate.isReadable(securitySettings.getAbsoluteFile())) { - try { - this.securitySettings = SecUtil.read(securitySettings.getAbsolutePath(), true); - } catch (SecDispatcherException e) { - // exception is ignored, just logged to end user, so he's aware of the problem - // this is default Maven behavior - log.log(Level.WARNING, "Unable to read security configuration from: " - + securitySettings.getAbsolutePath() + ". Configuration will be ignored.", e); - } - } - } - - @Override - public String decrypt(String str) throws SecDispatcherException { - if (!isEncryptedString(str)) { - return str; - } - - String bare = null; - - try { - bare = cipher.unDecorate(str); - } catch (PlexusCipherException e1) { - throw new SecDispatcherException(e1); - } - - Map attr = stripAttributes(bare); - - String res = null; - - if (attr == null || attr.get("type") == null) { - String master = getMaster(); - try { - res = cipher.decrypt(bare, master); - } catch (PlexusCipherException e) { - throw new SecDispatcherException("Unable to decrypt encrypted string", e); - } - } - else { - String type = attr.get(TYPE_ATTR); - throw new UnsupportedOperationException("Unable to lookup security dispatched of type " + type); - } - - return res; - } - - private Map stripAttributes(String str) { - int start = str.indexOf(ATTR_START); - int stop = str.indexOf(ATTR_STOP); - - if (start != -1 && stop != -1 && stop > start) { - if (stop == start + 1) { - return null; - } - - String attrs = str.substring(start + 1, stop).trim(); - - if (attrs == null || attrs.isEmpty()) { - return null; - } - - Map res = null; - StringTokenizer st = new StringTokenizer(attrs, ", "); - - while (st.hasMoreTokens()) { - if (res == null) { - res = new HashMap<>(st.countTokens()); - } - - String pair = st.nextToken(); - - int pos = pair.indexOf('='); - - if (pos == -1) { - continue; - } - - String key = pair.substring(0, pos).trim(); - - if (pos == pair.length()) { - res.put(key, null); - continue; - } - - String val = pair.substring(pos + 1); - - res.put(key, val.trim()); - } - - return res; - } - - return null; - } - - private boolean isEncryptedString(String str) { - if (str == null) { - return false; - } - - return cipher.isEncryptedString(str); - } - - private String getMaster() throws SecDispatcherException, InvalidConfigurationFileException { - - if (securitySettings == null) { - throw new InvalidConfigurationFileException( - "Unable to get security configuration from " + securitySettingsPath.getPath() - + ". Please define path to the settings-security.xml file via -D" + - MavenSettingsBuilder.ALT_SECURITY_SETTINGS_XML_LOCATION - + ", or put it the the default location defined by Maven."); - } - String master = securitySettings.getMaster(); - - if (master == null) { - throw new InvalidConfigurationFileException("Security configuration from " + securitySettingsPath.getPath() - + " does not contain master password"); - } - - try { - return cipher.decryptDecorated(master, DEFAULT_PASSPHRASE); - } catch (PlexusCipherException e) { - throw new SecDispatcherException(e); - } - } -} diff --git a/maven/impl-maven/src/main/java/org/jboss/shrinkwrap/resolver/impl/maven/internal/decrypt/MavenSettingsDecrypter.java b/maven/impl-maven/src/main/java/org/jboss/shrinkwrap/resolver/impl/maven/internal/decrypt/MavenSettingsDecrypter.java deleted file mode 100644 index 6210346b5..000000000 --- a/maven/impl-maven/src/main/java/org/jboss/shrinkwrap/resolver/impl/maven/internal/decrypt/MavenSettingsDecrypter.java +++ /dev/null @@ -1,95 +0,0 @@ -/* - * JBoss, Home of Professional Open Source - * Copyright 2013, Red Hat Middleware LLC, and individual contributors - * by the @authors tag. See the copyright.txt in the distribution for a - * full listing of individual contributors. - * - * 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.jboss.shrinkwrap.resolver.impl.maven.internal.decrypt; - -import java.io.File; -import java.util.ArrayList; -import java.util.List; - -import org.apache.maven.settings.Proxy; -import org.apache.maven.settings.Server; -import org.apache.maven.settings.building.DefaultSettingsProblem; -import org.apache.maven.settings.building.SettingsProblem; -import org.apache.maven.settings.building.SettingsProblem.Severity; -import org.apache.maven.settings.crypto.SettingsDecrypter; -import org.apache.maven.settings.crypto.SettingsDecryptionRequest; -import org.apache.maven.settings.crypto.SettingsDecryptionResult; -import org.sonatype.plexus.components.sec.dispatcher.SecDispatcher; -import org.sonatype.plexus.components.sec.dispatcher.SecDispatcherException; - -/** - * Default settings decrypter configured to decrypt Maven encrypted passwords. - *

- * Inspired by DefaultSettingsDecrypter from Sonatype - * - * @author Karel Piwko - * @author Benjamin Bentmann - */ -public class MavenSettingsDecrypter implements SettingsDecrypter { - - private final SecDispatcher securityDispatcher; - - public MavenSettingsDecrypter(File securitySettings) { - this.securityDispatcher = new MavenSecurityDispatcher(securitySettings); - } - - public SettingsDecryptionResult decrypt(SettingsDecryptionRequest request) { - List problems = new ArrayList<>(); - - List servers = new ArrayList<>(); - - for (Server server : request.getServers()) { - server = server.clone(); - servers.add(server); - - try { - server.setPassword(decrypt(server.getPassword())); - } catch (SecDispatcherException e) { - problems.add(new DefaultSettingsProblem("Failed to decrypt password for server " + server.getId() - + ": " + e.getMessage(), Severity.ERROR, "server: " + server.getId(), -1, -1, e)); - } - - try { - server.setPassphrase(decrypt(server.getPassphrase())); - } catch (SecDispatcherException e) { - problems.add(new DefaultSettingsProblem("Failed to decrypt passphrase for server " + server.getId() - + ": " + e.getMessage(), Severity.ERROR, "server: " + server.getId(), -1, -1, e)); - } - } - - List proxies = new ArrayList<>(); - - for (Proxy proxy : request.getProxies()) { - proxy = proxy.clone(); - proxies.add(proxy); - - try { - proxy.setPassword(decrypt(proxy.getPassword())); - } catch (SecDispatcherException e) { - problems.add(new DefaultSettingsProblem("Failed to decrypt password for proxy " + proxy.getId() - + ": " + e.getMessage(), Severity.ERROR, "proxy: " + proxy.getId(), -1, -1, e)); - } - } - - return new MavenSettingsDecryptionResult(servers, proxies, problems); - } - - private String decrypt(String str) throws SecDispatcherException { - return (str == null) ? null : securityDispatcher.decrypt(str); - } - -} diff --git a/maven/impl-maven/src/main/java/org/jboss/shrinkwrap/resolver/impl/maven/internal/decrypt/MavenSettingsDecryptionResult.java b/maven/impl-maven/src/main/java/org/jboss/shrinkwrap/resolver/impl/maven/internal/decrypt/MavenSettingsDecryptionResult.java deleted file mode 100644 index d06f3af6e..000000000 --- a/maven/impl-maven/src/main/java/org/jboss/shrinkwrap/resolver/impl/maven/internal/decrypt/MavenSettingsDecryptionResult.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * JBoss, Home of Professional Open Source - * Copyright 2013, Red Hat Middleware LLC, and individual contributors - * by the @authors tag. See the copyright.txt in the distribution for a - * full listing of individual contributors. - * - * 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.jboss.shrinkwrap.resolver.impl.maven.internal.decrypt; - -import java.util.Collections; -import java.util.List; - -import org.apache.maven.settings.Proxy; -import org.apache.maven.settings.Server; -import org.apache.maven.settings.building.SettingsProblem; -import org.apache.maven.settings.crypto.SettingsDecryptionResult; - -/** - * Basic envelope for MavenSettingsDecryption result - * - * @author Karel Piwko - * - */ -class MavenSettingsDecryptionResult implements SettingsDecryptionResult { - - private final List servers; - - private final List proxies; - - private final List problems; - - MavenSettingsDecryptionResult(List servers, List proxies, List problems) { - this.servers = servers == null ? Collections.emptyList() : servers; - this.proxies = proxies == null ? Collections.emptyList() : proxies; - this.problems = problems == null ? Collections.emptyList() : problems; - } - - @Override - public Server getServer() { - return servers.isEmpty() ? null : servers.get(0); - } - - @Override - public List getServers() { - return servers; - } - - @Override - public Proxy getProxy() { - return proxies.isEmpty() ? null : proxies.get(0); - } - - @Override - public List getProxies() { - return proxies; - } - - @Override - public List getProblems() { - return problems; - } - -} diff --git a/maven/impl-maven/src/test/java/org/jboss/shrinkwrap/resolver/impl/maven/bootstrap/DefaultSettingsXmlLocationTestCase.java b/maven/impl-maven/src/test/java/org/jboss/shrinkwrap/resolver/impl/maven/bootstrap/DefaultSettingsXmlLocationTestCase.java index 9ecd107b4..004f00ef4 100644 --- a/maven/impl-maven/src/test/java/org/jboss/shrinkwrap/resolver/impl/maven/bootstrap/DefaultSettingsXmlLocationTestCase.java +++ b/maven/impl-maven/src/test/java/org/jboss/shrinkwrap/resolver/impl/maven/bootstrap/DefaultSettingsXmlLocationTestCase.java @@ -20,7 +20,12 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import eu.maveniverse.maven.mima.context.Context; +import eu.maveniverse.maven.mima.context.ContextOverrides; +import eu.maveniverse.maven.mima.context.Runtime; +import eu.maveniverse.maven.mima.context.Runtimes; import org.apache.maven.settings.building.SettingsBuildingRequest; +import org.apache.maven.settings.crypto.SettingsDecrypter; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assumptions; import org.junit.jupiter.api.BeforeAll; @@ -75,7 +80,7 @@ private String removeDoubledSeparator(String path){ // this is calling internal private method that handles logic of settings.xml setup private SettingsBuildingRequest createBuildingRequest() { try { - MavenSettingsBuilder builder = new MavenSettingsBuilder(); + MavenSettingsBuilder builder = new MavenSettingsBuilder(getSettingsDecrypter()); Class clazz = builder.getClass(); Method m = clazz.getDeclaredMethod("getDefaultSettingsBuildingRequest"); m.setAccessible(true); @@ -89,4 +94,10 @@ private SettingsBuildingRequest createBuildingRequest() { return null; } + static SettingsDecrypter getSettingsDecrypter() { + Runtime runtime = Runtimes.INSTANCE.getRuntime(); + Context context = runtime.create(ContextOverrides.create().build()); + return context.lookup().lookup(SettingsDecrypter.class).get(); + } + } diff --git a/maven/impl-maven/src/test/java/org/jboss/shrinkwrap/resolver/impl/maven/bootstrap/MavenSettingsBuilderUnitTestCase.java b/maven/impl-maven/src/test/java/org/jboss/shrinkwrap/resolver/impl/maven/bootstrap/MavenSettingsBuilderUnitTestCase.java index 5e5362823..73f75a57b 100644 --- a/maven/impl-maven/src/test/java/org/jboss/shrinkwrap/resolver/impl/maven/bootstrap/MavenSettingsBuilderUnitTestCase.java +++ b/maven/impl-maven/src/test/java/org/jboss/shrinkwrap/resolver/impl/maven/bootstrap/MavenSettingsBuilderUnitTestCase.java @@ -16,8 +16,13 @@ */ package org.jboss.shrinkwrap.resolver.impl.maven.bootstrap; +import eu.maveniverse.maven.mima.context.Context; +import eu.maveniverse.maven.mima.context.ContextOverrides; +import eu.maveniverse.maven.mima.context.Runtime; +import eu.maveniverse.maven.mima.context.Runtimes; import org.apache.maven.settings.Server; import org.apache.maven.settings.Settings; +import org.apache.maven.settings.crypto.SettingsDecrypter; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; @@ -47,15 +52,21 @@ void afterMethod() { } + static SettingsDecrypter getSettingsDecrypter() { + Runtime runtime = Runtimes.INSTANCE.getRuntime(); + Context context = runtime.create(ContextOverrides.create().build()); + return context.lookup().lookup(SettingsDecrypter.class).get(); + } + @Test void findUserProfile() { - Settings mavenSettings = new MavenSettingsBuilder().buildDefaultSettings(); + Settings mavenSettings = new MavenSettingsBuilder(getSettingsDecrypter()).buildDefaultSettings(); Assertions.assertTrue(mavenSettings.getProfilesAsMap().containsKey("user-profile"), "Profile in user settings not found"); } @Test void findGlobalProfile() { - Settings mavenSettings = new MavenSettingsBuilder().buildDefaultSettings(); + Settings mavenSettings = new MavenSettingsBuilder(getSettingsDecrypter()).buildDefaultSettings(); Assertions.assertTrue(mavenSettings.getProfilesAsMap().containsKey("global-profile"), "Profile in global settings not found"); } @@ -66,7 +77,7 @@ void decryptEncryptedPassword() { System.setProperty(MavenSettingsBuilder.ALT_SECURITY_SETTINGS_XML_LOCATION, "target/settings/profiles/settings-security.xml"); - Settings mavenSettings = new MavenSettingsBuilder().buildDefaultSettings(); + Settings mavenSettings = new MavenSettingsBuilder(getSettingsDecrypter()).buildDefaultSettings(); Server server = mavenSettings.getServer("auth-repository"); Assertions.assertNotNull(server, "Server auth-repository is not null"); @@ -80,7 +91,7 @@ void missingSecuritySettingsNotNeeded() { System.setProperty(MavenSettingsBuilder.ALT_SECURITY_SETTINGS_XML_LOCATION, "target/settings/profiles/non-existing-settings-security.xml"); - Settings mavenSettings = new MavenSettingsBuilder().buildDefaultSettings(); + Settings mavenSettings = new MavenSettingsBuilder(getSettingsDecrypter()).buildDefaultSettings(); Server server = mavenSettings.getServer("auth-repository"); Assertions.assertNotNull(server, "Server auth-repository is not null"); diff --git a/maven/impl-maven/src/test/java/org/jboss/shrinkwrap/resolver/impl/maven/integration/MavenPlexusCipherTestCase.java b/maven/impl-maven/src/test/java/org/jboss/shrinkwrap/resolver/impl/maven/integration/MavenPlexusCipherTestCase.java deleted file mode 100644 index 783490e84..000000000 --- a/maven/impl-maven/src/test/java/org/jboss/shrinkwrap/resolver/impl/maven/integration/MavenPlexusCipherTestCase.java +++ /dev/null @@ -1,127 +0,0 @@ -/* - * JBoss, Home of Professional Open Source - * Copyright 2015, Red Hat Middleware LLC, and individual contributors - * by the @authors tag. See the copyright.txt in the distribution for a - * full listing of individual contributors. - * - * 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.jboss.shrinkwrap.resolver.impl.maven.integration; - -import java.util.ArrayList; -import java.util.List; -import java.util.stream.Stream; - -import org.jboss.shrinkwrap.resolver.impl.maven.internal.decrypt.MavenPlexusCipher; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.MethodSource; - -/** - * Tests the {@link MavenPlexusCipher} whether it correctly evaluates and undecorates the right strings containing a cipher. - * - * @author Matous Jobanek - */ -class MavenPlexusCipherTestCase { - - private static final String UNDECORATED_CIPHER = "70+YZM/w7f8HQrEZUGZABCHAW62qMo+Y8okw7xzLwOM="; - - private static final String ONLY_CIPHER = "{" + UNDECORATED_CIPHER + "}"; - - // Possible strings that could be before/after the cipher - private static final String[] DECORATION_VARIANTS = - { - "", - " ", - "\n", - " \n \n", - " blah \n blah \n", - " \n ${variable} \n", - "\t", - // "{", - "}" - }; - - // Possible strings that doesn't contain a valid cipher, but could be confusing - private static final String[] WITHOUT_CIPHER_VARIANTS = - { - "\\" + ONLY_CIPHER, - "$" + ONLY_CIPHER, - "\\\\" + ONLY_CIPHER, - "{" + UNDECORATED_CIPHER, - "{" + UNDECORATED_CIPHER + "\\}", - "{" + UNDECORATED_CIPHER + "\\\\}", - UNDECORATED_CIPHER + "}", - UNDECORATED_CIPHER - }; - - @MethodSource("getParameters") - @ParameterizedTest - void testIsEncryptedString(String str, boolean isStringEncrypted) { - MavenPlexusCipher mavenPlexusCipher = new MavenPlexusCipher(); - Assertions.assertEquals( - isStringEncrypted, mavenPlexusCipher.isEncryptedString(str), - "The evaluation of the string " + str + " whether it represents a cipher has failed"); - } - - @MethodSource("getParameters") - @ParameterizedTest - void testUnDecorate(String str, boolean isStringEncrypted) { - MavenPlexusCipher mavenPlexusCipher = new MavenPlexusCipher(); - try { - String undecorated = mavenPlexusCipher.unDecorate(str); - - if (isStringEncrypted) { - Assertions.assertEquals(UNDECORATED_CIPHER, undecorated, - "The comparison of the undecorated string and expected cipher has failed"); - } else { - Assertions.fail("The IllegalStateException should have been thrown here - the string: " + str - + " doesn't represent an encrypted string. The method \"unDecorate\" returned: " + undecorated); - } - - } catch (IllegalStateException ise) { - if (isStringEncrypted) { - Assertions.fail("The evaluation or undecoration of the string: " + str - + " has failed, although it should have passed - it represents an encrypted string"); - } - } - } - - private static Stream getParameters() { - List parameters = new ArrayList<>(); - for (String decorator1 : DECORATION_VARIANTS) { - for (String decorator2 : DECORATION_VARIANTS) { - String decCombination = decorator1 + decorator2; - - addCombinations(decorator1, decorator2, decCombination, ONLY_CIPHER, parameters, true); - for (String withoutString : WITHOUT_CIPHER_VARIANTS) { - - if ((withoutString.endsWith("\\}") || withoutString.endsWith(UNDECORATED_CIPHER)) - && (decorator1.contains("}") || decorator2.contains("}"))) { - continue; - } - addCombinations(decorator1, decorator2, decCombination, withoutString, parameters, false); - } - } - } - return parameters.stream(); - } - - private static void addCombinations(String decorator1, String decorator2, String decCombination, - String cipherString, List parameters, boolean isPresent) { - parameters.add(new Object[] { decCombination + cipherString, isPresent }); - parameters.add(new Object[] { cipherString + decCombination, isPresent }); - parameters.add(new Object[] { decCombination + cipherString + decCombination, isPresent }); - parameters.add(new Object[] { decorator1 + cipherString + decorator2, isPresent }); - parameters.add(new Object[] { decorator2 + cipherString + decorator1, isPresent }); - } -}