forked from jfrog/ide-plugins-common
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJfrogCliDriver.java
More file actions
247 lines (213 loc) · 11 KB
/
JfrogCliDriver.java
File metadata and controls
247 lines (213 loc) · 11 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
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
package com.jfrog.ide.common.configuration;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.SystemUtils;
import org.jfrog.build.api.util.Log;
import org.jfrog.build.extractor.executor.CommandExecutor;
import org.jfrog.build.extractor.executor.CommandResults;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static com.jfrog.ide.common.utils.Utils.*;
/**
* @author Tal Arian
*/
public class JfrogCliDriver {
private static final String JFROG_CLI_RELEASES_URL = "https://releases.jfrog.io/artifactory";
private static final ObjectMapper jsonReader = createMapper();
private final Log log;
private final String path;
private final Map<String, String> env;
@Getter
private String jfrogExec = "jf";
@SuppressWarnings("unused")
public JfrogCliDriver(Map<String, String> env, Log log) {
this(env, "", log);
}
public JfrogCliDriver(Map<String, String> env, String path, Log log) {
if (SystemUtils.IS_OS_WINDOWS) {
this.jfrogExec += ".exe";
}
addDefaultEnvVars(env);
this.env = env;
this.path = path;
this.log = log;
}
@SuppressWarnings("unused")
public boolean isJfrogCliInstalled() {
return runVersion(null) != null;
}
@SuppressWarnings("unused")
public JfrogCliServerConfig getServerConfig() throws IOException {
return getServerConfig(Paths.get(".").toAbsolutePath().normalize().toFile(), Collections.emptyList(), env);
}
public JfrogCliServerConfig getServerConfig(File workingDirectory, List<String> extraArgs, Map<String, String> envVars) throws IOException {
List<String> args = new ArrayList<>();
args.add("config");
args.add("export");
args.addAll(extraArgs);
try {
CommandResults commandResults = runCommand(workingDirectory, envVars, args.toArray(new String[0]), Collections.emptyList(),null, log);
String res = commandResults.getRes();
if (StringUtils.isBlank(res) || !commandResults.isOk()) {
throw new IOException(commandResults.getErr());
}
// The output of the export command should be decoded before being parsed.
byte[] decodedBytes = Base64.getDecoder().decode(res.trim());
String decodedString = new String(decodedBytes);
return new JfrogCliServerConfig(jsonReader.readTree(decodedString));
} catch (IOException | InterruptedException e) {
throw new IOException("'jfrog config export' command failed. " +
"That might be happen if you haven't config any CLI server yet or using the config encryption feature.", e);
}
}
public String runVersion(File workingDirectory) {
String versionOutput = null;
try {
versionOutput = runCommand(workingDirectory, env, new String[]{"--version"}, Collections.emptyList()).getRes();
} catch (IOException | InterruptedException e) {
log.error("Failed to get CLI version. Reason: " + e.getMessage());
}
return versionOutput;
}
private CommandResults runCommand(File workingDirectory, Map<String, String> envVars, String[] args, List<String> extraArgs) throws IOException,
InterruptedException {
return runCommand(workingDirectory, envVars, args, extraArgs,null, null);
}
public CommandResults runCommand(File workingDirectory, Map<String, String> commandEnvVars, String[] args, List<String> extraArgs,List<String> credentials, Log logger)
throws IOException, InterruptedException {
List<String> finalArgs = Stream.concat(Arrays.stream(args), extraArgs.stream()).collect(Collectors.toList());
Map<String, String> combinedEnvVars = new HashMap<>();
Optional.ofNullable(env).ifPresent(combinedEnvVars::putAll);
Optional.ofNullable(commandEnvVars).ifPresent(combinedEnvVars::putAll);
CommandExecutor commandExecutor = new CommandExecutor(Paths.get(path, this.jfrogExec).toString(), combinedEnvVars);
CommandResults commandResults = commandExecutor.exeCommand(workingDirectory, finalArgs, credentials, logger);
if (!commandResults.isOk()) {
throw new IOException(commandResults.getErr() + commandResults.getRes());
}
return commandResults;
}
public void downloadCliIfNeeded(String destinationPath, String jfrogCliVersion) throws IOException {
// verify installed cli version
if (Files.exists(Paths.get(path, jfrogExec))){
String cliVersion = extractVersionFromCliOutput(runVersion(new File(path)));
log.debug("Local CLI version is: " + cliVersion);
if (jfrogCliVersion.equals(cliVersion)) {
log.info("Local Jfrog CLI version has been verified and is compatible. Proceeding with its usage.");
} else {
log.info(String.format("JFrog CLI version %s is installed, but the required version is %s. " +
"Initiating download of version %s to the destination: %s.", cliVersion, jfrogCliVersion, jfrogCliVersion, destinationPath));
downloadCliFromReleases(jfrogCliVersion, destinationPath);
}
} else {
log.info(String.format("JFrog CLI is not installed. Initiating download of version %s to the destination: %s.", jfrogCliVersion, destinationPath));
downloadCliFromReleases(jfrogCliVersion, destinationPath);
}
}
public void downloadCliFromReleases(String cliVersion, String destinationFolder) throws IOException {
String[] urlParts = {"jfrog-cli/v2-jf", cliVersion, "jfrog-cli-" + getOSAndArc(), jfrogExec};
String fileLocationInReleases = String.join("/", urlParts);
Path basePath = Paths.get(destinationFolder);
String destinationPath = basePath.resolve(jfrogExec).toString();
String finalUrl = JFROG_CLI_RELEASES_URL + "/" + fileLocationInReleases;
// download executable from releases and save it in 'destinationPath'
try (InputStream in = new URL(finalUrl).openStream()){
Files.copy(in, basePath.resolve(jfrogExec), StandardCopyOption.REPLACE_EXISTING);
// setting the file as executable
File cliExecutable = new File(String.valueOf(basePath.resolve(jfrogExec)));
if (!cliExecutable.setExecutable(true)) {
log.error(String.format("Failed to set downloaded CLI as executable. Path: %s", destinationPath));
} else {
log.debug(String.format("Downloaded CLI to %s. Permission te execute: %s", destinationPath, cliExecutable.canExecute()));
}
} catch (IOException e) {
throw new IOException(String.format("Failed to download CLI from %s. Reason: %s", fileLocationInReleases, e.getMessage()), e.getCause());
}
}
public CommandResults addCliServerConfig(String xrayUrl, String artifactoryUrl, String cliServerId, String user, String password, String accessToken, File workingDirectory, Map<String, String> envVars) throws Exception {
List<String> args = new ArrayList<>();
List<String> credentials = new ArrayList<>();
args.add("config");
args.add("add");
args.add(cliServerId);
args.add("--interactive=false");
args.add("--overwrite");
args.add("--enc-password=false");
if (accessToken != null && !accessToken.isEmpty()) {
credentials.add("--access-token=" + accessToken);
} else {
args.add("--user=" + user);
credentials.add("--password=" + password);
}
args.add("--xray-url=" + xrayUrl);
args.add("--artifactory-url=" + artifactoryUrl);
try {
return runCommand(workingDirectory, envVars, args.toArray(new String[0]), Collections.emptyList(), credentials ,log);
} catch (IOException | InterruptedException e) {
throw new Exception("Failed to configure JFrog CLI server. Reason: " + e.getMessage(), e);
}
}
public CommandResults runCliAudit(File workingDirectory, List<String> scannedDirectories, String serverId, List<String> extraArgs, Map<String, String> envVars) throws Exception {
AuditConfig config = new AuditConfig.Builder()
.scannedDirectories(scannedDirectories)
.serverId(serverId)
.extraArgs(extraArgs)
.envVars(envVars)
.build();
return runCliAudit(workingDirectory, config);
}
public CommandResults runCliAudit(File workingDirectory, AuditConfig config) throws Exception {
List<String> args = new ArrayList<>();
args.add("audit");
if (config.getScannedDirectories() != null && !config.getScannedDirectories().isEmpty()) {
String workingDirsString = config.getScannedDirectories().size() > 1 ?
String.join(", ", config.getScannedDirectories()) :
config.getScannedDirectories().get(0);
args.add("--working-dirs=" + quoteArgumentForUnix(workingDirsString));
}
args.add("--server-id=" + config.getServerId());
args.add("--format=sarif");
if (config.getExcludedPattern() != null && !config.getExcludedPattern().isEmpty()) {
String excludedPatterns = String.join(",", config.getExcludedPattern());
args.add("--exclusions=" + quoteArgumentForUnix(excludedPatterns));
}
try {
return runCommand(workingDirectory, config.getEnvVars(), args.toArray(new String[0]),
config.getExtraArgs() != null ? config.getExtraArgs() : Collections.emptyList(), null, log);
} catch (IOException | InterruptedException e) {
throw new Exception("Failed to run JF audit. Reason: " + e.getMessage(), e);
}
}
private String extractVersionFromCliOutput(String input) {
if (input != null) {
// define a pattern for the version format 'x.x.x'
String regex = "\\b\\d+\\.\\d+\\.\\d+\\b";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
if (matcher.find()) {
return matcher.group();
}
}
return null;
}
private void addDefaultEnvVars(Map<String, String> env) {
if (env != null) {
env.put("JFROG_CLI_AVOID_NEW_VERSION_WARNING", "true");
}
}
private String quoteArgumentForUnix(String commaSeparatedValues) {
// macOS/Linux: add quotes around the comma-separated values
return SystemUtils.IS_OS_WINDOWS ? commaSeparatedValues : "\"" + commaSeparatedValues + "\"";
}
}