From 6a55bc3daab2102e29946f24612de5e5a597ccfe Mon Sep 17 00:00:00 2001 From: Austin Abro <37223396+AustinAbro321@users.noreply.github.com> Date: Tue, 5 Nov 2024 13:23:45 -0500 Subject: [PATCH] refactor: debug template print (#3171) Signed-off-by: Austin Abro --- src/internal/packager/template/template.go | 38 ++++++--- .../packager/template/template_test.go | 82 +++++++++++++++++++ src/pkg/packager/creator/normal.go | 5 +- 3 files changed, 110 insertions(+), 15 deletions(-) create mode 100644 src/internal/packager/template/template_test.go diff --git a/src/internal/packager/template/template.go b/src/internal/packager/template/template.go index f069698425..b673f25a6c 100644 --- a/src/internal/packager/template/template.go +++ b/src/internal/packager/template/template.go @@ -7,6 +7,7 @@ package template import ( "context" "encoding/base64" + "encoding/json" "fmt" "log/slog" "strings" @@ -102,7 +103,10 @@ func GetZarfTemplates(ctx context.Context, componentName string, state *types.Za } } - debugPrintTemplateMap(ctx, templateMap) + err = debugPrintTemplateMap(ctx, templateMap) + if err != nil { + return nil, err + } return templateMap, nil } @@ -127,21 +131,29 @@ func generateHtpasswd(regInfo *types.RegistryInfo) (string, error) { return "", nil } -func debugPrintTemplateMap(ctx context.Context, templateMap map[string]*variables.TextTemplate) { - // TODO (@austinabro321) sanitize the template by making a copy and changing the actual keys - // then use json.MarshalIndent to create the json - debugText := "templateMap = { " +func debugPrintTemplateMap(ctx context.Context, templateMap map[string]*variables.TextTemplate) error { + sanitizedMap := getSanitizedTemplateMap(templateMap) + + b, err := json.MarshalIndent(sanitizedMap, "", " ") + if err != nil { + return err + } + message.Debug(fmt.Sprintf("templateMap = %s", string(b))) + logger.From(ctx).Debug("cluster.debugPrintTemplateMap", "templateMap", sanitizedMap) + return nil +} + +func getSanitizedTemplateMap(templateMap map[string]*variables.TextTemplate) map[string]string { + sanitizedMap := make(map[string]string, len(templateMap)) for key, template := range templateMap { - if template.Sensitive { - debugText += fmt.Sprintf("\"%s\": \"**sanitized**\", ", key) + if template == nil { + sanitizedMap[key] = "" + } else if template.Sensitive { + sanitizedMap[key] = "**sanitized**" } else { - debugText += fmt.Sprintf("\"%s\": \"%s\", ", key, template.Value) + sanitizedMap[key] = template.Value } } - - debugText += " }" - - message.Debug(debugText) - logger.From(ctx).Debug(debugText) + return sanitizedMap } diff --git a/src/internal/packager/template/template_test.go b/src/internal/packager/template/template_test.go new file mode 100644 index 0000000000..d097debdfa --- /dev/null +++ b/src/internal/packager/template/template_test.go @@ -0,0 +1,82 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2021-Present The Zarf Authors + +// Package template provides functions for templating yaml files. +package template + +import ( + "testing" + + "github.com/stretchr/testify/require" + "github.com/zarf-dev/zarf/src/pkg/variables" +) + +func TestGetSanitizedTemplateMap(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + input map[string]*variables.TextTemplate + expected map[string]string + }{ + { + name: "Sensitive entry", + input: map[string]*variables.TextTemplate{ + "###SENSITIVE###": {Sensitive: true, Value: "secret"}, + }, + expected: map[string]string{ + "###SENSITIVE###": "**sanitized**", + }, + }, + { + name: "Non-sensitive entries", + input: map[string]*variables.TextTemplate{ + "###VARIABLE###": {Sensitive: false, Value: "value"}, + }, + expected: map[string]string{ + "###VARIABLE###": "value", + }, + }, + { + name: "Sensitive and non-sensitive entries", + input: map[string]*variables.TextTemplate{ + "###ZARF_GIT_AUTH_PULL###": {Sensitive: true, Value: "secret1"}, + "###ZARF_GIT_AUTH_PUSH###": {Sensitive: true, Value: "secret2"}, + "###ZARF_GIT_PUSH###": {Sensitive: false, Value: "zarf-git-user"}, + "###ZARF_GIT_PULL###": {Sensitive: false, Value: "zarf-git-read-user"}, + }, + expected: map[string]string{ + "###ZARF_GIT_AUTH_PULL###": "**sanitized**", + "###ZARF_GIT_AUTH_PUSH###": "**sanitized**", + "###ZARF_GIT_PULL###": "zarf-git-read-user", + "###ZARF_GIT_PUSH###": "zarf-git-user", + }, + }, + { + name: "Nil map", + input: nil, + expected: map[string]string{}, + }, + { + name: "Empty map", + input: map[string]*variables.TextTemplate{}, + expected: map[string]string{}, + }, + { + name: "Map with nil value", + input: map[string]*variables.TextTemplate{ + "###ZARF_GIT_AUTH_PULL###": nil, + }, + expected: map[string]string{ + "###ZARF_GIT_AUTH_PULL###": "", + }, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + output := getSanitizedTemplateMap(test.input) + require.Equal(t, test.expected, output) + }) + } +} diff --git a/src/pkg/packager/creator/normal.go b/src/pkg/packager/creator/normal.go index c97cab63c1..a844e18c9a 100644 --- a/src/pkg/packager/creator/normal.go +++ b/src/pkg/packager/creator/normal.go @@ -318,9 +318,10 @@ func (pc *PackageCreator) Output(ctx context.Context, dst *layout.PackagePaths, packageName := fmt.Sprintf("%s%s", sources.NameFromMetadata(pkg, pc.createOpts.IsSkeleton), sources.PkgSuffix(pkg.Metadata.Uncompressed)) tarballPath := filepath.Join(pc.createOpts.Output, packageName) - // Try to remove the package if it already exists. + // remove existing package with the same name err = os.Remove(tarballPath) - if err != nil { + // user only cares about this error if file exists and the remove failed + if err != nil && !errors.Is(err, os.ErrNotExist) { logger.From(ctx).Error(err.Error()) }