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

Feature/mip 432 #34

Merged
merged 5 commits into from
Jul 23, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ import groovy.xml.slurpersupport.Node
/**
* This class represents a Mule Configuration XML file.
*/
@SuppressWarnings('SpaceAroundMapEntryColon')
class ConfigurationFile extends ProjectFile {

static final String MULE_CORE_NAMESPACE = 'http://www.mulesoft.org/schema/mule/core'
static final String ELEMENT_FLOWREF = 'flow-ref'
MuleXmlParser parser
private final GPathResult configXml
private final Boolean exists
private Map<String, String> nonGlobalConfig = ['sub-flow':MULE_CORE_NAMESPACE,
'flow':MULE_CORE_NAMESPACE]
private Map<String, String> nonGlobalConfig = ['sub-flow' : MULE_CORE_NAMESPACE,
'flow' : MULE_CORE_NAMESPACE,
'error-handler': MULE_CORE_NAMESPACE]

ConfigurationFile(File file) {
super(file)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class RuleExecutor {
outputStream.write('Rule Results\n'.bytes)

results.each { violation ->
outputStream.write(" $violation.rule.severity: $violation.fileName ".bytes)
outputStream.write(" [$violation.rule.severity] $violation.rule.ruleId - $violation.fileName ".bytes)
outputStream.write((violation.lineNumber > 0 ? "( $violation.lineNumber ) " : '').bytes)
outputStream.write("$violation.message \n".bytes)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class OnErrorLogExceptionRule extends Rule {
'on-error-continue and on-error-propagate'
private static final List<String> ON_ERROR_COMPONENTS = ['on-error-continue', 'on-error-propagate']
private static final String ATTRIBUTE_NAME = 'logException'
private static final String ATTRIBUTE_VALUE_CHECK = 'false'
private static final String ATTRIBUTE_VALUE_CHECK = 'true'

OnErrorLogExceptionRule() {
this.ruleId = RULE_ID
Expand All @@ -25,7 +25,7 @@ class OnErrorLogExceptionRule extends Rule {
application.configurationFiles.each { file ->
ON_ERROR_COMPONENTS.each {
file.findComponents(it, file.MULE_CORE_NAMESPACE).each { comp ->
if (comp.getAttributeValue(ATTRIBUTE_NAME).equalsIgnoreCase(ATTRIBUTE_VALUE_CHECK)) {
if (comp.getAttributeValue(ATTRIBUTE_NAME) != ATTRIBUTE_VALUE_CHECK) {
violations.add(new RuleViolation(this, file.name, comp.lineNumber, RULE_VIOLATION_MESSAGE))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ class UnusedFlowRule extends Rule {

static final String RULE_ID = 'UNUSED_FLOW'
static final String RULE_NAME = 'All Flow and sub-flow are used in application.'
static final String RULE_VIOLATION_MESSAGE = 'Flow or sub-flow is not referenced by flow ref: '
static final String RULE_VIOLATION_MESSAGE = 'Flow component is unused. ' +
'The following flow is not referenced by any flow ref components: '

UnusedFlowRule() {
this.ruleId = RULE_ID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ class MuleArtifactHasSecurePropertiesRule extends Rule {
List<RuleViolation> violations = []
JsonArray sprops = app.muleArtifact.secureProperties
secureProperties.each { prop ->
if (!sprops.contains(prop)) {
violations.add(new RuleViolation(this, app.muleArtifact.name, sprops.lineNumber,
if (!sprops?.contains(prop)) {
violations.add(new RuleViolation(this, app.muleArtifact.name, sprops == null ? 0 : sprops.lineNumber,
'The secureProperties array does not contain the property ' + prop))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,5 @@ class GlobalConfigNoFlowsRuleTest extends Specification {
\t</sub-flow>
</mule>
'''

}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class GlobalConfigRuleTest extends Specification {
when:
testApp.addFile('src/main/mule/bad-global-with-listener.xml', BAD_CONFIG_1)
testApp.addFile('src/main/mule/bad-global-with-router.xml', BAD_CONFIG_2)
testApp.addFile('src/main/mule/global-error-handler.xml', GLOBAL_ERROR_HANDLER_CONFIG)
app = new Application(testApp.appDir)
List<RuleViolation> violations = rule.execute(app)

Expand All @@ -74,6 +75,7 @@ class GlobalConfigRuleTest extends Specification {
violations[0].lineNumber == 9
violations[0].message.contains('config')
violations[0].fileName.contains('bad-global-with-router.xml')
violations.findAll { it.fileName.endsWith('global-error-handler.xml') }.size() == 0
}

@SuppressWarnings(['MethodName', 'MethodReturnTypeRequired'])
Expand Down Expand Up @@ -126,4 +128,17 @@ class GlobalConfigRuleTest extends Specification {
\t</sub-flow>
</mule>
'''

private static final String GLOBAL_ERROR_HANDLER_CONFIG = '''<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
\txmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
\txsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd">
\t<error-handler name="global-error-handler" doc:id="82e9e554-63ed-4a45-bd9e-f3db5ffceca2" >
\t\t<on-error-propagate enableNotifications="true" logException="true" doc:name="On Error Propagate" doc:id="830f0e11-1353-4917-9f0f-a4c233e04700" >
\t\t\t<logger level="ERROR" doc:name="Log Error" doc:id="66c71fbb-dbce-435c-8d22-f9fed6beae53" message="Error!" category="com.avioconsulting.mulelinter"/>
\t\t</on-error-propagate>
\t</error-handler>
</mule>
'''

}
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ output application/java
\t\t\t<logger level="DEBUG" doc:name="Log End" message="Ending"
\t\t\t\t\tcategory="com.avioconsulting.mulelinter" />
\t\t\t<error-handler >
\t\t\t\t<on-error-propagate enableNotifications="true" logException="false"
\t\t\t\t<on-error-propagate enableNotifications="true"
\t\t\t\t\t\tdoc:name="Bad On Error Propagate" doc:id="dccce6bd-82dd-4223-a0cc-01bbe3f892cf" >
\t\t\t\t\t<logger level="ERROR" doc:name="Log Another Error"
\t\t\t\t\t\tmessage='An Error Occured... Propagating'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,30 @@ class MuleArtifactHasSecurePropertiesRuleTest extends Specification {
violations[1].message.contains('client_secret')
}

def 'Missing Secure Properties'() {
given:
Rule rule = new MuleArtifactHasSecurePropertiesRule()

when:
testApp.addFile(MuleArtifact.MULE_ARTIFACT_JSON, MISSING_SECURE_PROPS)
app = new Application(testApp.appDir)
List<RuleViolation> violations = rule.execute(app)

then:
app.muleArtifact != null
violations.size() == 2
violations[0].lineNumber == 0
violations[0].message.contains('client_id')
violations[1].message.contains('client_secret')
}

private static final String MISSING_PROPS_MULE_ARTIFACT = '''{
"minMuleVersion": "4.2.2",
"secureProperties": [
"new.property"
]
}'''
private static final String MISSING_SECURE_PROPS = '''{
"minMuleVersion": "4.2.2"
}'''
}