From 614475fc50a58d197a8e8fa0030bd3dda706f1cd Mon Sep 17 00:00:00 2001 From: Mikolaj Grzaslewicz Date: Tue, 11 Jan 2022 12:04:17 +0100 Subject: [PATCH] JPERF-729 Simplify password encryptor API - and use java.util.Function instead of kotlin interface - for java compatibility --- .../JiraUserPasswordEncryptor.kt | 36 +++++-------------- .../JiraUserPasswordOverridingDatabase.kt | 16 +++------ ...raUserPasswordEncryptionTypeServiceTest.kt | 25 +++++++------ .../JiraUserPasswordOverridingDatabaseTest.kt | 10 +++--- 4 files changed, 34 insertions(+), 53 deletions(-) diff --git a/src/main/kotlin/com/atlassian/performance/tools/infrastructure/api/database/passwordoverride/JiraUserPasswordEncryptor.kt b/src/main/kotlin/com/atlassian/performance/tools/infrastructure/api/database/passwordoverride/JiraUserPasswordEncryptor.kt index d7f023f8..ffa84a5a 100644 --- a/src/main/kotlin/com/atlassian/performance/tools/infrastructure/api/database/passwordoverride/JiraUserPasswordEncryptor.kt +++ b/src/main/kotlin/com/atlassian/performance/tools/infrastructure/api/database/passwordoverride/JiraUserPasswordEncryptor.kt @@ -4,33 +4,26 @@ import com.atlassian.performance.tools.infrastructure.database.SshSqlClient import com.atlassian.performance.tools.ssh.api.SshConnection import java.util.function.Function -enum class JiraUserPasswordEncryptionType { - PLAIN_TEXT, ENCRYPTED -} - interface JiraUserPasswordEncryptor { fun getEncryptedPassword(plainTextPassword: String): String } interface JiraUserPasswordEncryptorProvider { - fun get(jiraUserPasswordEncryptionType: JiraUserPasswordEncryptionType): JiraUserPasswordEncryptor -} - - -interface JiraUserPasswordEncryptionTypeService { - fun getEncryptionType(ssh: SshConnection, sqlClient: SshSqlClient): JiraUserPasswordEncryptionType + fun getEncryptor(ssh: SshConnection, sqlClient: SshSqlClient): JiraUserPasswordEncryptor } -class DefaultJiraUserPasswordEncryptionTypeService( - private val jiraDatabaseSchemaName: String -) : JiraUserPasswordEncryptionTypeService { +class DefaultJiraUserPasswordEncryptorProvider( + private val jiraDatabaseSchemaName: String, + private val plainTextPasswordEncryptor: JiraUserPasswordEncryptor, + private val encryptedPasswordEncryptor: JiraUserPasswordEncryptor +) : JiraUserPasswordEncryptorProvider { - override fun getEncryptionType(ssh: SshConnection, sqlClient: SshSqlClient): JiraUserPasswordEncryptionType { + 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") -> JiraUserPasswordEncryptionType.PLAIN_TEXT - sqlResult.contains("atlassian-security") -> JiraUserPasswordEncryptionType.ENCRYPTED + sqlResult.contains("plaintext") -> plainTextPasswordEncryptor + sqlResult.contains("atlassian-security") -> encryptedPasswordEncryptor else -> throw RuntimeException("Unknown jira user password encryption type") } } @@ -45,14 +38,3 @@ class EncryptedJiraUserPasswordEncryptor( class PlainTextJiraUserPasswordEncryptor : JiraUserPasswordEncryptor { override fun getEncryptedPassword(plainTextPassword: String) = plainTextPassword } - - -class DefaultJiraUserPasswordEncryptorProvider(passwordEncryptFunction: Function) : JiraUserPasswordEncryptorProvider { - private val encryptors = mapOf( - JiraUserPasswordEncryptionType.PLAIN_TEXT to PlainTextJiraUserPasswordEncryptor(), - JiraUserPasswordEncryptionType.ENCRYPTED to EncryptedJiraUserPasswordEncryptor(passwordEncryptFunction) - ) - - override fun get(jiraUserPasswordEncryptionType: JiraUserPasswordEncryptionType) = encryptors[jiraUserPasswordEncryptionType]!! - -} \ No newline at end of file diff --git a/src/main/kotlin/com/atlassian/performance/tools/infrastructure/api/database/passwordoverride/JiraUserPasswordOverridingDatabase.kt b/src/main/kotlin/com/atlassian/performance/tools/infrastructure/api/database/passwordoverride/JiraUserPasswordOverridingDatabase.kt index 4cbe53a6..b0ac37d8 100644 --- a/src/main/kotlin/com/atlassian/performance/tools/infrastructure/api/database/passwordoverride/JiraUserPasswordOverridingDatabase.kt +++ b/src/main/kotlin/com/atlassian/performance/tools/infrastructure/api/database/passwordoverride/JiraUserPasswordOverridingDatabase.kt @@ -15,7 +15,6 @@ class JiraUserPasswordOverridingDatabase internal constructor( private val username: String, private val jiraDatabaseSchemaName: String, private val userPasswordPlainText: String, - private val userPasswordEncryptionTypeService: JiraUserPasswordEncryptionTypeService, private val userPasswordEncryptorProvider: JiraUserPasswordEncryptorProvider ) : Database { private val logger: Logger = LogManager.getLogger(this::class.java) @@ -27,8 +26,7 @@ class JiraUserPasswordOverridingDatabase internal constructor( ssh: SshConnection ) { databaseDelegate.start(jira, ssh) - val userPasswordEncryptionType = userPasswordEncryptionTypeService.getEncryptionType(ssh, sqlClient) - val userPasswordEncryptor = userPasswordEncryptorProvider.get(userPasswordEncryptionType) + 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}'") @@ -38,7 +36,6 @@ class JiraUserPasswordOverridingDatabase internal constructor( class Builder( private var databaseDelegate: Database, private var userPasswordPlainText: String, - private var userPasswordEncryptionTypeService: JiraUserPasswordEncryptionTypeService, private var userPasswordEncryptorProvider: JiraUserPasswordEncryptorProvider ) { private var sqlClient: SshSqlClient = SshMysqlClient() @@ -50,9 +47,6 @@ class JiraUserPasswordOverridingDatabase internal constructor( 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 userPasswordEncryptionTypeService(userPasswordEncryptionTypeService: JiraUserPasswordEncryptionTypeService) = - apply { this.userPasswordEncryptionTypeService = userPasswordEncryptionTypeService } - fun userPasswordEncryptorProvider(userPasswordEncryptorProvider: JiraUserPasswordEncryptorProvider) = apply { this.userPasswordEncryptorProvider = userPasswordEncryptorProvider } @@ -62,7 +56,6 @@ class JiraUserPasswordOverridingDatabase internal constructor( username = username, userPasswordPlainText = userPasswordPlainText, jiraDatabaseSchemaName = jiraDatabaseSchemaName, - userPasswordEncryptionTypeService = userPasswordEncryptionTypeService, userPasswordEncryptorProvider = userPasswordEncryptorProvider ) } @@ -81,11 +74,10 @@ fun Database.withAdminPassword(adminPasswordPlainText: String, passwordEncryptFu return JiraUserPasswordOverridingDatabase.Builder( databaseDelegate = this, userPasswordPlainText = adminPasswordPlainText, - userPasswordEncryptionTypeService = DefaultJiraUserPasswordEncryptionTypeService( - jiraDatabaseSchemaName = jiraDatabaseSchemaName - ), userPasswordEncryptorProvider = DefaultJiraUserPasswordEncryptorProvider( - passwordEncryptFunction = passwordEncryptFunction + jiraDatabaseSchemaName = jiraDatabaseSchemaName, + plainTextPasswordEncryptor = PlainTextJiraUserPasswordEncryptor(), + encryptedPasswordEncryptor = EncryptedJiraUserPasswordEncryptor(passwordEncryptFunction) ) ) .jiraDatabaseSchemaName(jiraDatabaseSchemaName) diff --git a/src/test/kotlin/com/atlassian/performance/tools/infrastructure/api/database/passwordoverride/DefaultJiraUserPasswordEncryptionTypeServiceTest.kt b/src/test/kotlin/com/atlassian/performance/tools/infrastructure/api/database/passwordoverride/DefaultJiraUserPasswordEncryptionTypeServiceTest.kt index 8f88ef68..3f3acf17 100644 --- a/src/test/kotlin/com/atlassian/performance/tools/infrastructure/api/database/passwordoverride/DefaultJiraUserPasswordEncryptionTypeServiceTest.kt +++ b/src/test/kotlin/com/atlassian/performance/tools/infrastructure/api/database/passwordoverride/DefaultJiraUserPasswordEncryptionTypeServiceTest.kt @@ -6,18 +6,23 @@ 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: JiraUserPasswordEncryptionTypeService + private lateinit var tested: JiraUserPasswordEncryptorProvider + private val plainTextPasswordEncryptor: JiraUserPasswordEncryptor = PlainTextJiraUserPasswordEncryptor() + private val encryptedPasswordEncryptor: JiraUserPasswordEncryptor = EncryptedJiraUserPasswordEncryptor(Function { "" }) @Before fun setup() { sqlClient = MockSshSqlClient() sshConnection = RememberingSshConnection() - tested = DefaultJiraUserPasswordEncryptionTypeService( - jiraDatabaseSchemaName = "jiradb" + tested = DefaultJiraUserPasswordEncryptorProvider( + jiraDatabaseSchemaName = "jiradb", + plainTextPasswordEncryptor = plainTextPasswordEncryptor, + encryptedPasswordEncryptor = encryptedPasswordEncryptor ) } @@ -34,7 +39,7 @@ class DefaultJiraUserPasswordEncryptionTypeServiceTest { ) ) // when - tested.getEncryptionType(sshConnection, sqlClient) + tested.getEncryptor(sshConnection, sqlClient) // then assertThat(sqlClient.getLog()) .`as`("sql queries executed") @@ -48,9 +53,9 @@ class DefaultJiraUserPasswordEncryptionTypeServiceTest { // when var exception: RuntimeException? = null try { - tested.getEncryptionType(sshConnection, sqlClient) + tested.getEncryptor(sshConnection, sqlClient) } catch (e: RuntimeException) { - exception = e + exception = e } // then assertThat(exception).isNotNull() @@ -70,9 +75,9 @@ class DefaultJiraUserPasswordEncryptionTypeServiceTest { ) ) // when - val encryptionType = tested.getEncryptionType(sshConnection, sqlClient) + val passwordEncryptor = tested.getEncryptor(sshConnection, sqlClient) // then - assertThat(encryptionType).isEqualTo(JiraUserPasswordEncryptionType.ENCRYPTED) + assertThat(passwordEncryptor).isEqualTo(encryptedPasswordEncryptor) } @Test @@ -88,9 +93,9 @@ class DefaultJiraUserPasswordEncryptionTypeServiceTest { ) ) // when - val encryptionType = tested.getEncryptionType(sshConnection, sqlClient) + val passwordEncryptor = tested.getEncryptor(sshConnection, sqlClient) // then - assertThat(encryptionType).isEqualTo(JiraUserPasswordEncryptionType.PLAIN_TEXT) + assertThat(passwordEncryptor).isEqualTo(plainTextPasswordEncryptor) } } \ No newline at end of file diff --git a/src/test/kotlin/com/atlassian/performance/tools/infrastructure/api/database/passwordoverride/JiraUserPasswordOverridingDatabaseTest.kt b/src/test/kotlin/com/atlassian/performance/tools/infrastructure/api/database/passwordoverride/JiraUserPasswordOverridingDatabaseTest.kt index 25412ba9..05fb5514 100644 --- a/src/test/kotlin/com/atlassian/performance/tools/infrastructure/api/database/passwordoverride/JiraUserPasswordOverridingDatabaseTest.kt +++ b/src/test/kotlin/com/atlassian/performance/tools/infrastructure/api/database/passwordoverride/JiraUserPasswordOverridingDatabaseTest.kt @@ -10,7 +10,6 @@ import org.assertj.core.api.Assertions.assertThat import org.junit.Before import org.junit.Test import java.net.URI -import java.util.function.Function class JiraUserPasswordOverridingDatabaseTest { @@ -32,9 +31,12 @@ class JiraUserPasswordOverridingDatabaseTest { .Builder( databaseDelegate = underlyingDatabase, userPasswordPlainText = samplePassword, - userPasswordEncryptorProvider = DefaultJiraUserPasswordEncryptorProvider(passwordEncryptFunction = Function { expectedEncryptedPassword }), - userPasswordEncryptionTypeService = object : JiraUserPasswordEncryptionTypeService { - override fun getEncryptionType(ssh: SshConnection, sqlClient: SshSqlClient) = JiraUserPasswordEncryptionType.ENCRYPTED + userPasswordEncryptorProvider = object : JiraUserPasswordEncryptorProvider { + override fun getEncryptor(ssh: SshConnection, sqlClient: SshSqlClient): JiraUserPasswordEncryptor { + return object : JiraUserPasswordEncryptor { + override fun getEncryptedPassword(plainTextPassword: String) = expectedEncryptedPassword + } + } } ) .sqlClient(sqlClient)