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

View File

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