Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor and merge test classes for Command class #441

Merged
merged 1 commit into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class Command {
"^[ ]*[a-z0-9]([-a-z0-9 ]*[a-z0-9 ])?(\\.[a-z0-9 ]([-a-z0-9 ]*[a-z0-9 ])?)*[ ]*$");
private static final Logger LOGGER = LoggerFactory.getLogger(Command.class);

private static ProcessExecutor getProcessExecutor() {
static ProcessExecutor getProcessExecutor() { // Changed to package-private
ProcessExecutor processExecutor = new ProcessExecutor();
processExecutor.redirectError(System.err);
processExecutor.readOutput(true);
Expand All @@ -40,45 +40,52 @@ public void afterStart(Process process, ProcessExecutor executor) {
public static ProcessResult executeAndGetResponseAsJson(
HelmConfiguration helmConfiguration, String command)
throws InvalidExitValueException, IOException, InterruptedException, TimeoutException {
validateCommand(command);
return getProcessExecutor()
.environment(getEnv(helmConfiguration))
.commandSplit(addConfigToCommand(command, helmConfiguration) + " --output json")
.commandSplit(buildSecureCommand(command, helmConfiguration) + " --output json")
.execute();
}

public static ProcessResult executeAndGetResponseAsJson(String command)
throws InvalidExitValueException, IOException, InterruptedException, TimeoutException {
validateCommand(command);
return executeAndGetResponseAsJson(null, command);
}

public static ProcessResult executeAndGetResponseAsRaw(
HelmConfiguration helmConfiguration, String command)
throws InvalidExitValueException, IOException, InterruptedException, TimeoutException {
validateCommand(command);
return getProcessExecutor()
.environment(getEnv(helmConfiguration))
.commandSplit(addConfigToCommand(command, helmConfiguration))
.commandSplit(buildSecureCommand(command, helmConfiguration))
.execute();
}

public static ProcessResult executeAndGetResponseAsRaw(String command)
throws InvalidExitValueException, IOException, InterruptedException, TimeoutException {
validateCommand(command);
return executeAndGetResponseAsRaw(null, command);
}

public static ProcessResult execute(HelmConfiguration helmConfiguration, String command)
throws InvalidExitValueException, IOException, InterruptedException, TimeoutException {
validateCommand(command);
return getProcessExecutor()
.environment(getEnv(helmConfiguration))
.commandSplit(addConfigToCommand(command, helmConfiguration))
.commandSplit(buildSecureCommand(command, helmConfiguration))
.execute();
}

public static ProcessResult execute(String command)
throws InvalidExitValueException, IOException, InterruptedException, TimeoutException {
validateCommand(command);
return execute(null, command);
}

private static Map<String, String> getEnv(HelmConfiguration helmConfiguration) {
static Map<String, String> getEnv(
HelmConfiguration helmConfiguration) { // Changed to package-private
Map<String, String> env = new HashMap<>();
if (System.getProperty("http.proxyHost") != null) {
env.put(
Expand All @@ -105,45 +112,50 @@ private static Map<String, String> getEnv(HelmConfiguration helmConfiguration) {
return env;
}

private static String addConfigToCommand(String command, HelmConfiguration helmConfiguration) {
if (helmConfiguration == null) {
return command;
}
String newCommand = command;
newCommand = newCommand.concat(" ");
if (helmConfiguration.getAsKubeUser() != null) {
newCommand =
newCommand.concat(" --kube-as-user " + helmConfiguration.getAsKubeUser() + " ");
}
String kubeConfig = null;
if (StringUtils.isNotEmpty(helmConfiguration.getApiserverUrl())) {
newCommand =
newCommand
.concat(" --kube-apiserver=" + helmConfiguration.getApiserverUrl())
.concat(" ");
// Kubeconfig should be set to /dev/null to prevent mixing user provided configuration
// with pre-existing local kubeconfig (most likely re-using a cluster certificate from
// another cluster)
kubeConfig = "/dev/null";
}

if (StringUtils.isNotEmpty(helmConfiguration.getKubeToken())) {
newCommand =
newCommand
.concat(" --kube-token=" + helmConfiguration.getKubeToken())
.concat(" ");
kubeConfig = "/dev/null";
}

if (StringUtils.isNotEmpty(helmConfiguration.getKubeConfig())) {
kubeConfig = helmConfiguration.getKubeConfig();
static String buildSecureCommand(
String command, HelmConfiguration helmConfiguration) { // Changed to package-private
StringBuilder newCommand = new StringBuilder(command).append(" ");
if (helmConfiguration != null) {
if (helmConfiguration.getAsKubeUser() != null) {
newCommand
.append(" --kube-as-user ")
.append(escapeArgument(helmConfiguration.getAsKubeUser()))
.append(" ");
}
String kubeConfig = null;
if (StringUtils.isNotEmpty(helmConfiguration.getApiserverUrl())) {
newCommand
.append(" --kube-apiserver=")
.append(escapeArgument(helmConfiguration.getApiserverUrl()))
.append(" ");
kubeConfig = "/dev/null";
}
if (StringUtils.isNotEmpty(helmConfiguration.getKubeToken())) {
newCommand
.append(" --kube-token=")
.append(escapeArgument(helmConfiguration.getKubeToken()))
.append(" ");
kubeConfig = "/dev/null";
}
if (StringUtils.isNotEmpty(helmConfiguration.getKubeConfig())) {
kubeConfig = helmConfiguration.getKubeConfig();
}
if (kubeConfig != null) {
newCommand.append(" --kubeconfig=").append(escapeArgument(kubeConfig)).append(" ");
}
}
return newCommand.toString();
}

if (kubeConfig != null) {
newCommand = newCommand.concat(" --kubeconfig=" + kubeConfig).concat(" ");
static void validateCommand(String command)
throws IllegalArgumentException { // Changed to package-private
if (!safeToConcatenate.matcher(command).matches()) {
throw new IllegalArgumentException("Illegal characters in command");
}
}

return newCommand;
static String escapeArgument(String argument) { // Changed to package-private
return argument.replaceAll("([\"\\\\])", "\\\\$1");
}

public static void safeConcat(StringBuilder currentCommand, String toConcat)
Expand Down

This file was deleted.

Loading
Loading