Skip to content

Commit

Permalink
Move off direct SecDispatcher use (#1848)
Browse files Browse the repository at this point in the history
Use the "official" way instead, and it does
provide backward compatibility support as well.
  • Loading branch information
cstamas authored Jan 2, 2025
1 parent 2444e2b commit 1b28e22
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 19 deletions.
22 changes: 14 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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>
Expand Down Expand Up @@ -244,7 +258,6 @@
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
<version>3.0.24</version>
<scope>provided</scope>
</dependency>

<dependency>
Expand Down Expand Up @@ -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>
Expand Down
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
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
}

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

0 comments on commit 1b28e22

Please sign in to comment.