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

Add rule to disable API Console. #55

Merged
merged 1 commit into from
Oct 7, 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
@@ -0,0 +1,28 @@
package com.avioconsulting.mule.linter.rule.configuration

import com.avioconsulting.mule.linter.model.Application
import com.avioconsulting.mule.linter.model.rule.Rule
import com.avioconsulting.mule.linter.model.rule.RuleViolation

class ApiConsoleDisabledRule extends Rule {

static final String RULE_ID = 'API_CONSOLE_DISABLED'
static final String RULE_NAME = 'API Console Disabled Rule'
static final String RULE_VIOLATION_MESSAGE = 'API Console should be removed or disabled before deployment'

ApiConsoleDisabledRule() {
this.ruleId = RULE_ID
this.ruleName = RULE_NAME
}

@Override
List<RuleViolation> execute(Application application) {
List<RuleViolation> violations = []
application.flows.findAll({it.children.any({it.componentName == 'console'})}).each {
if (!it.hasAttributeValue('initialState') || it.getAttributeValue('initialState') != 'stopped') {
violations.add(new RuleViolation(this, it.file.path, it.lineNumber, RULE_VIOLATION_MESSAGE))
}
}
return violations
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package com.avioconsulting.mule.linter.rule.configuration

import com.avioconsulting.mule.linter.TestApplication
import com.avioconsulting.mule.linter.model.Application
import com.avioconsulting.mule.linter.model.rule.Rule
import com.avioconsulting.mule.linter.model.rule.RuleViolation
import spock.lang.Specification

class ApiConsoleDisabledRuleTest extends Specification{

private final TestApplication testApp = new TestApplication()
private Application app

def setup() {
testApp.initialize()
testApp.addPom()
testApp.addFile('src/main/mule/main.xml', FLOWS)

app = new Application(testApp.appDir)
}

def cleanup() {
testApp.remove()
}

def 'Active flows with API Console should fail'() {
given:
Rule rule = new ApiConsoleDisabledRule()

when:
List<RuleViolation> violations = rule.execute(app)

then:
violations.size() == 1
violations[0].message == ApiConsoleDisabledRule.RULE_VIOLATION_MESSAGE
}

private static final String FLOWS = '''
<mule xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:apikit="http://www.mulesoft.org/schema/mule/mule-apikit"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/mule-apikit http://www.mulesoft.org/schema/mule/mule-apikit/current/mule-apikit.xsd
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
\t<http:listener-config name="httpListenerConfig">
\t\t<http:listener-connection host="0.0.0.0" port="8081" />
\t</http:listener-config>
\t<apikit:config name="api-config" api="api.raml" raml="api.raml" outboundHeadersMapName="outboundHeaders" httpStatusVarName="httpStatus" />

\t<flow name="api-main">
\t\t<http:listener config-ref="httpListenerConfig" path="/api/*">
\t\t\t<http:response statusCode="#[vars.httpStatus default 200]">
\t\t\t\t<http:headers><![CDATA[#[output application/java
---
{
\t"x-correlation-id" : vars.correlationId
}]]]></http:headers>
\t\t\t</http:response>
\t\t\t<http:error-response statusCode="#[vars.httpStatus default 500]">
\t\t\t\t<http:body><![CDATA[#[payload]]]></http:body>
\t\t\t\t<http:headers><![CDATA[#[output application/java
---
{
\t"x-correlation-id" : vars.correlationId
}]]]></http:headers>
\t\t\t</http:error-response>
\t\t</http:listener>
\t\t<apikit:router config-ref="api-config" />
\t</flow>

\t<flow name="api-console">
\t\t<http:listener config-ref="httpListenerConfig" path="/console/*">
\t\t\t<http:response statusCode="#[vars.httpStatus default 200]" />
\t\t\t<http:error-response statusCode="#[vars.httpStatus default 500]">
\t\t\t\t<http:body><![CDATA[#[payload]]]></http:body>
\t\t\t</http:error-response>
\t\t</http:listener>
\t\t<apikit:console config-ref="api-config" />
\t</flow>

\t<flow name="api-console-2" initialState="stopped">
\t\t<http:listener config-ref="httpListenerConfig" path="/console/*">
\t\t\t<http:response statusCode="#[vars.httpStatus default 200]" />
\t\t\t<http:error-response statusCode="#[vars.httpStatus default 500]">
\t\t\t\t<http:body><![CDATA[#[payload]]]></http:body>
\t\t\t</http:error-response>
\t\t</http:listener>
\t\t<apikit:console config-ref="api-config" />
\t</flow>

\t<flow name="get:\\v1\\widgets:api-config">
\t\t<flow-ref doc:name="get-widgets" doc:id="450de59d-d74f-4df2-aa99-a7a3a3199ead" name="get-widgets" />
\t</flow>
</mule>'''
}