-
Notifications
You must be signed in to change notification settings - Fork 23
JPERF-729: Create a way to configure Jira admin user password during database setup #107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 15 commits
a5f1e36
761f912
e71d277
64b388b
8e2bd06
9e9fbc3
341818c
9b8ae41
10f9d35
26aeb0e
881f9b2
fb2be73
56ad890
0471a2a
13bcd56
4c49b61
bfc5e2e
f5fb3b8
64c957f
614475f
6009d11
148c9ab
9be53d5
16425ea
bab7c59
9b2fbec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package com.atlassian.performance.tools.infrastructure.api.database.passwordoverride | ||
|
|
||
| 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 | ||
|
|
||
| interface JiraUserPasswordEncryptor { | ||
| fun getEncryptedPassword(ssh: SshConnection): String | ||
|
dagguh marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| internal class DefaultJiraUserPasswordEncryptor( | ||
|
dagguh marked this conversation as resolved.
Outdated
|
||
| private val passwordEncryptFunction: (String) -> String, | ||
| private val userPasswordPlainText: String, | ||
| private val sqlClient: SshSqlClient, | ||
| private val jiraDatabaseSchemaName: String | ||
| ): JiraUserPasswordEncryptor { | ||
| private val logger: Logger = LogManager.getLogger(this::class.java) | ||
|
|
||
| override fun getEncryptedPassword(ssh: SshConnection): String { | ||
| return if (shouldUseEncryption(ssh)) { | ||
| logger.debug("Using credential with encrypted password") | ||
| passwordEncryptFunction(userPasswordPlainText) | ||
| } else { | ||
| logger.debug("Using credential with plain text password") | ||
| userPasswordPlainText | ||
| } | ||
| } | ||
|
|
||
| private fun shouldUseEncryption(ssh: SshConnection): Boolean { | ||
| val sqlResult = | ||
| sqlClient.runSql(ssh, "select attribute_value from ${jiraDatabaseSchemaName}.cwd_directory_attribute where attribute_name = 'user_encryption_method';").output | ||
| return when { | ||
| sqlResult.contains("plaintext") -> false | ||
| sqlResult.contains("atlassian-security") -> true | ||
|
dagguh marked this conversation as resolved.
Outdated
|
||
| else -> { | ||
| logger.warn("Unknown user_encryption_method. Assuming encrypted password should be used") | ||
|
dagguh marked this conversation as resolved.
Outdated
|
||
| true | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| package com.atlassian.performance.tools.infrastructure.api.database | ||
|
|
||
| import com.atlassian.performance.tools.infrastructure.api.database.passwordoverride.DefaultJiraUserPasswordEncryptor | ||
| import com.atlassian.performance.tools.infrastructure.api.database.passwordoverride.JiraUserPasswordEncryptor | ||
| 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) | ||
|
dagguh marked this conversation as resolved.
Outdated
|
||
| * from the [com.atlassian.crowd.crowd-password-encoders](https://mvnrepository.com/artifact/com.atlassian.crowd/crowd-password-encoders/4.2.2). | ||
| */ | ||
|
|
||
| class JiraUserPasswordOverridingDatabase internal constructor( | ||
| private val databaseDelegate: Database, | ||
| private val sqlClient: SshSqlClient, | ||
| private val username: String, | ||
| private val jiraDatabaseSchemaName: String, | ||
| private val userPasswordPlainText: String, | ||
| private val jiraUserPasswordEncryptor: JiraUserPasswordEncryptor | ||
|
dagguh marked this conversation as resolved.
Outdated
|
||
| ) : Database { | ||
| private val logger: Logger = LogManager.getLogger(this::class.java) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Logging responsibility could be split to a decorator on this class.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What would be the benefit?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are multiple benefits:
Again I'm not insisting, especially with this one, because our practice was so far to not decouple logger from other business logic. Still I wanted to point it out to see your thoughts on it. I personally would prefer us to follow the decoupling practices, because I see how not following it damages the flexibility of our implementations. I don't believe in popular thinking that everything can be rewritten to match the new requirements - from my experience it never happens. However we can write code that is modular and later one be able to initialise it differently to achieve new requirements.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I understand all the benefits, but do we really need them?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @pczuj I think these are interesting dev experiments, they deserve to be explored. However, we shouldn't mix them inside a big and overdue feature PR. |
||
|
|
||
| override fun setup(ssh: SshConnection): String = databaseDelegate.setup(ssh) | ||
|
|
||
| override fun start( | ||
| jira: URI, | ||
| ssh: SshConnection | ||
| ) { | ||
| databaseDelegate.start(jira, ssh) | ||
| val password = jiraUserPasswordEncryptor.getEncryptedPassword(ssh) | ||
| sqlClient.runSql(ssh, "UPDATE ${jiraDatabaseSchemaName}.cwd_user SET credential='$password' WHERE user_name='$username';") | ||
| logger.debug("Password for user '$username' updated to '${userPasswordPlainText}'") | ||
| } | ||
|
|
||
|
|
||
| class Builder( | ||
| private var databaseDelegate: Database, | ||
| private var userPasswordPlainText: String, | ||
| private var jiraUserPasswordEncryptor: JiraUserPasswordEncryptor | ||
| ) { | ||
| private var sqlClient: SshSqlClient = SshMysqlClient() | ||
| private var jiraDatabaseSchemaName: String = "jiradb" | ||
| private var username: String = "admin" | ||
|
|
||
| fun databaseDelegate(databaseDelegate: Database) = apply { this.databaseDelegate = databaseDelegate } | ||
| fun username(username: String) = apply { this.username = username } | ||
| fun userPasswordPlainText(userPassword: String) = apply { this.userPasswordPlainText = userPassword } | ||
| fun sqlClient(sqlClient: SshSqlClient) = apply { this.sqlClient = sqlClient } | ||
| fun jiraDatabaseSchemaName(jiraDatabaseSchemaName: String) = apply { this.jiraDatabaseSchemaName = jiraDatabaseSchemaName } | ||
| fun jiraUserPasswordEncryptor(jiraUserPasswordEncryptor: JiraUserPasswordEncryptor) = apply { this.jiraUserPasswordEncryptor = jiraUserPasswordEncryptor } | ||
|
|
||
| fun build() = JiraUserPasswordOverridingDatabase( | ||
| databaseDelegate = databaseDelegate, | ||
| sqlClient = sqlClient, | ||
| username = username, | ||
| userPasswordPlainText = userPasswordPlainText, | ||
| jiraDatabaseSchemaName = jiraDatabaseSchemaName, | ||
| jiraUserPasswordEncryptor = jiraUserPasswordEncryptor | ||
| ) | ||
| } | ||
|
|
||
| } | ||
|
|
||
| fun Database.withAdminPassword(adminPasswordPlainText: String, passwordEncryptFunction: (String) -> String): JiraUserPasswordOverridingDatabase { | ||
| val jiraDatabaseSchemaName = "jiradb" | ||
| val sqlClient = SshMysqlClient() | ||
| return JiraUserPasswordOverridingDatabase.Builder( | ||
| databaseDelegate = this, | ||
| userPasswordPlainText = adminPasswordPlainText, | ||
| jiraUserPasswordEncryptor = DefaultJiraUserPasswordEncryptor( | ||
| passwordEncryptFunction = passwordEncryptFunction, | ||
| userPasswordPlainText = adminPasswordPlainText, | ||
| sqlClient = sqlClient, | ||
| jiraDatabaseSchemaName = jiraDatabaseSchemaName | ||
| ) | ||
| ) | ||
| .jiraDatabaseSchemaName(jiraDatabaseSchemaName) | ||
| .sqlClient(sqlClient) | ||
| .build() | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| package com.atlassian.performance.tools.infrastructure.api.database.passwordoverride | ||
|
|
||
| import com.atlassian.performance.tools.infrastructure.mock.MockSshSqlClient | ||
| import com.atlassian.performance.tools.infrastructure.mock.RememberingSshConnection | ||
| import com.atlassian.performance.tools.ssh.api.SshConnection | ||
| import org.assertj.core.api.Assertions.assertThat | ||
| import org.junit.Before | ||
| import org.junit.Test | ||
|
|
||
| class DefaultJiraUserPasswordEncryptorTest { | ||
| private val plainTextPassword = "plain text password" | ||
| private val encryptedPassword = "***** ***" | ||
|
|
||
| private lateinit var sqlClient: MockSshSqlClient | ||
| private lateinit var sshConnection: RememberingSshConnection | ||
| private lateinit var jiraUserPasswordEncryptor: JiraUserPasswordEncryptor | ||
|
|
||
| @Before | ||
| fun setup() { | ||
| sqlClient = MockSshSqlClient() | ||
| sshConnection = RememberingSshConnection() | ||
| jiraUserPasswordEncryptor = DefaultJiraUserPasswordEncryptor( | ||
| passwordEncryptFunction = { _ -> encryptedPassword }, | ||
| userPasswordPlainText = plainTextPassword, | ||
| sqlClient = sqlClient, | ||
| jiraDatabaseSchemaName = "jiradb" | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun shouldQueryEncryptionMethod() { | ||
| // when | ||
| jiraUserPasswordEncryptor.getEncryptedPassword(sshConnection) | ||
| // then | ||
| assertThat(sqlClient.getLog()) | ||
| .`as`("sql queries executed") | ||
| .containsExactly( | ||
| "select attribute_value from jiradb.cwd_directory_attribute where attribute_name = 'user_encryption_method';" | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun shouldReturnEncryptedPasswordByDefault() { | ||
| // when | ||
| val encryptedPassword = jiraUserPasswordEncryptor.getEncryptedPassword(sshConnection) | ||
| // then | ||
| assertThat(encryptedPassword).isEqualTo(encryptedPassword) | ||
| } | ||
|
|
||
| @Test | ||
| fun shouldUpdateEncryptedPassword() { | ||
| // given | ||
| sqlClient.queueReturnedSqlCommandResult( | ||
| SshConnection.SshResult( | ||
| exitStatus = 0, | ||
| output = """attribute_value | ||
| atlassian-security | ||
| """.trimMargin(), | ||
| errorOutput = "" | ||
| ) | ||
| ) | ||
| // when | ||
| val encryptedPassword = jiraUserPasswordEncryptor.getEncryptedPassword(sshConnection) | ||
| // then | ||
| assertThat(encryptedPassword).isEqualTo(encryptedPassword) | ||
| } | ||
|
|
||
| @Test | ||
| fun shouldUpdatePlaintextPassword() { | ||
| // given | ||
| sqlClient.queueReturnedSqlCommandResult( | ||
| SshConnection.SshResult( | ||
| exitStatus = 0, | ||
| output = """attribute_value | ||
| plaintext | ||
| """.trimMargin(), | ||
| errorOutput = "" | ||
| ) | ||
| ) | ||
| // when | ||
| val encryptedPassword = jiraUserPasswordEncryptor.getEncryptedPassword(sshConnection) | ||
| // then | ||
| assertThat(encryptedPassword).isEqualTo(plainTextPassword) | ||
| } | ||
|
|
||
| } |
Uh oh!
There was an error while loading. Please reload this page.