Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: better mongo simple statement check (#8298)
Danny Xudfbb6ce923f8
diff --git a/backend/plugin/db/mongodb/mongodb.go b/backend/plugin/db/mongodb/mongodb.goindex c07a5f3038..6b27e63dc6 100644--- a/backend/plugin/db/mongodb/mongodb.go+++ b/backend/plugin/db/mongodb/mongodb.go@@ -171,10 +171,7 @@ func getMongoDBConnectionURI(connConfig db.ConnectionConfig) string {// QueryConn queries a SQL statement in a given connection.func (driver *Driver) QueryConn(ctx context.Context, _ *sql.Conn, statement string, queryContext *db.QueryContext) ([]*v1pb.QueryResult, error) {- simpleStatement := false- if _, err := parser.ParseMongo(statement); err == nil {- simpleStatement = true- }+ simpleStatement := isMongoStatement(statement)startTime := time.Now()connectionURI := getMongoDBConnectionURI(driver.connCfg)// For MongoDB query, we execute the statement in mongosh with flag --eval for the following reasons:@@ -293,3 +290,12 @@ func getSimpleStatementResult(data []byte) (*v1pb.QueryResult, error) {func (driver *Driver) RunStatement(ctx context.Context, _ *sql.Conn, statement string) ([]*v1pb.QueryResult, error) {return driver.QueryConn(ctx, nil, statement, nil)}++func isMongoStatement(statement string) bool {+ if _, err := parser.ParseMongo(statement); err == nil {+ return true+ }+ statement = strings.TrimLeft(statement, " \n\t")+ statement = strings.ToLower(statement)+ return strings.HasPrefix(statement, "db.")+}diff --git a/backend/plugin/db/mongodb/mongodb_test.go b/backend/plugin/db/mongodb/mongodb_test.goindex d64bb9b43c..b15a723a45 100644--- a/backend/plugin/db/mongodb/mongodb_test.go+++ b/backend/plugin/db/mongodb/mongodb_test.go@@ -77,6 +77,34 @@ func TestGetMongoDBConnectionURL(t *testing.T) {}}+func TestIsMongoStatement(t *testing.T) {+ tests := []struct {+ statement string+ want bool+ }{+ {+ statement: `show collections`,+ want: false,+ },+ {+ statement: `db.cpl_station_info.find().limit(100)`,+ want: true,+ },+ {+ statement: `+ db.cpl_station_info.find().limit(100);+ `,+ want: true,+ },+ }++ a := require.New(t)+ for _, tt := range tests {+ got := isMongoStatement(tt.statement)+ a.Equal(tt.want, got, tt.statement)+ }+}+func TestGetSimpleStatementResult(t *testing.T) {v1 := `{"age": 13,