-
-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathComponentIdentity.java
More file actions
207 lines (179 loc) · 6.63 KB
/
ComponentIdentity.java
File metadata and controls
207 lines (179 loc) · 6.63 KB
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
/*
* 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 com.github.packageurl.MalformedPackageURLException;
import com.github.packageurl.PackageURL;
import org.cyclonedx.model.component.crypto.enums.AssetType;
import org.dependencytrack.util.PurlUtil;
import org.json.JSONObject;
import java.util.Objects;
import java.util.UUID;
/**
* A transient object that carries component identity information.
*
* @since 4.0.0
*/
public class ComponentIdentity {
public enum ObjectType {
COMPONENT,
SERVICE
}
private ObjectType objectType;
private PackageURL purl;
private PackageURL purlCoordinates;
private String cpe;
private String swidTagId;
private String group;
private String name;
private String version;
private UUID uuid;
private String oid;
private AssetType assetType;
public ComponentIdentity(final PackageURL purl, final String cpe, final String swidTagId,
final String group, final String name, final String version) {
this.purl = purl;
this.purlCoordinates = PurlUtil.silentPurlCoordinatesOnly(purl);
this.cpe = cpe;
this.swidTagId = swidTagId;
this.group = group;
this.name = name;
this.version = version;
this.objectType = ObjectType.COMPONENT;
}
public ComponentIdentity(final Component component) {
this.purl = component.getPurl();
this.purlCoordinates = PurlUtil.silentPurlCoordinatesOnly(purl);
this.cpe = component.getCpe();
this.swidTagId = component.getSwidTagId();
this.group = component.getGroup();
this.name = component.getName();
this.version = component.getVersion();
this.uuid = component.getUuid();
this.objectType = ObjectType.COMPONENT;
this.assetType = component.getCryptoAssetProperties() != null ? component.getCryptoAssetProperties().getAssetType() : null;
this.oid = component.getCryptoAssetProperties() != null ? component.getCryptoAssetProperties().getOid() : null;
}
public ComponentIdentity(final org.cyclonedx.model.Component component) {
try {
this.purl = new PackageURL(component.getPurl());
this.purlCoordinates = PurlUtil.purlCoordinatesOnly(purl);
} catch (MalformedPackageURLException e) {
// throw it away
}
this.cpe = component.getCpe();
this.swidTagId = (component.getSwid() != null) ? component.getSwid().getTagId() : null;
this.group = component.getGroup();
this.name = component.getName();
this.version = component.getVersion();
this.objectType = ObjectType.COMPONENT;
this.assetType = component.getCryptoProperties() != null ? component.getCryptoProperties().getAssetType() : null;
this.oid = component.getCryptoProperties() != null ? component.getCryptoProperties().getOid() : null;
}
public ComponentIdentity(final ServiceComponent service) {
this.group = service.getGroup();
this.name = service.getName();
this.version = service.getVersion();
this.uuid = service.getUuid();
this.objectType = ObjectType.SERVICE;
}
public ComponentIdentity(final org.cyclonedx.model.Service service) {
this.group = service.getGroup();
this.name = service.getName();
this.version = service.getVersion();
this.objectType = ObjectType.SERVICE;
}
// search crypto assets by asset type
public ComponentIdentity(AssetType assetType) {
this.objectType = ObjectType.COMPONENT;
this.assetType = assetType;
}
public ObjectType getObjectType() {
return objectType;
}
public PackageURL getPurl() {
return purl;
}
public PackageURL getPurlCoordinates() {
return purlCoordinates;
}
public String getCpe() {
return cpe;
}
public String getSwidTagId() {
return swidTagId;
}
public String getGroup() {
return group;
}
public String getName() {
return name;
}
public String getVersion() {
return version;
}
public UUID getUuid() {
return uuid;
}
public AssetType getAssetType() {
return assetType;
}
public void setAssetType(AssetType assetType) {
this.assetType = assetType;
}
public String getOid() {
return oid;
}
public void setOid(String oid) {
this.oid = oid;
}
@Override
public boolean equals(final Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final ComponentIdentity that = (ComponentIdentity) o;
return objectType == that.objectType &&
Objects.equals(purl, that.purl) &&
Objects.equals(purlCoordinates, that.purlCoordinates) &&
Objects.equals(cpe, that.cpe) &&
Objects.equals(swidTagId, that.swidTagId) &&
Objects.equals(group, that.group) &&
Objects.equals(name, that.name) &&
Objects.equals(version, that.version) &&
Objects.equals(uuid, that.uuid) &&
assetType == that.assetType &&
Objects.equals(oid, that.oid);
}
@Override
public int hashCode() {
return Objects.hash(objectType, purl, purlCoordinates, cpe, swidTagId, group, name, version, uuid, assetType, oid);
}
public JSONObject toJSON() {
final JSONObject jsonObject = new JSONObject();
jsonObject.put("uuid", this.getUuid());
jsonObject.put("group", this.getGroup());
jsonObject.put("name", this.getName());
jsonObject.put("version", this.getVersion());
jsonObject.put("purl", this.getPurl());
jsonObject.put("purlCoordinates", this.getPurlCoordinates());
jsonObject.put("cpe", this.getCpe());
jsonObject.put("swidTagId", this.getSwidTagId());
jsonObject.put("objectType", this.getObjectType());
return jsonObject;
}
}