From 9ab25264408e8903429a95888cd460fcd5fc6cdb Mon Sep 17 00:00:00 2001 From: mgoerens Date: Wed, 15 Nov 2023 09:44:59 +0100 Subject: [PATCH] Encode chart_entry to base64 (#297) This commit gets rid of the json.loads() call as the argparse type for chart_entry in update-index. Instead we now encode the index entry to base64 in chart-repo-manager before adding it to the GITHUB_OUTPUT and decode it in update-index back to a dict. This allows the use of double quotes when passing the "--chart-entry" argument to update-index, which enables the possibility for variable evaluation. This paves the way for using environment variables instead of using step's output directly. Signed-off-by: Matthias Goerens --- .github/workflows/build.yml | 2 +- .../src/chartrepomanager/chartrepomanager.py | 21 +++++++++++++++- scripts/src/updateindex/updateindex.py | 25 ++++++++++++++++--- 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index db373af3..ef3da747 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -519,7 +519,7 @@ jobs: --index-branch=${INDEX_BRANCH} \ --index-file=${INDEX_FILE} \ --repository=${{ github.repository }} \ - --chart-entry='${{ steps.prepare-chart-release.outputs.chart_entry }}' \ + --chart-entry="${{ steps.prepare-chart-release.outputs.chart_entry }}" \ --chart-url="${{ steps.prepare-chart-release.outputs.chart_url }}" \ --version="${{ steps.prepare-chart-release.outputs.version }}" diff --git a/scripts/src/chartrepomanager/chartrepomanager.py b/scripts/src/chartrepomanager/chartrepomanager.py index c62e2182..02c9802d 100644 --- a/scripts/src/chartrepomanager/chartrepomanager.py +++ b/scripts/src/chartrepomanager/chartrepomanager.py @@ -20,6 +20,7 @@ """ import argparse +import base64 import json import shutil import os @@ -47,6 +48,24 @@ from tools import gitutils +def _encode_chart_entry(chart_entry): + """Encode the chart_entry to base64. This is needed to pass it as an argument to + the update index step. + + Args: + chart_entry (dict): the index entry for this chart to encode + + Returns: + str: The encoded base64 string equivalent. + + """ + chart_entry_str = json.dumps(chart_entry) + chart_entry_bytes = chart_entry_str.encode() + + # Decoding to string for the GITHUB_OUTPUT + return base64.b64encode(chart_entry_bytes).decode() + + def get_modified_charts(api_url): """Get the category, organization, chart name, and new version corresponding to the chart being added or modified by this PR. @@ -491,7 +510,7 @@ def main(): print(f"[INFO] Add key file for release : {current_dir}/{public_key_file}") gitutils.add_output("public_key_file", f"{current_dir}/{public_key_file}") - gitutils.add_output("chart_entry", json.dumps(chart_entry)) + gitutils.add_output("chart_entry", _encode_chart_entry(chart_entry)) gitutils.add_output("chart_url", chart_url) gitutils.add_output("version", version) diff --git a/scripts/src/updateindex/updateindex.py b/scripts/src/updateindex/updateindex.py index 3055e446..62c688f0 100644 --- a/scripts/src/updateindex/updateindex.py +++ b/scripts/src/updateindex/updateindex.py @@ -2,6 +2,7 @@ """ import argparse +import base64 import hashlib import json import os @@ -18,6 +19,22 @@ from yaml import Loader, Dumper +def _decode_chart_entry(chart_entry_encoded): + """Decode the base64 encoded index entry to add. + + Args: + chart_entry_encoded (str): base64 encode index entry for this chart + + Returns: + dict: Decoded index entry + + """ + chart_entry_bytes = base64.b64decode(chart_entry_encoded) + chart_entry_str = chart_entry_bytes.decode() + + return json.loads(chart_entry_str) + + def download_index(index_file, repository, branch): """Download the index file to disk and retrieve its content. @@ -181,8 +198,8 @@ def main(): parser.add_argument( "-e", "--chart-entry", - dest="chart_entry", - type=json.loads, + dest="chart_entry_encoded", + type=str, required=True, help="Index entry to add", ) @@ -196,6 +213,8 @@ def main(): ) args = parser.parse_args() + chart_entry = _decode_chart_entry(args.chart_entry_encoded) + env = Env() web_catalog_only = env.bool("WEB_CATALOG_ONLY", False) @@ -204,7 +223,7 @@ def main(): index_data, args.version, args.chart_url, - args.chart_entry, + chart_entry, web_catalog_only, ) write_index_file(index_data, args.index_file)