PM-19498: Update cipher migration logic (#4921)

This commit is contained in:
David Perez 2025-03-26 12:29:34 -05:00 committed by GitHub
parent 94a91702cc
commit 693d9f18db
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 33 additions and 21 deletions

View File

@ -474,30 +474,36 @@ class CipherManagerImpl(
userId: String, userId: String,
cipherId: String, cipherId: String,
): Result<Cipher> = ): Result<Cipher> =
if (this.key == null) { vaultSdkSource
vaultSdkSource .encryptCipher(userId = userId, cipherView = this)
.encryptCipher(userId = userId, cipherView = this) .flatMap {
.flatMap { // We only migrate the cipher if the original cipher did not have a key and the
ciphersService.updateCipher( // new cipher does. This means the SDK created the key and migration is required.
cipherId = cipherId, if (it.key != null && this.key == null) {
body = it.toEncryptedNetworkCipher(), ciphersService
) .updateCipher(
} cipherId = cipherId,
.flatMap { response -> body = it.toEncryptedNetworkCipher(),
when (response) { )
is UpdateCipherResponseJson.Invalid -> { .flatMap { response ->
IllegalStateException(response.message).asFailure() when (response) {
} is UpdateCipherResponseJson.Invalid -> {
IllegalStateException(response.message).asFailure()
}
is UpdateCipherResponseJson.Success -> { is UpdateCipherResponseJson.Success -> {
vaultDiskSource.saveCipher(userId = userId, cipher = response.cipher) vaultDiskSource.saveCipher(
response.cipher.toEncryptedSdkCipher().asSuccess() userId = userId,
cipher = response.cipher,
)
response.cipher.toEncryptedSdkCipher().asSuccess()
}
}
} }
} } else {
it.asSuccess()
} }
} else { }
vaultSdkSource.encryptCipher(userId = userId, cipherView = this)
}
private suspend fun migrateAttachments( private suspend fun migrateAttachments(
userId: String, userId: String,

View File

@ -1707,6 +1707,7 @@ class CipherManagerTest {
val attachmentId = "mockId-1" val attachmentId = "mockId-1"
val cipher = mockk<Cipher> { val cipher = mockk<Cipher> {
every { key } returns "key"
every { attachments } returns emptyList() every { attachments } returns emptyList()
every { id } returns "mockId-1" every { id } returns "mockId-1"
} }
@ -1747,6 +1748,7 @@ class CipherManagerTest {
every { id } returns attachmentId every { id } returns attachmentId
} }
val cipher = mockk<Cipher> { val cipher = mockk<Cipher> {
every { key } returns "key"
every { attachments } returns listOf(attachment) every { attachments } returns listOf(attachment)
every { id } returns "mockId-1" every { id } returns "mockId-1"
} }
@ -1792,6 +1794,7 @@ class CipherManagerTest {
every { id } returns attachmentId every { id } returns attachmentId
} }
val cipher = mockk<Cipher> { val cipher = mockk<Cipher> {
every { key } returns "key"
every { attachments } returns listOf(attachment) every { attachments } returns listOf(attachment)
every { id } returns "mockId-1" every { id } returns "mockId-1"
} }
@ -1839,6 +1842,7 @@ class CipherManagerTest {
every { id } returns attachmentId every { id } returns attachmentId
} }
val cipher = mockk<Cipher> { val cipher = mockk<Cipher> {
every { key } returns "key"
every { attachments } returns listOf(attachment) every { attachments } returns listOf(attachment)
every { id } returns "mockId-1" every { id } returns "mockId-1"
} }
@ -1892,6 +1896,7 @@ class CipherManagerTest {
every { id } returns attachmentId every { id } returns attachmentId
} }
val cipher = mockk<Cipher> { val cipher = mockk<Cipher> {
every { key } returns "key"
every { attachments } returns listOf(attachment) every { attachments } returns listOf(attachment)
every { id } returns "mockId-1" every { id } returns "mockId-1"
} }
@ -1967,6 +1972,7 @@ class CipherManagerTest {
every { id } returns attachmentId every { id } returns attachmentId
} }
val cipher = mockk<Cipher> { val cipher = mockk<Cipher> {
every { key } returns "key"
every { attachments } returns listOf(attachment) every { attachments } returns listOf(attachment)
every { id } returns "mockId-1" every { id } returns "mockId-1"
} }