Skip to main content

redis: Interacting with a Redis database

The redis built-in function allow you to interact with a Redis database.

info

Check out our tutorial on querying Redis.

redis.query

The redis.query function allows you to make a query against a Redis database.

ParameterTypeRequiredDefaultDescription
addrStringYesAddress to connect to Redis at.
dbIntNo0Redis database to use.
authObjectNoSee Auth
cacheBoolNofalseCache the results of queries.
cache_durationIntegerNo60Duration (in seconds) to keep cached query results.
raise_errorBoolNotrueSee Errors.
commandStringYesRedis command to execute. This field is case-insensitive.
argsArrayYesArguments to pass to the Redis command.

Note that only the following Redis commands are supported. Using a Redis command not in the list below as the value for the command field will cause Enterprise OPA to exit with an error.

Example usage

redis.query({
"addr": "localhost:6379",
"auth": {
"password": "letmein1!",
},
"command": "get",
"args": ["some string key"]
}) # => { "results": "<value associated with 'some string key'>" }

Auth

ParameterTypeRequiredDefaultDescription
auth.usernameStringNo(empty string)Stores the username to connect to the Redis server with.
auth.passwordStringNo(empty string)Stores the password of the Redis server to connect to. If the database does not require a password, an empty string should may be used, or the field may be omitted entirely.

Errors

By default—and if raise_error is true—then an error returned will halt policy evaluation.

If raise_error is false, then the response object contains the error in an error key instead of its usual response.

{
"error": ...
}

Utility helpers

Enterprise OPA comes with a helper method for using this builtin together with vault.send: redis.query.

This method is available in Enterprise OPA at data.system.eopa.utils.redis.v1.vault.

package example
import data.system.eopa.utils.redis.v1.vault as redis

example_1 := redis.query({"addr": " ... ", "command": " ... ", "args": [ ... ]})
# => {"results": [ ... ]}

The utility method will lookup connection data from a map it expects to find in Vault, under the path secret/redis:

{
"username": "...",
"password": "...",
}

See Auth for more information.

To override the secret path within Vault, use:

package example
import data.system.eopa.utils.redis.v1.vault as redis

redis_query(req) := result {
result := redis.query(req)
with redis.override.secret_path as "secret/prod/eopa-redis"

example_2 := redis_query({"addr": " ... ", "command": " ... ", "args": [ ... ]})
) # => {"results": ... }

If you need to override the Vault address or token, you can use this:

package example
import data.system.eopa.utils.vault.v1.env as vault
import data.system.eopa.utils.redis.v1.vault as redis

redis_query(req) := result {
result := redis.query(req)
with redis.override.secret_path as "secret/prod/eopa-redis"
with vault.override.address as "localhost"
with vault.override.token as "dev-token-2"
}

example_3 := redis_query({"addr": " ... ", "command": " ... ", "args": [ ... ]})
) # => {"results": [ ... ]}