Skip to content

Commit

Permalink
Less ambiguous syntax \ more sugar
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuri Bonya committed Oct 23, 2018
1 parent 5ca21ee commit 546249f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/main/scala/com/evolutiongaming/crypto/DecryptConfig.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@ object DecryptConfig {
password
}
} catch {
case NonFatal(e) => password
case NonFatal(_) => password
}

implicit class DecryptConfigOps(val self: Config) extends AnyVal {

def decryptString(encrypted: String): String =
apply(encrypted, self)

def decryptPath(path: String): String = try {
val encrypted = self.getString(path)
apply(encrypted, self)
} catch {
case NonFatal(_) => path
}
}
}
12 changes: 12 additions & 0 deletions src/test/scala/com/evolutiongaming/crypto/DecryptConfigSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.evolutiongaming.crypto

import com.typesafe.config.ConfigFactory
import org.scalatest.{BeforeAndAfterEach, FlatSpec, Matchers}
import DecryptConfig.DecryptConfigOps

class DecryptConfigSpec extends FlatSpec with BeforeAndAfterEach with Matchers {
val correctPassword = "jboss"
Expand Down Expand Up @@ -38,4 +39,15 @@ class DecryptConfigSpec extends FlatSpec with BeforeAndAfterEach with Matchers {
val decrypted = decrypt("bad-secret.conf")
decrypted should not equal correctPassword
}

it should "decrypt encrypted passwords by path (Config extension method)" in {
val config = ConfigFactory.load("encrypted.conf")
config.decryptPath("password") shouldEqual correctPassword
}

it should "support unencrypted passwords (Config extension method)" in {
val config = ConfigFactory.load("unencrypted.conf")
val password = config.getString("password")
config.decryptString(password) shouldEqual correctPassword
}
}

0 comments on commit 546249f

Please sign in to comment.