diff --git a/src/main/kotlin/com/neva/gradle/fork/PropsExtension.kt b/src/main/kotlin/com/neva/gradle/fork/PropsExtension.kt index 10b6b51..6988ba2 100644 --- a/src/main/kotlin/com/neva/gradle/fork/PropsExtension.kt +++ b/src/main/kotlin/com/neva/gradle/fork/PropsExtension.kt @@ -12,7 +12,7 @@ open class PropsExtension(private val project: Project) { fun read(file: File): Map { 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? { @@ -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 { diff --git a/src/main/kotlin/com/neva/gradle/fork/config/Config.kt b/src/main/kotlin/com/neva/gradle/fork/config/Config.kt index c758076..ed0c0f5 100644 --- a/src/main/kotlin/com/neva/gradle/fork/config/Config.kt +++ b/src/main/kotlin/com/neva/gradle/fork/config/Config.kt @@ -222,7 +222,7 @@ 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) } } } } @@ -230,7 +230,7 @@ abstract class Config(val fork: ForkExtension, val name: String) { 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) } } } } diff --git a/src/main/kotlin/com/neva/gradle/fork/encryption/Encryption.kt b/src/main/kotlin/com/neva/gradle/fork/encryption/Encryption.kt index d369bf7..201910f 100644 --- a/src/main/kotlin/com/neva/gradle/fork/encryption/Encryption.kt +++ b/src/main/kotlin/com/neva/gradle/fork/encryption/Encryption.kt @@ -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 } @@ -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) } } @@ -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() - ?: "<>").toCharArray()) + return of((project.findProperty("fork.encryption.passphrase")?.toString() ?: "aPJdETzzmmA7liO8aHNW").toCharArray()) } } }