문제

애플리케이션 구성 파일에 암호화되지 않은 비밀번호를 넣고 싶지 않습니다.

이 질문: application.conf에서 DB 비밀번호 암호화 문제에 대한 훌륭한 솔루션이 있지만 Play 1에서만 작동합니다.

Play 2.0에서 작동하는 솔루션을 아는 사람이 있나요?Scala 버전의 Play 2.0.2에서 anorm을 사용하고 있습니다.

도움이 되었습니까?

해결책 3

Raffaele와의 토론과 코드 조사를 따르면 Play 2.0이 DB 암호를 암호화 할 수없는 것으로 보입니다.

내가 누락 된 경우 알려 주시기 바랍니다.

편집 : 다음과 같은 방법으로 사용자 정의 데이터베이스 드라이버를 사용하여 문제를 해결할 수 있습니다.

// 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를 바꿉니다.완벽하지 않지만 나에게 일하고 있습니다 ...

다른 팁

모든 노력은 무의미합니다. 우리가 해시 된 암호를 데이터베이스에 넣을 때 인간이 뇌에 암호를 유지할 수 있고 뇌는 을 읽을 수 없기 때문입니다. 비대칭 암호화라고합니다.

대칭 암호화에서만 이야기하는 것은 런타임에 키가 있으며이 키를 사용하여 DB 암호를 해독합니다. 그러나 키로 암호화 된 DB 암호를 저장하는 점은 무엇 이며이 키를 공개적으로 사용할 수 있습니까? (이것은 Java 소스와 컴파일 된 클래스 모두에 해당됩니다). 체인은 가장 약한 링크만큼 강하다.

컴퓨터가 DB에 연결 해야하는 경우, 암호가 필요합니다.이 암호는 프로그램이 그대로 사용해야하기 때문에이 암호를 일반 텍스트로 저장하고 인간의 입력이 필요하지 않아 가 필요하지 않습니다. 보안을 적용하기 위해 할 수있는 모든 것은이 일반 텍스트 파일에 대한 액세스를 제한하고 궁극적으로 관리자의 정신에만 저장된 암호로만 보호하는 것입니다 (BTW, 관리자가 데이터베이스의 모든 암호를 유지할 가능성이있는 경우 마스터 암호). 언급 된 Play 플러그인을 사용하면 상황이 변경되지 않습니다.

내 마음에 오는 유일한 다른 것은 관리자가 DB 암호를 입력 할 때만 DB에 연결하는 재생 앱입니다 (그러나 이것은 정말로 사고 운동 일뿐입니다)

조금 늦었다는 것을 알고 있지만 이 문제에 대한 새로운 논의는 없습니다.제안된 대로 실제 솔루션(Play v.2.5.X)을 공유하고 싶습니다. 선적 서류 비치, 이제 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"
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top