Home > Back-end >  ktor hocon configuration not working for one password variable
ktor hocon configuration not working for one password variable

Time:10-02

I am setting up a ktor api that connects with a postgres container in docker. I can get everything working when I hard code things. When I try to use the hocon config and print values I can see my dburl and dbuser but when I print my dbpassword I get an address like value.

Here is my hocon config:

ktor {
    deployment {
        port = 8080
        port = ${?PORT}
    }
    application {
        modules = [ com.personal.ApplicationKt.module ]
    }
    security {
        ssl {
            keystore = ""
        }
    }
}
jwt {
    secret = "redacted"
    issuer = "https://jwt-provider-domain/"
    audience = "jwt-audience"
    realm = "ktor sample app"
}
db {
    jdbcUrl = "jdbc:postgresql://docker-compose-db-name:5432/db_name"
    dbUser = "redacted"
    dbPassword = "redacted_password"
}

and here is my application code that is trying to use those values for loading/migrating my db:

    private val appConfig = HoconApplicationConfig(ConfigFactory.load())
    private val dbUrl = appConfig.property("db.jdbcUrl").getString() 
    private val dbUser =  appConfig.property("db.dbUser").getString()
    private val dbPassword = appConfig.property("db.dbPassword").toString()  

    fun init() {
        Database.connect(hikari())
        // auto migration when spinning up db
        println("URL: $dbUrl")
        println("USER: $dbUser")
        println("PASSWORD: $dbPassword")
        val flyway = Flyway.configure().dataSource(dbUrl, dbUser, dbPassword).load()
        flyway.migrate()
    }

    private fun hikari(): HikariDataSource {
        val config = HikariConfig()
        println("URL: $dbUrl")
        println("USER: $dbUser")
        println("PASSWORD: $dbPassword")
        config.driverClassName = "org.postgresql.Driver"
        config.jdbcUrl = dbUrl
        config.username = dbUser
        config.password = dbPassword
        config.maximumPoolSize = 3
        // auto commit sql operations
        config.isAutoCommit = false
        // no dirty reads allowed. Only see data that was committed prior to start of statement
        config.transactionIsolation = "TRANSACTION_READ_COMMITTED"
        config.validate()
        return HikariDataSource(config)
    }

those three print lines print out: "URL: jdbc:postgresql://docker-compose-db-name:5432/db_name" "USER: redacted" "PASSWORD: io.ktor.config.HoconApplicationConfig$HoconApplicationConfigValue@4b9df8a"

I then get auth failed for redacted. When I hardcode the values then it works 100%. Is my password too cryptic or contain values that can't be used in a hoconstring?

CodePudding user response:

The problem is that the last call for getting value of db.dbPassword is toString instead of getString.

  • Related