|
| 1 | +/* |
| 2 | + * Copyright (C) 2025 The ORT Server Authors (See <https://github.com/eclipse-apoapsis/ort-server/blob/main/NOTICE>) |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + * SPDX-License-Identifier: Apache-2.0 |
| 17 | + * License-Filename: LICENSE |
| 18 | + */ |
| 19 | + |
| 20 | +package org.eclipse.apoapsis.ortserver.components.resolutions.routes.vulnerabilities |
| 21 | + |
| 22 | +import io.github.smiley4.ktoropenapi.post |
| 23 | + |
| 24 | +import io.ktor.http.HttpStatusCode |
| 25 | +import io.ktor.server.auth.principal |
| 26 | +import io.ktor.server.response.respond |
| 27 | +import io.ktor.server.routing.Route |
| 28 | + |
| 29 | +import kotlinx.datetime.Instant |
| 30 | + |
| 31 | +import org.eclipse.apoapsis.ortserver.components.authorization.keycloak.AuthorizationException |
| 32 | +import org.eclipse.apoapsis.ortserver.components.authorization.keycloak.OrtPrincipal |
| 33 | +import org.eclipse.apoapsis.ortserver.components.authorization.keycloak.getFullName |
| 34 | +import org.eclipse.apoapsis.ortserver.components.authorization.keycloak.getUserId |
| 35 | +import org.eclipse.apoapsis.ortserver.components.authorization.keycloak.getUsername |
| 36 | +import org.eclipse.apoapsis.ortserver.components.authorization.keycloak.permissions.RepositoryPermission |
| 37 | +import org.eclipse.apoapsis.ortserver.components.authorization.keycloak.requirePermission |
| 38 | +import org.eclipse.apoapsis.ortserver.components.resolutions.VulnerabilityResolutionDefinitionService |
| 39 | +import org.eclipse.apoapsis.ortserver.model.UserDisplayName as ModelUserDisplayName |
| 40 | +import org.eclipse.apoapsis.ortserver.shared.apimappings.mapToApi |
| 41 | +import org.eclipse.apoapsis.ortserver.shared.apimodel.ChangeEvent |
| 42 | +import org.eclipse.apoapsis.ortserver.shared.apimodel.ChangeEventAction |
| 43 | +import org.eclipse.apoapsis.ortserver.shared.apimodel.UserDisplayName |
| 44 | +import org.eclipse.apoapsis.ortserver.shared.apimodel.VulnerabilityResolutionDefinition |
| 45 | +import org.eclipse.apoapsis.ortserver.shared.apimodel.VulnerabilityResolutionReason |
| 46 | +import org.eclipse.apoapsis.ortserver.shared.ktorutils.jsonBody |
| 47 | +import org.eclipse.apoapsis.ortserver.shared.ktorutils.requireIdParameter |
| 48 | +import org.eclipse.apoapsis.ortserver.shared.ktorutils.respondError |
| 49 | + |
| 50 | +internal fun Route.restoreVulnerabilityResolution( |
| 51 | + vulnerabilityResolutionDefinitionService: VulnerabilityResolutionDefinitionService |
| 52 | +) = post("/resolutions/vulnerabilities/{id}/restore", { |
| 53 | + operationId = "restoreVulnerabilityResolution" |
| 54 | + summary = "Restore a vulnerability resolution" |
| 55 | + tags = listOf("Resolutions") |
| 56 | + |
| 57 | + request { |
| 58 | + pathParameter<Long>("id") { |
| 59 | + description = "The ID of the vulnerability resolution definition." |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + response { |
| 64 | + HttpStatusCode.OK to { |
| 65 | + description = "Success" |
| 66 | + jsonBody<VulnerabilityResolutionDefinition> { |
| 67 | + example("Restore Vulnerability Resolution") { |
| 68 | + value = VulnerabilityResolutionDefinition( |
| 69 | + id = 1, |
| 70 | + idMatchers = listOf("CVE-2020-15250", "GHSA-269g-pwp5-87pp"), |
| 71 | + reason = VulnerabilityResolutionReason.INEFFECTIVE_VULNERABILITY, |
| 72 | + comment = "Comment", |
| 73 | + archived = false, |
| 74 | + changes = listOf( |
| 75 | + ChangeEvent( |
| 76 | + user = UserDisplayName(username = "User"), |
| 77 | + occurredAt = Instant.parse("2024-01-01T00:00:00Z"), |
| 78 | + ChangeEventAction.CREATE |
| 79 | + ), |
| 80 | + ChangeEvent( |
| 81 | + user = UserDisplayName(username = "User"), |
| 82 | + occurredAt = Instant.parse("2024-01-03T00:00:00Z"), |
| 83 | + ChangeEventAction.ARCHIVE |
| 84 | + ), |
| 85 | + ChangeEvent( |
| 86 | + user = UserDisplayName(username = "User"), |
| 87 | + occurredAt = Instant.parse("2024-01-04T00:00:00Z"), |
| 88 | + ChangeEventAction.RESTORE |
| 89 | + ) |
| 90 | + ) |
| 91 | + ) |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + HttpStatusCode.NoContent to { |
| 97 | + description = "The vulnerability resolution is not archived." |
| 98 | + } |
| 99 | + } |
| 100 | +}) { |
| 101 | + val id = call.requireIdParameter("id") |
| 102 | + |
| 103 | + val definition = vulnerabilityResolutionDefinitionService.getById(id) ?: throw AuthorizationException() |
| 104 | + |
| 105 | + requirePermission(RepositoryPermission.WRITE.roleName(definition.hierarchyId.value)) |
| 106 | + |
| 107 | + if (!definition.archived) { |
| 108 | + call.respond(HttpStatusCode.NoContent, "The vulnerability resolution is not archived.") |
| 109 | + return@post |
| 110 | + } |
| 111 | + |
| 112 | + // Extract the user information from the principal. |
| 113 | + val userDisplayName = call.principal<OrtPrincipal>()?.let { principal -> |
| 114 | + ModelUserDisplayName(principal.getUserId(), principal.getUsername(), principal.getFullName()) |
| 115 | + } |
| 116 | + |
| 117 | + if (userDisplayName == null) { |
| 118 | + call.respondError(HttpStatusCode.InternalServerError, "Unable to resolve user display name from token.") |
| 119 | + return@post |
| 120 | + } |
| 121 | + |
| 122 | + val vulnerabilityResolutionDefinition = |
| 123 | + vulnerabilityResolutionDefinitionService.restore(id, userDisplayName).mapToApi() |
| 124 | + |
| 125 | + call.respond(HttpStatusCode.OK, vulnerabilityResolutionDefinition) |
| 126 | +} |
0 commit comments