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 Postgres detector #2301

Closed
wants to merge 5 commits into from
Closed
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
14 changes: 7 additions & 7 deletions pkg/detectors/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ func (s Scanner) Keywords() []string {

func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) ([]detectors.Result, error) {
var results []detectors.Result
var pgURLs []url.URL
pgURLs = append(pgURLs, findUriMatches(string(data)))
pgURLs := findUriMatches(string(data))
pgURLs = append(pgURLs, findComponentMatches(verify, string(data))...)

for _, pgURL := range pgURLs {
Expand Down Expand Up @@ -80,18 +79,19 @@ func getDeadlineInSeconds(ctx context.Context) int {
return int(duration.Seconds())
}

func findUriMatches(dataStr string) url.URL {
var pgURL url.URL
for _, uri := range uriPattern.FindAllString(dataStr, -1) {
func findUriMatches(dataStr string) []url.URL {
var results []url.URL
all := uriPattern.FindAllString(dataStr, -1)
for _, uri := range all {
pgURL, err := url.Parse(uri)
if err != nil {
continue
}
if pgURL.User != nil {
return *pgURL
results = append(results, *pgURL)
}
}
return pgURL
return results
}

// check if postgres is running
Expand Down
145 changes: 97 additions & 48 deletions pkg/detectors/postgres/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const (
postgresUser = "postgres"
postgresPass = "23201dabb56ca236f3dc6736c0f9afad"
postgresHost = "localhost"
postgresPort = "5433"
postgresPort = "5434" // don't use 5433 because it can conflict with local development in certain cases

inactiveUser = "inactive"
inactivePass = "inactive"
Expand All @@ -46,33 +46,37 @@ func TestPostgres_FromChunk(t *testing.T) {
tests := []struct {
name string
s Scanner
args args
args func() args
want []detectors.Result
wantErr bool
}{
{
name: "not found",
s: Scanner{},
args: args{
ctx: context.Background(),
data: []byte("You cannot find the secret within"),
verify: true,
args: func() args {
return args{
ctx: context.Background(),
data: []byte("You cannot find the secret within"),
verify: true,
}
},
want: nil,
wantErr: false,
},
{
name: "found with seperated credentials, verified",
s: Scanner{},
args: args{
ctx: context.Background(),
data: []byte(fmt.Sprintf(`
args: func() args {
return args{
ctx: context.Background(),
data: []byte(fmt.Sprintf(`
POSTGRES_USER=%s
POSTGRES_PASSWORD=%s
POSTGRES_ADDRESS=%s
POSTGRES_PORT=%s
`, postgresUser, postgresPass, postgresHost, postgresPort)),
verify: true,
verify: true,
}
},
want: []detectors.Result{
{
Expand All @@ -85,10 +89,12 @@ func TestPostgres_FromChunk(t *testing.T) {
{
name: "found with single line credentials, verified",
s: Scanner{},
args: args{
ctx: context.Background(),
data: []byte(fmt.Sprintf(`postgresql://%s:%s@%s:%s/postgres`, postgresUser, postgresPass, postgresHost, postgresPort)),
verify: true,
args: func() args {
return args{
ctx: context.Background(),
data: []byte(fmt.Sprintf(`postgresql://%s:%s@%s:%s/postgres`, postgresUser, postgresPass, postgresHost, postgresPort)),
verify: true,
}
},
want: []detectors.Result{
{
Expand All @@ -101,11 +107,13 @@ func TestPostgres_FromChunk(t *testing.T) {
{
name: "found with json credentials, verified",
s: Scanner{},
args: args{
ctx: context.Background(),
data: []byte(fmt.Sprintf(
`DB_CONFIG={"user": "%s", "password": "%s", "host": "%s", "port": "%s", "database": "postgres"}`, postgresUser, postgresPass, postgresHost, postgresPort)),
verify: true,
args: func() args {
return args{
ctx: context.Background(),
data: []byte(fmt.Sprintf(
`DB_CONFIG={"user": "%s", "password": "%s", "host": "%s", "port": "%s", "database": "postgres"}`, postgresUser, postgresPass, postgresHost, postgresPort)),
verify: true,
}
},
want: []detectors.Result{
{
Expand All @@ -118,15 +126,17 @@ func TestPostgres_FromChunk(t *testing.T) {
{
name: "found with seperated credentials, unverified",
s: Scanner{},
args: args{
ctx: context.Background(),
data: []byte(fmt.Sprintf(`
args: func() args {
return args{
ctx: context.Background(),
data: []byte(fmt.Sprintf(`
POSTGRES_USER=%s
POSTGRES_PASSWORD=%s
POSTGRES_ADDRESS=%s
POSTGRES_PORT=%s
`, postgresUser, inactivePass, postgresHost, postgresPort)),
verify: true,
verify: true,
}
},
want: []detectors.Result{
{
Expand All @@ -139,14 +149,16 @@ func TestPostgres_FromChunk(t *testing.T) {
{
name: "found with seperated credentials - no port, unverified",
s: Scanner{},
args: args{
ctx: context.Background(),
data: []byte(fmt.Sprintf(`
args: func() args {
return args{
ctx: context.Background(),
data: []byte(fmt.Sprintf(`
POSTGRES_USER=%s
POSTGRES_PASSWORD=%s
POSTGRES_ADDRESS=%s
`, postgresUser, inactivePass, postgresHost)),
verify: true,
verify: true,
}
},
want: []detectors.Result{
{
Expand All @@ -159,10 +171,12 @@ func TestPostgres_FromChunk(t *testing.T) {
{
name: "found with single line credentials, unverified",
s: Scanner{},
args: args{
ctx: context.Background(),
data: []byte(fmt.Sprintf(`postgresql://%s:%s@%s:%s/postgres`, postgresUser, inactivePass, postgresHost, postgresPort)),
verify: true,
args: func() args {
return args{
ctx: context.Background(),
data: []byte(fmt.Sprintf(`postgresql://%s:%s@%s:%s/postgres`, postgresUser, inactivePass, postgresHost, postgresPort)),
verify: true,
}
},
want: []detectors.Result{
{
Expand All @@ -175,11 +189,13 @@ func TestPostgres_FromChunk(t *testing.T) {
{
name: "found with json credentials, unverified - inactive password",
s: Scanner{},
args: args{
ctx: context.Background(),
data: []byte(fmt.Sprintf(
`DB_CONFIG={"user": "%s", "password": "%s", "host": "%s", "port": "%s", "database": "postgres"}`, postgresUser, inactivePass, postgresHost, postgresPort)),
verify: true,
args: func() args {
return args{
ctx: context.Background(),
data: []byte(fmt.Sprintf(
`DB_CONFIG={"user": "%s", "password": "%s", "host": "%s", "port": "%s", "database": "postgres"}`, postgresUser, inactivePass, postgresHost, postgresPort)),
verify: true,
}
},
want: []detectors.Result{
{
Expand All @@ -192,11 +208,13 @@ func TestPostgres_FromChunk(t *testing.T) {
{
name: "found with json credentials, unverified - inactive user",
s: Scanner{},
args: args{
ctx: context.Background(),
data: []byte(fmt.Sprintf(
`DB_CONFIG={"user": "%s", "password": "%s", "host": "%s", "port": "%s", "database": "postgres"}`, inactiveUser, postgresPass, postgresHost, postgresPort)),
verify: true,
args: func() args {
return args{
ctx: context.Background(),
data: []byte(fmt.Sprintf(
`DB_CONFIG={"user": "%s", "password": "%s", "host": "%s", "port": "%s", "database": "postgres"}`, inactiveUser, postgresPass, postgresHost, postgresPort)),
verify: true,
}
},
want: []detectors.Result{
{
Expand All @@ -209,10 +227,12 @@ func TestPostgres_FromChunk(t *testing.T) {
{
name: "found, unverified due to error - inactive port",
s: Scanner{},
args: args{
ctx: context.Background(),
data: []byte(fmt.Sprintf(`postgresql://%s:%s@%s:%s/postgres`, postgresUser, postgresPass, postgresHost, inactivePort)),
verify: true,
args: func() args {
return args{
ctx: context.Background(),
data: []byte(fmt.Sprintf(`postgresql://%s:%s@%s:%s/postgres`, postgresUser, postgresPass, postgresHost, inactivePort)),
verify: true,
}
},
want: func() []detectors.Result {
r := detectors.Result{
Expand All @@ -223,7 +243,6 @@ func TestPostgres_FromChunk(t *testing.T) {
}(),
wantErr: false,
},
// This test seems take a long time to run (70s+) even with the timeout set to 1s. It's not clear why.
{
name: "found, unverified due to error - inactive host",
s: Scanner{},
Expand All @@ -235,7 +254,7 @@ func TestPostgres_FromChunk(t *testing.T) {
data: []byte(fmt.Sprintf(`postgresql://%s:%s@%s:%s/postgres`, postgresUser, postgresPass, inactiveHost, postgresPort)),
verify: true,
}
}(),
},
want: func() []detectors.Result {
r := detectors.Result{
DetectorType: detectorspb.DetectorType_Postgres,
Expand All @@ -246,11 +265,41 @@ func TestPostgres_FromChunk(t *testing.T) {
}(),
wantErr: false,
},
{
name: "verified credentials next to bad host credentials",
s: Scanner{},
args: func() args {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
return args{
ctx: ctx,
data: []byte(fmt.Sprintf(`
postgresql://user:[email protected]:5432/mydb?sslmode=disable
postgresql://%s:%s@%s:%s/postgres`,
postgresUser, postgresPass, postgresHost, postgresPort)),
verify: true,
}
},
want: func() []detectors.Result {
first := detectors.Result{
DetectorType: detectorspb.DetectorType_Postgres,
Verified: false,
}
first.SetVerificationError(errors.New("i/o timeout"))
second := detectors.Result{
DetectorType: detectorspb.DetectorType_Postgres,
Verified: true,
}
return []detectors.Result{first, second}
}(),
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := Scanner{}
got, err := s.FromData(tt.args.ctx, tt.args.verify, tt.args.data)
args := tt.args()
got, err := s.FromData(args.ctx, args.verify, args.data)
if (err != nil) != tt.wantErr {
t.Errorf("postgres.FromData() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down
Loading