forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trace.go
97 lines (77 loc) · 2.19 KB
/
trace.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package trace
import (
"fmt"
. "github.com/cloudfoundry/cli/cf/i18n"
"github.com/cloudfoundry/gofileutils/fileutils"
"io"
"log"
"os"
"regexp"
)
var (
PRIVATE_DATA_PLACEHOLDER = T("[PRIVATE DATA HIDDEN]")
)
const CF_TRACE = "CF_TRACE"
type Printer interface {
Print(v ...interface{})
Printf(format string, v ...interface{})
Println(v ...interface{})
}
type nullLogger struct{}
func (*nullLogger) Print(v ...interface{}) {}
func (*nullLogger) Printf(format string, v ...interface{}) {}
func (*nullLogger) Println(v ...interface{}) {}
var stdOut io.Writer = os.Stdout
var Logger Printer
func init() {
Logger = NewLogger("")
}
func EnableTrace() {
Logger = newStdoutLogger()
}
func DisableTrace() {
Logger = new(nullLogger)
}
func SetStdout(s io.Writer) {
stdOut = s
}
func NewLogger(cf_trace string) Printer {
switch cf_trace {
case "", "false":
Logger = new(nullLogger)
case "true":
Logger = newStdoutLogger()
default:
Logger = newFileLogger(cf_trace)
}
return Logger
}
func newStdoutLogger() Printer {
return log.New(stdOut, "", 0)
}
func newFileLogger(path string) Printer {
file, err := fileutils.Open(path)
if err != nil {
logger := newStdoutLogger()
logger.Printf(T("CF_TRACE ERROR CREATING LOG FILE {{.Path}}:\n{{.Err}}",
map[string]interface{}{"Path": path, "Err": err}))
return logger
}
return log.New(file, "", 0)
}
func Sanitize(input string) (sanitized string) {
var sanitizeJson = func(propertyName string, json string) string {
regex := regexp.MustCompile(fmt.Sprintf(`"%s":\s*"[^"]*"`, propertyName))
return regex.ReplaceAllString(json, fmt.Sprintf(`"%s":"%s"`, propertyName, PRIVATE_DATA_PLACEHOLDER))
}
re := regexp.MustCompile(`(?m)^Authorization: .*`)
sanitized = re.ReplaceAllString(input, "Authorization: "+PRIVATE_DATA_PLACEHOLDER)
re = regexp.MustCompile(`password=[^&]*&`)
sanitized = re.ReplaceAllString(sanitized, "password="+PRIVATE_DATA_PLACEHOLDER+"&")
sanitized = sanitizeJson("access_token", sanitized)
sanitized = sanitizeJson("refresh_token", sanitized)
sanitized = sanitizeJson("token", sanitized)
sanitized = sanitizeJson("password", sanitized)
sanitized = sanitizeJson("oldPassword", sanitized)
return
}