Skip to content

Commit 6e83f2a

Browse files
authored
topdown: jwt cache (#7274)
Adding cache to `io.jwt` token verification built-ins Signed-off-by: Johan Fylling <johan.dev@fylling.se>
1 parent 211e95d commit 6e83f2a

File tree

10 files changed

+1319
-208
lines changed

10 files changed

+1319
-208
lines changed

docs/content/configuration.md

+7-6
Original file line numberDiff line numberDiff line change
@@ -864,12 +864,13 @@ It also represents the configuration of the inter-query _value_ cache that built
864864
this cache is utilized by the `regex` and `glob` built-in functions for compiled regex and glob match patterns
865865
respectively, and the `json.schema_match` built-in function for compiled JSON schemas.
866866

867-
| Field | Type | Required | Description |
868-
|--------------------------------------------------------------------------| --- | --- |-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
869-
| `caching.inter_query_builtin_cache.max_size_bytes` | `int64` | No | Inter-query cache size limit in bytes. OPA will drop old items from the cache if this limit is exceeded. By default, no limit is set. |
870-
| `caching.inter_query_builtin_cache.forced_eviction_threshold_percentage` | `int64` | No | Threshold limit configured as percentage of `caching.inter_query_builtin_cache.max_size_bytes`, when exceeded OPA will start dropping old items permaturely. By default, set to `100`. |
871-
| `caching.inter_query_builtin_cache.stale_entry_eviction_period_seconds` | `int64` | No | Stale entry eviction period in seconds. OPA will drop expired items from the cache every `stale_entry_eviction_period_seconds`. By default, set to `0` indicating stale entry eviction is disabled. |
872-
| `caching.inter_query_builtin_value_cache.max_num_entries` | `int` | No | Maximum number of entries in the Inter-query value cache. OPA will drop random items from the cache if this limit is exceeded. By default, set to `0` indicating unlimited size. |
867+
| Field | Type | Required | Description |
868+
|--------------------------------------------------------------------------|---------|----------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
869+
| `caching.inter_query_builtin_cache.max_size_bytes` | `int64` | No | Inter-query cache size limit in bytes. OPA will drop old items from the cache if this limit is exceeded. By default, no limit is set. |
870+
| `caching.inter_query_builtin_cache.forced_eviction_threshold_percentage` | `int64` | No | Threshold limit configured as percentage of `caching.inter_query_builtin_cache.max_size_bytes`, when exceeded OPA will start dropping old items permaturely. By default, set to `100`. |
871+
| `caching.inter_query_builtin_cache.stale_entry_eviction_period_seconds` | `int64` | No | Stale entry eviction period in seconds. OPA will drop expired items from the cache every `stale_entry_eviction_period_seconds`. By default, set to `0` indicating stale entry eviction is disabled. |
872+
| `caching.inter_query_builtin_value_cache.max_num_entries` | `int` | No | Maximum number of entries in the Inter-query value cache. OPA will drop random items from the cache if this limit is exceeded. By default, set to `0` indicating unlimited size. |
873+
| `caching.inter_query_builtin_value_cache.named.io_jwt.max_num_entries` | `int` | No | Maximum number of entries in the `io_jwt` cache, used by the [`io.jwt` token verification](../policy-reference/#tokens) built-in functions. OPA will drop random items from the cache if this limit is exceeded. By default, this cache is disabled. |
873874

874875
## Distributed tracing
875876

v1/plugins/discovery/discovery_test.go

+39-6
Original file line numberDiff line numberDiff line change
@@ -1717,7 +1717,14 @@ func TestReconfigureWithLocalOverride(t *testing.T) {
17171717
"decision_logs": {"console": true},
17181718
"nd_builtin_cache": false,
17191719
"distributed_tracing": {"type": "grpc"},
1720-
"caching": {"inter_query_builtin_cache": {"max_size_bytes": 10000000, "forced_eviction_threshold_percentage": 90}}
1720+
"caching": {
1721+
"inter_query_builtin_cache": {"max_size_bytes": 10000000, "forced_eviction_threshold_percentage": 90},
1722+
"inter_query_builtin_value_cache": {
1723+
"named": {
1724+
"io_jwt": {"max_num_entries": 55}
1725+
}
1726+
}
1727+
}
17211728
}`)
17221729

17231730
manager, err := plugins.New(bootConfigRaw, "test-id", inmem.New())
@@ -1877,7 +1884,14 @@ func TestReconfigureWithLocalOverride(t *testing.T) {
18771884
serviceBundle = makeDataBundle(7, `
18781885
{
18791886
"config": {
1880-
"caching": {"inter_query_builtin_cache": {"max_size_bytes": 200, "stale_entry_eviction_period_seconds": 10, "forced_eviction_threshold_percentage": 200}}
1887+
"caching": {
1888+
"inter_query_builtin_cache": {"max_size_bytes": 200, "stale_entry_eviction_period_seconds": 10, "forced_eviction_threshold_percentage": 200},
1889+
"inter_query_builtin_value_cache": {
1890+
"named": {
1891+
"io_jwt": {"max_num_entries": 10}
1892+
}
1893+
}
1894+
}
18811895
}
18821896
}
18831897
`)
@@ -1888,7 +1902,11 @@ func TestReconfigureWithLocalOverride(t *testing.T) {
18881902
t.Fatal("Expected to find status, found nil")
18891903
}
18901904

1891-
expectedOverriddenKeys := []string{"caching.inter_query_builtin_cache.max_size_bytes", "caching.inter_query_builtin_cache.forced_eviction_threshold_percentage"}
1905+
expectedOverriddenKeys := []string{
1906+
"caching.inter_query_builtin_cache.max_size_bytes",
1907+
"caching.inter_query_builtin_cache.forced_eviction_threshold_percentage",
1908+
"caching.inter_query_builtin_value_cache.named.io_jwt.max_num_entries",
1909+
}
18921910
for _, k := range expectedOverriddenKeys {
18931911
if !strings.Contains(disco.status.Message, k) {
18941912
t.Fatalf("expected key \"%v\" to be overridden", k)
@@ -1908,9 +1926,24 @@ func TestReconfigureWithLocalOverride(t *testing.T) {
19081926
*threshold = 90
19091927
maxNumEntriesInterQueryValueCache := new(int)
19101928
*maxNumEntriesInterQueryValueCache = 0
1911-
1912-
expectedCacheConf := &cache.Config{InterQueryBuiltinCache: cache.InterQueryBuiltinCacheConfig{MaxSizeBytes: maxSize, StaleEntryEvictionPeriodSeconds: period, ForcedEvictionThresholdPercentage: threshold},
1913-
InterQueryBuiltinValueCache: cache.InterQueryBuiltinValueCacheConfig{MaxNumEntries: maxNumEntriesInterQueryValueCache}}
1929+
maxNumEntriesJWTValueCache := new(int)
1930+
*maxNumEntriesJWTValueCache = 55
1931+
1932+
expectedCacheConf := &cache.Config{
1933+
InterQueryBuiltinCache: cache.InterQueryBuiltinCacheConfig{
1934+
MaxSizeBytes: maxSize,
1935+
StaleEntryEvictionPeriodSeconds: period,
1936+
ForcedEvictionThresholdPercentage: threshold,
1937+
},
1938+
InterQueryBuiltinValueCache: cache.InterQueryBuiltinValueCacheConfig{
1939+
MaxNumEntries: maxNumEntriesInterQueryValueCache,
1940+
NamedCacheConfigs: map[string]*cache.NamedValueCacheConfig{
1941+
"io_jwt": {
1942+
MaxNumEntries: maxNumEntriesJWTValueCache,
1943+
},
1944+
},
1945+
},
1946+
}
19141947

19151948
if !reflect.DeepEqual(cacheConf, expectedCacheConf) {
19161949
t.Fatalf("want %v got %v", expectedCacheConf, cacheConf)

0 commit comments

Comments
 (0)