Skip to content
Open
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 @@ -36,6 +36,7 @@ public class TerraformClient implements AutoCloseable {
private static final String TERRAFORM_PLAN_REFRESH_FALSE="-refresh=false";
private static final String TERRAFORM_PLAN_REFRESH_ONLY="-refresh-only";
private static final String TF_STATE_PULL="pull";
private static final String GIT_SSH_COMMAND = "GIT_SSH_COMMAND";
// Deliberately separate from ProcessLauncher.GRACE_PERIOD_SECONDS: that one
// answers "how long may terraform take to shut down after a job timeout",
// this one "how much of the pod's termination grace period may close()
Expand Down Expand Up @@ -128,6 +129,13 @@ public CompletableFuture<Boolean> showPlan() throws IOException {
return this.run(TerraformCommand.showPlan);
}

/**
* Runs {@code terraform init -input=false}. With an {@code sshFile}, git
* fetches module sources with {@code GIT_SSH_COMMAND} set to
* {@code ssh -i <sshFile> -o StrictHostKeyChecking=no}. That value
* replaces any {@code GIT_SSH_COMMAND} among the terraform environment
* variables for init; other commands still get the caller's value.
*/
public CompletableFuture<Boolean> init(@NonNull TerraformProcessData terraformProcessData, @NonNull Consumer<String> outputListener, Consumer<String> errorListener) throws IOException {
checkVarFileParam(terraformProcessData);
checkTerraformVariablesParam(terraformProcessData);
Expand Down Expand Up @@ -376,10 +384,6 @@ private ProcessLauncher getTerraformLauncher(TerraformProcessData terraformProce
TerraformDownloader terraformDownloader = createTerraformDownloader();
String terraformPath = terraformProcessData.isTofu() ? terraformDownloader.downloadTofuVersion(terraformProcessData.getTerraformVersion()) : terraformDownloader.downloadTerraformVersion(terraformProcessData.getTerraformVersion());

if (terraformProcessData.sshFile != null && command.equals(TerraformCommand.init)) {
return getTerraformInitWithSSH(terraformPath, terraformProcessData, outputListener, errorListener);
}

ProcessLauncher launcher = createCommandLauncher(terraformProcessData, terraformPath, command);
launcher.setOutputListener(outputListener);
launcher.setErrorListener(errorListener);
Expand All @@ -398,6 +402,17 @@ ProcessLauncher createCommandLauncher(TerraformProcessData terraformProcessData,
launcher.setEnvironmentVariable(entry.getKey(), entry.getValue());
}

// Set after the caller's environment variables, so the sshFile wins
// over a GIT_SSH_COMMAND among them, as the old inline bash prefix did:
// callers pass user-controlled variables there, and such a value (even
// "") would otherwise drop the key and fail every SSH module fetch.
if (command == TerraformCommand.init && terraformProcessData.getSshFile() != null) {
String sshFilePath = terraformProcessData.getSshFile().getAbsolutePath();
log.info("Running init with an SSH key for module sources");
log.debug("SSH key file: {}", sshFilePath);
launcher.setEnvironmentVariable(GIT_SSH_COMMAND, gitSshCommand(sshFilePath));
}

if (!this.showColor)
launcher.appendCommands(TERRAFORM_PARAM_NO_COLOR);

Expand Down Expand Up @@ -588,31 +603,16 @@ private void registerLiveLauncher(ProcessLauncher launcher) {
}
}

private ProcessLauncher getTerraformInitWithSSH(String terraformPath, TerraformProcessData terraformProcessData, Consumer<String> outputListener, Consumer<String> errorListener) {
String initSSHCommand = String.format("GIT_SSH_COMMAND='ssh -i %s -o StrictHostKeyChecking=no' %s init", terraformProcessData.getSshFile().getAbsolutePath(), terraformPath);
ProcessLauncher processLauncher = newLauncher(terraformProcessData, "bash", "-c");
processLauncher.setInheritIO(this.isInheritIO());
processLauncher.setDirectory(terraformProcessData.getWorkingDirectory());

if (terraformProcessData.getTerraformEnvironmentVariables() != null)
for (Map.Entry<String, String> entry : terraformProcessData.getTerraformEnvironmentVariables().entrySet()) {
processLauncher.setEnvironmentVariable(entry.getKey(), entry.getValue());
}

if (!this.showColor)
initSSHCommand = initSSHCommand.concat(" " + TERRAFORM_PARAM_NO_COLOR);

if (terraformProcessData.getTerraformBackendConfigFileName() != null) {
initSSHCommand = initSSHCommand.concat(" " + TERRAFORM_PARAM_BACKEND.concat(terraformProcessData.getTerraformBackendConfigFileName()));
}
initSSHCommand = initSSHCommand.concat(" " + TERRAFORM_PARAM_DISABLE_USER_INPUT);

log.warn("Running terraform init with command {},", initSSHCommand);
processLauncher.appendCommands(initSSHCommand);
processLauncher.setOutputListener(outputListener);
processLauncher.setErrorListener(errorListener);
// Git runs GIT_SSH_COMMAND through sh, so the key path must be quoted for
// the shell: unquoted, a space splits it and a ; or $(...) runs a command.
static String gitSshCommand(String sshFilePath) {
return "ssh -i " + shellQuote(sshFilePath) + " -o StrictHostKeyChecking=no";
}

return processLauncher;
// POSIX single quotes take everything literally except ' itself, which
// is written as '\'' - close the quote, an escaped quote, reopen.
static String shellQuote(String value) {
return "'" + value.replace("'", "'\\''") + "'";
}

public TerraformDownloader createTerraformDownloader() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import java.util.stream.Collectors;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
Expand Down Expand Up @@ -76,6 +77,12 @@
downloads.incrementAndGet();
return binary;
}

@Override
public String downloadTofuVersion(String tofuVersion) {
downloads.incrementAndGet();
return binary;
}
};
}
};
Expand Down Expand Up @@ -205,6 +212,138 @@
assertEquals(0, downloads.get(), "contradictory flags must be rejected before any binary is fetched");
}

