-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #109 from modelix/fix-do-not-fall-back-on-a-hard-c…
…oded-encryption-key fix(workspace-manger): do not fall back on a hard-coded encryption key
- Loading branch information
Showing
8 changed files
with
98 additions
and
78 deletions.
There are no files selected for viewing
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
58 changes: 58 additions & 0 deletions
58
workspace-manager/src/main/kotlin/org/modelix/workspace/manager/CredentialsEncryption.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,58 @@ | ||
package org.modelix.workspace.manager | ||
|
||
import org.jasypt.util.text.AES256TextEncryptor | ||
import org.modelix.workspaces.Credentials | ||
import org.modelix.workspaces.GitRepository | ||
import org.modelix.workspaces.Workspace | ||
|
||
/** | ||
* | ||
*/ | ||
class CredentialsEncryption(key: String) { | ||
companion object { | ||
private const val ENCRYPTED_PREFIX = "encrypted:" | ||
} | ||
|
||
|
||
private val encryptor = AES256TextEncryptor() | ||
|
||
init { | ||
encryptor.setPassword(key) | ||
} | ||
|
||
fun decrypt(credentials: Credentials): Credentials { | ||
return Credentials(decrypt(credentials.user), decrypt(credentials.password)) | ||
} | ||
|
||
fun encrypt(credentials: Credentials): Credentials { | ||
return Credentials(encrypt(credentials.user), encrypt(credentials.password)) | ||
} | ||
|
||
private fun encrypt(input: String): String { | ||
return if (input.startsWith(ENCRYPTED_PREFIX)) { | ||
input | ||
} else { | ||
ENCRYPTED_PREFIX + encryptor.encrypt(input) | ||
} | ||
} | ||
|
||
private fun decrypt(input: String): String { | ||
return if (input.startsWith(ENCRYPTED_PREFIX)) { | ||
encryptor.decrypt(input.drop(ENCRYPTED_PREFIX.length)) | ||
} else { | ||
input | ||
} | ||
} | ||
} | ||
|
||
fun CredentialsEncryption.copyWithEncryptedCredentials(workspace: Workspace): Workspace = | ||
workspace.copy(gitRepositories = workspace.gitRepositories.map(::copyWithEncryptedCredentials)) | ||
|
||
fun CredentialsEncryption.copyWithDecryptedCredentials(workspace: Workspace): Workspace = | ||
workspace.copy(gitRepositories = workspace.gitRepositories.map(::copyWithDecryptedCredentials)) | ||
|
||
fun CredentialsEncryption.copyWithEncryptedCredentials(gitRepository: GitRepository): GitRepository = | ||
gitRepository.copy(credentials = gitRepository.credentials?.run(::encrypt)) | ||
|
||
fun CredentialsEncryption.copyWithDecryptedCredentials(gitRepository: GitRepository): GitRepository = | ||
gitRepository.copy(credentials = gitRepository.credentials?.run(::decrypt)) |
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
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
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
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
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
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