Skip to content

Commit

Permalink
Password decription filter
Browse files Browse the repository at this point in the history
  • Loading branch information
krystian-panek-vmltech committed May 10, 2021
1 parent d1103c3 commit ccd4568
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/com/neva/gradle/fork/config/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ abstract class Config(val fork: ForkExtension, val name: String) {
convention(project.layout.projectDirectory.dir("gradle/fork"))
}

val templateEngine = TemplateEngine(project)
val templateEngine = TemplateEngine(fork)

fun findTemplateFile(templateName: String): File? {
val pebFile = templateDir.get().asFile.resolve("$templateName.peb")
Expand Down
41 changes: 23 additions & 18 deletions src/main/kotlin/com/neva/gradle/fork/template/TemplateEngine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package com.neva.gradle.fork.template
import com.mitchellbosecke.pebble.PebbleEngine
import com.mitchellbosecke.pebble.lexer.Syntax
import com.mitchellbosecke.pebble.loader.StringLoader
import org.gradle.api.Project
import com.neva.gradle.fork.ForkExtension
import java.io.StringWriter
import java.util.regex.Pattern

class TemplateEngine(val project: Project) {
class TemplateEngine(val fork: ForkExtension) {

private val project = fork.project

private val projectProperties by lazy {
mapOf("prop" to project.properties)
Expand All @@ -25,10 +27,28 @@ class TemplateEngine(val project: Project) {
mapOf("system" to result)
}

private val engine by lazy {
PebbleEngine.Builder()
.extension(TemplateExtension(fork.props))
.autoEscaping(false)
.cacheActive(false)
.strictVariables(true)
.newLineTrimming(false)
.loader(StringLoader())
.syntax(
Syntax.Builder()
.setEnableNewLineTrimming(false)
.setPrintOpenDelimiter(VAR_PREFIX)
.setPrintCloseDelimiter(VAR_SUFFIX)
.build()
)
.build()
}

fun render(template: String, props: Map<String, Any?>): String {
val effectiveProps = envProperties + systemProperties + projectProperties + props
val renderer = StringWriter()
ENGINE.getTemplate(template).evaluate(renderer, effectiveProps)
engine.getTemplate(template).evaluate(renderer, effectiveProps)

return renderer.toString()
}
Expand Down Expand Up @@ -60,20 +80,5 @@ class TemplateEngine(val project: Project) {
private const val VAR_PREFIX = "{{"

private const val VAR_SUFFIX = "}}"

private val ENGINE = PebbleEngine.Builder()
.extension(TemplateExtension())
.autoEscaping(false)
.cacheActive(false)
.strictVariables(true)
.newLineTrimming(false)
.loader(StringLoader())
.syntax(Syntax.Builder()
.setEnableNewLineTrimming(false)
.setPrintOpenDelimiter(VAR_PREFIX)
.setPrintCloseDelimiter(VAR_SUFFIX)
.build()
)
.build()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package com.neva.gradle.fork.template

import com.mitchellbosecke.pebble.extension.AbstractExtension
import com.mitchellbosecke.pebble.extension.Filter
import com.neva.gradle.fork.PropsExtension
import com.neva.gradle.fork.template.filter.PasswordFilter
import com.neva.gradle.fork.template.filter.SubstituteFilter

class TemplateExtension : AbstractExtension() {
class TemplateExtension(val props: PropsExtension) : AbstractExtension() {

override fun getFilters(): MutableMap<String, Filter> {
return mutableMapOf(
"substitute" to SubstituteFilter()
"substitute" to SubstituteFilter(),
"password" to PasswordFilter(props)
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.neva.gradle.fork.template.filter

import com.mitchellbosecke.pebble.extension.Filter
import com.mitchellbosecke.pebble.template.EvaluationContext
import com.mitchellbosecke.pebble.template.PebbleTemplate
import com.neva.gradle.fork.PropsExtension

/**
* Decrypts password on demand.
*/
class PasswordFilter(val props: PropsExtension) : Filter {
override fun apply(input: Any, args: MutableMap<String, Any>, self: PebbleTemplate, context: EvaluationContext, lineNumber: Int): Any? {
return if (input is String) {
props.encryptor.decrypt(input)
} else {
null
}
}

override fun getArgumentNames(): List<String>? {
return listOf()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,7 @@ import org.apache.commons.lang3.StringUtils
* There is 'replace' filter in core but takes a map as argument.
*/
class SubstituteFilter : Filter {
override fun apply(
input: Any,
args: MutableMap<String, Any>,
self: PebbleTemplate,
context: EvaluationContext,
lineNumber: Int
): Any? {
override fun apply(input: Any, args: MutableMap<String, Any>, self: PebbleTemplate, context: EvaluationContext, lineNumber: Int): Any? {
return if (input is String) {
val search = args["search"] ?: throw IllegalArgumentException("No search argument passed to substitute filter.")
val replace = args["replace"] ?: throw IllegalArgumentException("No replace argument passed to substitute filter.")
Expand Down

0 comments on commit ccd4568

Please sign in to comment.