From mboxrd@z Thu Jan  1 00:00:00 1970
From: Peter =?utf-8?q?M=C3=BCller?= <peter.mueller@ipfire.org>
To: development@lists.ipfire.org
Subject: Re: [PATCH] oci: user-data: Try to decode base64 content
Date: Mon, 21 Feb 2022 21:32:07 +0000
Message-ID: <9acebb7b-7bae-f826-b6ac-934c7f02267b@ipfire.org>
In-Reply-To: <20220221172456.1055696-1-michael.tremer@ipfire.org>
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="===============2309728997615565035=="
List-Id: <development.lists.ipfire.org>

--===============2309728997615565035==
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: quoted-printable

Ah, this must be enterprise cloud security: "Military-grade Base64 ..." m(

Anyway,

Reviewed-by: Peter M=C3=BCller <peter.mueller(a)ipfire.org>

> Terraform only supports sending any shell scripts encoded in base64
> which is however not required by Oracle. Therefore we have to test if
> the script is encoded or not.
>=20
> Signed-off-by: Michael Tremer <michael.tremer(a)ipfire.org>
> ---
>  src/initscripts/helper/oci-setup | 21 +++++++++++++++++++++
>  1 file changed, 21 insertions(+)
>=20
> diff --git a/src/initscripts/helper/oci-setup b/src/initscripts/helper/oci-=
setup
> index 0763a96e7..98b9858d6 100644
> --- a/src/initscripts/helper/oci-setup
> +++ b/src/initscripts/helper/oci-setup
> @@ -15,6 +15,24 @@ get() {
>  	wget -qO - "http://169.254.169.254/opc/v1/${file}"
>  }
> =20
> +try_base64_decode() {
> +	local input=3D"${1}"
> +
> +	local tmp=3D"$(mktemp)"
> +
> +	# Try to decode this and return output if successful
> +	if base64 -d <<< "${input}" > "${tmp}" 2>/dev/null; then
> +		echo "$(<${tmp})"
> +
> +	# Otherwise just return the input
> +	else
> +		echo "${input}"
> +	fi
> +
> +	# Cleanup
> +	unlink "${tmp}"
> +}
> +
>  to_address() {
>  	local n=3D"${1}"
> =20
> @@ -115,6 +133,9 @@ import_oci_configuration() {
>  		# Download a startup script
>  		local script=3D"$(get instance/metadata/user_data)"
> =20
> +		# Try to decode this
> +		script=3D"$(try_base64_decode "${script}")"
> +
>  		# Execute the script
>  		if [ "${script:0:2}" =3D "#!" ]; then
>  			echo "${script}" > /tmp/user-data.script

--===============2309728997615565035==--