// Prints, one per line: GIT_SSH_COMMAND as the process sees it; how sh
// splits it into words, which is what git does with it, and the third word,
// which is the key path; then each argument, so argument boundaries show.
private static String printInitScript(Path bin) throws IOException {
return script(bin, "print-init.sh", String.join("\n",
"printf 'GIT_SSH_COMMAND=%s\\n' \"${GIT_SSH_COMMAND-<unset>}\"",
"(eval \"set -- $GIT_SSH_COMMAND\"; printf 'words=%s key=%s\\n' \"$#\" \"$3\")",
"printf 'arg=%s\\n' \"$@\"",
"echo to-stderr >&2"));
}

private static List<String> initOutput(TerraformClient client, TerraformProcessData data) throws Exception {
List<String> output = new CopyOnWriteArrayList<>();
assertTrue(client.init(data, output::add, IGNORE).get(30, TimeUnit.SECONDS));
return output;
}

private static List<String> args(List<String> output) {
return output.stream().filter(line -> line.startsWith("arg=")).collect(Collectors.toList());

Check warning on line 233 in terraform-client/src/test/java/io/terrakube/terraform/TerraformClientApiTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Replace this usage of 'Stream.collect(Collectors.toList())' with 'Stream.toList()' and ensure that the list is unmodified.

See more on https://sonarcloud.io/project/issues?id=AzBuilder_terraform-spring-boot&issues=AaHsyZ8JaQv_6ZgOTcVu&open=AaHsyZ8JaQv_6ZgOTcVu&pullRequest=119
}

private static String line(List<String> output, String prefix) {
return output.stream().filter(line -> line.startsWith(prefix)).findFirst()
.orElseThrow(() -> new AssertionError("no " + prefix + " line in " + output));
}

@Test
void shellQuoteKeepsEveryCharacterLiteral() throws Exception {
assertEquals("'it'\\''s'", TerraformClient.shellQuote("it's"));
List<String> values = List.of("", "plain", "with space", "it's", "''", "$HOME", "${PATH}",
"`id`", "$(id)", "a;b", "a && b", "back\\slash", "new\nline", "a 'b' $c `d` ;e");
for (String value : values) {
// The way git uses it: the shell evaluates the quoted text.
ProcessBuilder shell = new ProcessBuilder("sh", "-c", "eval \"set -- $Q\"; printf '%s:%s' \"$#\" \"$1\"");
shell.environment().put("Q", TerraformClient.shellQuote(value));
Process process = shell.start();
String result = new String(process.getInputStream().readAllBytes());
assertEquals(0, process.waitFor());
assertEquals("1:" + value, result, "shell-quoted " + value);
}
}

