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

Settings decryption request #267

Merged
merged 1 commit into from
Dec 15, 2021
Merged
Show file tree
Hide file tree
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
7 changes: 6 additions & 1 deletion src/main/java/org/openrewrite/maven/AbstractRewriteMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<SourceFile> sourceFiles = projectParser.listSourceFiles(styles, ctx);

getLog().info("Running recipe(s)...");
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/openrewrite/maven/CycloneDxBomMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
23 changes: 17 additions & 6 deletions src/main/java/org/openrewrite/maven/MavenMojoProjectParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -66,9 +71,10 @@ public class MavenMojoProjectParser {
private final Collection<String> 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<String> exclusions, int thresholdMb, MavenSession session) {
public MavenMojoProjectParser(Log logger, Path baseDir, boolean pomCacheEnabled, @Nullable String pomCacheDirectory, MavenProject mavenProject, RuntimeInformation runtime, boolean skipMavenParsing, Collection<String> exclusions, int thresholdMb, MavenSession session, SettingsDecrypter settingsDecrypter) {
this.logger = logger;
this.baseDir = baseDir;
this.mavenProject = mavenProject;
Expand All @@ -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");
Expand Down Expand Up @@ -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);
}
Expand Down