|
| 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.patch |
| 23 | + |
| 24 | +import io.ktor.http.HttpStatusCode |
| 25 | +import io.ktor.server.auth.principal |
| 26 | +import io.ktor.server.request.receive |
| 27 | +import io.ktor.server.response.respond |
| 28 | +import io.ktor.server.routing.Route |
| 29 | + |
| 30 | +import kotlinx.datetime.Instant |
| 31 | + |
| 32 | +import org.eclipse.apoapsis.ortserver.components.authorization.keycloak.AuthorizationException |
| 33 | +import org.eclipse.apoapsis.ortserver.components.authorization.keycloak.OrtPrincipal |
| 34 | +import org.eclipse.apoapsis.ortserver.components.authorization.keycloak.getFullName |
| 35 | +import org.eclipse.apoapsis.ortserver.components.authorization.keycloak.getUserId |
| 36 | +import org.eclipse.apoapsis.ortserver.components.authorization.keycloak.getUsername |
| 37 | +import org.eclipse.apoapsis.ortserver.components.authorization.keycloak.permissions.RepositoryPermission |
| 38 | +import org.eclipse.apoapsis.ortserver.components.authorization.keycloak.requirePermission |
| 39 | +import org.eclipse.apoapsis.ortserver.components.resolutions.PatchVulnerabilityResolution |
| 40 | +import org.eclipse.apoapsis.ortserver.components.resolutions.VulnerabilityResolutionDefinitionService |
| 41 | +import org.eclipse.apoapsis.ortserver.model.UserDisplayName as ModelUserDisplayName |
| 42 | +import org.eclipse.apoapsis.ortserver.shared.apimappings.mapToApi |
| 43 | +import org.eclipse.apoapsis.ortserver.shared.apimappings.mapToModel |
| 44 | +import org.eclipse.apoapsis.ortserver.shared.apimodel.ChangeEvent |
| 45 | +import org.eclipse.apoapsis.ortserver.shared.apimodel.ChangeEventAction |
| 46 | +import org.eclipse.apoapsis.ortserver.shared.apimodel.UserDisplayName |
| 47 | +import org.eclipse.apoapsis.ortserver.shared.apimodel.VulnerabilityResolutionDefinition |
| 48 | +import org.eclipse.apoapsis.ortserver.shared.apimodel.VulnerabilityResolutionReason |
| 49 | +import org.eclipse.apoapsis.ortserver.shared.apimodel.asPresent |
| 50 | +import org.eclipse.apoapsis.ortserver.shared.ktorutils.jsonBody |
| 51 | +import org.eclipse.apoapsis.ortserver.shared.ktorutils.requireIdParameter |
| 52 | +import org.eclipse.apoapsis.ortserver.shared.ktorutils.respondError |
| 53 | + |
| 54 | +internal fun Route.patchVulnerabilityResolution( |
| 55 | + vulnerabilityResolutionDefinitionService: VulnerabilityResolutionDefinitionService |
| 56 | +) = patch("/resolutions/vulnerabilities/{id}", { |
| 57 | + operationId = "patchVulnerabilityResolution" |
| 58 | + summary = "Update a vulnerability resolution" |
| 59 | + tags = listOf("Resolutions") |
| 60 | + |
| 61 | + request { |
| 62 | + pathParameter<Long>("id") { |
| 63 | + description = "The ID of the vulnerability resolution definition" |
| 64 | + } |
| 65 | + |
| 66 | + jsonBody<PatchVulnerabilityResolution> { |
| 67 | + description = "Set the values that should be updated." |
| 68 | + example("Update Vulnerability Resolution") { |
| 69 | + value = PatchVulnerabilityResolution( |
| 70 | + idMatchers = listOf("CVE-2020-15250", "GHSA-269g-pwp5-87pp").asPresent(), |
| 71 | + reason = VulnerabilityResolutionReason.WILL_NOT_FIX_VULNERABILITY.asPresent(), |
| 72 | + comment = "Updated comment.".asPresent() |
| 73 | + ) |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + response { |
| 79 | + HttpStatusCode.OK to { |
| 80 | + description = "Success" |
| 81 | + |
| 82 | + jsonBody<VulnerabilityResolutionDefinition> { |
| 83 | + example("Update Vulnerability Resolution") { |
| 84 | + value = VulnerabilityResolutionDefinition( |
| 85 | + id = 1, |
| 86 | + idMatchers = listOf("CVE-2020-15250", "GHSA-269g-pwp5-87pp"), |
| 87 | + reason = VulnerabilityResolutionReason.WILL_NOT_FIX_VULNERABILITY, |
| 88 | + comment = "Updated comment.", |
| 89 | + archived = false, |
| 90 | + changes = listOf( |
| 91 | + ChangeEvent( |
| 92 | + user = UserDisplayName(username = "User"), |
| 93 | + occurredAt = Instant.parse("2024-01-01T00:00:00Z"), |
| 94 | + action = ChangeEventAction.CREATE |
| 95 | + ), |
| 96 | + ChangeEvent( |
| 97 | + user = UserDisplayName(username = "User"), |
| 98 | + occurredAt = Instant.parse("2024-01-02T00:00:00Z"), |
| 99 | + action = ChangeEventAction.UPDATE |
| 100 | + ) |
| 101 | + ) |
| 102 | + ) |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + HttpStatusCode.BadRequest to { |
| 108 | + description = "The requested vulnerability resolution is archived." |
| 109 | + } |
| 110 | + } |
| 111 | +}) { |
| 112 | + val id = call.requireIdParameter("id") |
| 113 | + |
| 114 | + val definition = vulnerabilityResolutionDefinitionService.getById(id) ?: throw AuthorizationException() |
| 115 | + |
| 116 | + requirePermission(RepositoryPermission.WRITE.roleName(definition.hierarchyId.value)) |
| 117 | + |
| 118 | + if (definition.archived) { |
| 119 | + call.respondError(HttpStatusCode.Conflict, "The requested vulnerability resolution is archived.") |
| 120 | + return@patch |
| 121 | + } |
| 122 | + |
| 123 | + val updateResolution = call.receive<PatchVulnerabilityResolution>() |
| 124 | + |
| 125 | + // Extract the user information from the principal. |
| 126 | + val userDisplayName = call.principal<OrtPrincipal>()?.let { principal -> |
| 127 | + ModelUserDisplayName(principal.getUserId(), principal.getUsername(), principal.getFullName()) |
| 128 | + } |
| 129 | + |
| 130 | + if (userDisplayName == null) { |
| 131 | + call.respondError(HttpStatusCode.InternalServerError, "Unable to resolve user display name from token.") |
| 132 | + return@patch |
| 133 | + } |
| 134 | + |
| 135 | + val updatedDefinition = vulnerabilityResolutionDefinitionService.update( |
| 136 | + id, |
| 137 | + userDisplayName, |
| 138 | + updateResolution.idMatchers.mapToModel(), |
| 139 | + updateResolution.reason.mapToModel { it.mapToModel() }, |
| 140 | + updateResolution.comment.mapToModel() |
| 141 | + ).mapToApi() |
| 142 | + |
| 143 | + call.respond(HttpStatusCode.OK, updatedDefinition) |
| 144 | +} |
0 commit comments