Find, verify, and analyze leaked credentials
Go credentialsdevsecopsdynamic-analysisprecommitscanningsecretsecret-managementsecretssecuritysecurity-toolstrufflehogverification
unify JDBC detector ping logic (#1506)
Cody Rosecb1a63a4e20e
diff --git a/pkg/detectors/jdbc/jdbc.go b/pkg/detectors/jdbc/jdbc.goindex 18bb2ead9..c45df67ee 100644--- a/pkg/detectors/jdbc/jdbc.go+++ b/pkg/detectors/jdbc/jdbc.go@@ -220,11 +220,13 @@ func newJDBC(conn string) (jdbc, error) {return parser(subname)}-func ping(ctx context.Context, driverName, conn string) bool {- if err := pingErr(ctx, driverName, conn); err != nil {- return false+func ping(ctx context.Context, driverName string, candidateConns ...string) bool {+ for _, c := range candidateConns {+ if err := pingErr(ctx, driverName, c); err == nil {+ return true+ }}- return true+ return false}func pingErr(ctx context.Context, driverName, conn string) error {diff --git a/pkg/detectors/jdbc/jdbc_integration_test.go b/pkg/detectors/jdbc/jdbc_integration_test.goindex 0c69571f3..9ce26d50d 100644--- a/pkg/detectors/jdbc/jdbc_integration_test.go+++ b/pkg/detectors/jdbc/jdbc_integration_test.go@@ -105,6 +105,22 @@ func TestJdbcVerified(t *testing.T) {},wantErr: false,},+ {+ name: "sql server verified",+ args: args{+ ctx: context.Background(),+ data: []byte(fmt.Sprintf("jdbc:sqlserver://odbc:server=localhost;database=%s;password=%s", sqlServerDatabase, sqlServerPass)),+ verify: true,+ },+ want: []detectors.Result{+ {+ DetectorType: detectorspb.DetectorType_JDBC,+ Verified: true,+ Redacted: "jdbc:sqlserver://odbc:server=localhost;database=master;password=**************",+ },+ },+ wantErr: false,+ },}for _, tt := range tests {t.Run(tt.name, func(t *testing.T) {diff --git a/pkg/detectors/jdbc/mysql.go b/pkg/detectors/jdbc/mysql.goindex 768d1ed2a..2e98a045b 100644--- a/pkg/detectors/jdbc/mysql.go+++ b/pkg/detectors/jdbc/mysql.go@@ -17,25 +17,19 @@ type mysqlJDBC struct {}func (s *mysqlJDBC) ping(ctx context.Context) bool {- if ping(ctx, "mysql", s.conn) {- return true- }- // try building connection string (should be same as s.conn though)- if ping(ctx, "mysql", s.build()) {- return true- }- // try removing database- s.database = ""- return ping(ctx, "mysql", s.build())+ return ping(ctx, "mysql",+ s.conn,+ buildMySQLConnectionString(s.host, s.database, s.userPass, s.params),+ buildMySQLConnectionString(s.host, "", s.userPass, s.params))}-func (s *mysqlJDBC) build() string {- conn := s.host + "/" + s.database- if s.userPass != "" {- conn = s.userPass + "@" + conn+func buildMySQLConnectionString(host, database, userPass, params string) string {+ conn := host + "/" + database+ if userPass != "" {+ conn = userPass + "@" + conn}- if s.params != "" {- conn = conn + "?" + s.params+ if params != "" {+ conn = conn + "?" + params}return conn}diff --git a/pkg/detectors/jdbc/postgres.go b/pkg/detectors/jdbc/postgres.goindex 9136879bd..5d806855e 100644--- a/pkg/detectors/jdbc/postgres.go+++ b/pkg/detectors/jdbc/postgres.go@@ -15,37 +15,11 @@ type postgresJDBC struct {}func (s *postgresJDBC) ping(ctx context.Context) bool {- // try the provided connection string directly- if ping(ctx, "postgres", s.conn) {- return true- }- // try as a URL- if ping(ctx, "postgres", "postgres://"+s.conn) {- return true- }- // build a connection string- data := map[string]string{- // default user- "user": "postgres",- }- for key, val := range s.params {- if key == "host" {- if h, p, found := strings.Cut(val, ":"); found {- data["host"] = h- data["port"] = p- continue- }- }- data[key] = val- }- if ping(ctx, "postgres", joinKeyValues(data, " ")) {- return true- }- if s.params["dbname"] != "" {- delete(s.params, "dbname")- return s.ping(ctx)- }- return false+ return ping(ctx, "postgres",+ s.conn,+ "postgres://"+s.conn,+ buildPostgresConnectionString(s.params, true),+ buildPostgresConnectionString(s.params, false))}func joinKeyValues(m map[string]string, sep string) string {@@ -79,3 +53,25 @@ func parsePostgres(subname string) (jdbc, error) {return &postgresJDBC{subname[2:], params}, nil}++func buildPostgresConnectionString(params map[string]string, includeDbName bool) string {+ data := map[string]string{+ // default user+ "user": "postgres",+ }+ for key, val := range params {+ if key == "host" {+ if h, p, found := strings.Cut(val, ":"); found {+ data["host"] = h+ data["port"] = p+ continue+ }+ }+ if key == "dbname" && !includeDbName {+ continue+ }+ data[key] = val+ }++ return joinKeyValues(data, " ")+}diff --git a/pkg/detectors/jdbc/sqlserver.go b/pkg/detectors/jdbc/sqlserver.goindex ab3506a6b..ac781002f 100644--- a/pkg/detectors/jdbc/sqlserver.go+++ b/pkg/detectors/jdbc/sqlserver.go@@ -14,14 +14,10 @@ type sqlServerJDBC struct {}func (s *sqlServerJDBC) ping(ctx context.Context) bool {- if ping(ctx, "mssql", s.conn) {- return true- }- if ping(ctx, "mssql", joinKeyValues(s.params, ";")) {- return true- }- // try URL format- return ping(ctx, "mssql", "sqlserver://"+s.conn)+ return ping(ctx, "mssql",+ s.conn,+ joinKeyValues(s.params, ";"),+ "sqlserver://"+s.conn)}func parseSqlServer(subname string) (jdbc, error) {diff --git a/pkg/detectors/jdbc/sqlserver_integration_test.go b/pkg/detectors/jdbc/sqlserver_integration_test.goindex 590e37607..e76e6abc8 100644--- a/pkg/detectors/jdbc/sqlserver_integration_test.go+++ b/pkg/detectors/jdbc/sqlserver_integration_test.go@@ -35,7 +35,7 @@ func TestSqlServer(t *testing.T) {wantPing: true,},{- input: "//localhost;database= master;spring.datasource.password=" + sqlServerPass,+ input: "//localhost;database=master;spring.datasource.password=" + sqlServerPass,wantPing: true,},}