-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JPERF-729: Create a way to configure Jira admin user password during …
…database setup
- Loading branch information
Showing
5 changed files
with
187 additions
and
15 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
...ssian/performance/tools/infrastructure/api/database/JiraUserPasswordOverridingDatabase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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';") | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
...n/performance/tools/infrastructure/api/database/JiraUserPasswordOverridingDatabaseTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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';") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/test/kotlin/com/atlassian/performance/tools/infrastructure/mock/RememberingDatabase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...est/kotlin/com/atlassian/performance/tools/infrastructure/mock/RememberingSshSqlClient.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |