Files
trufflehog/pkg/detectors/planetscaledb/planetscaledb.go
0f427b3c6a
Lint / golangci-lint (push) Waiting to run
Lint / semgrep (push) Waiting to run
Release / Release (push) Waiting to run
Scan for secrets / test (push) Waiting to run
Snifftest / Run Snifftest (push) Waiting to run
Test / test (push) Waiting to run
Test / test-community (push) Waiting to run
Adding Descriptions (#3258)
* adding AI generated descriptions of the key types and their capabilities

* removing empty file

* Update abbysale.go

* update to interface

* fixes

* fix

* small cleanup

---------

Co-authored-by: Dylan Ayrey <[email protected]>
Co-authored-by: Dustin Decker <[email protected]>
2024-09-24 16:10:16 -07:00

87 lines
2.4 KiB
Go

package planetscaledb
import (
"context"
"database/sql"
regexp "github.com/wasilibs/go-re2"
"strings"
"github.com/go-sql-driver/mysql"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
)
type Scanner struct{
detectors.DefaultMultiPartCredentialProvider
}
// Ensure the Scanner satisfies the interface at compile time.
var _ detectors.Detector = (*Scanner)(nil)
var (
usernamePat = regexp.MustCompile(`\b[a-z0-9]{20}\b`)
passwordPat = regexp.MustCompile(`\bpscale_pw_[A-Za-z0-9_]{43}\b`)
hostPat = regexp.MustCompile(`\b(aws|gcp)\.connect\.psdb\.cloud\b`)
)
// Keywords are used for efficiently pre-filtering chunks.
func (s Scanner) Keywords() []string {
return []string{"pscale_pw_"}
}
// FromData will find and optionally verify Planetscaledb secrets in a given set of bytes.
func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {
dataStr := string(data)
usernameMatches := usernamePat.FindAllStringSubmatch(dataStr, -1)
passwordMatches := passwordPat.FindAllStringSubmatch(dataStr, -1)
hostMatches := hostPat.FindAllString(dataStr, -1)
for _, username := range usernameMatches {
for _, password := range passwordMatches {
for _, host := range hostMatches {
s1 := detectors.Result{
DetectorType: detectorspb.DetectorType_PlanetScaleDb,
Raw: []byte(strings.Join([]string{host, username[0], password[0]}, "\t")),
}
if verify {
cfg := mysql.Config{
User: username[0],
Passwd: password[0],
Net: "tcp",
Addr: host,
TLSConfig: "true", // assuming SSL is required
AllowNativePasswords: true,
}
db, err := sql.Open("mysql", cfg.FormatDSN())
if err != nil {
s1.SetVerificationError(err, password[0])
} else {
err = db.PingContext(ctx)
if err == nil {
s1.Verified = true
} else {
s1.SetVerificationError(err, password[0])
}
db.Close()
}
}
results = append(results, s1)
}
}
}
return results, nil
}
func (s Scanner) Type() detectorspb.DetectorType {
return detectorspb.DetectorType_PlanetScaleDb
}
func (s Scanner) Description() string {
return "PlanetScaleDB is a serverless database platform built on Vitess. Credentials found here can be used to connect to the database and perform operations."
}