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

This commit is contained in:
David Perez
2025-03-26 12:29:28 -05:00
committed by GitHub
parent 6081b7e932
commit 3e15b60178
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"
}