Skip to content

Commit

Permalink
Merge pull request #23 from avioconsulting/feature/MIP-419
Browse files Browse the repository at this point in the history
Feature/mip 419
  • Loading branch information
thedevnisarg authored Jul 14, 2020
2 parents b2fe2b8 + d82f546 commit a6887b3
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ class PropertyFile {
return file
}

Properties getProperties() {
return properties
}

Integer getPropertyCount() {
return properties.size()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.avioconsulting.mule.linter.rule.property

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

class EncryptedPasswordRule extends Rule {

static final String RULE_ID = 'ENCRYPTED_VALUE'
static final String RULE_NAME = 'Property File contain ‘secret’ and ‘password’ encrypted'
static final String RULE_VIOLATION_MESSAGE = 'Property file contain ‘secret’ or ‘password’ not encrypted: '
static final String ENC_REGEX = '(\\!\\[.*?\\])'

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

static Boolean isEncrypted(String value) {
return value.trim().matches(ENC_REGEX)
}

@Override
List<RuleViolation> execute(Application application) {
List<RuleViolation> violations = []

List<PropertyFile> propFiles = application.propertyFiles

propFiles.each { file ->
file.getProperties().each {
String propName = it.key.toLowerCase()
if ( propName.contains('password') || propName.contains('secret')) {
if (!isEncrypted(it.value.toString())) {
violations.add(new RuleViolation(this, file.getFile().absolutePath,
0, RULE_VIOLATION_MESSAGE + propName))
}
}
}
}
return violations
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,22 @@ class TestApplication {
}
}

void cleanDirectory(String directory) {
File directoryPath = new File(appDir, directory)
directoryPath.listFiles().each {
if (!it.isDirectory())
it.delete();
}
}

void buildConfigContent(String filename, String content) {
addFile("src/main/mule/$filename", MULE_CONFIG_START + content + MULE_CONFIG_END)
}

void buildPropertyContent(String filename, String content) {
addFile("src/main/resources/properties/$filename", content)
}

private void buildDirectoryStructure() {
DIRECTORY_STRUCTURE.each { dir ->
File directory = new File(appDir, dir)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.avioconsulting.mule.linter.rule.property

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

@SuppressWarnings(['MethodName', 'MethodReturnTypeRequired', 'GStringExpressionWithinString'])
class EncryptedPasswordRuleTest extends Specification {

private final TestApplication testApp = new TestApplication()

def setup() {
testApp.create()
testApp.cleanDirectory('src/main/resources/properties/')
}

def cleanup() {
testApp.remove()
}

def 'Property File encrypted password'() {
given:
testApp.addFile('src/main/resources/properties/sample-mule-app.test.properties', GOOD_PROPERTY_1)
testApp.addFile('src/main/resources/properties/sample-mule-app.dev.properties', GOOD_PROPERTY_1)
Rule rule = new EncryptedPasswordRule()

when:
Application app = new Application(testApp.appDir)
List<RuleViolation> violations = rule.execute(app)

then:
app.propertyFiles.size() == 2
violations.size() == 0
}

def 'Property File not encrypted password'() {
given:
testApp.addFile('src/main/resources/properties/sample-mule-app.test.properties', BAD_PROPERTY_1)
testApp.addFile('src/main/resources/properties/sample-mule-app.dev.properties', BAD_PROPERTY_2)
Rule rule = new EncryptedPasswordRule()

when:
Application app = new Application(testApp.appDir)
List<RuleViolation> violations = rule.execute(app)

then:
app.propertyFiles.size() == 2
violations.size() == 3
violations[0].fileName.contains('sample-mule-app.test.properties')
violations[0].message.endsWith('db.secret')
violations[1].fileName.contains('sample-mule-app.dev.properties')
violations[1].message.endsWith('db.secret')
violations[2].message.endsWith('password')
}

private static final String GOOD_PROPERTY_1 = '''
user=jallen
password=![abcdef==]
db.port = 1521
db.host = localhost
db.user = areed
db.secret = ![abcdef==]
'''

private static final String BAD_PROPERTY_1 = '''
user=jallen
password= ![abcdef==]
db.port = 1521
db.host = localhost
db.user = areed
db.secret = BillsRule!
'''

private static final String BAD_PROPERTY_2 = '''
user=jallen
password=mypassword
db.port = 1521
db.host = localhost
db.user = areed
db.secret = BillsRule!
'''

}

0 comments on commit a6887b3

Please sign in to comment.