|
91 | 91 | import java.util.Arrays; |
92 | 92 | import java.util.Base64; |
93 | 93 | import java.util.List; |
| 94 | +import java.util.Objects; |
94 | 95 | import java.util.Set; |
| 96 | +import java.util.UUID; |
| 97 | +import java.util.stream.Stream; |
95 | 98 |
|
96 | 99 | import static java.util.function.Predicate.not; |
97 | 100 | import static org.dependencytrack.model.ConfigPropertyConstants.BOM_VALIDATION_MODE; |
@@ -308,46 +311,48 @@ public Response uploadBom(@Parameter(required = true) BomSubmitRequest request) |
308 | 311 | validator.validateProperty(request, "bom") |
309 | 312 | ); |
310 | 313 | try (QueryManager qm = new QueryManager()) { |
311 | | - Project project = qm.getProject(request.getProjectName(), request.getProjectVersion()); |
| 314 | + Project parent = null; |
| 315 | + UUID parentUuid = null; |
| 316 | + if (request.getParentUUID() != null) { |
| 317 | + failOnValidationError(validator.validateProperty(request, "parentUUID")); |
| 318 | + parent = qm.getProject(request.getParentUUID()); |
| 319 | + parentUuid = parent.getUuid(); |
| 320 | + } |
| 321 | + |
| 322 | + Project project = qm.getProject(request.getProjectName(), request.getProjectVersion(), parentUuid); |
312 | 323 | if (project == null && request.isAutoCreate()) { |
313 | | - if (hasPermission(Permissions.Constants.PORTFOLIO_MANAGEMENT) || hasPermission(Permissions.Constants.PORTFOLIO_MANAGEMENT_CREATE) || hasPermission(Permissions.Constants.PROJECT_CREATION_UPLOAD)) { |
314 | | - Project parent = null; |
315 | | - if (request.getParentUUID() != null || request.getParentName() != null) { |
316 | | - if (request.getParentUUID() != null) { |
317 | | - failOnValidationError(validator.validateProperty(request, "parentUUID")); |
318 | | - parent = qm.getObjectByUuid(Project.class, request.getParentUUID()); |
319 | | - } else { |
320 | | - failOnValidationError( |
321 | | - validator.validateProperty(request, "parentName"), |
322 | | - validator.validateProperty(request, "parentVersion") |
323 | | - ); |
324 | | - final String trimmedParentName = StringUtils.trimToNull(request.getParentName()); |
325 | | - final String trimmedParentVersion = StringUtils.trimToNull(request.getParentVersion()); |
326 | | - parent = qm.getProject(trimmedParentName, trimmedParentVersion); |
327 | | - } |
328 | | - |
329 | | - if (parent == null) { // if parent project is specified but not found |
330 | | - return Response.status(Response.Status.NOT_FOUND).entity("The parent project could not be found.").build(); |
331 | | - } |
332 | | - requireAccess(qm, parent, "Access to the specified parent project is forbidden"); |
333 | | - } |
334 | | - final String trimmedProjectName = StringUtils.trimToNull(request.getProjectName()); |
335 | | - if (request.isLatestProjectVersion()) { |
336 | | - final Project oldLatest = qm.getLatestProjectVersion(trimmedProjectName); |
337 | | - if(oldLatest != null) { |
338 | | - requireAccess(qm, oldLatest, "Access to the previous latest project version is forbidden"); |
339 | | - } |
340 | | - } |
341 | | - project = qm.createProject(trimmedProjectName, null, |
342 | | - StringUtils.trimToNull(request.getProjectVersion()), request.getProjectTags(), parent, |
343 | | - null, null, request.isLatestProjectVersion(), true); |
344 | | - Principal principal = getPrincipal(); |
345 | | - qm.updateNewProjectACL(project, principal); |
346 | | - } else { |
| 324 | + if (Stream.of(Permissions.Constants.PORTFOLIO_MANAGEMENT, Permissions.Constants.PORTFOLIO_MANAGEMENT_CREATE, Permissions.Constants.PROJECT_CREATION_UPLOAD).noneMatch(this::hasPermission)) { |
347 | 325 | return Response.status(Response.Status.UNAUTHORIZED).entity("The principal does not have permission to create project.").build(); |
348 | 326 | } |
| 327 | + |
| 328 | + if (parent == null) { |
| 329 | + failOnValidationError( |
| 330 | + validator.validateProperty(request, "parentName"), |
| 331 | + validator.validateProperty(request, "parentVersion")); |
| 332 | + final String trimmedParentName = StringUtils.trimToNull(request.getParentName()); |
| 333 | + final String trimmedParentVersion = StringUtils.trimToNull(request.getParentVersion()); |
| 334 | + parent = qm.getProject(trimmedParentName, trimmedParentVersion, parentUuid); |
| 335 | + } |
| 336 | + |
| 337 | + Objects.requireNonNull(parent); |
| 338 | + requireAccess(qm, parent, "Access to the specified parent project is forbidden"); |
| 339 | + |
| 340 | + final String trimmedProjectName = StringUtils.trimToNull(request.getProjectName()); |
| 341 | + if (request.isLatestProjectVersion()) { |
| 342 | + final Project oldLatest = qm.getLatestProjectVersion(trimmedProjectName, parentUuid); |
| 343 | + if(oldLatest != null) { |
| 344 | + requireAccess(qm, oldLatest, "Access to the previous latest project version is forbidden"); |
| 345 | + } |
| 346 | + } |
| 347 | + project = qm.createProject(trimmedProjectName, null, |
| 348 | + StringUtils.trimToNull(request.getProjectVersion()), request.getProjectTags(), parent, |
| 349 | + null, null, request.isLatestProjectVersion(), true); |
| 350 | + Principal principal = getPrincipal(); |
| 351 | + qm.updateNewProjectACL(project, principal); |
349 | 352 | } |
350 | 353 | return process(qm, project, request.getBom()); |
| 354 | + } catch (NullPointerException e) { |
| 355 | + return Response.status(Response.Status.NOT_FOUND).entity("The parent project could not be found.").build(); |
351 | 356 | } |
352 | 357 | } |
353 | 358 | } |
@@ -419,42 +424,47 @@ public Response uploadBom( |
419 | 424 | try (QueryManager qm = new QueryManager()) { |
420 | 425 | final String trimmedProjectName = StringUtils.trimToNull(projectName); |
421 | 426 | final String trimmedProjectVersion = StringUtils.trimToNull(projectVersion); |
422 | | - Project project = qm.getProject(trimmedProjectName, trimmedProjectVersion); |
| 427 | + Project parent = null; |
| 428 | + UUID uuid = null; |
| 429 | + if (parentUUID != null) { |
| 430 | + parent = qm.getProject(parentUUID); |
| 431 | + uuid = parent.getUuid(); |
| 432 | + } |
| 433 | + Project project = qm.getProject(trimmedProjectName, trimmedProjectVersion, uuid); |
423 | 434 | if (project == null && autoCreate) { |
424 | | - if (hasPermission(Permissions.Constants.PORTFOLIO_MANAGEMENT) || hasPermission(Permissions.Constants.PORTFOLIO_MANAGEMENT_CREATE) || hasPermission(Permissions.Constants.PROJECT_CREATION_UPLOAD)) { |
425 | | - Project parent = null; |
426 | | - if (parentUUID != null || parentName != null) { |
427 | | - if (parentUUID != null) { |
428 | | - |
429 | | - parent = qm.getObjectByUuid(Project.class, parentUUID); |
430 | | - } else { |
431 | | - final String trimmedParentName = StringUtils.trimToNull(parentName); |
432 | | - final String trimmedParentVersion = StringUtils.trimToNull(parentVersion); |
433 | | - parent = qm.getProject(trimmedParentName, trimmedParentVersion); |
434 | | - } |
435 | | - |
436 | | - if (parent == null) { // if parent project is specified but not found |
437 | | - return Response.status(Response.Status.NOT_FOUND).entity("The parent project could not be found.").build(); |
438 | | - } |
439 | | - requireAccess(qm, parent, "Access to the specified parent project is forbidden"); |
440 | | - } |
441 | | - if (isLatest) { |
442 | | - final Project oldLatest = qm.getLatestProjectVersion(trimmedProjectName); |
443 | | - if(oldLatest != null) { |
444 | | - requireAccess(qm, oldLatest, "Access to the previous latest project version is forbidden"); |
445 | | - } |
| 435 | + if (Stream.of(Permissions.Constants.PORTFOLIO_MANAGEMENT, |
| 436 | + Permissions.Constants.PORTFOLIO_MANAGEMENT_CREATE, |
| 437 | + Permissions.Constants.PROJECT_CREATION_UPLOAD).noneMatch(this::hasPermission)) { |
| 438 | + return Response.status(Response.Status.UNAUTHORIZED) |
| 439 | + .entity("The principal does not have permission to create project.") |
| 440 | + .build(); |
| 441 | + } |
| 442 | + |
| 443 | + if (parent == null) { |
| 444 | + final String trimmedParentName = StringUtils.trimToNull(parentName); |
| 445 | + final String trimmedParentVersion = StringUtils.trimToNull(parentVersion); |
| 446 | + parent = qm.getProject(trimmedParentName, trimmedParentVersion, uuid); |
| 447 | + } |
| 448 | + |
| 449 | + Objects.requireNonNull(parent); |
| 450 | + requireAccess(qm, parent, "Access to the specified parent project is forbidden"); |
| 451 | + |
| 452 | + if (isLatest) { |
| 453 | + final Project oldLatest = qm.getLatestProjectVersion(trimmedProjectName, uuid); |
| 454 | + if(oldLatest != null) { |
| 455 | + requireAccess(qm, oldLatest, "Access to the previous latest project version is forbidden"); |
446 | 456 | } |
447 | | - final List<org.dependencytrack.model.Tag> tags = (projectTags != null && !projectTags.isBlank()) |
448 | | - ? Arrays.stream(projectTags.split(",")).map(String::trim).filter(not(String::isEmpty)).map(org.dependencytrack.model.Tag::new).toList() |
449 | | - : null; |
450 | | - project = qm.createProject(trimmedProjectName, null, trimmedProjectVersion, tags, parent, null, null, isLatest, true); |
451 | | - Principal principal = getPrincipal(); |
452 | | - qm.updateNewProjectACL(project, principal); |
453 | | - } else { |
454 | | - return Response.status(Response.Status.UNAUTHORIZED).entity("The principal does not have permission to create project.").build(); |
455 | 457 | } |
| 458 | + final List<org.dependencytrack.model.Tag> tags = (projectTags != null && !projectTags.isBlank()) |
| 459 | + ? Arrays.stream(projectTags.split(",")).map(String::trim).filter(not(String::isEmpty)).map(org.dependencytrack.model.Tag::new).toList() |
| 460 | + : null; |
| 461 | + project = qm.createProject(trimmedProjectName, null, trimmedProjectVersion, tags, parent, null, null, isLatest, true); |
| 462 | + Principal principal = getPrincipal(); |
| 463 | + qm.updateNewProjectACL(project, principal); |
456 | 464 | } |
457 | 465 | return process(qm, project, artifactParts); |
| 466 | + } catch (NullPointerException e) { |
| 467 | + return Response.status(Response.Status.NOT_FOUND).entity("The parent project could not be found.").build(); |
458 | 468 | } |
459 | 469 | } |
460 | 470 | } |
|
0 commit comments