Play 2.0 /アノームのためのApplication.confでDBパスワードの暗号化
-
11-12-2019 - |
質問
アプリケーション設定ファイルに暗号化されていないパスワードを置きたくありません。
この質問: Application.confでDBパスワードの暗号化問題の解決策がプレイ1のためだけに機能します。
誰もがプレイ2.0のために機能する解決策を知っていますか?私はScala版のPlay 2.0.2でアノームを使用しています。
解決 3
Raffaeleとの議論のおかげで、コードの私自身の調査に続いて、Play 2.0はあなたがDBパスワードを暗号化することを許可しないようです。
何かを逃したら私に知らせてください。
edit :次の方法でカスタムデータベースドライバを使用して問題を回避できます。
.
// Just redirect everything to the delegate
class DelegatingDriver(delegate: Driver) extends Driver
{
def connect(url: String, info: Properties) = delegate.connect(url, info)
def acceptsURL(url: String) = delegate.acceptsURL(url)
def getPropertyInfo(url: String, info: Properties) = delegate.getPropertyInfo(url, info)
def getMajorVersion = delegate.getMajorVersion
def getMinorVersion = delegate.getMinorVersion
def jdbcCompliant() = delegate.jdbcCompliant()
}
// Replace password in properties with the decrypted one
class MyDecryptingDriver extends DelegatingDriver(Class.forName("<my.original.Driver>").newInstance().asInstanceOf[Driver])
{
override def connect(url: String, info: Properties)= {
// copy Properties
val overriddenProperties= clone(info)
// override password property with the decrypted value
Option(info.getProperty("password")).foreach(value => overriddenProperties.setProperty("password", decryptPassword(value)))
super.connect(url, overriddenProperties)
}
def clone(orig: Properties)= {
val result= new Properties()
orig.propertyNames().map(_.asInstanceOf[String]).foreach(pName => result.setProperty(pName, orig.getProperty(pName)))
result
}
def decryptPassword(encrypted: String)= ...
}
その後、my.com.MyDecryptingドライバでapplication.conf / db..driverを置き換えます。完璧ではありませんが私のために働く...
他のヒント
すべての努力は無意味です。データベースにハッシュしたパスワードを入れると、人間は自分の脳にパスワードを保持できるためです。 非対称暗号化と呼ばれています。
話しているものは symmetric 暗号化でのみ可能です。プログラムは実行時に鍵を持ち、このキーを使用してDBパスワードを復号化します。ただし、DBパスワードをキーで暗号化した場合は何があるか、まだこのキーを公開していますか? (これはJavaソースとコンパイル済みクラスの両方に当てはまります)。チェーンは最も弱いリンクと同じくらい強いだけです。
マシンがDBに接続している場合は、パスワードが必要です。プログラムはそのまま使用しなければならず、入力が必要なので、このパスワードをプレーンテキストに格納します。セキュリティを強制するためにできるのは、このプレーンテキストファイルへのアクセスを制限することだけです。マスターパスワード)。上記のPlayプラグインを使用すると、物事が変わらないことに注意してください。
私の心にやってくる唯一のものは、管理者がDBパスワードを入力したときにのみDBに接続するPlayアプリです(しかし、これは実際には思考の練習だけです)
私はそれが少し遅くなっているのを知っていますが、この問題についての新しい議論はありません。 Documentation は、GuiceApplicationLoader
をオーバーライドしてGuiceApplicationBuilder
を設定して初期設定をプロセスします。
新しいクラスmodules/ApplicationLoaderConfig.scala
:
import javax.crypto.Cipher
import javax.crypto.spec.SecretKeySpec
import javax.xml.bind.DatatypeConverter
import play.api.inject.guice._
import play.api.{ApplicationLoader, Configuration}
class ApplicationLoaderConfig extends GuiceApplicationLoader() {
override def builder(context: ApplicationLoader.Context): GuiceApplicationBuilder = {
// Decrypt secrets
val decryptedConfig = context.initialConfiguration ++
Configuration("config.to.descrypt.1" -> decryptDES(context.initialConfiguration.getString("config.to.descrypt.1").get)) ++
Configuration("config.to.descrypt.2" -> decryptDES(context.initialConfiguration.getString("config.to.descrypt.2").get))
initialBuilder
.in(context.environment)
.loadConfig(decryptedConfig)
.overrides(overrides(context): _*)
}
private def decryptDES(secret: String): String = {
val key = "12345678"
val skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "DES")
val cipher = Cipher.getInstance("DES/ECB/PKCS5Padding")
cipher.init(Cipher.DECRYPT_MODE, skeySpec)
new String(cipher.doFinal(DatatypeConverter.parseBase64Binary(secret)))
}
}
.
application.config
に追加:
play.application.loader = "modules.ApplicationLoaderConfig"
.