Skip to content

Commit 1b28e22

Browse files
authored
Move off direct SecDispatcher use (#1848)
Use the "official" way instead, and it does provide backward compatibility support as well.
1 parent 2444e2b commit 1b28e22

File tree

4 files changed

+30
-19
lines changed

4 files changed

+30
-19
lines changed

pom.xml

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,13 @@
169169
<scope>provided</scope>
170170
</dependency>
171171

172+
<dependency>
173+
<groupId>org.apache.maven</groupId>
174+
<artifactId>maven-compat</artifactId>
175+
<version>${maven.version}</version>
176+
<scope>provided</scope>
177+
</dependency>
178+
172179
<dependency>
173180
<groupId>org.apache.maven</groupId>
174181
<artifactId>maven-core</artifactId>
@@ -190,6 +197,13 @@
190197
<scope>provided</scope>
191198
</dependency>
192199

200+
<dependency>
201+
<groupId>org.apache.maven</groupId>
202+
<artifactId>maven-settings-builder</artifactId>
203+
<version>${maven.version}</version>
204+
<scope>provided</scope>
205+
</dependency>
206+
193207
<dependency>
194208
<groupId>org.apache.maven.plugin-tools</groupId>
195209
<artifactId>maven-plugin-annotations</artifactId>
@@ -244,7 +258,6 @@
244258
<groupId>org.codehaus.plexus</groupId>
245259
<artifactId>plexus-utils</artifactId>
246260
<version>3.0.24</version>
247-
<scope>provided</scope>
248261
</dependency>
249262

250263
<dependency>
@@ -295,13 +308,6 @@
295308
<scope>test</scope>
296309
</dependency>
297310

298-
<dependency>
299-
<groupId>org.sonatype.plexus</groupId>
300-
<artifactId>plexus-sec-dispatcher</artifactId>
301-
<version>1.3</version>
302-
<scope>provided</scope>
303-
</dependency>
304-
305311
<dependency>
306312
<groupId>org.yaml</groupId>
307313
<artifactId>snakeyaml</artifactId>

src/main/java/io/fabric8/maven/docker/util/AuthConfigFactory.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import java.io.Reader;
77
import com.google.gson.JsonObject;
88

9-
import java.lang.reflect.Method;
109
import java.net.URI;
1110
import java.net.URISyntaxException;
1211
import java.nio.charset.StandardCharsets;
@@ -33,10 +32,12 @@
3332
import org.apache.maven.plugin.MojoFailureException;
3433
import org.apache.maven.settings.Server;
3534
import org.apache.maven.settings.Settings;
35+
import org.apache.maven.settings.crypto.DefaultSettingsDecryptionRequest;
36+
import org.apache.maven.settings.crypto.SettingsDecrypter;
37+
import org.apache.maven.settings.crypto.SettingsDecryptionResult;
3638
import org.codehaus.plexus.PlexusContainer;
3739
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
3840
import org.codehaus.plexus.util.xml.Xpp3Dom;
39-
import org.sonatype.plexus.components.sec.dispatcher.SecDispatcher;
4041

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

671672
private String decrypt(String password) throws MojoExecutionException {
672673
try {
673-
// Done by reflection since I have classloader issues otherwise
674-
Object secDispatcher = container.lookup(SecDispatcher.ROLE, "maven");
675-
Method method = secDispatcher.getClass().getMethod("decrypt",String.class);
676-
synchronized(secDispatcher) {
677-
return (String) method.invoke(secDispatcher, password);
678-
}
674+
SettingsDecrypter settingsDecrypter = container.lookup(SettingsDecrypter.class);
675+
Server stub = new Server();
676+
stub.setUsername("whatever");
677+
stub.setPassword(password);
678+
679+
SettingsDecryptionResult result = settingsDecrypter.decrypt(new DefaultSettingsDecryptionRequest(stub));
680+
return result.getServers().get(0).getPassword();
679681
} catch (ComponentLookupException e) {
680682
throw new MojoExecutionException("Error looking security dispatcher",e);
681-
} catch (ReflectiveOperationException e) {
682-
throw new MojoExecutionException("Cannot decrypt password: " + e.getCause(),e);
683683
}
684684
}
685685

src/test/java/io/fabric8/maven/docker/PushMojoBuildXTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import org.apache.maven.project.MavenProject;
1515
import org.apache.maven.settings.Server;
1616
import org.apache.maven.settings.Settings;
17+
import org.apache.maven.settings.crypto.DefaultSettingsDecrypter;
18+
import org.apache.maven.settings.crypto.SettingsDecrypter;
1719
import org.codehaus.plexus.PlexusContainer;
1820
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
1921
import org.junit.jupiter.api.AfterEach;
@@ -62,6 +64,7 @@ void setup() throws MojoExecutionException, MojoFailureException, IOException, C
6264
DockerAccess dockerAccess = mock(DockerAccess.class);
6365
PlexusContainer mockedPlexusContainer = mock(PlexusContainer.class);
6466
SecDispatcher mockedSecDispatcher = mock(SecDispatcher.class);
67+
when(mockedPlexusContainer.lookup(SettingsDecrypter.class)).thenReturn(new DefaultSettingsDecrypter(mockedSecDispatcher));
6568
ServiceHubFactory serviceHubFactory = new ServiceHubFactory();
6669
when(mockedMavenSettings.getInteractiveMode()).thenReturn(false);
6770
Properties properties = new Properties();

src/test/java/io/fabric8/maven/docker/util/AuthConfigFactoryTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import org.apache.maven.plugin.MojoExecutionException;
1515
import org.apache.maven.settings.Server;
1616
import org.apache.maven.settings.Settings;
17+
import org.apache.maven.settings.crypto.DefaultSettingsDecrypter;
18+
import org.apache.maven.settings.crypto.SettingsDecrypter;
1719
import org.codehaus.plexus.PlexusContainer;
1820
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
1921
import org.codehaus.plexus.util.Base64;
@@ -101,7 +103,7 @@ public String decrypt(String password) {
101103

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

107109
factory = new AuthConfigFactory(container);

0 commit comments

Comments
 (0)