-
-
Notifications
You must be signed in to change notification settings - Fork 595
/
Copy pathProject.java
616 lines (512 loc) · 19 KB
/
Project.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
/*
* This file is part of Dependency-Track.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* Copyright (c) OWASP Foundation. All Rights Reserved.
*/
package org.dependencytrack.model;
import alpine.common.validation.RegexSequence;
import alpine.model.Team;
import alpine.server.json.TrimmedStringDeserializer;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonIncludeProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSetter;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.github.packageurl.MalformedPackageURLException;
import com.github.packageurl.PackageURL;
import io.swagger.v3.oas.annotations.media.Schema;
import org.dependencytrack.parser.cyclonedx.util.ModelConverter;
import org.dependencytrack.persistence.converter.OrganizationalContactsJsonConverter;
import org.dependencytrack.persistence.converter.OrganizationalEntityJsonConverter;
import org.dependencytrack.resources.v1.serializers.CustomPackageURLSerializer;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;
import jakarta.validation.constraints.Size;
import javax.jdo.annotations.Column;
import javax.jdo.annotations.Convert;
import javax.jdo.annotations.Element;
import javax.jdo.annotations.Extension;
import javax.jdo.annotations.FetchGroup;
import javax.jdo.annotations.FetchGroups;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.Index;
import javax.jdo.annotations.Join;
import javax.jdo.annotations.Order;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import javax.jdo.annotations.Serialized;
import javax.jdo.annotations.Unique;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.UUID;
/**
* Model for tracking individual projects. Projects are high-level containers
* of components and (optionally) other projects. Project are often software
* applications, firmware, operating systems, or devices.
*
* @author Steve Springett
* @since 3.0.0
*/
@PersistenceCapable
@FetchGroups({
@FetchGroup(name = "ALL", members = {
@Persistent(name = "name"),
@Persistent(name = "authors"),
@Persistent(name = "publisher"),
@Persistent(name = "supplier"),
@Persistent(name = "group"),
@Persistent(name = "name"),
@Persistent(name = "description"),
@Persistent(name = "version"),
@Persistent(name = "classifier"),
@Persistent(name = "cpe"),
@Persistent(name = "purl"),
@Persistent(name = "swidTagId"),
@Persistent(name = "uuid"),
@Persistent(name = "parent"),
@Persistent(name = "children"),
@Persistent(name = "properties"),
@Persistent(name = "tags"),
@Persistent(name = "accessTeams"),
@Persistent(name = "metadata"),
@Persistent(name = "isLatest")
}),
@FetchGroup(name = "METADATA", members = {
@Persistent(name = "metadata")
}),
@FetchGroup(name = "METRICS_UPDATE", members = {
@Persistent(name = "id"),
@Persistent(name = "lastInheritedRiskScore"),
@Persistent(name = "uuid")
}),
@FetchGroup(name = "PARENT", members = {
@Persistent(name = "parent")
})
})
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Project implements Serializable {
private static final long serialVersionUID = -7592438796591673355L;
/**
* Defines JDO fetch groups for this class.
*/
public enum FetchGroup {
ALL,
METADATA,
METRICS_UPDATE,
PARENT
}
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.NATIVE)
@JsonIgnore
private long id;
@Persistent(defaultFetchGroup = "true")
@Convert(OrganizationalContactsJsonConverter.class)
@Column(name = "AUTHORS", jdbcType = "CLOB", allowsNull = "true")
private List<OrganizationalContact> authors;
@Persistent
@Column(name = "PUBLISHER", jdbcType = "VARCHAR")
@Size(max = 255)
@JsonDeserialize(using = TrimmedStringDeserializer.class)
@Pattern(regexp = RegexSequence.Definition.PRINTABLE_CHARS, message = "The publisher may only contain printable characters")
private String publisher;
@Persistent(defaultFetchGroup = "true")
@Convert(OrganizationalEntityJsonConverter.class)
@Column(name = "MANUFACTURER", jdbcType = "CLOB", allowsNull = "true")
private OrganizationalEntity manufacturer;
@Persistent(defaultFetchGroup = "true")
@Convert(OrganizationalEntityJsonConverter.class)
@Column(name = "SUPPLIER", jdbcType = "CLOB", allowsNull = "true")
private OrganizationalEntity supplier;
@Persistent
@Column(name = "GROUP", jdbcType = "VARCHAR")
@Index(name = "PROJECT_GROUP_IDX")
@Size(max = 255)
@JsonDeserialize(using = TrimmedStringDeserializer.class)
@Pattern(regexp = RegexSequence.Definition.PRINTABLE_CHARS, message = "The group may only contain printable characters")
private String group;
@Persistent
@Index(name = "PROJECT_NAME_IDX")
@Column(name = "NAME", jdbcType = "VARCHAR", allowsNull = "false")
@NotBlank
@Size(min = 1, max = 255)
@JsonDeserialize(using = TrimmedStringDeserializer.class)
@Pattern(regexp = RegexSequence.Definition.PRINTABLE_CHARS, message = "The name may only contain printable characters")
private String name;
@Persistent
@Column(name = "DESCRIPTION", jdbcType = "VARCHAR")
@JsonDeserialize(using = TrimmedStringDeserializer.class)
@Pattern(regexp = RegexSequence.Definition.PRINTABLE_CHARS, message = "The description may only contain printable characters")
private String description;
@Persistent
@Index(name = "PROJECT_VERSION_IDX")
@Column(name = "VERSION", jdbcType = "VARCHAR")
@JsonDeserialize(using = TrimmedStringDeserializer.class)
@Pattern(regexp = RegexSequence.Definition.PRINTABLE_CHARS, message = "The version may only contain printable characters")
private String version;
@Persistent
@Column(name = "CLASSIFIER", jdbcType = "VARCHAR")
@Index(name = "PROJECT_CLASSIFIER_IDX")
@Extension(vendorName = "datanucleus", key = "enum-check-constraint", value = "true")
private Classifier classifier;
@Persistent
@Index(name = "PROJECT_CPE_IDX")
@Size(max = 255)
@JsonDeserialize(using = TrimmedStringDeserializer.class)
//Patterns obtained from https://csrc.nist.gov/schema/cpe/2.3/cpe-naming_2.3.xsd
@Pattern(regexp = "(cpe:2\\.3:[aho\\*\\-](:(((\\?*|\\*?)([a-zA-Z0-9\\-\\._]|(\\\\[\\\\\\*\\?!\"#$$%&'\\(\\)\\+,/:;<=>@\\[\\]\\^`\\{\\|}~]))+(\\?*|\\*?))|[\\*\\-])){5}(:(([a-zA-Z]{2,3}(-([a-zA-Z]{2}|[0-9]{3}))?)|[\\*\\-]))(:(((\\?*|\\*?)([a-zA-Z0-9\\-\\._]|(\\\\[\\\\\\*\\?!\"#$$%&'\\(\\)\\+,/:;<=>@\\[\\]\\^`\\{\\|}~]))+(\\?*|\\*?))|[\\*\\-])){4})|([c][pP][eE]:/[AHOaho]?(:[A-Za-z0-9\\._\\-~%]*){0,6})", message = "The CPE must conform to the CPE v2.2 or v2.3 specification defined by NIST")
private String cpe;
@Persistent
@Index(name = "PROJECT_PURL_IDX")
@Column(name = "PURL", length = 786)
@Size(max = 786)
@com.github.packageurl.validator.PackageURL
@JsonDeserialize(using = TrimmedStringDeserializer.class)
@Schema(type = "string")
private String purl;
@Persistent
@Index(name = "PROJECT_SWID_TAGID_IDX")
@Size(max = 255)
@JsonDeserialize(using = TrimmedStringDeserializer.class)
@Pattern(regexp = RegexSequence.Definition.PRINTABLE_CHARS, message = "The SWID tagId may only contain printable characters")
private String swidTagId;
@Persistent(defaultFetchGroup = "true")
@Column(name = "DIRECT_DEPENDENCIES", jdbcType = "CLOB")
@JsonDeserialize(using = TrimmedStringDeserializer.class)
private String directDependencies; // This will be a JSON string
@Persistent(customValueStrategy = "uuid")
@Unique(name = "PROJECT_UUID_IDX")
@Column(name = "UUID", jdbcType = "VARCHAR", length = 36, allowsNull = "false")
@NotNull
private UUID uuid;
@Persistent
@Column(name = "PARENT_PROJECT_ID")
@JsonIncludeProperties(value = {"name", "version", "uuid"})
private Project parent;
@Persistent(mappedBy = "parent")
private Collection<Project> children;
@Persistent(mappedBy = "project", defaultFetchGroup = "true")
@Order(extensions = @Extension(vendorName = "datanucleus", key = "list-ordering", value = "groupName ASC, propertyName ASC"))
private List<ProjectProperty> properties;
@Persistent(table = "PROJECTS_TAGS", defaultFetchGroup = "true", mappedBy = "projects")
@Join(column = "PROJECT_ID")
@Element(column = "TAG_ID")
@Order(extensions = @Extension(vendorName = "datanucleus", key = "list-ordering", value = "name ASC"))
private List<Tag> tags;
/**
* Convenience field which will contain the date of the last entry in the {@link Bom} table
*/
@Persistent
@Index(name = "PROJECT_LASTBOMIMPORT_IDX")
@Column(name = "LAST_BOM_IMPORTED")
@Schema(type = "integer", format = "int64", requiredMode = Schema.RequiredMode.REQUIRED, description = "UNIX epoch timestamp in milliseconds")
private Date lastBomImport;
/**
* Convenience field which will contain the format of the last entry in the {@link Bom} table
*/
@Persistent
@Index(name = "PROJECT_LASTBOMIMPORT_FORMAT_IDX")
@Column(name = "LAST_BOM_IMPORTED_FORMAT")
private String lastBomImportFormat;
/**
* Convenience field which stores the Inherited Risk Score (IRS) of the last metric in the {@link ProjectMetrics} table
*/
@Persistent
@Index(name = "PROJECT_LAST_RISKSCORE_IDX")
@Column(name = "LAST_RISKSCORE", allowsNull = "true") // New column, must allow nulls on existing databases))
private Double lastInheritedRiskScore;
@Persistent
@Column(name = "ACTIVE", defaultValue = "true")
@JsonSerialize(nullsUsing = BooleanDefaultTrueSerializer.class)
private boolean active = true;
@Persistent
@Index(name = "PROJECT_IS_LATEST_IDX")
@Column(name = "IS_LATEST", defaultValue = "false")
private boolean isLatest = false; // Added in v4.12.
@Persistent(table = "PROJECT_ACCESS_TEAMS", defaultFetchGroup = "true")
@Join(column = "PROJECT_ID")
@Element(column = "TEAM_ID")
@Order(extensions = @Extension(vendorName = "datanucleus", key = "list-ordering", value = "name ASC"))
private List<Team> accessTeams;
@Persistent(defaultFetchGroup = "true")
@Column(name = "EXTERNAL_REFERENCES")
@Serialized
private List<ExternalReference> externalReferences;
@Persistent(mappedBy = "project")
@Schema(accessMode = Schema.AccessMode.READ_ONLY)
private ProjectMetadata metadata;
private transient String bomRef;
private transient ProjectMetrics metrics;
private transient List<ProjectVersion> versions;
private transient List<Component> dependencyGraph;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public List<OrganizationalContact> getAuthors() {
return authors;
}
public void setAuthors(List<OrganizationalContact> authors) {
this.authors = authors;
}
@Deprecated
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public String getAuthor(){
return ModelConverter.convertContactsToString(this.authors);
}
@Deprecated
public void setAuthor(String author){
if(this.authors==null){
this.authors = new ArrayList<>();
} else{
this.authors.clear();
}
this.authors.add(new OrganizationalContact() {{
setName(author);
}});
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public OrganizationalEntity getManufacturer() {
return manufacturer;
}
public void setManufacturer(final OrganizationalEntity manufacturer) {
this.manufacturer = manufacturer;
}
public OrganizationalEntity getSupplier() {
return supplier;
}
public void setSupplier(OrganizationalEntity supplier) {
this.supplier = supplier;
}
public String getGroup() {
return group;
}
public void setGroup(String group) {
this.group = group;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public Classifier getClassifier() {
return classifier;
}
public void setClassifier(Classifier classifier) {
this.classifier = classifier;
}
public String getCpe() {
return cpe;
}
public void setCpe(String cpe) {
this.cpe = cpe;
}
@JsonSerialize(using = CustomPackageURLSerializer.class)
public PackageURL getPurl() {
try {
return new PackageURL(purl);
} catch (MalformedPackageURLException e) {
return null;
}
}
public void setPurl(PackageURL purl) {
if (purl != null) {
this.purl = purl.canonicalize();
} else {
this.purl = null;
}
}
public void setPurl(String purl) {
this.purl = purl;
}
public String getSwidTagId() {
return swidTagId;
}
public void setSwidTagId(String swidTagId) {
this.swidTagId = swidTagId;
}
public String getDirectDependencies() {
return directDependencies;
}
public void setDirectDependencies(String directDependencies) {
this.directDependencies = directDependencies;
}
public UUID getUuid() {
return uuid;
}
public void setUuid(UUID uuid) {
this.uuid = uuid;
}
public Project getParent() {
return parent;
}
public void setParent(Project parent) {
this.parent = parent;
}
public Collection<Project> getChildren() {
return children;
}
public void setChildren(Collection<Project> children) {
this.children = children;
}
public List<ProjectProperty> getProperties() {
return properties;
}
public void setProperties(List<ProjectProperty> properties) {
this.properties = properties;
}
public List<Tag> getTags() {
return tags;
}
public void setTags(List<Tag> tags) {
this.tags = tags;
}
public Date getLastBomImport() {
return lastBomImport;
}
public void setLastBomImport(Date lastBomImport) {
this.lastBomImport = lastBomImport;
}
public String getLastBomImportFormat() {
return lastBomImportFormat;
}
public void setLastBomImportFormat(String lastBomImportFormat) {
this.lastBomImportFormat = lastBomImportFormat;
}
public Double getLastInheritedRiskScore() {
return lastInheritedRiskScore;
}
public void setLastInheritedRiskScore(Double lastInheritedRiskScore) {
this.lastInheritedRiskScore = lastInheritedRiskScore;
}
public List<ExternalReference> getExternalReferences() {
return externalReferences;
}
public void setExternalReferences(List<ExternalReference> externalReferences) {
this.externalReferences = externalReferences;
}
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}
@JsonProperty("isLatest")
public boolean isLatest() {
return isLatest;
}
public void setIsLatest(Boolean latest) {
isLatest = latest != null ? latest : false;
}
public String getBomRef() {
return bomRef;
}
public void setBomRef(String bomRef) {
this.bomRef = bomRef;
}
public ProjectMetrics getMetrics() {
return metrics;
}
public void setMetrics(ProjectMetrics metrics) {
this.metrics = metrics;
}
public List<ProjectVersion> getVersions() {
return versions;
}
public void setVersions(List<ProjectVersion> versions) {
this.versions = versions;
}
@JsonIgnore
public List<Team> getAccessTeams() {
return accessTeams;
}
@JsonSetter
public void setAccessTeams(List<Team> accessTeams) {
this.accessTeams = accessTeams;
}
public void addAccessTeam(Team accessTeam) {
if (this.accessTeams == null) {
this.accessTeams = new ArrayList<>();
}
this.accessTeams.add(accessTeam);
}
public ProjectMetadata getMetadata() {
return metadata;
}
public void setMetadata(final ProjectMetadata metadata) {
this.metadata = metadata;
}
@JsonIgnore
public List<Component> getDependencyGraph() {
return dependencyGraph;
}
@JsonIgnore
public void setDependencyGraph(List<Component> dependencyGraph) {
this.dependencyGraph = dependencyGraph;
}
@Override
public String toString() {
if (getPurl() != null) {
return getPurl().canonicalize();
} else {
StringBuilder sb = new StringBuilder();
if (getGroup() != null) {
sb.append(getGroup()).append(" : ");
}
sb.append(getName());
if (getVersion() != null) {
sb.append(" : ").append(getVersion());
}
return sb.toString();
}
}
private final static class BooleanDefaultTrueSerializer extends JsonSerializer<Boolean> {
@Override
public void serialize(Boolean value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeBoolean(value != null ? value : true);
}
}
}