-
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 20 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 |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| .gradle | ||
| build | ||
| build | ||
| .idea | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| 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 java.util.function.Function | ||
|
|
||
| interface JiraUserPasswordEncryptor { | ||
| fun getEncryptedPassword(plainTextPassword: String): String | ||
| } | ||
|
|
||
| interface JiraUserPasswordEncryptorProvider { | ||
| fun getEncryptor(ssh: SshConnection, sqlClient: SshSqlClient): JiraUserPasswordEncryptor | ||
| } | ||
|
|
||
| class DefaultJiraUserPasswordEncryptorProvider( | ||
|
mgrzaslewicz marked this conversation as resolved.
Outdated
|
||
| private val jiraDatabaseSchemaName: String, | ||
| private val plainTextPasswordEncryptor: JiraUserPasswordEncryptor, | ||
| private val encryptedPasswordEncryptor: JiraUserPasswordEncryptor | ||
| ) : JiraUserPasswordEncryptorProvider { | ||
|
|
||
| override fun getEncryptor(ssh: SshConnection, sqlClient: SshSqlClient): JiraUserPasswordEncryptor { | ||
| 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") -> plainTextPasswordEncryptor | ||
| sqlResult.contains("atlassian-security") -> encryptedPasswordEncryptor | ||
| else -> throw RuntimeException("Unknown jira user password encryption type") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| class EncryptedJiraUserPasswordEncryptor( | ||
| private val passwordEncryptFunction: Function<String, String> | ||
| ) : JiraUserPasswordEncryptor { | ||
| override fun getEncryptedPassword(plainTextPassword: String) = passwordEncryptFunction.apply(plainTextPassword) | ||
| } | ||
|
|
||
| class PlainTextJiraUserPasswordEncryptor : JiraUserPasswordEncryptor { | ||
| override fun getEncryptedPassword(plainTextPassword: String) = plainTextPassword | ||
| } | ||
| 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.api.database.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 | ||
| import java.util.function.Function | ||
|
|
||
| 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 userPasswordEncryptorProvider: JiraUserPasswordEncryptorProvider | ||
| ) : 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 userPasswordEncryptor = userPasswordEncryptorProvider.getEncryptor(ssh, sqlClient) | ||
| val password = userPasswordEncryptor.getEncryptedPassword(userPasswordPlainText) | ||
| 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 userPasswordEncryptorProvider: JiraUserPasswordEncryptorProvider | ||
| ) { | ||
| 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 userPasswordEncryptorProvider(userPasswordEncryptorProvider: JiraUserPasswordEncryptorProvider) = | ||
| apply { this.userPasswordEncryptorProvider = userPasswordEncryptorProvider } | ||
|
|
||
| fun build() = JiraUserPasswordOverridingDatabase( | ||
| databaseDelegate = databaseDelegate, | ||
| sqlClient = sqlClient, | ||
| username = username, | ||
| userPasswordPlainText = userPasswordPlainText, | ||
| jiraDatabaseSchemaName = jiraDatabaseSchemaName, | ||
| userPasswordEncryptorProvider = userPasswordEncryptorProvider | ||
| ) | ||
| } | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * @param passwordEncryptFunction Based on [retrieving-the-jira-administrator](https://confluence.atlassian.com/jira/retrieving-the-jira-administrator-192836.html) | ||
| * to encode the password in Jira format 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). | ||
| * | ||
| */ | ||
| fun Database.withAdminPassword(adminPasswordPlainText: String, passwordEncryptFunction: Function<String, String>): Database { | ||
| val jiraDatabaseSchemaName = "jiradb" | ||
| val sqlClient = SshMysqlClient() | ||
| return JiraUserPasswordOverridingDatabase.Builder( | ||
| databaseDelegate = this, | ||
| userPasswordPlainText = adminPasswordPlainText, | ||
| userPasswordEncryptorProvider = DefaultJiraUserPasswordEncryptorProvider( | ||
| jiraDatabaseSchemaName = jiraDatabaseSchemaName, | ||
| plainTextPasswordEncryptor = PlainTextJiraUserPasswordEncryptor(), | ||
| encryptedPasswordEncryptor = EncryptedJiraUserPasswordEncryptor(passwordEncryptFunction) | ||
| ) | ||
| ) | ||
| .jiraDatabaseSchemaName(jiraDatabaseSchemaName) | ||
| .sqlClient(sqlClient) | ||
| .build() | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| 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 | ||
| import java.util.function.Function | ||
|
|
||
| class DefaultJiraUserPasswordEncryptionTypeServiceTest { | ||
| private lateinit var sqlClient: MockSshSqlClient | ||
| private lateinit var sshConnection: RememberingSshConnection | ||
| private lateinit var tested: JiraUserPasswordEncryptorProvider | ||
| private val plainTextPasswordEncryptor: JiraUserPasswordEncryptor = PlainTextJiraUserPasswordEncryptor() | ||
| private val encryptedPasswordEncryptor: JiraUserPasswordEncryptor = EncryptedJiraUserPasswordEncryptor(Function { "" }) | ||
|
|
||
| @Before | ||
| fun setup() { | ||
| sqlClient = MockSshSqlClient() | ||
| sshConnection = RememberingSshConnection() | ||
| tested = DefaultJiraUserPasswordEncryptorProvider( | ||
| jiraDatabaseSchemaName = "jiradb", | ||
| plainTextPasswordEncryptor = plainTextPasswordEncryptor, | ||
| encryptedPasswordEncryptor = encryptedPasswordEncryptor | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun shouldQueryEncryptionMethod() { | ||
| // given | ||
| sqlClient.queueReturnedSqlCommandResult( | ||
| SshConnection.SshResult( | ||
| exitStatus = 0, | ||
| output = """attribute_value | ||
| atlassian-security | ||
| """.trimMargin(), | ||
| errorOutput = "" | ||
| ) | ||
| ) | ||
| // when | ||
| tested.getEncryptor(sshConnection, sqlClient) | ||
| // 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 shouldThrowExceptionWhenUnknownEncryption() { | ||
| // when | ||
| var exception: RuntimeException? = null | ||
| try { | ||
| tested.getEncryptor(sshConnection, sqlClient) | ||
| } catch (e: RuntimeException) { | ||
| exception = e | ||
| } | ||
| // then | ||
| assertThat(exception).isNotNull() | ||
| assertThat(exception!!.message).isEqualTo("Unknown jira user password encryption type") | ||
| } | ||
|
|
||
| @Test | ||
| fun shouldReturnEncrypted() { | ||
| // given | ||
| sqlClient.queueReturnedSqlCommandResult( | ||
| SshConnection.SshResult( | ||
| exitStatus = 0, | ||
| output = """attribute_value | ||
| atlassian-security | ||
| """.trimMargin(), | ||
| errorOutput = "" | ||
| ) | ||
| ) | ||
| // when | ||
| val passwordEncryptor = tested.getEncryptor(sshConnection, sqlClient) | ||
| // then | ||
| assertThat(passwordEncryptor).isEqualTo(encryptedPasswordEncryptor) | ||
| } | ||
|
|
||
| @Test | ||
| fun shouldReturnPlainText() { | ||
| // given | ||
| sqlClient.queueReturnedSqlCommandResult( | ||
| SshConnection.SshResult( | ||
| exitStatus = 0, | ||
| output = """attribute_value | ||
| plaintext | ||
| """.trimMargin(), | ||
| errorOutput = "" | ||
| ) | ||
| ) | ||
| // when | ||
| val passwordEncryptor = tested.getEncryptor(sshConnection, sqlClient) | ||
| // then | ||
| assertThat(passwordEncryptor).isEqualTo(plainTextPasswordEncryptor) | ||
| } | ||
|
|
||
| } |
Uh oh!
There was an error while loading. Please reload this page.