Skip to content

Commit

Permalink
Use stdin if available
Browse files Browse the repository at this point in the history
  • Loading branch information
EyalDelarea committed Sep 8, 2024
1 parent 8377109 commit f57a192
Showing 1 changed file with 41 additions and 3 deletions.
44 changes: 41 additions & 3 deletions src/main/java/io/jenkins/plugins/jfrog/JfStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.kohsuke.stapler.DataBoundConstructor;

import javax.annotation.Nonnull;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
Expand All @@ -47,6 +48,7 @@
public class JfStep extends Builder implements SimpleBuildStep {
private final ObjectMapper mapper = createMapper();
static final String STEP_NAME = "jf";
private static final String MIN_CLI_VERSION_PASSWORD_STDIN ="2.31.0";
protected String[] args;

@DataBoundConstructor
Expand Down Expand Up @@ -197,7 +199,7 @@ private void configAllServers(Launcher.ProcStarter launcher, String jfrogBinaryP
for (JFrogPlatformInstance jfrogPlatformInstance : jfrogInstances) {
// Build 'jf' command
ArgumentListBuilder builder = new ArgumentListBuilder();
addConfigArguments(builder, jfrogPlatformInstance, jfrogBinaryPath, job);
addConfigArguments(builder, jfrogPlatformInstance, jfrogBinaryPath, job, launcher);
if (isWindows) {
builder = builder.toWindowsCommand();
}
Expand All @@ -210,7 +212,7 @@ private void configAllServers(Launcher.ProcStarter launcher, String jfrogBinaryP
}
}

private void addConfigArguments(ArgumentListBuilder builder, JFrogPlatformInstance jfrogPlatformInstance, String jfrogBinaryPath, Job<?, ?> job) {
private void addConfigArguments(ArgumentListBuilder builder, JFrogPlatformInstance jfrogPlatformInstance, String jfrogBinaryPath, Job<?, ?> job, Launcher.ProcStarter launcher) throws IOException, InterruptedException {
String credentialsId = jfrogPlatformInstance.getCredentialsConfig().getCredentialsId();
builder.add(jfrogBinaryPath).add("c").add("add").add(jfrogPlatformInstance.getId());
// Add credentials
Expand All @@ -220,7 +222,15 @@ private void addConfigArguments(ArgumentListBuilder builder, JFrogPlatformInstan
} else {
Credentials credentials = PluginsUtils.credentialsLookup(credentialsId, job);
builder.add("--user=" + credentials.getUsername());
builder.addMasked("--password=" + credentials.getPassword());
String cliVersion = getJfrogCliVersion(launcher, launcher.pwd());
// Use password-stdin if available
if (isCliVersionGreaterThan(cliVersion, MIN_CLI_VERSION_PASSWORD_STDIN)) {
builder.add("--password-stdin=");
ByteArrayInputStream inputStream = new ByteArrayInputStream(credentials.getPassword().getPlainText().getBytes());
launcher.stdin(inputStream);
} else {
builder.addMasked("--password=" + credentials.getPassword());
}
}
// Add URLs
builder.add("--url=" + jfrogPlatformInstance.getUrl());
Expand Down Expand Up @@ -294,4 +304,32 @@ public boolean isApplicable(Class<? extends AbstractProject> jobType) {
return true;
}
}

private String getJfrogCliVersion(Launcher.ProcStarter launcher, FilePath workspace) throws IOException, InterruptedException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
int exitCode = launcher.cmds("jf", "--version")
.pwd(workspace)
.stdout(outputStream)
.join();
if (exitCode != 0) {
throw new IOException("Failed to get JFrog CLI version");
}
String versionOutput = outputStream.toString(StandardCharsets.UTF_8).trim();
return versionOutput.split(" ")[2]; // Assuming the version is the third word in the output
}

private boolean isCliVersionGreaterThan(String currentVersion, String targetVersion) {
String[] currentParts = currentVersion.split("\\.");
String[] targetParts = targetVersion.split("\\.");
for (int i = 0; i < Math.min(currentParts.length, targetParts.length); i++) {
int currentPart = Integer.parseInt(currentParts[i]);
int targetPart = Integer.parseInt(targetParts[i]);
if (currentPart > targetPart) {
return true;
} else if (currentPart < targetPart) {
return false;
}
}
return currentParts.length > targetParts.length;
}
}

0 comments on commit f57a192

Please sign in to comment.