Expand description
The Redis key schema (spec section 08, interop): every key pattern, every value format, and the ban-key migration, byte-compatible with the Go and Python family ports.
A Rust deployment pointed at the same Redis as a Python guard-core or a
Go port MUST interoperate byte-for-byte: shared bans, shared counters,
shared caches. This module is the pure half - the key builders and the
value codecs; the wire half lives in the facade’s redis_store (the
redis feature), which drives these shapes over a real connection.
§Key namespace
Every key is prefix + namespace + ":" + key (plain concatenation, no
normalization; the namespace itself may carry : and so may the inner
key). The default prefix is guard_core:. sha256hex(x) is the
lowercase hex SHA-256 of the UTF-8 encoding (identity_hash).
| Pattern | Type | Builder |
|---|---|---|
{P}rate_limit:rate:{ip} | zset | crate::distributed::rate_window_key |
{P}rate_limit:rate:{ip}:{sha256hex(path)} | zset | crate::distributed::rate_window_key_endpoint |
{P}banned_ips:{ip} | string | crate::distributed::ban_key |
{P}banned_networks:{cidr} | string | ban_network_key |
{P}behavior_usage:behavior:usage:{sha256hex(endpoint)}:{sha256hex(ip)} | zset | behavior_usage_key |
{P}behavior_returns:behavior:return:{sha256hex(endpoint)}:{sha256hex(ip)}:{sha256hex(pattern)} | zset | behavior_return_key |
{P}security_headers:csp_config / hsts_config / custom_headers | string (JSON) | security_headers_config_key |
{P}patterns:custom | string | patterns_custom_key |
{P}ipinfo:database | string | ipinfo_database_key |
{P}cloud_ranges_v2:{provider} | string | cloud_ranges_key |
{P}cloud_ip_v2:{provider} | string (JSON) | cloud_ip_key |
{P}dynamic_rules:last_known | string | dynamic_rules_last_known_key |
The behavior keys’ double segment (behavior_usage:behavior:usage:) is
a verbatim reference oddity: namespace plus an in-key literal prefix.
Do not clean it up.
§Value formats
banned_ips/banned_networks: the ban expiry as the GoFormatFloat(expiry, 'f', -1, 64)decimal string (format_expiry), read back with a plain float parse andnow <= expiry(the reference_check_redis_exact).- behavior zsets: member = 32 lowercase hex chars (
random_member), score = the float epoch; pruning is the exclusiveZREMRANGEBYSCORE key "-inf" "({window_start"bound (exclusive_prune_bound). patterns:custom: the custom patterns joined with,(encode_patterns_custom); restored by splitting on,(decode_patterns_custom).cloud_ranges_v2: comma-joined, lexically sorted"<network>"/"<network>|<region>"entries (encode_cloud_ranges/decode_cloud_ranges);<network>is the canonical masked CIDR string (canonical_network_string), an unparseable prefix decodes as a corrupt-cache error, not a skip.cloud_ip_v2: a JSON array of CIDR strings, sorted lexically (encode_cloud_ip_v2/decode_cloud_ip_v2); a non-list or undecodable payload is a cache miss.ipinfo:database: the fullMaxMindDBfile bytes carried as the latin-1 string of their byte values (latin1_to_wire/wire_to_latin1).security_headers:*:json.dumpsof the config object (encode_json_object).dynamic_rules:last_known: the snapshot payload stored verbatim, no TTL.
§Legacy ban-key migration
migrate_legacy_ban_keys is the versioning bridge for IP
canonicalization changes (the reference _ipban_migration.py, via the
Go port’s migrateLegacyBanKeys): SCAN the {P}banned_ips:* keys,
canonicalize each raw IP segment, skip keys already canonical, move the
longer expiry to the canonical key (SET ... PX old_pttl) when the
canonical key’s remaining TTL is shorter, and delete the legacy key.
Failures never fail startup: callers log and continue.
§Example
use guard_core_engine::redis_schema::*;
// The key grammar, byte-identical to the Python and Go ports.
assert_eq!(
ban_network_key("guard_core:", "10.9.9.9/8").expect("valid cidr"),
"guard_core:banned_networks:10.0.0.0/8"
);
assert_eq!(
patterns_custom_key("guard_core:"),
"guard_core:patterns:custom"
);
// The ban expiry value: the Go FormatFloat('f', -1) decimal string.
assert_eq!(format_expiry(1060.0), "1060");
// The cloud ranges round-trip, sorted and comma-joined.
let payload = encode_cloud_ranges(&[
(String::from("203.0.113.0/24"), Some("us-east")),
(String::from("198.51.100.0/24"), None),
]);
assert_eq!(payload, "198.51.100.0/24,203.0.113.0/24|us-east");Structs§
- Cloud
Range Decode Error - A corrupt
cloud_ranges_v2payload.
Constants§
- CLOUD_
IP_ NAMESPACE - The cloud IP store protocol namespace (
cloud_ip_v2:{provider}). - CLOUD_
RANGES_ NAMESPACE - The cloud ranges cache namespace (
cloud_ranges_v2:{provider}). - CLOUD_
RANGES_ TTL_ DEFAULT_ SECONDS - The cloud ranges cache TTL default (the reference
ttlparam). - CSP_
CONFIG_ KEY - The CSP config key segment (
security_headers:csp_config). - CUSTOM_
HEADERS_ KEY - The custom-headers key segment (
security_headers:custom_headers). - DEFAULT_
REDIS_ PREFIX - The default key prefix (
SecurityConfig.redis_prefix). - DYNAMIC_
RULES_ NAMESPACE - The dynamic-rules namespace and last-known key
(
dynamic_rules:last_known, no TTL). - HSTS_
CONFIG_ KEY - The HSTS config key segment (
security_headers:hsts_config). - IPINFO_
DATABASE_ KEY - The
ipinfodatabase key segment. - IPINFO_
DATABASE_ TTL_ DEFAULT_ SECONDS - The
GeoIPdatabase cache TTL default (the reference constructormax_age). - IPINFO_
NAMESPACE - The
GeoIPdatabase cache namespace and key (ipinfo:database). - LAST_
KNOWN_ RULES_ KEY - The last-known-rules key segment.
- PATTERNS_
CUSTOM_ KEY - The custom-patterns key segment.
- PATTERNS_
NAMESPACE - The custom-pattern registry namespace and key (
patterns:custom). - SECURITY_
HEADERS_ NAMESPACE - The security-headers config namespace.
- SECURITY_
HEADERS_ TTL_ SECONDS - The security-headers config TTL in seconds (the reference
_cache_configuration).
Traits§
- Redis
Admin Store - The admin surface the legacy ban-key migration needs (the reference
RedisAdmin, the sync seam pattern ofcrate::distributed).
Functions§
- ban_
network_ key - The
{prefix}banned_networks:{cidr}key: a CIDR ban, stored under the canonical network string (host bits cleared, the referencestr(ipaddress.ip_network(cidr, strict=False))). - behavior_
return_ key - The behavior return-patterns zset key.
- behavior_
usage_ key - The
{prefix}behavior_usage:behavior:usage:{sha256(endpoint)}:{sha256(ip)}zset key: the reference behavior usage rules. - canonical_
network_ string - The canonical network string of a bare IP or CIDR entry.
- canonicalize_
ip_ string - The string-level IP canonicalization the migration runs on each
scanned key segment (
_canonicalize_ip/ the GoCanonicalizeIP). - cloud_
ip_ key - The
{prefix}cloud_ip_v2:{provider}key: the cloud IP store protocol. - cloud_
ranges_ key - The
{prefix}cloud_ranges_v2:{provider}key: the cloud-range cache. - decode_
cloud_ ip_ v2 - Decode the
cloud_ip_v2payload:Some(ranges)for a JSON array of strings,Nonefor anything else. - decode_
cloud_ ranges - Decode the
cloud_ranges_v2payload into(networks, regions). - decode_
json_ object - Decode a JSON object payload into its string pairs (the
custom_headersload arm).Nonewhen the payload is not an object of strings. - decode_
patterns_ custom - Split the registry payload back into patterns (the reference
cached_patterns.split(",")). - dynamic_
rules_ last_ known_ key - The
{prefix}dynamic_rules:last_knownkey: the last-known dynamic rules snapshot (persisted, no TTL). - encode_
cloud_ ip_ v2 - The
cloud_ip_v2payload:json.dumps(sorted(ranges)). - encode_
cloud_ ranges - The
cloud_ranges_v2payload: comma-joined, lexically sorted entries. - encode_
json_ object - A JSON object payload (
json.dumpsof a name -> value mapping), thesecurity_headers:*config value shape. The pairs are written in the given order (Python dict insertion order). - encode_
patterns_ custom - The custom-pattern registry payload: the patterns joined with
,(the reference",".join(custom_patterns)). - exclusive_
prune_ bound - The behavior window’s prune bound:
"(x", the exclusive lower bound. - format_
expiry - The ban expiry string, the Go port’s
FormatFloat(expiry, 'f', -1, 64)decimal form. - full_
key {prefix}{namespace}:{key}: the reference namespaced helper’s plain concatenation.- ipinfo_
database_ key - The
{prefix}ipinfo:databasekey: theGeoIPDB cache. - latin1_
to_ wire - The
ipinfo:databasewire encoding: theMaxMindDBfile bytes read as the latin-1 string of their byte values. - migrate_
legacy_ ban_ keys - The legacy ban-key migration (the reference
_ipban_migration.py, via the Go port’smigrateLegacyBanKeys). - migrate_
one_ ban_ key - One key’s migration pass.
Ok(false)when the key was already canonical (untouched),Ok(true)when it was migrated or dropped. - patterns_
custom_ key - The
{prefix}patterns:customkey: the custom-pattern registry. - random_
member - A fresh zset member: 32 lowercase hex chars, 16 random bytes hex-encoded.
- security_
headers_ config_ key - The
{prefix}security_headers:{segment}config key for one ofCSP_CONFIG_KEY,HSTS_CONFIG_KEY,CUSTOM_HEADERS_KEY. - wire_
to_ latin1 - Recover the binary
GeoIPdatabase from the cached string.