Skip to content
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

Fix for Masking User Cred in PG DB URI #5845

Merged
merged 5 commits into from
Oct 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions components/data-feed-service/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,16 @@ func Configure() (*DataFeedConfig, error) {
var err error
log.Infof("Database %s", config.PostgresConfig.Database)
config.PostgresConfig.ConnectionString, err = platform_config.PGURIFromEnvironment(config.PostgresConfig.Database)
log.Infof("Database connection string %s", config.PostgresConfig.ConnectionString)
maskedDBConnString := MaskPGCredInURI(config.PostgresConfig.ConnectionString)
log.Infof("Masked Database connection string %s", maskedDBConnString)
if err != nil {
log.WithError(err).Error("Failed to get pg uri")
return nil, err
}
}
log.Debugf("DATA FEED SERVICE CONFIG: %+v", config)
logConfigForDebug := fmt.Sprintf("DATA FEED SERVICE CONFIG: %+v", config)
maskedLogConfigForDebug := MaskPGCredInURI(logConfigForDebug)
log.Debug(maskedLogConfigForDebug)
log.Debug("end config.go Configure() ->")
return config, nil
}
Expand Down
12 changes: 12 additions & 0 deletions components/data-feed-service/config/mask.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package config

import (
"regexp"
)

func MaskPGCredInURI(s string) string {
selector := regexp.MustCompile("^(.*?)postgresql://(.*):(.*)@(.*)$")
replaceRegex := "${1}postgresql://<USER>:<PASSWORD>@${4}"
maskedPGCredInURI := selector.ReplaceAllString(s, replaceRegex)
return maskedPGCredInURI
}
35 changes: 35 additions & 0 deletions components/data-feed-service/config/mask_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package config_test

import (
"fmt"
"testing"

"github.com/chef/automate/components/data-feed-service/config"
"github.com/stretchr/testify/assert"
)

func TestMaskPGCredInURI(t *testing.T) {
configData := &config.DataFeedConfig{
CerealConfig: config.CerealConfig{Target: "cereal://user:pass@127.0.01:3333"},
PostgresConfig: config.PostgresConfig{ConnectionString: "postgresql://user:pass@127.0.0.1:10145"},
}
fullConfig := fmt.Sprintf("DATA FEED SERVICE CONFIG: %+v", configData)
tests := []struct {
source string
expected string
}{
{source: "postgresql://user:pass@127.0.0.1:10145", expected: "postgresql://<USER>:<PASSWORD>@127.0.0.1:10145"},
{source: "postgresql://user:@127.0.0.1:10145", expected: "postgresql://<USER>:<PASSWORD>@127.0.0.1:10145"},
{source: "postgresql://:pass@127.0.0.1:10145", expected: "postgresql://<USER>:<PASSWORD>@127.0.0.1:10145"},
{source: "postgresql://127.0.0.1:10145", expected: "postgresql://127.0.0.1:10145"},
{source: "postgresql://:@127.0.0.1:10145", expected: "postgresql://<USER>:<PASSWORD>@127.0.0.1:10145"},
{source: "postgresql://user:pass127.0.0.1:10145", expected: "postgresql://user:pass127.0.0.1:10145"},
{source: fullConfig, expected: "DATA FEED SERVICE CONFIG: &{ServiceConfig:{Host: Port:0 FeedInterval:0s AssetPageSize:0 ReportsPageSize:0 NodeBatchSize:0 UpdatedNodesOnly:false DisableCIDRFilter:false CIDRFilter: ExternalFqdn: AcceptedStatusCodes:[] ContentType:} LogConfig:{LogLevel: LogFormat:} TLSConfig:{CertPath: KeyPath: RootCACertPath:} SecretsConfig:{Target:} CfgmgmtConfig:{Target:} ComplianceConfig:{Target:} CerealConfig:{Target:cereal://user:pass@127.0.01:3333} PostgresConfig:{ConnectionString:postgresql://<USER>:<PASSWORD>@127.0.0.1:10145 Database: MigrationsPath:} ServiceCerts:<nil>}"},
}

for _, test := range tests {
actual := config.MaskPGCredInURI(test.source)

assert.Equal(t, test.expected, actual)
}
}