Skip to content

Commit

Permalink
JPERF-729: Create a way to configure Jira admin user password during …
Browse files Browse the repository at this point in the history
…database setup
  • Loading branch information
pczuj committed Jun 28, 2021
1 parent 861bef3 commit 3e98d8e
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.atlassian.performance.tools.infrastructure.api.database

import com.atlassian.performance.tools.infrastructure.database.SshMysqlClient
import com.atlassian.performance.tools.infrastructure.database.SshSqlClient
import com.atlassian.performance.tools.ssh.api.SshConnection
import org.apache.logging.log4j.LogManager
import org.apache.logging.log4j.Logger
import java.net.URI

/**
* Based on https://confluence.atlassian.com/jira/retrieving-the-jira-administrator-192836.html
*
* To encode the password use [com.atlassian.crowd.password.encoder.AtlassianSecurityPasswordEncoder](https://docs.atlassian.com/atlassian-crowd/4.2.2/com/atlassian/crowd/password/encoder/AtlassianSecurityPasswordEncoder.html)
* from the [com.atlassian.crowd.crowd-password-encoders](https://mvnrepository.com/artifact/com.atlassian.crowd/crowd-password-encoders/4.2.2).
*/
class JiraUserPasswordOverridingDatabase(
private val databaseDelegate: Database,
private val sqlClient: SshSqlClient,
private val username: String,
private val encodedPassword: String,
private val cwdUserTableName: String
) : Database {
private val logger: Logger = LogManager.getLogger(this::class.java)

constructor(
databaseDelegate: Database,
sqlClient: SshSqlClient,
username: String,
encodedPassword: String
) : this(
databaseDelegate = databaseDelegate,
sqlClient = sqlClient,
username = username,
encodedPassword = encodedPassword,
cwdUserTableName = "jiradb.cwd_user"
)

constructor(
databaseDelegate: Database,
username: String,
encodedPassword: String
) : this(
databaseDelegate = databaseDelegate,
sqlClient = SshMysqlClient(),
username = username,
encodedPassword = encodedPassword
)

override fun setup(
ssh: SshConnection
) = databaseDelegate.setup(ssh)

override fun start(
jira: URI,
ssh: SshConnection
) {
databaseDelegate.start(jira, ssh)
logger.debug("Changing Jira user $username password to (encoded) $encodedPassword in table $cwdUserTableName")
sqlClient.runSql(ssh, "UPDATE $cwdUserTableName SET credential='$encodedPassword' WHERE user_name='$username';")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.atlassian.performance.tools.infrastructure.api.database

import com.atlassian.performance.tools.infrastructure.mock.RememberingDatabase
import com.atlassian.performance.tools.infrastructure.mock.RememberingSshConnection
import com.atlassian.performance.tools.infrastructure.mock.RememberingSshSqlClient
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test
import java.net.URI

class JiraUserPasswordOverridingDatabaseTest {

private val jira = URI("http://localhost/")
private val exampleEncodedPassword = "uQieO/1CGMUIXXftw3ynrsaYLShI+GTcPS4LdUGWbIusFvHPfUzD7CZvms6yMMvA8I7FViHVEqr6Mj4pCLKAFQ=="

@Test
fun shouldSetupUnderlyingDatabase() {
val underlyingDatabase = RememberingDatabase()
val sqlClient = RememberingSshSqlClient()
val database = JiraUserPasswordOverridingDatabase(
databaseDelegate = underlyingDatabase,
sqlClient = sqlClient,
username = "admin",
encodedPassword = exampleEncodedPassword
)
val sshConnection = RememberingSshConnection()

database.setup(sshConnection)
database.start(jira, sshConnection)

assertThat(underlyingDatabase.setup)
.`as`("underlying database setup")
.isTrue()
}

@Test
fun shouldStartUnderlyingDatabase() {
val underlyingDatabase = RememberingDatabase()
val sqlClient = RememberingSshSqlClient()
val database = JiraUserPasswordOverridingDatabase(
databaseDelegate = underlyingDatabase,
sqlClient = sqlClient,
username = "admin",
encodedPassword = exampleEncodedPassword
)
val sshConnection = RememberingSshConnection()

database.setup(sshConnection)
database.start(jira, sshConnection)

assertThat(underlyingDatabase.started)
.`as`("underlying database started")
.isTrue()
}

@Test
fun shouldExecuteUpdateOnCwdUserTable() {
val cwdUserTableName = "cwd_user"
val underlyingDatabase = RememberingDatabase()
val sqlClient = RememberingSshSqlClient()
val database = JiraUserPasswordOverridingDatabase(
databaseDelegate = underlyingDatabase,
sqlClient = sqlClient,
username = "admin",
encodedPassword = exampleEncodedPassword,
cwdUserTableName = cwdUserTableName
)
val sshConnection = RememberingSshConnection()

database.setup(sshConnection)
database.start(jira, sshConnection)

assertThat(sqlClient.getLog())
.`as`("sql command executed")
.contains("UPDATE $cwdUserTableName SET credential='$exampleEncodedPassword' WHERE user_name='admin';")
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.atlassian.performance.tools.infrastructure.api.database

import com.atlassian.performance.tools.infrastructure.mock.RememberingDatabase
import com.atlassian.performance.tools.infrastructure.mock.RememberingSshConnection
import com.atlassian.performance.tools.ssh.api.SshConnection
import org.assertj.core.api.Assertions.assertThat
Expand Down Expand Up @@ -161,19 +162,4 @@ class LicenseOverridingMysqlTest {
val underlyingDatabase: RememberingDatabase,
val ssh: RememberingSshConnection
)

private class RememberingDatabase : Database {

var setup = false
var started = false

override fun setup(ssh: SshConnection): String {
setup = true
return "."
}

override fun start(jira: URI, ssh: SshConnection) {
started = true
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.atlassian.performance.tools.infrastructure.mock

import com.atlassian.performance.tools.infrastructure.api.database.Database
import com.atlassian.performance.tools.ssh.api.SshConnection
import java.net.URI

class RememberingDatabase : Database {

var setup = false
var started = false

override fun setup(ssh: SshConnection): String {
setup = true
return "."
}

override fun start(jira: URI, ssh: SshConnection) {
started = true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.atlassian.performance.tools.infrastructure.mock

import com.atlassian.performance.tools.infrastructure.database.SshSqlClient
import com.atlassian.performance.tools.ssh.api.SshConnection
import java.io.File

class RememberingSshSqlClient : SshSqlClient {
private val log = mutableListOf<String>()

override fun runSql(
ssh: SshConnection,
sql: String
) = SshConnection.SshResult(
exitStatus = 0,
output = "",
errorOutput = ""
).also { log.add(sql) }

override fun runSql(
ssh: SshConnection,
sql: File
) = SshConnection.SshResult(
exitStatus = 0,
output = "",
errorOutput = ""
).also { log.add(sql.readText()) }

fun getLog(): List<String> = log
}

0 comments on commit 3e98d8e

Please sign in to comment.