From 1233a94e4a4c6d992562abf9ed79106b6919020d Mon Sep 17 00:00:00 2001 From: Chris Allen Date: Fri, 22 Sep 2023 14:46:04 -0500 Subject: [PATCH 1/2] Add template_vars to environment variables when executing a hook --- tfworker/commands/terraform.py | 8 ++++++++ tfworker/definitions.py | 4 ++++ 2 files changed, 12 insertions(+) diff --git a/tfworker/commands/terraform.py b/tfworker/commands/terraform.py index ebbd222..aae6021 100644 --- a/tfworker/commands/terraform.py +++ b/tfworker/commands/terraform.py @@ -415,6 +415,7 @@ def _run( self._terraform_bin, debug=debug, b64_encode=self._b64_encode, + extra_vars=definition.template_vars, ) except HookError as e: click.secho( @@ -487,6 +488,7 @@ def _run( self._terraform_bin, debug=debug, b64_encode=self._b64_encode, + extra_vars=definition.template_vars, ) except HookError as e: click.secho( @@ -504,6 +506,7 @@ def hook_exec( terraform_path, debug=False, b64_encode=False, + extra_vars={}, ): """ hook_exec executes a hook script. @@ -587,6 +590,11 @@ def hook_exec( fg="red", ) + for k, v in extra_vars.items(): + if b64_encode: + v = base64.b64encode(v.encode("utf-8")).decode() + local_env[f"TF_EXTRA_{k.upper()}"] = v + # execute the hook (exit_code, stdout, stderr) = pipe_exec( f"{hook_script} {phase} {command}", diff --git a/tfworker/definitions.py b/tfworker/definitions.py index e278520..aaa7760 100644 --- a/tfworker/definitions.py +++ b/tfworker/definitions.py @@ -115,6 +115,10 @@ def provider_names(self): def plan_file(self): return self._plan_file + @property + def template_vars(self): + return self._template_vars + @plan_file.setter def plan_file(self, value: Path): if type(value) not in [PosixPath, WindowsPath, Path]: From 0ec147dab605456e780ff808887cb167c6c34ad5 Mon Sep 17 00:00:00 2001 From: Chris Allen Date: Mon, 25 Sep 2023 10:50:35 -0500 Subject: [PATCH 2/2] Fix env variable encoding when the base64 encoding flag is set --- tfworker/commands/terraform.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/tfworker/commands/terraform.py b/tfworker/commands/terraform.py index aae6021..4d5f268 100644 --- a/tfworker/commands/terraform.py +++ b/tfworker/commands/terraform.py @@ -562,9 +562,7 @@ def hook_exec( if state_value is not None: if b64_encode: - state_value = base64.b64encode( - state_value.encode("utf-8") - ).decode() + state_value = base64.b64encode(state_value.encode("utf-8")) local_env[f"TF_REMOTE_{state}_{item}".upper()] = state_value # populate environment with terraform variables @@ -581,7 +579,7 @@ def hook_exec( tf_var[1] = tf_var[1].replace(k, v) if b64_encode: - tf_var[1] = base64.b64encode(tf_var[1].encode("utf-8")).decode() + tf_var[1] = base64.b64encode(tf_var[1].encode("utf-8")) local_env[f"TF_VAR_{tf_var[0].upper()}"] = tf_var[1] else: @@ -592,7 +590,7 @@ def hook_exec( for k, v in extra_vars.items(): if b64_encode: - v = base64.b64encode(v.encode("utf-8")).decode() + v = base64.b64encode(v.encode("utf-8")) local_env[f"TF_EXTRA_{k.upper()}"] = v # execute the hook