2

I'm using play-slick_2.11-1.0.1 + HikariCP 2.4.1 to access SqlServer in my Play4-based application.
The database connection in application.conf:

slick.dbs.myDatabase = {
  driver="com.typesafe.slick.driver.ms.SQLServerDriver$"
  db{
    url = "jdbc:sqlserver://sqlserverhost"
    driver = com.microsoft.sqlserver.jdbc.SQLServerDriver
    user = "admin"
    password = "ENCRYPTED_PASSWORD"
  }
}

The problem is that the database password configured here must be encrypted based on our company policy.

How can I inject my decryption code to decrypt the password for the connection?

0

2 Answers 2

2

Just found a solution:

  def createDecryptedDbConfig (dbConfigProvider: DatabaseConfigProvider) : DatabaseConfig[JdbcProfile] = {
    val dbConfig = dbConfigProvider.get[JdbcProfile]
    val decryptedConfig = dbConfig.config.
      withValue("db.user", ConfigValueFactory.fromAnyRef(decrypt(dbConfig.config.getConfig("db").getString("user")))).
      withValue("db.password", ConfigValueFactory.fromAnyRef(decrypt(dbConfig.config.getConfig("db").getString("password"))))
    DatabaseConfig.forConfig[JdbcProfile]("", decryptedConfig)
  }
0

i was also trying with latest play 2.7 with slick 3.5

package models

import com.typesafe.config.ConfigValueFactory
import javax.inject.Inject
import play.api.Logger
import play.api.db.slick.DatabaseConfigProvider
import services.EncryptDecryptService
import slick.basic.{BasicProfile, DatabaseConfig}
import slick.jdbc.JdbcProfile

trait HasDatabaseConfigTalachitas[P <: BasicProfile] {
  /** The Slick database configuration. */
  var dbConfig: DatabaseConfig[P] // field is declared as a val because we want a stable identifier.
  /** The Slick profile extracted from `dbConfig`. */
  protected final lazy val profile: P = dbConfig.profile // field is lazy to avoid early initializer problems.
  @deprecated("Use `profile` instead of `driver`", "2.1")
  protected final lazy val driver: P = dbConfig.profile // field is lazy to avoid early initializer problems.
  /** The Slick database extracted from `dbConfig`. */
  protected final def db: P#Backend#Database = dbConfig.db
}

trait HasDatabaseConfigProviderTalachitas[P <: BasicProfile] extends HasDatabaseConfigTalachitas[P] {
  /** The provider of a Slick `DatabaseConfig` instance.*/
  protected val dbConfigProvider: DatabaseConfigProvider
  override var dbConfig: DatabaseConfig[P] = dbConfigProvider.get[P] // field is lazy to avoid early initializer problems.
}

import play.api.Configuration

class CustomizedSlickConfig @Inject()(config: Configuration,
                                      encryptDecryptService: EncryptDecryptService) {

  val logger: Logger = Logger(this.getClass())

  def createDbConfigCustomized (dbConfigProvider: DatabaseConfigProvider) : DatabaseConfig[JdbcProfile] = {

    val user: String = encryptDecryptService.decrypt(config.get[String]("slick.dbs.default.db.user"))
    val pass: String = encryptDecryptService.decrypt(config.get[String]("slick.dbs.default.db.password"))

    val dbConfigOwn = dbConfigProvider.get[JdbcProfile]
    val decryptedConfig = dbConfigOwn.config
      .withValue("db.user", ConfigValueFactory.fromAnyRef(user))
      .withValue("db.password", ConfigValueFactory.fromAnyRef(pass))

    DatabaseConfig.forConfig[JdbcProfile]("", decryptedConfig)

  }
}

and this:


class Reservations @Inject()(val dbConfigProvider: DatabaseConfigProvider,
                             config: Configuration,
                             customizedSlickConfig: CustomizedSlickConfig)
  extends HasDatabaseConfigProviderTalachitas[JdbcProfile] {

  val logger: Logger = Logger(this.getClass())

  this.dbConfig = customizedSlickConfig.createDbConfigCustomized(dbConfigProvider)

needed to override completely that default slick using some https://github.com/google/tink library for encryption

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.