Skip to content

Commit

Permalink
Merge pull request kubernetes-sigs#132 from dobsonj/OCPBUGS-43642
Browse files Browse the repository at this point in the history
OCPBUGS-43642: redact sensitive information when logging VCenter config
  • Loading branch information
openshift-merge-bot[bot] authored Oct 22, 2024
2 parents 3162762 + de54615 commit cfcad01
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
23 changes: 22 additions & 1 deletion pkg/common/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"io"
"os"
"reflect"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -413,7 +414,7 @@ func validateConfig(ctx context.Context, cfg *Config) error {
if setCfgGlobalvCenter && cfg.Global.VCenterIP == "" {
cfg.Global.VCenterIP = vcServer
}
// Print out the config. WARNING: This will print the password used in plain text.
// Print out the config.
log.Debugf("vc server %s config: %+v", vcServer, vcConfig)
}

Expand Down Expand Up @@ -785,3 +786,23 @@ func GetSessionUserAgent(ctx context.Context) (string, error) {
}
return useragent, nil
}

// String returns a string representation of VirtualCenterConfig with sensitive fields redacted
func (vc VirtualCenterConfig) String() string {
val := reflect.ValueOf(vc)
typ := val.Type()

var fields []string
for i := 0; i < val.NumField(); i++ {
field := typ.Field(i)
value := val.Field(i)

if field.Tag.Get("sensitive") == "true" {
fields = append(fields, fmt.Sprintf("%s:%s", field.Name, strings.Repeat("*", value.Len())))
} else {
fields = append(fields, fmt.Sprintf("%s:%v", field.Name, value.Interface()))
}
}

return fmt.Sprintf("{%s}", strings.Join(fields, " "))
}
17 changes: 17 additions & 0 deletions pkg/common/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ package config

import (
"context"
"fmt"
"os"
"reflect"
"strings"
"testing"
)

Expand Down Expand Up @@ -219,6 +221,21 @@ func TestValidateConfigWithValidUsername2(t *testing.T) {
}
}

func TestSensitiveConfigFieldsRedacted(t *testing.T) {
vc := VirtualCenterConfig{
User: "[email protected]",
Password: "sensitivepassword",
VCenterPort: "443",
Datacenters: "dc1",
InsecureFlag: true,
}

s := fmt.Sprintf("%+v", vc)
if strings.Contains(s, "sensitivepassword") {
t.Errorf("Sensitive information leaked in VirtualCenterConfig struct:\n%s", s)
}
}

func TestSnapshotConfigWhenMaxUnspecified(t *testing.T) {
cfg := &Config{
VirtualCenter: idealVCConfig,
Expand Down
4 changes: 2 additions & 2 deletions pkg/common/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ type NetPermissionConfig struct {
// endpoint.
type VirtualCenterConfig struct {
// vCenter username.
User string `gcfg:"user"`
User string `gcfg:"user" sensitive:"true"`
// vCenter password in clear text.
Password string `gcfg:"password"`
Password string `gcfg:"password" sensitive:"true"`
// vCenter port.
VCenterPort string `gcfg:"port"`
// True if vCenter uses self-signed cert.
Expand Down

0 comments on commit cfcad01

Please sign in to comment.