diff --git a/src/main/java/io/github/dgroup/arch4u/pmd/ObfuscationRequired.java b/src/main/java/io/github/dgroup/arch4u/pmd/ObfuscationRequired.java index b0c519a..fac1241 100644 --- a/src/main/java/io/github/dgroup/arch4u/pmd/ObfuscationRequired.java +++ b/src/main/java/io/github/dgroup/arch4u/pmd/ObfuscationRequired.java @@ -69,21 +69,31 @@ public final class ObfuscationRequired extends AbstractJavaRule { .build(); /** - * Property descriptor with the list of the prohibited methods. + * Property descriptor with the list of the prohibited classes. */ - private static final PropertyDescriptor> SENSITIVE = + private static final PropertyDescriptor> CLASSES = PropertyFactory.stringListProperty("sensitiveClasses") .desc("List of prohibited methods") .emptyDefaultValue() .build(); + /** + * Property descriptor with the list of the prohibited packages. + */ + private static final PropertyDescriptor> PACKAGES = + PropertyFactory.stringListProperty("sensitivePackages") + .desc("List of prohibited packages") + .emptyDefaultValue() + .build(); + /** * Constructor for defining property descriptor. */ @SuppressWarnings("PMD.ConstructorOnlyInitializesOrCallOtherConstructors") public ObfuscationRequired() { this.definePropertyDescriptor(LOGGERS); - this.definePropertyDescriptor(SENSITIVE); + this.definePropertyDescriptor(CLASSES); + this.definePropertyDescriptor(PACKAGES); } @Override @@ -93,7 +103,7 @@ public Object visit(final ASTVariableDeclaratorId vardecl, final Object data) { final JavaNameOccurrence occurrence = (JavaNameOccurrence) usage; getArguments(occurrence) .stream() - .filter(this::isSensitiveData) + .filter(this::hasSensitiveData) .forEach(arg -> this.addViolation(data, arg)); } } @@ -126,6 +136,16 @@ private static List getArguments(final JavaNameOccurrence occurre .orElse(Collections.emptyList()); } + /** + * Checks if the argument is a class with sensitive data + * or if it contains in the prohibited package with such classes. + * @param argument Expression node, logger argument. + * @return True if there is sensitive data. + */ + private boolean hasSensitiveData(final ASTExpression argument) { + return this.isSensitiveData(argument) || this.isInProhibitedPackage(argument); + } + /** * Checks if the object has sensitive data. In this case it's not allowed * to log it without applying obfuscation. @@ -140,7 +160,7 @@ private boolean isSensitiveData(final ASTExpression argument) { } else { node = argument; } - return this.getProperty(SENSITIVE) + return this.getProperty(CLASSES) .stream() .anyMatch(clss -> TypeIsFunction.typeIs(node, clss)); } @@ -160,4 +180,17 @@ private static boolean hasDirectToStringInvocation(final ASTExpression expressio .isPresent(); } + /** + * Checks if the argument contains in the prohibited package. + * @param node Expression node, logger argument. + * @return True if the argument contains in the prohibited package. + */ + private boolean isInProhibitedPackage(final net.sourceforge.pmd.lang.java.ast.TypeNode node) { + final String fulltypename = Optional.ofNullable(node.getType()) + .map(Class::getTypeName) + .orElse(null); + return fulltypename != null + && this.getProperty(PACKAGES).stream().anyMatch(fulltypename::startsWith); + } + } diff --git a/src/main/resources/io/github/dgroup/arch4u/pmd/arch4u-template-ruleset.xml b/src/main/resources/io/github/dgroup/arch4u/pmd/arch4u-template-ruleset.xml index 7132b33..f6d89a5 100644 --- a/src/main/resources/io/github/dgroup/arch4u/pmd/arch4u-template-ruleset.xml +++ b/src/main/resources/io/github/dgroup/arch4u/pmd/arch4u-template-ruleset.xml @@ -121,6 +121,7 @@ |org.apache.log4j.Logger |org.apache.logging.log4j.Logger"/> + + + + + [BAD]: prohibited package and subpackage + org.slf4j.Logger + java.lang.Integer + io.github.dgroup.arch4u.pmd.test_entity.secret + 2 + 10, 14 +