Skip to content

Commit

Permalink
fix: minor fixes for delete key and remove key
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperBatata committed Nov 22, 2024
1 parent 0b706dd commit 7924fde
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -516,65 +516,41 @@ class SSIKit2WalletService(
}
}

override suspend fun deleteKey(alias: String): Boolean = runCatching {
val key = KeysService.get(walletId, alias)
key?.let {
val resolvedKey = KeyManager.resolveSerializedKey(it.document)
val deleteRemoteKey = resolvedKey.deleteKey()
if (deleteRemoteKey) {
KeysService.delete(walletId, alias)
} else {
throw WebException(HttpStatusCode.BadRequest, "Failed to delete remote key : $alias")
}
eventUseCase.log(
action = EventType.Key.Delete,
originator = "wallet",
tenant = tenant,
accountId = accountId,
walletId = walletId,
data = eventUseCase.keyEventData(
id = alias,
algorithm = resolvedKey.keyType.name,
kmsType = EventDataNotAvailable
)
)
} ?: throw NotFoundException("Key not found for alias: $alias")
override suspend fun deleteKey(alias: String): Boolean =
performKeyDelete(alias = alias, isTotalDelete = true).getOrThrow().first

}.fold(
onSuccess = { true },
onFailure = {
logger.error(it) { "Failed to delete key: ${it.message}" }
throw WebException(HttpStatusCode.BadRequest, "Failed to delete key: ${it.message}")
}
)

override suspend fun removeKey(alias: String): Boolean =
performKeyDelete(alias = alias, isTotalDelete = false).getOrThrow().first

override suspend fun removeKey(alias: String): Boolean = runCatching {
val key = KeysService.get(walletId, alias)
key?.let {
val resolvedKey = KeyManager.resolveSerializedKey(it.document)
KeysService.delete(walletId, alias)
eventUseCase.log(
action = EventType.Key.Delete,
originator = "wallet",
tenant = tenant,
accountId = accountId,
walletId = walletId,
data = eventUseCase.keyEventData(
id = alias,
algorithm = resolvedKey.keyType.name,
kmsType = EventDataNotAvailable
)
)
} ?: throw NotFoundException("Key not found for alias: $alias")

}.fold(
onSuccess = { true },
onFailure = {
logger.error(it) { "Failed to delete key: ${it.message}" }
throw WebException(HttpStatusCode.BadRequest, "Failed to delete key: ${it.message}")
}
)
private suspend fun performKeyDelete(alias: String, isTotalDelete: Boolean) = runCatching {
val key = getKey(alias)
val canDeleteFromStorage = isTotalDelete && key.deleteKey() || !isTotalDelete
val opSucceed = canDeleteFromStorage && KeysService.delete(walletId, alias)
if (isTotalDelete && !opSucceed) throw WebException(
HttpStatusCode.BadRequest,
"Failed to delete remote key : $alias"
)
Pair(opSucceed, key)
}.onSuccess {
eventUseCase.log(
action = (if (isTotalDelete) EventType.Key.Delete else EventType.Key.Remove),
originator = "wallet",
tenant = tenant,
accountId = accountId,
walletId = walletId,
data = eventUseCase.keyEventData(
id = alias,
algorithm = it.second.keyType.name,
kmsType = EventDataNotAvailable
)
)
}.onFailure {
val errorMessage = "Failed to delete key: ${it.message}"
logger.error(it) { errorMessage }
throw WebException(HttpStatusCode.BadRequest, errorMessage)
}


override fun getHistory(limit: Int, offset: Long): List<WalletOperationHistory> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ sealed interface EventType {
data object Import : Action(this.javaClass.simpleName)
data object Export : Action(this.javaClass.simpleName)
data object Sign : Action(this.javaClass.simpleName)
data object Remove : Action(this.javaClass.simpleName)
}

data object Did : EventType {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.server.util.getOrFail
import kotlinx.coroutines.runBlocking
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive
Expand Down Expand Up @@ -221,29 +222,27 @@ fun Application.keys() = walletRoute {
}

delete({
summary = "Delete a specific key (hard delete)"
summary =
"Delete a specific key , will delete the key (AWS , OCI , TSE) and its reference in the wallet"
response {
HttpStatusCode.Accepted to { description = "Key deleted" }
HttpStatusCode.BadRequest to { description = "Key could not be deleted" }
}
}) {
val keyId = context.parameters["keyId"] ?: throw IllegalArgumentException("No key id provided.")
val keyId = context.parameters.getOrFail("keyId")

val success = getWalletService().deleteKey(keyId)
context.respond(if (success) HttpStatusCode.Accepted else HttpStatusCode.BadRequest)
}

delete("remove", {
summary = "Remove a specific key (soft delete)"
summary = "Remove a specific key from the wallet"
response {
HttpStatusCode.Accepted to { description = "Key removed" }
HttpStatusCode.BadRequest to { description = "Failed to remove the key" }
}
}) {
val keyId = context.parameters["keyId"] ?: return@delete context.respond(
HttpStatusCode.BadRequest,
"Key ID is missing"
)
val keyId = context.parameters.getOrFail("keyId")

val success = getWalletService().removeKey(keyId)
context.respond(if (success) HttpStatusCode.Accepted else HttpStatusCode.BadRequest)
Expand Down

0 comments on commit 7924fde

Please sign in to comment.