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

Move off direct SecDispatcher use #1848

Merged
merged 1 commit into from
Jan 2, 2025
Merged
Changes from all 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
22 changes: 14 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -169,6 +169,13 @@
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-compat</artifactId>
<version>${maven.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
@@ -190,6 +197,13 @@
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-settings-builder</artifactId>
<version>${maven.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
@@ -244,7 +258,6 @@
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
<version>3.0.24</version>
<scope>provided</scope>
</dependency>

<dependency>
@@ -295,13 +308,6 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.sonatype.plexus</groupId>
<artifactId>plexus-sec-dispatcher</artifactId>
<version>1.3</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
20 changes: 10 additions & 10 deletions src/main/java/io/fabric8/maven/docker/util/AuthConfigFactory.java
Original file line number Diff line number Diff line change
@@ -6,7 +6,6 @@
import java.io.Reader;
import com.google.gson.JsonObject;

import java.lang.reflect.Method;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
@@ -33,10 +32,12 @@
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.settings.Server;
import org.apache.maven.settings.Settings;
import org.apache.maven.settings.crypto.DefaultSettingsDecryptionRequest;
import org.apache.maven.settings.crypto.SettingsDecrypter;
import org.apache.maven.settings.crypto.SettingsDecryptionResult;
import org.codehaus.plexus.PlexusContainer;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.sonatype.plexus.components.sec.dispatcher.SecDispatcher;

import com.google.common.net.UrlEscapers;
import com.google.gson.Gson;
@@ -670,16 +671,15 @@ private Server checkForServer(Server server, String id, String registry, String

private String decrypt(String password) throws MojoExecutionException {
try {
// Done by reflection since I have classloader issues otherwise
Object secDispatcher = container.lookup(SecDispatcher.ROLE, "maven");
Method method = secDispatcher.getClass().getMethod("decrypt",String.class);
synchronized(secDispatcher) {
return (String) method.invoke(secDispatcher, password);
}
SettingsDecrypter settingsDecrypter = container.lookup(SettingsDecrypter.class);
Server stub = new Server();
stub.setUsername("whatever");
stub.setPassword(password);

SettingsDecryptionResult result = settingsDecrypter.decrypt(new DefaultSettingsDecryptionRequest(stub));
return result.getServers().get(0).getPassword();
} catch (ComponentLookupException e) {
throw new MojoExecutionException("Error looking security dispatcher",e);
} catch (ReflectiveOperationException e) {
throw new MojoExecutionException("Cannot decrypt password: " + e.getCause(),e);
}
}

3 changes: 3 additions & 0 deletions src/test/java/io/fabric8/maven/docker/PushMojoBuildXTest.java
Original file line number Diff line number Diff line change
@@ -14,6 +14,8 @@
import org.apache.maven.project.MavenProject;
import org.apache.maven.settings.Server;
import org.apache.maven.settings.Settings;
import org.apache.maven.settings.crypto.DefaultSettingsDecrypter;
import org.apache.maven.settings.crypto.SettingsDecrypter;
import org.codehaus.plexus.PlexusContainer;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
import org.junit.jupiter.api.AfterEach;
@@ -62,6 +64,7 @@ void setup() throws MojoExecutionException, MojoFailureException, IOException, C
DockerAccess dockerAccess = mock(DockerAccess.class);
PlexusContainer mockedPlexusContainer = mock(PlexusContainer.class);
SecDispatcher mockedSecDispatcher = mock(SecDispatcher.class);
when(mockedPlexusContainer.lookup(SettingsDecrypter.class)).thenReturn(new DefaultSettingsDecrypter(mockedSecDispatcher));
ServiceHubFactory serviceHubFactory = new ServiceHubFactory();
when(mockedMavenSettings.getInteractiveMode()).thenReturn(false);
Properties properties = new Properties();
Original file line number Diff line number Diff line change
@@ -14,6 +14,8 @@
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.settings.Server;
import org.apache.maven.settings.Settings;
import org.apache.maven.settings.crypto.DefaultSettingsDecrypter;
import org.apache.maven.settings.crypto.SettingsDecrypter;
import org.codehaus.plexus.PlexusContainer;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
import org.codehaus.plexus.util.Base64;
@@ -101,7 +103,7 @@ public String decrypt(String password) {

@BeforeEach
void containerSetup() throws ComponentLookupException, SecDispatcherException {
Mockito.lenient().when(container.lookup(SecDispatcher.ROLE, "maven")).thenReturn(secDispatcher);
Mockito.lenient().when(container.lookup(SettingsDecrypter.class)).thenReturn(new DefaultSettingsDecrypter(secDispatcher));
Mockito.lenient().when(secDispatcher.decrypt(Mockito.anyString())).thenAnswer(invocation -> invocation.getArguments()[0]);

factory = new AuthConfigFactory(container);