Skip to content

Commit

Permalink
Encryption imprs
Browse files Browse the repository at this point in the history
  • Loading branch information
Krystian Panek committed Feb 3, 2021
1 parent e5c349d commit b9f1cfa
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
7 changes: 1 addition & 6 deletions src/main/kotlin/com/neva/gradle/fork/ForkExtension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import com.neva.gradle.fork.config.properties.PropertyDefinitions
import com.neva.gradle.fork.tasks.RequirePropertiesTask
import com.neva.gradle.fork.tasks.ForkTask
import com.neva.gradle.fork.tasks.PropertiesTask
import nu.studer.java.util.OrderedProperties
import org.gradle.api.Action
import org.gradle.api.Project
import org.gradle.api.tasks.Internal
Expand Down Expand Up @@ -68,12 +67,8 @@ open class ForkExtension(val project: Project, val props: PropsExtension) {
}

logger.info("Loading properties from file '$file'")

val override = project.findProperty("fork.properties.override")?.toString()?.toBoolean() ?: true
OrderedProperties().apply { file.inputStream().use { load(it.bufferedReader()) } }.entrySet().forEach { (n, v) ->
val name = n.toString()
val value = props.encryptor.decrypt(v as String)!!

props.read(file).forEach { (name, value) ->
when {
name.startsWith(SYSTEM_PROP_PREFIX) -> System.setProperty(name.substringAfter(SYSTEM_PROP_PREFIX), value)
else -> {
Expand Down
29 changes: 25 additions & 4 deletions src/main/kotlin/com/neva/gradle/fork/PropsExtension.kt
Original file line number Diff line number Diff line change
@@ -1,22 +1,43 @@
package com.neva.gradle.fork

import com.neva.gradle.fork.config.properties.PropertyType
import com.neva.gradle.fork.encryption.Encryption
import nu.studer.java.util.OrderedProperties
import org.apache.commons.io.FilenameUtils
import org.gradle.api.Project
import java.io.File

open class PropsExtension(private val project: Project) {

internal val encryptor = Encryption.of(project)
internal val encryptor by lazy { Encryption.of(project) }

fun named(name: String): String? {
private val encrypted get() = project.extensions.findByType(ForkExtension::class.java)
?.propertyDefinitions?.all.orEmpty().asSequence()
.filter { it.type == PropertyType.PASSWORD }.map { it.name }.toList()

fun read(file: File): Map<String, String?> {
val properties = OrderedProperties().apply { file.inputStream().use { load(it.bufferedReader()) } }
return properties.entrySet().map { (k, v) -> k to decrypt(k, v) }.toMap()
}

operator fun get(name: String): String? {
val value = project.findProperty(name)?.toString()
if (value.isNullOrBlank()) {
return value
}

return encryptor.decrypt(value)
return decrypt(name, value)
}

operator fun get(name: String) = named(name)
private fun decrypt(name: String, value: String?) = when {
value.isNullOrBlank() -> value
encrypted.contains(name) || isEncrypted(value) -> encryptor.decrypt(value)
else -> value
}

private fun isEncrypted(text: String): Boolean {
return FilenameUtils.wildcardMatch(text, "{*=}")
}

companion object {

Expand Down
9 changes: 2 additions & 7 deletions src/main/kotlin/com/neva/gradle/fork/encryption/Encryption.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.neva.gradle.fork.encryption

import com.neva.gradle.fork.ForkException
import org.apache.commons.io.FilenameUtils
import org.gradle.api.Project
import javax.crypto.Cipher
import javax.crypto.SecretKeyFactory
Expand All @@ -22,7 +21,7 @@ internal class Encryption private constructor(private val ecipher: Cipher, priva

@Suppress("TooGenericExceptionCaught")
fun encrypt(text: String?): String? {
if (text.isNullOrBlank() || isEncrypted(text)) {
if (text.isNullOrBlank()) {
return text
}

Expand All @@ -35,13 +34,9 @@ internal class Encryption private constructor(private val ecipher: Cipher, priva
}
}

private fun isEncrypted(text: String): Boolean {
return FilenameUtils.wildcardMatch(text, "{*=}")
}

@Suppress("TooGenericExceptionCaught")
fun decrypt(text: String?): String? {
if (text.isNullOrBlank() || !isEncrypted(text)) {
if (text.isNullOrBlank()) {
return text
}

Expand Down

0 comments on commit b9f1cfa

Please sign in to comment.