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

[MNG-8419][MNG-8424] Too aggressive warning for pre-Maven4 passwords #1970

Merged
merged 8 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.api.services;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

import org.apache.maven.api.annotations.Experimental;

/**
* Base class for all maven exceptions.
*
* @since 4.0.0
*/
@Experimental
public abstract class MavenBuilderException extends MavenException {

private final List<BuilderProblem> problems;

public MavenBuilderException(String message, Throwable cause) {
super(message, cause);
problems = List.of();
}

public MavenBuilderException(String message, List<BuilderProblem> problems) {
super(buildMessage(message, problems), null);
this.problems = problems;
}

protected static String buildMessage(String message, List<BuilderProblem> problems) {
StringBuilder msg = new StringBuilder(message);
ArrayList<BuilderProblem> sorted = new ArrayList<>(problems);
sorted.sort(Comparator.comparing(BuilderProblem::getSeverity));
for (BuilderProblem problem : sorted) {
msg.append("\n * ").append(problem.getSeverity().name()).append(" ").append(problem.getMessage());
}
return msg.toString();
}

public List<BuilderProblem> getProblems() {
return problems;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import java.io.Serial;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.maven.api.annotations.Experimental;

Expand All @@ -30,28 +29,20 @@
* @since 4.0.0
*/
@Experimental
public class SettingsBuilderException extends MavenException {
public class SettingsBuilderException extends MavenBuilderException {

@Serial
private static final long serialVersionUID = 4714858598345418083L;

private final List<BuilderProblem> problems;

/**
* @param message the message to give
* @param e the {@link Exception}
*/
public SettingsBuilderException(String message, Exception e) {
super(message, e);
this.problems = List.of();
}

public SettingsBuilderException(String message, List<BuilderProblem> problems) {
super(message + ": " + problems.stream().map(BuilderProblem::toString).collect(Collectors.joining(", ")), null);
this.problems = List.copyOf(problems);
}

public List<BuilderProblem> getProblems() {
return problems;
super(message, problems);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import java.io.Serial;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.maven.api.annotations.Experimental;

Expand All @@ -30,28 +29,20 @@
* @since 4.0.0
*/
@Experimental
public class ToolchainsBuilderException extends MavenException {
public class ToolchainsBuilderException extends MavenBuilderException {

@Serial
private static final long serialVersionUID = 7899871809665729349L;

private final List<BuilderProblem> problems;

/**
* @param message the message to give
* @param e the {@link Exception}
*/
public ToolchainsBuilderException(String message, Exception e) {
super(message, e);
this.problems = List.of();
}

public ToolchainsBuilderException(String message, List<BuilderProblem> problems) {
super(message + ": " + problems.stream().map(BuilderProblem::toString).collect(Collectors.joining(", ")), null);
this.problems = List.copyOf(problems);
}

public List<BuilderProblem> getProblems() {
return problems;
super(message, problems);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -558,13 +558,16 @@ protected void settings(C context, SettingsBuilder settingsBuilder) throws Excep
context.localRepositoryPath = localRepositoryPath(context);

if (!settingsResult.getProblems().isEmpty()) {
context.logger.warn("");
context.logger.warn("Some problems were encountered while building the effective settings");
context.logger.info("");
context.logger.info(
"Some problems were encountered while building the effective settings (use -X to see details)");

for (BuilderProblem problem : settingsResult.getProblems()) {
context.logger.warn(problem.getMessage() + " @ " + problem.getLocation());
if (context.invokerRequest.options().verbose().orElse(false)) {
for (BuilderProblem problem : settingsResult.getProblems()) {
context.logger.warn(problem.getMessage() + " @ " + problem.getLocation());
}
}
context.logger.warn("");
context.logger.info("");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import java.util.function.Supplier;

Expand Down Expand Up @@ -273,18 +274,12 @@ private Settings decrypt(
return settings;
}
SecDispatcher secDispatcher = new DefaultSecDispatcher(dispatchers, getSecuritySettings(request.getSession()));
final AtomicInteger preMaven4Passwords = new AtomicInteger(0);
Function<String, String> decryptFunction = str -> {
if (str != null && !str.isEmpty() && !str.contains("${") && secDispatcher.isAnyEncryptedString(str)) {
if (secDispatcher.isLegacyEncryptedString(str)) {
// add a problem
problems.add(new DefaultBuilderProblem(
settingsSource.getLocation(),
-1,
-1,
null,
"Pre-Maven 4 legacy encrypted password detected "
+ " - configure password encryption with the help of mvnenc to be compatible with Maven 4.",
BuilderProblem.Severity.WARNING));
preMaven4Passwords.incrementAndGet();
}
try {
return secDispatcher.decrypt(str);
Expand All @@ -295,12 +290,23 @@ private Settings decrypt(
-1,
e,
"Could not decrypt password (fix the corrupted password or remove it, if unused) " + str,
BuilderProblem.Severity.ERROR));
BuilderProblem.Severity.FATAL));
}
}
return str;
};
return new SettingsTransformer(decryptFunction).visit(settings);
Settings result = new SettingsTransformer(decryptFunction).visit(settings);
if (preMaven4Passwords.get() > 0) {
problems.add(new DefaultBuilderProblem(
settingsSource.getLocation(),
-1,
-1,
null,
"Detected " + preMaven4Passwords.get() + " pre-Maven 4 legacy encrypted password(s) "
+ "- configure password encryption with the help of mvnenc for increased security.",
BuilderProblem.Severity.WARNING));
}
return result;
}

private Path getSecuritySettings(ProtoSession session) {
Expand Down
Loading