@Test
void sshInitRunsTheBinaryDirectlyWithTheSameArgumentsAsInit(@TempDir File workingDirectory, @TempDir Path bin) throws Exception {
String binary = printInitScript(bin);
File sshFile = new File(workingDirectory, "id_rsa");
String backendConfig = "backend dir/a;b.hcl";
for (boolean tofu : new boolean[]{false, true}) {
TerraformProcessData plain = TerraformProcessData.builder()
.terraformVersion("1.5.7")
.workingDirectory(workingDirectory)
.terraformBackendConfigFileName(backendConfig)
.tofu(tofu)
.build();
TerraformProcessData withSsh = plain.toBuilder().sshFile(sshFile).build();

try (TerraformClient client = fakeBinaryClient(binary, new AtomicInteger())) {
// The binary itself, not bash -c with a command string.
List<String> sshCommands = client.createCommandLauncher(withSsh, binary, TerraformCommand.init).getCommands();
assertEquals(binary, sshCommands.get(0));
assertEquals(client.createCommandLauncher(plain, binary, TerraformCommand.init).getCommands(), sshCommands);

List<String> plainOutput = initOutput(client, plain);
List<String> sshOutput = initOutput(client, withSsh);

// The backend config file name arrives as one literal argument:
// through bash, the space split it and the ; ended the command.
List<String> expectedArgs = List.of("arg=init", "arg=-no-color",
"arg=-backend-config=" + backendConfig, "arg=-input=false");
assertEquals(expectedArgs, args(plainOutput), "tofu=" + tofu);
assertEquals(expectedArgs, args(sshOutput), "tofu=" + tofu);

String inherited = System.getenv("GIT_SSH_COMMAND");
assertEquals("GIT_SSH_COMMAND=" + (inherited == null ? "<unset>" : inherited),
line(plainOutput, "GIT_SSH_COMMAND="), "init without an sshFile must not set it");
assertEquals("GIT_SSH_COMMAND=ssh -i '" + sshFile.getAbsolutePath() + "' -o StrictHostKeyChecking=no",
line(sshOutput, "GIT_SSH_COMMAND="));
}
}
}

@Test
void sshKeyPathReachesGitsShellIntact(@TempDir File workingDirectory, @TempDir Path bin) throws Exception {
File sshFile = new File(workingDirectory, "my keys/it's $HOME `id`; echo injected/id_rsa");
TerraformProcessData data = processData(workingDirectory).toBuilder().sshFile(sshFile).build();

try (TerraformClient client = fakeBinaryClient(printInitScript(bin), new AtomicInteger())) {
List<String> output = initOutput(client, data);
assertEquals("words=5 key=" + sshFile.getAbsolutePath(), line(output, "words="));
}
}

@Test
void sshFileTakesPrecedenceOverExplicitGitSshCommand(@TempDir File workingDirectory, @TempDir Path bin) throws Exception {
File sshFile = new File(workingDirectory, "id_rsa");
String expected = "GIT_SSH_COMMAND=ssh -i '" + sshFile.getAbsolutePath() + "' -o StrictHostKeyChecking=no";
String binary = printInitScript(bin);
// Callers pass user-controlled variables in this map, so a global value,
// even an empty one, must not drop the key and fail every SSH fetch.
for (String callerValue : List.of("ssh -F /custom/config", "")) {
TerraformProcessData data = processData(workingDirectory).toBuilder()
.sshFile(sshFile)
.terraformEnvironmentVariable("GIT_SSH_COMMAND", callerValue)
.build();

try (TerraformClient client = fakeBinaryClient(binary, new AtomicInteger())) {
assertEquals(expected, line(initOutput(client, data), "GIT_SSH_COMMAND="), "caller value '" + callerValue + "'");

// Only init is affected: other commands get the caller's value as is.
List<String> output = new CopyOnWriteArrayList<>();
assertTrue(client.plan(data, output::add, IGNORE).get(30, TimeUnit.SECONDS));
assertEquals("GIT_SSH_COMMAND=" + callerValue, line(output, "GIT_SSH_COMMAND="));
}
}
}

@Test
void sshInitHonoursRedirectErrorStream(@TempDir File workingDirectory, @TempDir Path bin) throws Exception {
TerraformProcessData data = processData(workingDirectory).toBuilder()
.sshFile(new File(workingDirectory, "id_rsa"))
.build();

try (TerraformClient client = fakeBinaryClient(printInitScript(bin), new AtomicInteger())) {
client.setRedirectErrorStream(true);
List<String> output = new CopyOnWriteArrayList<>();
List<String> errors = new CopyOnWriteArrayList<>();
assertTrue(client.init(data, output::add, errors::add).get(30, TimeUnit.SECONDS));
assertTrue(output.contains("to-stderr"), output.toString());
assertEquals(List.of(), errors);
}
}

@Test
void nullProcessDataIsRejectedWithAClearMessage() throws Exception {
try (TerraformClient client = TerraformClient.builder().build()) {
Expand Down
Loading