Skip to content

Commit

Permalink
Encryption impr
Browse files Browse the repository at this point in the history
  • Loading branch information
krystian-panek-vmltech committed Dec 13, 2021
1 parent cd20708 commit fd98ab4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/main/kotlin/com/neva/gradle/fork/PropsExtension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ open class PropsExtension(private val project: Project) {

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 encryptor.decrypt(v) }.toMap()
return properties.entrySet().associate { (k, v) -> k to encryptor.decrypt(v, k) }
}

operator fun get(name: String): String? {
Expand All @@ -21,7 +21,7 @@ open class PropsExtension(private val project: Project) {
return value
}

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

private fun isEncrypted(text: String): Boolean {
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/com/neva/gradle/fork/config/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -222,15 +222,15 @@ abstract class Config(val fork: ForkExtension, val name: String) {
private fun promptPreProcess() {
definedProperties.forEach { property ->
if (property.type == PropertyType.PASSWORD) {
prompts[property.name]?.apply { value = fork.props.encryptor.decrypt(value) }
prompts[property.name]?.apply { value = fork.props.encryptor.decrypt(value, property.name) }
}
}
}

private fun promptPostProcess() {
definedProperties.forEach { property ->
if (property.type == PropertyType.PASSWORD) {
prompts[property.name]?.apply { value = fork.props.encryptor.encrypt(value) }
prompts[property.name]?.apply { value = fork.props.encryptor.encrypt(value, property.name) }
}
}
}
Expand Down
17 changes: 10 additions & 7 deletions src/main/kotlin/com/neva/gradle/fork/encryption/Encryption.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal class Encryption private constructor(private val ecipher: Cipher, priva
private fun decode(string: String): ByteArray = BASE64.decode(string)

@Suppress("TooGenericExceptionCaught")
fun encrypt(text: String?): String? {
fun encrypt(text: String?, context: String? = null): String? {
if (text.isNullOrBlank()) {
return text
}
Expand All @@ -30,23 +30,27 @@ internal class Encryption private constructor(private val ecipher: Cipher, priva
val enc = ecipher.doFinal(utf8)
return "${TOKEN_START}${encode(enc)}$TOKEN_END"
} catch (e: Exception) {
throw ForkException("Encryption failed", e)
throw ForkException("Fork property encryption failed! Context: '${context.orEmpty()}'", e)
}
}

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

val raw = text.removeSurrounding(TOKEN_START, TOKEN_END)
try {
val raw = text.removeSurrounding(TOKEN_START, TOKEN_END)
val dec = decode(raw)
val utf8 = dcipher.doFinal(dec)
return String(utf8, charset(CHARSET))
} catch (e: Exception) {
throw ForkException("Decryption failed", e)
throw ForkException(listOf(
"Fork property decryption failed! Context: '${context.orEmpty()}'",
"Most probably encrypted value got corrupted or salt/key changed in the meantime.",
"Consider regenerating encrypted values to fix the problem."
).joinToString("\n"), e)
}
}

Expand Down Expand Up @@ -109,8 +113,7 @@ internal class Encryption private constructor(private val ecipher: Cipher, priva
}

internal fun of(project: Project): Encryption {
return of((project.findProperty("fork.encryption.passphrase")?.toString()
?: "<<Default passphrase to encrypt passwords!>>").toCharArray())
return of((project.findProperty("fork.encryption.passphrase")?.toString() ?: "aPJdETzzmmA7liO8aHNW").toCharArray())
}
}
}

0 comments on commit fd98ab4

Please sign in to comment.