Skip to content

Commit

Permalink
Fix postgresql password exposure in metrics
Browse files Browse the repository at this point in the history
Fix the password exposure in the metrics or tags.

closes #821
closes #845
  • Loading branch information
menardorama authored and sparrc committed Mar 14, 2016
1 parent a4d60d9 commit 2fbcb5c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
- [#713](https:/influxdata/telegraf/issues/713): packaging: insecure permissions error on log directory
- [#816](https:/influxdata/telegraf/issues/816): Fix phpfpm panic if fcgi endpoint unreachable.
- [#828](https:/influxdata/telegraf/issues/828): fix net_response plugin overwriting host tag.
- [#821](https:/influxdata/telegraf/issues/821): Remove postgres password from server tag. Thanks @menardorama!

## v0.10.4.1

Expand Down
37 changes: 31 additions & 6 deletions plugins/inputs/postgresql/postgresql.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@ import (
"bytes"
"database/sql"
"fmt"
"regexp"
"sort"
"strings"

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/inputs"

_ "github.com/lib/pq"
"github.com/lib/pq"
)

type Postgresql struct {
Address string
Databases []string
OrderedColumns []string
AllColumns []string
Address string
Databases []string
OrderedColumns []string
AllColumns []string
sanitizedAddress string
}

var ignoredColumns = map[string]bool{"datid": true, "datname": true, "stats_reset": true}
Expand Down Expand Up @@ -133,6 +135,23 @@ type scanner interface {
Scan(dest ...interface{}) error
}

var passwordKVMatcher, _ = regexp.Compile("password=\\S+ ?")

func (p *Postgresql) SanitizedAddress() (_ string, err error) {
var canonicalizedAddress string
if strings.HasPrefix(p.Address, "postgres://") || strings.HasPrefix(p.Address, "postgresql://") {
canonicalizedAddress, err = pq.ParseURL(p.Address)
if err != nil {
return p.sanitizedAddress, err
}
} else {
canonicalizedAddress = p.Address
}
p.sanitizedAddress = passwordKVMatcher.ReplaceAllString(canonicalizedAddress, "")

return p.sanitizedAddress, err
}

func (p *Postgresql) accRow(row scanner, acc telegraf.Accumulator) error {
var columnVars []interface{}
var dbname bytes.Buffer
Expand Down Expand Up @@ -165,7 +184,13 @@ func (p *Postgresql) accRow(row scanner, acc telegraf.Accumulator) error {
dbname.WriteString("postgres")
}

tags := map[string]string{"server": p.Address, "db": dbname.String()}
var tagAddress string
tagAddress, err = p.SanitizedAddress()
if err != nil {
return err
}

tags := map[string]string{"server": tagAddress, "db": dbname.String()}

fields := make(map[string]interface{})
for col, val := range columnMap {
Expand Down

0 comments on commit 2fbcb5c

Please sign in to comment.