Skip to content

Commit

Permalink
Fix application conf documents and improve logging from tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mcdan committed Feb 3, 2020
1 parent 10355da commit 5c5d4a8
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 18 deletions.
6 changes: 3 additions & 3 deletions common/scala/src/main/resources/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -486,12 +486,12 @@ whisk {
# require a currently non-existing migration step.
parameter-storage {
# Base64 encoded 256 bit key
#aes256 = ""
#aes-256 = ""
# Base64 encoded 128 bit key
#aes128 = ""
#aes-128 = ""
# The current algorithm to use for parameter encryption, this can be changed but you have to leave all the keys
# configured for any algorithm you used previously.
#current = "aes128|aes256"
#current = "aes-128|aes-256"
}
}
#placeholder for test overrides so that tests can override defaults in application.conf (todo: move all defaults to reference.conf)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ protected[core] object Parameters extends ArgNormalizer[Parameters] {

def readMergedList(value: JsValue): Parameters =
Try {

val JsObject(obj) = value
new Parameters(
obj
Expand Down Expand Up @@ -340,7 +341,7 @@ protected[core] object Parameters extends ArgNormalizer[Parameters] {
(key, value)
case Seq(JsString(k), v: JsValue, e: JsString) if (i.asJsObject.fields.contains("encryption")) =>
val key = new ParameterName(k)
val value = ParameterValue(v, false, None)
val value = ParameterValue(v, false, Some(e))
(key, value)
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ private trait AesEncryption extends encrypter {
val key: Array[Byte]
val ivLen: Int
val name: String
private val tLen = key.length * 8
private val tLen = 128
private val secretKey = new SecretKeySpec(key, "AES")

private val secureRandom = new SecureRandom()
Expand Down Expand Up @@ -138,21 +138,21 @@ private trait AesEncryption extends encrypter {
}

private object Aes128 {
val name: String = "aes128"
val name: String = "aes-128"
}
private case class Aes128(val key: Array[Byte], val ivLen: Int = 12, val name: String = Aes128.name)
extends AesEncryption
with encrypter

private object Aes256 {
val name: String = "aes256"
val name: String = "aes-256"
}
private case class Aes256(val key: Array[Byte], val ivLen: Int = 128, val name: String = Aes256.name)
extends AesEncryption
with encrypter

private class NoopCrypt extends encrypter {
val name = "noop"
val name = ""
def encrypt(p: ParameterValue): ParameterValue = {
p
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class ParameterEncryptionTests extends FlatSpec with Matchers with BeforeAndAfte
}

it should "read the merged message payload from kafka into parameters" in {
ParameterEncryption.storageConfig = new ParameterStorageConfig("aes128", "ra1V6AfOYAv0jCzEdufIFA==")
ParameterEncryption.storageConfig = new ParameterStorageConfig("aes-128", "ra1V6AfOYAv0jCzEdufIFA==")
val locked = ParameterEncryption.lock(parameters)

val unlockedParam = new ParameterValue(JsString("test-plain"), false)
Expand All @@ -114,30 +114,30 @@ class ParameterEncryptionTests extends FlatSpec with Matchers with BeforeAndAfte

behavior of "AesParameterEncryption"
it should "correctly mark the encrypted parameters after lock" in {
ParameterEncryption.storageConfig = new ParameterStorageConfig("aes128", "ra1V6AfOYAv0jCzEdufIFA==")
ParameterEncryption.storageConfig = new ParameterStorageConfig("aes-128", "ra1V6AfOYAv0jCzEdufIFA==")
val locked = ParameterEncryption.lock(parameters)
locked.getMap.map(({
case (_, paramValue) =>
paramValue.encryption.convertTo[String] shouldBe "aes128"
paramValue.encryption.convertTo[String] shouldBe "aes-128"
paramValue.value.convertTo[String] should not be "secret"
}))
}

it should "serialize to json correctly" in {
val output =
"""\Q{"one":{"encryption":"aes128","init":false,"value":"\E.*\Q"},"two":{"encryption":"aes128","init":true,"value":"\E.*\Q"}}""".stripMargin.r
ParameterEncryption.storageConfig = new ParameterStorageConfig("aes128", "ra1V6AfOYAv0jCzEdufIFA==")
"""\Q{"one":{"encryption":"aes-128","init":false,"value":"\E.*\Q"},"two":{"encryption":"aes-128","init":true,"value":"\E.*\Q"}}""".stripMargin.r
ParameterEncryption.storageConfig = new ParameterStorageConfig("aes-128", "ra1V6AfOYAv0jCzEdufIFA==")
val locked = ParameterEncryption.lock(parameters)
val dbString = locked.toJsObject.toString
dbString should fullyMatch regex output
}

it should "correctly decrypted encrypted values" in {
ParameterEncryption.storageConfig = new ParameterStorageConfig("aes128", "ra1V6AfOYAv0jCzEdufIFA==")
ParameterEncryption.storageConfig = new ParameterStorageConfig("aes-128", "ra1V6AfOYAv0jCzEdufIFA==")
val locked = ParameterEncryption.lock(parameters)
locked.getMap.map(({
case (_, paramValue) =>
paramValue.encryption.convertTo[String] shouldBe "aes128"
paramValue.encryption.convertTo[String] shouldBe "aes-128"
paramValue.value.convertTo[String] should not be "secret"
}))

Expand All @@ -152,12 +152,12 @@ class ParameterEncryptionTests extends FlatSpec with Matchers with BeforeAndAfte
// Not sure having cancelled tests is a good idea either, need to work on aes256 packaging.
it should "work if with aes256 if policy allows it" in {
ParameterEncryption.storageConfig =
new ParameterStorageConfig("aes256", "", "j5rLzhtxwzPyUVUy8/p8XJmBoKeDoSzNJP1SITJEY9E=")
new ParameterStorageConfig("aes-256", "", "j5rLzhtxwzPyUVUy8/p8XJmBoKeDoSzNJP1SITJEY9E=")
try {
val locked = ParameterEncryption.lock(parameters)
locked.getMap.map(({
case (_, paramValue) =>
paramValue.encryption.convertTo[String] shouldBe "aes256"
paramValue.encryption.convertTo[String] shouldBe "aes-256"
paramValue.value.convertTo[String] should not be "secret"
}))

Expand All @@ -169,7 +169,7 @@ class ParameterEncryptionTests extends FlatSpec with Matchers with BeforeAndAfte
}))
} catch {
case e: InvalidAlgorithmParameterException =>
cancel(e)
cancel(e.toString)
}
}

Expand Down

0 comments on commit 5c5d4a8

Please sign in to comment.