Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: debug template print #3171

Merged
merged 3 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
34 changes: 22 additions & 12 deletions src/internal/packager/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package template
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"log/slog"
"strings"
Expand Down Expand Up @@ -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
}
Expand All @@ -127,21 +131,27 @@ 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, "", " ")
AustinAbro321 marked this conversation as resolved.
Show resolved Hide resolved
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)
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
}
34 changes: 34 additions & 0 deletions src/internal/packager/template/template_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// 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()
input := map[string]*variables.TextTemplate{
"###ZARF_GIT_AUTH_PULL###": {Sensitive: true, Value: "secret1"},
"###ZARF_GIT_AUTH_PUSH###": {Sensitive: true, Value: "secret2"},
"###ZARF_REGISTRY###": {Sensitive: false, Value: "127.0.0.1:31999"},
"###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",
"###ZARF_REGISTRY###": "127.0.0.1:31999",
}

output := getSanitizedTemplateMap(input)
require.Equal(t, expected, output)
AustinAbro321 marked this conversation as resolved.
Show resolved Hide resolved
}
5 changes: 3 additions & 2 deletions src/pkg/packager/creator/normal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

Expand Down