forked from guacsec/trustify-da-java-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYarnBerryProcessor.java
More file actions
143 lines (129 loc) · 4.62 KB
/
Copy pathYarnBerryProcessor.java
File metadata and controls
143 lines (129 loc) · 4.62 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
/*
* Copyright © 2023 Red Hat, Inc.
*
* 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.
*/
package com.redhat.exhort.providers;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.github.packageurl.PackageURL;
import com.redhat.exhort.providers.javascript.model.Manifest;
import com.redhat.exhort.sbom.Sbom;
import com.redhat.exhort.tools.Operations;
import java.nio.file.Path;
import java.util.Map;
import java.util.TreeMap;
import java.util.regex.Pattern;
/** Concrete implementation of the Yarn Berry processor, supporting Yarn 2.x and later. */
public final class YarnBerryProcessor extends YarnProcessor {
private static final Pattern LOCATOR_PATTERN = Pattern.compile("^(@?[^@]+(?:/[^@]+)?)@npm:(.+)$");
private static final Pattern VIRTUAL_LOCATOR_PATTERN =
Pattern.compile("^(@?[^@]+(?:/[^@]+)?)@virtual:[^#]+#npm:(.+)$");
public YarnBerryProcessor(String packageManager, Manifest manifest) {
super(packageManager, manifest);
}
@Override
public String[] installCmd(Path manifestDir) {
if (manifestDir != null) {
return new String[] {
packageManager, "--cwd", manifestDir.toString(), "install", "--immutable"
};
}
return new String[] {packageManager, "install", "--immutable"};
}
@Override
public String[] listDepsCmd(boolean includeTransitive, Path manifestDir) {
if (manifestDir != null) {
return new String[] {
packageManager,
"--cwd",
manifestDir.toString(),
"info",
includeTransitive ? "--recursive" : "--all",
"--json",
};
}
return new String[] {
packageManager, "info", includeTransitive ? "--recursive" : "--all", "--json",
};
}
@Override
protected Map<String, PackageURL> getRootDependencies(JsonNode depTree) {
Map<String, PackageURL> rootDeps = new TreeMap<>();
var nodes = (ArrayNode) depTree;
if (nodes == null || nodes.isEmpty()) {
return rootDeps;
}
for (JsonNode node : nodes) {
var depName = node.get("value").asText();
if (!isRoot(depName)) {
var versionIdx = depName.lastIndexOf("@");
var name = depName.substring(0, versionIdx);
var version = node.get("children").get("Version").asText();
rootDeps.put(name, JavaScriptProvider.toPurl(name, version));
}
}
return rootDeps;
}
private boolean isRoot(String name) {
return name.endsWith("@workspace:.");
}
@Override
public String parseDepTreeOutput(String output) {
return "["
+ output.trim().replaceAll(Operations.GENERIC_LINE_SEPARATOR, "").replace("}{", "},{")
+ "]";
}
private PackageURL purlFromNode(String normalizedLocator, JsonNode node) {
var name = normalizedLocator.substring(0, normalizedLocator.lastIndexOf("@"));
var version = node.get("children").get("Version").asText();
return JavaScriptProvider.toPurl(name, version);
}
@Override
void addDependenciesToSbom(Sbom sbom, JsonNode depTree) {
if (depTree == null) {
return;
}
depTree.forEach(
n -> {
var depName = n.get("value").asText();
var from = isRoot(depName) ? sbom.getRoot() : purlFromNode(depName, n);
var deps = (ArrayNode) n.get("children").get("Dependencies");
if (deps != null && !deps.isEmpty()) {
deps.forEach(
d -> {
var target = purlFromlocator(d.get("locator").asText());
if (target != null) {
sbom.addDependency(from, target, null);
}
});
}
});
}
private PackageURL purlFromlocator(String locator) {
if (locator == null) return null;
var matcher = LOCATOR_PATTERN.matcher(locator);
if (matcher.matches()) {
var name = matcher.group(1);
var version = matcher.group(2);
return JavaScriptProvider.toPurl(name, version);
}
matcher = VIRTUAL_LOCATOR_PATTERN.matcher(locator);
if (matcher.matches()) {
var name = matcher.group(1);
var version = matcher.group(2);
return JavaScriptProvider.toPurl(name, version);
}
return null;
}
}