Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: read command for redis and better error message (#15859)

rebelice3152d784c1da
diff --git a/backend/api/v1/error.go b/backend/api/v1/error.go
index dc75a64226..86a4f19d7a 100644
--- a/backend/api/v1/error.go
+++ b/backend/api/v1/error.go
@@ -18,4 +18,15 @@ var (
},
},
)
+ // nonReadOnlyCommandError returns an error indicating a non-read-only command error.
+ nonReadOnlyCommandError, _ = status.New(codes.InvalidArgument, "Support read-only command statements only").WithDetails(
+ &errdetails.BadRequest{
+ FieldViolations: []*errdetails.BadRequest_FieldViolation{
+ {
+ Field: "statement",
+ Description: "statement must be read-only command statement",
+ },
+ },
+ },
+ )
)
diff --git a/backend/api/v1/sql_service.go b/backend/api/v1/sql_service.go
index c96f940129..c09003803e 100644
--- a/backend/api/v1/sql_service.go
+++ b/backend/api/v1/sql_service.go
@@ -1405,7 +1405,12 @@ func validateQueryRequest(instance *store.InstanceMessage, statement string) err
return err
}
if !ok {
- return nonSelectSQLError.Err()
+ switch instance.Metadata.GetEngine() {
+ case storepb.Engine_REDIS, storepb.Engine_MONGODB:
+ return nonReadOnlyCommandError.Err()
+ default:
+ return nonSelectSQLError.Err()
+ }
}
return nil
}
diff --git a/backend/plugin/parser/redis/query.go b/backend/plugin/parser/redis/query.go
index 2115cdc0e6..7b69150279 100644
--- a/backend/plugin/parser/redis/query.go
+++ b/backend/plugin/parser/redis/query.go
@@ -11,36 +11,103 @@ import (
)
var readCommands = map[string]bool{
- "exists": true,
- "get": true,
- "hexists": true,
- "hget": true,
- "hgetall": true,
- "hkeys": true,
- "hmget": true,
- "hlen": true,
- "hvals": true,
- "keys": true,
- "lindex": true,
- "llen": true,
- "lrange": true,
- "pttl": true,
- "scan": true,
- "scard": true,
- "sdiff": true,
- "select": true,
- "sismember": true,
- "smembers": true,
- "sunion": true,
- "strlen": true,
- "ttl": true,
- "type": true,
- "zcard": true,
- "zcount": true,
- "zrange": true,
- "zrangebyscore": true,
- "zrank": true,
- "zscore": true,
+ "bitcount": true,
+ "bitfield_ro": true,
+ "bitpos": true,
+ "dbsize": true,
+ "dump": true,
+ "exists": true,
+ "expiretime": true,
+ "get": true,
+ "getrange": true,
+ "geodist": true,
+ "geohash": true,
+ "georadius_ro": true,
+ "georadiusbymember_ro": true,
+ "geopos": true,
+ "geosearch": true,
+ "getbit": true,
+ "hexists": true,
+ "hget": true,
+ "hgetall": true,
+ "hkeys": true,
+ "hmget": true,
+ "hlen": true,
+ "hrandfield": true,
+ "hscan": true,
+ "hvals": true,
+ "hstrlen": true,
+ "keys": true,
+ "lcs": true,
+ "lindex": true,
+ "llen": true,
+ "lrange": true,
+ "lolwut": true,
+ "lpos": true,
+ "mget": true,
+ "pfcount": true,
+ "pttl": true,
+ "pexpiretime": true,
+ "randomkey": true,
+ "scan": true,
+ "sscan": true,
+ "scard": true,
+ "sdiff": true,
+ "select": true,
+ "smismember": true,
+ "sismember": true,
+ "sinter": true,
+ "sintercard": true,
+ "smembers": true,
+ "sort_ro": true,
+ "srandmember": true,
+ "substr": true,
+ "sunion": true,
+ "strlen": true,
+ "ttl": true,
+ "touch": true,
+ "type": true,
+ "xpending": true,
+ "xrange": true,
+ "xread": true,
+ "xrevrange": true,
+ "xlen": true,
+ "zcard": true,
+ "zcount": true,
+ "zdiff": true,
+ "zinter": true,
+ "zintercard": true,
+ "zlexcount": true,
+ "zmscore": true,
+ "zrange": true,
+ "zrangebyscore": true,
+ "zrangebylex": true,
+ "zrandmember": true,
+ "zrank": true,
+ "zrevrank": true,
+ "zrevrange": true,
+ "zrevrangebylex": true,
+ "zrevrangebyscore": true,
+ "zscore": true,
+ "zscan": true,
+ "zunion": true,
+}
+
+var doubleNameReadCommands = map[string]map[string]bool{
+ "xinfo": {
+ "groups": true,
+ "stream": true,
+ "consumers": true,
+ },
+ "object": {
+ "freq": true,
+ "encoding": true,
+ "idletime": true,
+ "refcount": true,
+ },
+ "memory": {
+ "usage": true,
+ },
}
func init() {
@@ -57,10 +124,27 @@ func validateQuery(statement string) (bool, bool, error) {
if len(fields) == 0 {
continue
}
- command := strings.ToLower(fields[0])
- if !readCommands[command] {
+ if !isReadCommand(fields) {
return false, false, nil
}
}
return true, true, nil
}
+
+func isReadCommand(fields []string) bool {
+ if len(fields) == 0 {
+ return false
+ }
+ command := strings.ToLower(fields[0])
+ if _, ok := readCommands[command]; ok {
+ return true
+ }
+ if doubleNameReadCommands[command] != nil && len(fields) > 1 {
+ if d, ok := doubleNameReadCommands[command]; ok && d != nil {
+ if _, ok := d[strings.ToLower(fields[1])]; ok {
+ return true
+ }
+ }
+ }
+ return false
+}
diff --git a/backend/plugin/parser/redis/query_test.go b/backend/plugin/parser/redis/query_test.go
index b6a9ae7e08..8e5c08fec0 100644
--- a/backend/plugin/parser/redis/query_test.go
+++ b/backend/plugin/parser/redis/query_test.go
@@ -23,6 +23,11 @@ func TestValidateQuery(t *testing.T) {
valid: false,
allQuery: false,
},
+ {
+ statement: "memory usage hello",
+ valid: true,
+ allQuery: true,
+ },
}
for _, test := range tests {