Skip to content

Commit

Permalink
cli: redact config output
Browse files Browse the repository at this point in the history
  • Loading branch information
andig committed Feb 14, 2025
1 parent 6fb3dbb commit 0b5376b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 11 deletions.
17 changes: 16 additions & 1 deletion cmd/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package cmd

import (
"encoding/json"
"fmt"
"slices"
"strings"

"github.com/evcc-io/evcc/util/config"
Expand Down Expand Up @@ -54,7 +56,20 @@ func runConfig(cmd *cobra.Command, args []string) {
}

for _, c := range configurable {

Check failure on line 58 in cmd/config.go

View workflow job for this annotation

GitHub Actions / Lint

unnecessary leading newline (whitespace)
fmt.Println(config.NameForID(c.ID), "type:"+c.Type, c.Value)

var j map[string]any
if err := json.Unmarshal([]byte(c.Value), &j); err != nil {
panic(err)
}

for k := range j {
if slices.Contains(redactSecrets, k) {
j[k] = "*****"
}
}

jstr, _ := json.Marshal(j)
fmt.Println(config.NameForID(c.ID), "type:"+c.Type, string(jstr))
}

fmt.Println("")
Expand Down
39 changes: 29 additions & 10 deletions cmd/helper.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package cmd

import (
"encoding/json"
"errors"
"fmt"
"net"
"os"
"regexp"
"slices"
"strings"

"github.com/evcc-io/evcc/cmd/shutdown"
Expand Down Expand Up @@ -45,22 +47,39 @@ func unwrap(err error) (res []string) {
return
}

var redactSecrets = []string{
"mac", // infrastructure
"sponsortoken", "plant", // global settings
"user", "password", "pin", // users
"token", "access", "refresh", "accesstoken", "refreshtoken", // tokens, including template variations
"ain", "secret", "serial", "deviceid", "machineid", "idtag", // devices
"app", "chats", "recipients", // push messaging
"vin", // vehicles
}

// redact redacts a configuration string
func redact(src string) string {
secrets := []string{
"mac", // infrastructure
"sponsortoken", "plant", // global settings
"user", "password", "pin", // users
"token", "access", "refresh", "accesstoken", "refreshtoken", // tokens, including template variations
"ain", "secret", "serial", "deviceid", "machineid", "idtag", // devices
"app", "chats", "recipients", // push messaging
"vin", // vehicles
}
return regexp.
MustCompile(fmt.Sprintf(`(?i)\b(%s)\b.*?:.*`, strings.Join(secrets, "|"))).
MustCompile(fmt.Sprintf(`(?i)\b(%s)\b.*?:.*`, strings.Join(redactSecrets, "|"))).
ReplaceAllString(src, "$1: *****")
}

func redactJson(src string) string {

Check failure on line 67 in cmd/helper.go

View workflow job for this annotation

GitHub Actions / Lint

func `redactJson` is unused (unused)
var j map[string]any
if err := json.Unmarshal([]byte(src), &j); err != nil {
panic(err)
}

for k := range j {
if slices.Contains(redactSecrets, k) {
j[k] = "*****"
}
}

res, _ := json.Marshal(j)
return string(res)
}

// fatal logs a fatal error and runs shutdown functions before terminating
func fatal(err error) {
log.FATAL.Println(err)
Expand Down

0 comments on commit 0b5376b

Please sign in to comment.