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 all 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
38 changes: 25 additions & 13 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,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, "", " ")
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)
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
}
82 changes: 82 additions & 0 deletions src/internal/packager/template/template_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
}
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