From 6a1fe4ff4ac2b95e46d4d6c0813d7be9aecde3c5 Mon Sep 17 00:00:00 2001 From: Aaron Gershman Date: Tue, 14 Dec 2021 18:44:51 -0600 Subject: [PATCH] Settings decryption request --- .../maven/AbstractRewriteMojo.java | 7 +++++- .../openrewrite/maven/CycloneDxBomMojo.java | 2 +- .../maven/MavenMojoProjectParser.java | 23 ++++++++++++++----- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/openrewrite/maven/AbstractRewriteMojo.java b/src/main/java/org/openrewrite/maven/AbstractRewriteMojo.java index a492ddf5..a7f28bba 100644 --- a/src/main/java/org/openrewrite/maven/AbstractRewriteMojo.java +++ b/src/main/java/org/openrewrite/maven/AbstractRewriteMojo.java @@ -12,6 +12,7 @@ import org.apache.maven.project.MavenProject; import org.apache.maven.repository.RepositorySystem; import org.apache.maven.rtinfo.RuntimeInformation; +import org.apache.maven.settings.crypto.SettingsDecrypter; import org.codehaus.plexus.util.xml.Xpp3Dom; import org.openrewrite.*; import org.openrewrite.config.ClasspathScanningLoader; @@ -47,6 +48,10 @@ public abstract class AbstractRewriteMojo extends ConfigurableRewriteMojo { @Component protected RuntimeInformation runtime; + @SuppressWarnings("NotNullFieldNotInitialized") + @Component + protected SettingsDecrypter settingsDecrypter; + @SuppressWarnings("NotNullFieldNotInitialized") @Component protected RepositorySystem repositorySystem; @@ -166,7 +171,7 @@ protected ResultsContainer listResults() throws MojoExecutionException { } } ExecutionContext ctx = executionContext(); - MavenMojoProjectParser projectParser = new MavenMojoProjectParser(getLog(), baseDir, pomCacheEnabled, pomCacheDirectory, project, runtime, skipMavenParsing, getExclusions(), sizeThresholdMb, mavenSession); + MavenMojoProjectParser projectParser = new MavenMojoProjectParser(getLog(), baseDir, pomCacheEnabled, pomCacheDirectory, project, runtime, skipMavenParsing, getExclusions(), sizeThresholdMb, mavenSession, settingsDecrypter); List sourceFiles = projectParser.listSourceFiles(styles, ctx); getLog().info("Running recipe(s)..."); diff --git a/src/main/java/org/openrewrite/maven/CycloneDxBomMojo.java b/src/main/java/org/openrewrite/maven/CycloneDxBomMojo.java index d47aac68..18191e85 100644 --- a/src/main/java/org/openrewrite/maven/CycloneDxBomMojo.java +++ b/src/main/java/org/openrewrite/maven/CycloneDxBomMojo.java @@ -32,7 +32,7 @@ public class CycloneDxBomMojo extends AbstractRewriteMojo { public void execute() throws MojoExecutionException { ExecutionContext ctx = executionContext(); Path baseDir = getBaseDir(); - Maven maven = new MavenMojoProjectParser(getLog(), baseDir, pomCacheEnabled, pomCacheDirectory, project, runtime, skipMavenParsing, getExclusions(), sizeThresholdMb, mavenSession).parseMaven(ctx); + Maven maven = new MavenMojoProjectParser(getLog(), baseDir, pomCacheEnabled, pomCacheDirectory, project, runtime, skipMavenParsing, getExclusions(), sizeThresholdMb, mavenSession, settingsDecrypter).parseMaven(ctx); if (maven != null) { File cycloneDxBom = buildCycloneDxBom(maven); projectHelper.attachArtifact(project, "xml", "cyclonedx", cycloneDxBom); diff --git a/src/main/java/org/openrewrite/maven/MavenMojoProjectParser.java b/src/main/java/org/openrewrite/maven/MavenMojoProjectParser.java index 10b08316..1869b99f 100644 --- a/src/main/java/org/openrewrite/maven/MavenMojoProjectParser.java +++ b/src/main/java/org/openrewrite/maven/MavenMojoProjectParser.java @@ -6,8 +6,13 @@ import org.apache.maven.model.Repository; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.logging.Log; +import org.apache.maven.plugins.annotations.Component; import org.apache.maven.project.MavenProject; import org.apache.maven.rtinfo.RuntimeInformation; +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; import org.openrewrite.ExecutionContext; import org.openrewrite.SourceFile; import org.openrewrite.internal.ListUtils; @@ -66,9 +71,10 @@ public class MavenMojoProjectParser { private final Collection exclusions; private final int sizeThresholdMb; private final MavenSession mavenSession; + private final SettingsDecrypter settingsDecrypter; @SuppressWarnings("BooleanParameter") - public MavenMojoProjectParser(Log logger, Path baseDir, boolean pomCacheEnabled, @Nullable String pomCacheDirectory, MavenProject mavenProject, RuntimeInformation runtime, boolean skipMavenParsing, Collection exclusions, int thresholdMb, MavenSession session) { + public MavenMojoProjectParser(Log logger, Path baseDir, boolean pomCacheEnabled, @Nullable String pomCacheDirectory, MavenProject mavenProject, RuntimeInformation runtime, boolean skipMavenParsing, Collection exclusions, int thresholdMb, MavenSession session, SettingsDecrypter settingsDecrypter) { this.logger = logger; this.baseDir = baseDir; this.mavenProject = mavenProject; @@ -78,6 +84,7 @@ public MavenMojoProjectParser(Log logger, Path baseDir, boolean pomCacheEnabled, this.exclusions = exclusions; sizeThresholdMb = thresholdMb; this.mavenSession = session; + this.settingsDecrypter = settingsDecrypter; String javaRuntimeVersion = System.getProperty("java.runtime.version"); String javaVendor = System.getProperty("java.vm.vendor"); @@ -209,11 +216,15 @@ private MavenSettings buildSettings() { ); MavenSettings.Servers servers = new MavenSettings.Servers(); - servers.setServers(mer.getServers().stream().map(s -> new MavenSettings.Server( - s.getId(), - s.getUsername(), - s.getPassword() - )).collect(toList())); + servers.setServers(mer.getServers().stream().map(s -> { + SettingsDecryptionRequest decryptionRequest = new DefaultSettingsDecryptionRequest(s); + SettingsDecryptionResult decryptionResult = settingsDecrypter.decrypt(decryptionRequest); + return new MavenSettings.Server( + s.getId(), + s.getUsername(), + decryptionResult.getServer().getPassword() + ); + }).collect(toList())); return new MavenSettings(profiles, activeProfiles, mirrors, servers); }