-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Sanitize password from couchbase metric #1680 #3033
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
package couchbase | ||
|
||
import ( | ||
"log" | ||
"regexp" | ||
"sync" | ||
|
||
couchbase "github.com/couchbase/go-couchbase" | ||
"github.com/influxdata/telegraf" | ||
"github.com/influxdata/telegraf/plugins/inputs" | ||
"sync" | ||
) | ||
|
||
type Couchbase struct { | ||
|
@@ -55,6 +58,21 @@ func (r *Couchbase) Gather(acc telegraf.Accumulator) error { | |
return nil | ||
} | ||
|
||
// sanitizeURI by removing information about user and/or password from string | ||
// it also removes schema name from URI | ||
func sanitizeURI(uri string) (result string, err error) { | ||
|
||
re, err := regexp.Compile("(\\S+:\\/\\/)?(\\S+\\:\\S+@)") | ||
|
||
if err != nil { | ||
return | ||
} | ||
|
||
result = re.ReplaceAllString(uri, "${1}") | ||
|
||
return | ||
} | ||
|
||
func (r *Couchbase) gatherServer(addr string, acc telegraf.Accumulator, pool *couchbase.Pool) error { | ||
if pool == nil { | ||
client, err := couchbase.Connect(addr) | ||
|
@@ -71,15 +89,26 @@ func (r *Couchbase) gatherServer(addr string, acc telegraf.Accumulator, pool *co | |
} | ||
pool = &p | ||
} | ||
|
||
sanitizedAddress, err := sanitizeURI(addr) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if len(sanitizedAddress) <= 1 { | ||
log.Printf("I! WARNING: Cluster address tag \"'%s'\" is too short.", sanitizedAddress) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure it's possible to hit this line, if it is can you add a testcase? You can use "W!" to prefix the log message to indicate a warning. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line should be hit when |
||
} | ||
|
||
for i := 0; i < len(pool.Nodes); i++ { | ||
node := pool.Nodes[i] | ||
tags := map[string]string{"cluster": addr, "hostname": node.Hostname} | ||
tags := map[string]string{"cluster": sanitizedAddress, "hostname": node.Hostname} | ||
fields := make(map[string]interface{}) | ||
fields["memory_free"] = node.MemoryFree | ||
fields["memory_total"] = node.MemoryTotal | ||
acc.AddFields("couchbase_node", fields, tags) | ||
} | ||
for bucketName, _ := range pool.BucketMap { | ||
|
||
for bucketName := range pool.BucketMap { | ||
tags := map[string]string{"cluster": addr, "bucket": bucketName} | ||
bs := pool.BucketMap[bucketName].BasicStats | ||
fields := make(map[string]interface{}) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move to package scope and use MustCompile, this way it only will be compiled once. Personally I would use backtick strings so you don't need to double escape, also you don't need to escape
/
or:
. This would leave you withThis is a nitpick, but it would be nicer if there was less vertical whitespace in this function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. In this case I think I can simplify those whole thing by removing function (
MustCompile
is gonna panic anyway).