@prosopo/user-access-policy 3.5.32 → 3.7.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build$colon$cjs.log +23 -21
- package/.turbo/turbo-build$colon$tsc.log +41 -0
- package/.turbo/turbo-build.log +28 -22
- package/CHANGELOG.md +393 -0
- package/dist/.export.d.ts +6 -0
- package/dist/.export.d.ts.map +1 -0
- package/dist/.export.js.map +1 -0
- package/dist/api/.export.d.ts +7 -0
- package/dist/api/.export.d.ts.map +1 -0
- package/dist/api/.export.js.map +1 -0
- package/dist/api/accessRulesApiClient.d.ts +2 -0
- package/dist/api/accessRulesApiClient.d.ts.map +1 -0
- package/dist/api/accessRulesApiClient.js +2 -0
- package/dist/api/accessRulesApiClient.js.map +1 -0
- package/dist/api/delete/.export.d.ts +2 -0
- package/dist/api/delete/.export.d.ts.map +1 -0
- package/dist/api/delete/.export.js.map +1 -0
- package/dist/api/delete/deleteAllRules.d.ts +11 -0
- package/dist/api/delete/deleteAllRules.d.ts.map +1 -0
- package/dist/api/delete/deleteAllRules.js +3 -2
- package/dist/api/delete/deleteAllRules.js.map +1 -0
- package/dist/api/delete/deleteRuleGroups.d.ts +19 -0
- package/dist/api/delete/deleteRuleGroups.d.ts.map +1 -0
- package/dist/api/delete/deleteRuleGroups.js +3 -2
- package/dist/api/delete/deleteRuleGroups.js.map +1 -0
- package/dist/api/delete/deleteRules.d.ts +15 -0
- package/dist/api/delete/deleteRules.d.ts.map +1 -0
- package/dist/api/delete/deleteRules.js +3 -2
- package/dist/api/delete/deleteRules.js.map +1 -0
- package/dist/api/read/.export.d.ts +4 -0
- package/dist/api/read/.export.d.ts.map +1 -0
- package/dist/api/read/.export.js.map +1 -0
- package/dist/api/read/fetchRules.d.ts +53 -0
- package/dist/api/read/fetchRules.d.ts.map +1 -0
- package/dist/api/read/fetchRules.js +4 -3
- package/dist/api/read/fetchRules.js.map +1 -0
- package/dist/api/read/findRuleIds.d.ts +28 -0
- package/dist/api/read/findRuleIds.d.ts.map +1 -0
- package/dist/api/read/findRuleIds.js +3 -2
- package/dist/api/read/findRuleIds.js.map +1 -0
- package/dist/api/read/getMissingIds.d.ts +28 -0
- package/dist/api/read/getMissingIds.d.ts.map +1 -0
- package/dist/api/read/getMissingIds.js +4 -3
- package/dist/api/read/getMissingIds.js.map +1 -0
- package/dist/api/ruleApiRoutes.d.ts +43 -0
- package/dist/api/ruleApiRoutes.d.ts.map +1 -0
- package/dist/api/ruleApiRoutes.js.map +1 -0
- package/dist/api/rulesApiClient.d.ts +20 -0
- package/dist/api/rulesApiClient.d.ts.map +1 -0
- package/dist/api/rulesApiClient.js +18 -19
- package/dist/api/rulesApiClient.js.map +1 -0
- package/dist/api/write/.export.d.ts +2 -0
- package/dist/api/write/.export.d.ts.map +1 -0
- package/dist/api/write/.export.js.map +1 -0
- package/dist/api/write/insertRules.d.ts +29 -0
- package/dist/api/write/insertRules.d.ts.map +1 -0
- package/dist/api/write/insertRules.js +12 -9
- package/dist/api/write/insertRules.js.map +1 -0
- package/dist/api/write/rehashRules.d.ts +11 -0
- package/dist/api/write/rehashRules.d.ts.map +1 -0
- package/dist/api/write/rehashRules.js +7 -6
- package/dist/api/write/rehashRules.js.map +1 -0
- package/dist/cjs/api/delete/deleteAllRules.cjs +3 -2
- package/dist/cjs/api/delete/deleteRuleGroups.cjs +3 -2
- package/dist/cjs/api/delete/deleteRules.cjs +3 -2
- package/dist/cjs/api/read/fetchRules.cjs +4 -3
- package/dist/cjs/api/read/findRuleIds.cjs +3 -2
- package/dist/cjs/api/read/getMissingIds.cjs +4 -3
- package/dist/cjs/api/rulesApiClient.cjs +18 -19
- package/dist/cjs/api/write/insertRules.cjs +13 -10
- package/dist/cjs/api/write/rehashRules.cjs +7 -6
- package/dist/cjs/mongoose/mongooseRuleSchema.cjs +4 -1
- package/dist/cjs/redis/reader/redisRulesQuery.cjs +18 -1
- package/dist/cjs/redis/reader/redisRulesReader.cjs +13 -4
- package/dist/cjs/redis/redisRuleIndex.cjs +5 -1
- package/dist/cjs/redis/redisRulesWriter.cjs +6 -0
- package/dist/cjs/ruleInput/policyInput.cjs +8 -0
- package/dist/cjs/ruleInput/userScopeInput.cjs +4 -1
- package/dist/cjs/ruleRecord.cjs +4 -1
- package/dist/mongoose/.export.d.ts +2 -0
- package/dist/mongoose/.export.d.ts.map +1 -0
- package/dist/mongoose/.export.js.map +1 -0
- package/dist/mongoose/mongooseRuleSchema.d.ts +4 -0
- package/dist/mongoose/mongooseRuleSchema.d.ts.map +1 -0
- package/dist/mongoose/mongooseRuleSchema.js +4 -1
- package/dist/mongoose/mongooseRuleSchema.js.map +1 -0
- package/dist/redis/.export.d.ts +3 -0
- package/dist/redis/.export.d.ts.map +1 -0
- package/dist/redis/.export.js.map +1 -0
- package/dist/redis/reader/redisAggregate.d.ts +4 -0
- package/dist/redis/reader/redisAggregate.d.ts.map +1 -0
- package/dist/redis/reader/redisAggregate.js.map +1 -0
- package/dist/redis/reader/redisRulesQuery.d.ts +4 -0
- package/dist/redis/reader/redisRulesQuery.d.ts.map +1 -0
- package/dist/redis/reader/redisRulesQuery.js +18 -1
- package/dist/redis/reader/redisRulesQuery.js.map +1 -0
- package/dist/redis/reader/redisRulesReader.d.ts +26 -0
- package/dist/redis/reader/redisRulesReader.d.ts.map +1 -0
- package/dist/redis/reader/redisRulesReader.js +14 -5
- package/dist/redis/reader/redisRulesReader.js.map +1 -0
- package/dist/redis/redisClient.d.ts +11 -0
- package/dist/redis/redisClient.d.ts.map +1 -0
- package/dist/redis/redisClient.js.map +1 -0
- package/dist/redis/redisRuleIndex.d.ts +13 -0
- package/dist/redis/redisRuleIndex.d.ts.map +1 -0
- package/dist/redis/redisRuleIndex.js +5 -1
- package/dist/redis/redisRuleIndex.js.map +1 -0
- package/dist/redis/redisRulesStorage.d.ts +5 -0
- package/dist/redis/redisRulesStorage.d.ts.map +1 -0
- package/dist/redis/redisRulesStorage.js.map +1 -0
- package/dist/redis/redisRulesWriter.d.ts +22 -0
- package/dist/redis/redisRulesWriter.d.ts.map +1 -0
- package/dist/redis/redisRulesWriter.js +6 -0
- package/dist/redis/redisRulesWriter.js.map +1 -0
- package/dist/rule.d.ts +37 -0
- package/dist/rule.d.ts.map +1 -0
- package/dist/rule.js.map +1 -0
- package/dist/ruleInput/.export.d.ts +4 -0
- package/dist/ruleInput/.export.d.ts.map +1 -0
- package/dist/ruleInput/.export.js.map +1 -0
- package/dist/ruleInput/policyInput.d.ts +39 -0
- package/dist/ruleInput/policyInput.d.ts.map +1 -0
- package/dist/ruleInput/policyInput.js +9 -1
- package/dist/ruleInput/policyInput.js.map +1 -0
- package/dist/ruleInput/ruleInput.d.ts +163 -0
- package/dist/ruleInput/ruleInput.d.ts.map +1 -0
- package/dist/ruleInput/ruleInput.js.map +1 -0
- package/dist/ruleInput/userScopeInput.d.ts +117 -0
- package/dist/ruleInput/userScopeInput.d.ts.map +1 -0
- package/dist/ruleInput/userScopeInput.js +4 -1
- package/dist/ruleInput/userScopeInput.js.map +1 -0
- package/dist/ruleRecord.d.ts +18 -0
- package/dist/ruleRecord.d.ts.map +1 -0
- package/dist/ruleRecord.js +4 -1
- package/dist/ruleRecord.js.map +1 -0
- package/dist/rulesStorage.d.ts +30 -0
- package/dist/rulesStorage.d.ts.map +1 -0
- package/dist/rulesStorage.js.map +1 -0
- package/dist/tests/insertRulesEndpoint.unit.test.d.ts +2 -0
- package/dist/tests/insertRulesEndpoint.unit.test.d.ts.map +1 -0
- package/dist/tests/insertRulesEndpoint.unit.test.js +57 -0
- package/dist/tests/insertRulesEndpoint.unit.test.js.map +1 -0
- package/dist/tests/policyInput.unit.test.d.ts +2 -0
- package/dist/tests/policyInput.unit.test.d.ts.map +1 -0
- package/dist/tests/policyInput.unit.test.js +116 -0
- package/dist/tests/policyInput.unit.test.js.map +1 -0
- package/dist/tests/redis/reader/redisRulesQuery.unit.test.d.ts +2 -0
- package/dist/tests/redis/reader/redisRulesQuery.unit.test.d.ts.map +1 -0
- package/dist/tests/redis/reader/redisRulesQuery.unit.test.js +199 -0
- package/dist/tests/redis/reader/redisRulesQuery.unit.test.js.map +1 -0
- package/dist/tests/redis/redisRulesStorage.integration.test.d.ts +2 -0
- package/dist/tests/redis/redisRulesStorage.integration.test.d.ts.map +1 -0
- package/dist/tests/redis/redisRulesStorage.integration.test.js +831 -0
- package/dist/tests/redis/redisRulesStorage.integration.test.js.map +1 -0
- package/dist/tests/testLogger.d.ts +4 -0
- package/dist/tests/testLogger.d.ts.map +1 -0
- package/dist/tests/testLogger.js +22 -0
- package/dist/tests/testLogger.js.map +1 -0
- package/dist/tests/transformRule.unit.test.d.ts +2 -0
- package/dist/tests/transformRule.unit.test.d.ts.map +1 -0
- package/dist/tests/transformRule.unit.test.js +191 -0
- package/dist/tests/transformRule.unit.test.js.map +1 -0
- package/dist/transformRule.d.ts +7 -0
- package/dist/transformRule.d.ts.map +1 -0
- package/dist/transformRule.js.map +1 -0
- package/entries.ts +1 -1
- package/package.json +18 -12
- package/src/.export.ts +44 -0
- package/src/api/.export.ts +25 -0
- package/src/api/accessRulesApiClient.ts +13 -0
- package/src/api/delete/.export.ts +18 -0
- package/src/api/delete/deleteAllRules.ts +47 -0
- package/src/api/delete/deleteRuleGroups.ts +96 -0
- package/src/api/delete/deleteRules.ts +81 -0
- package/src/api/read/.export.ts +25 -0
- package/src/api/read/fetchRules.ts +88 -0
- package/src/api/read/findRuleIds.ts +95 -0
- package/src/api/read/getMissingIds.ts +81 -0
- package/src/api/ruleApiRoutes.ts +146 -0
- package/src/api/rulesApiClient.ts +154 -0
- package/src/api/write/.export.ts +15 -0
- package/src/api/write/insertRules.ts +183 -0
- package/src/api/write/rehashRules.ts +85 -0
- package/src/mongoose/.export.ts +15 -0
- package/src/mongoose/mongooseRuleSchema.ts +65 -0
- package/src/redis/.export.ts +17 -0
- package/src/redis/reader/redisAggregate.ts +103 -0
- package/src/redis/reader/redisRulesQuery.ts +217 -0
- package/src/redis/reader/redisRulesReader.ts +318 -0
- package/src/redis/redisClient.ts +120 -0
- package/src/redis/redisRuleIndex.ts +85 -0
- package/src/redis/redisRulesStorage.ts +68 -0
- package/src/redis/redisRulesWriter.ts +158 -0
- package/src/rule.ts +59 -0
- package/src/ruleInput/.export.ts +19 -0
- package/src/ruleInput/policyInput.ts +51 -0
- package/src/ruleInput/ruleInput.ts +103 -0
- package/src/ruleInput/userScopeInput.ts +108 -0
- package/src/ruleRecord.ts +69 -0
- package/src/rulesStorage.ts +72 -0
- package/src/tests/insertRulesEndpoint.unit.test.ts +89 -0
- package/src/tests/policyInput.unit.test.ts +150 -0
- package/src/tests/redis/reader/redisRulesQuery.unit.test.ts +284 -0
- package/src/tests/redis/redisRulesStorage.integration.test.ts +1156 -0
- package/src/tests/testLogger.ts +38 -0
- package/src/tests/transformRule.unit.test.ts +255 -0
- package/src/transformRule.ts +128 -0
- package/tsconfig.cjs.json +41 -0
- package/tsconfig.json +47 -0
- package/tsconfig.tsbuildinfo +1 -0
- package/tsconfig.types.json +9 -0
- package/vite.cjs.config.ts +1 -1
- package/vite.esm.config.ts +1 -1
- package/vite.test.config.ts +1 -1
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
// Copyright 2021-2026 Prosopo (UK) Ltd.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
|
|
15
|
+
import * as util from "node:util";
|
|
16
|
+
import { chunkIntoBatches, executeBatchesSequentially } from "@prosopo/common";
|
|
17
|
+
import type { Logger } from "@prosopo/logger";
|
|
18
|
+
import type { RedisClientType } from "redis";
|
|
19
|
+
import {
|
|
20
|
+
REDIS_QUERY_DIALECT,
|
|
21
|
+
getRulesRedisQuery,
|
|
22
|
+
} from "#policy/redis/reader/redisRulesQuery.js";
|
|
23
|
+
import {
|
|
24
|
+
REDIS_BATCH_SIZE,
|
|
25
|
+
fetchRedisHashRecords,
|
|
26
|
+
getMissingRedisKeys,
|
|
27
|
+
parseRedisRecords,
|
|
28
|
+
} from "#policy/redis/redisClient.js";
|
|
29
|
+
import {
|
|
30
|
+
ACCESS_RULES_REDIS_INDEX_NAME,
|
|
31
|
+
ACCESS_RULE_REDIS_KEY_PREFIX,
|
|
32
|
+
} from "#policy/redis/redisRuleIndex.js";
|
|
33
|
+
import type { AccessRule } from "#policy/rule.js";
|
|
34
|
+
import { accessRuleInput } from "#policy/ruleInput/ruleInput.js";
|
|
35
|
+
import type {
|
|
36
|
+
AccessRuleEntry,
|
|
37
|
+
AccessRulesFilter,
|
|
38
|
+
AccessRulesReader,
|
|
39
|
+
} from "#policy/rulesStorage.js";
|
|
40
|
+
import { aggregateRedisKeys } from "./redisAggregate.js";
|
|
41
|
+
|
|
42
|
+
export class RedisRulesReader implements AccessRulesReader {
|
|
43
|
+
constructor(
|
|
44
|
+
private readonly client: RedisClientType,
|
|
45
|
+
private readonly logger: Logger,
|
|
46
|
+
) {}
|
|
47
|
+
|
|
48
|
+
async getMissingRuleIds(ruleIds: string[]): Promise<string[]> {
|
|
49
|
+
const ruleKeys = this.getRuleKeys(ruleIds);
|
|
50
|
+
const keyBatches = chunkIntoBatches(ruleKeys, REDIS_BATCH_SIZE);
|
|
51
|
+
|
|
52
|
+
const missingKeyBatches = await executeBatchesSequentially(
|
|
53
|
+
keyBatches,
|
|
54
|
+
async (keysBatch) => getMissingRedisKeys(this.client, keysBatch),
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
return missingKeyBatches
|
|
58
|
+
.flat()
|
|
59
|
+
.map((ruleKey) => ruleKey.slice(ACCESS_RULE_REDIS_KEY_PREFIX.length));
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async fetchRules(ruleIds: string[]): Promise<AccessRuleEntry[]> {
|
|
63
|
+
const ruleKeys = this.getRuleKeys(ruleIds);
|
|
64
|
+
|
|
65
|
+
const keyBatches = chunkIntoBatches(ruleKeys, REDIS_BATCH_SIZE);
|
|
66
|
+
|
|
67
|
+
const entryBatches = await executeBatchesSequentially(
|
|
68
|
+
keyBatches,
|
|
69
|
+
(keysBatch) => this.fetchRuleEntries(keysBatch),
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
return entryBatches.flat();
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
async findRules(
|
|
76
|
+
filter: AccessRulesFilter,
|
|
77
|
+
matchingFieldsOnly = false,
|
|
78
|
+
skipEmptyUserScopes = true,
|
|
79
|
+
): Promise<AccessRule[]> {
|
|
80
|
+
const query = getRulesRedisQuery(filter, matchingFieldsOnly);
|
|
81
|
+
|
|
82
|
+
if (skipEmptyUserScopes && query === "ismissing(@clientId)") {
|
|
83
|
+
// We don't want to accidentally return all rules when the filter is empty
|
|
84
|
+
return [];
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
try {
|
|
88
|
+
// Use searchNoContent to get document keys only, then fetch data separately.
|
|
89
|
+
// This avoids a bug in @redis/search where ft.search crashes with
|
|
90
|
+
// "Cannot read properties of null (reading 'length')" when a document
|
|
91
|
+
// is deleted/expired between the index scan and data retrieval.
|
|
92
|
+
const searchReply = await this.client.ft.searchNoContent(
|
|
93
|
+
ACCESS_RULES_REDIS_INDEX_NAME,
|
|
94
|
+
query,
|
|
95
|
+
{
|
|
96
|
+
DIALECT: REDIS_QUERY_DIALECT,
|
|
97
|
+
// FT.search doesn't support "unlimited" selects
|
|
98
|
+
LIMIT: {
|
|
99
|
+
from: 0,
|
|
100
|
+
size: REDIS_BATCH_SIZE,
|
|
101
|
+
},
|
|
102
|
+
},
|
|
103
|
+
);
|
|
104
|
+
|
|
105
|
+
if (searchReply.total > 0) {
|
|
106
|
+
this.logger.debug(() => ({
|
|
107
|
+
msg: "Executed search query",
|
|
108
|
+
data: {
|
|
109
|
+
inspect: util.inspect(
|
|
110
|
+
{
|
|
111
|
+
filter: filter,
|
|
112
|
+
searchReply: searchReply,
|
|
113
|
+
query: query,
|
|
114
|
+
},
|
|
115
|
+
{ depth: null },
|
|
116
|
+
),
|
|
117
|
+
},
|
|
118
|
+
}));
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (searchReply.documents.length === 0) {
|
|
122
|
+
return [];
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const { records } = await fetchRedisHashRecords(
|
|
126
|
+
this.client,
|
|
127
|
+
searchReply.documents,
|
|
128
|
+
this.logger,
|
|
129
|
+
);
|
|
130
|
+
|
|
131
|
+
const nonEmptyRecords = records.filter(
|
|
132
|
+
(record) => Object.keys(record).length > 0,
|
|
133
|
+
);
|
|
134
|
+
|
|
135
|
+
return parseRedisRecords(nonEmptyRecords, accessRuleInput, this.logger);
|
|
136
|
+
} catch (e) {
|
|
137
|
+
this.logger.error(() => ({
|
|
138
|
+
err: e,
|
|
139
|
+
data: {
|
|
140
|
+
inspect: util.inspect(
|
|
141
|
+
{
|
|
142
|
+
query: query,
|
|
143
|
+
filter: filter,
|
|
144
|
+
},
|
|
145
|
+
{
|
|
146
|
+
depth: null,
|
|
147
|
+
},
|
|
148
|
+
),
|
|
149
|
+
},
|
|
150
|
+
msg: "failed to execute search query",
|
|
151
|
+
}));
|
|
152
|
+
|
|
153
|
+
return [];
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
async findRuleIds(
|
|
158
|
+
filter: AccessRulesFilter,
|
|
159
|
+
matchingFieldsOnly = false,
|
|
160
|
+
): Promise<string[]> {
|
|
161
|
+
const query = getRulesRedisQuery(filter, matchingFieldsOnly);
|
|
162
|
+
|
|
163
|
+
let ruleIds: string[] = [];
|
|
164
|
+
|
|
165
|
+
try {
|
|
166
|
+
// aggregation is used instead ft.search to overcome the limitation on search results number
|
|
167
|
+
const ruleKeys = await aggregateRedisKeys(
|
|
168
|
+
this.client,
|
|
169
|
+
query,
|
|
170
|
+
this.logger,
|
|
171
|
+
);
|
|
172
|
+
|
|
173
|
+
ruleIds = ruleKeys.map((ruleKey) =>
|
|
174
|
+
ruleKey.slice(ACCESS_RULE_REDIS_KEY_PREFIX.length),
|
|
175
|
+
);
|
|
176
|
+
} catch (e) {
|
|
177
|
+
this.logger.error(() => ({
|
|
178
|
+
err: e,
|
|
179
|
+
data: {
|
|
180
|
+
inspect: util.inspect(
|
|
181
|
+
{
|
|
182
|
+
query: query,
|
|
183
|
+
filter: filter,
|
|
184
|
+
},
|
|
185
|
+
{
|
|
186
|
+
depth: null,
|
|
187
|
+
},
|
|
188
|
+
),
|
|
189
|
+
},
|
|
190
|
+
msg: "Failed to execute search query for rule IDs",
|
|
191
|
+
}));
|
|
192
|
+
|
|
193
|
+
return [];
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
this.logger.debug(() => ({
|
|
197
|
+
msg: "Executed search query for rule IDs",
|
|
198
|
+
data: {
|
|
199
|
+
query: query,
|
|
200
|
+
foundCount: ruleIds.length,
|
|
201
|
+
foundIds: ruleIds,
|
|
202
|
+
},
|
|
203
|
+
}));
|
|
204
|
+
|
|
205
|
+
return ruleIds;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
async fetchAllRuleIds(
|
|
209
|
+
batchHandler: (ruleIds: string[]) => Promise<void>,
|
|
210
|
+
): Promise<void> {
|
|
211
|
+
const keysBatchHandler = async (keys: string[]) => {
|
|
212
|
+
const ids = keys.map((ruleKey) =>
|
|
213
|
+
ruleKey.slice(ACCESS_RULE_REDIS_KEY_PREFIX.length),
|
|
214
|
+
);
|
|
215
|
+
|
|
216
|
+
await batchHandler(ids);
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
await aggregateRedisKeys(this.client, "*", this.logger, keysBatchHandler);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
protected async fetchRuleEntries(keys: string[]): Promise<AccessRuleEntry[]> {
|
|
223
|
+
const { records, expirations } = await fetchRedisHashRecords(
|
|
224
|
+
this.client,
|
|
225
|
+
keys,
|
|
226
|
+
this.logger,
|
|
227
|
+
);
|
|
228
|
+
const entries: AccessRuleEntry[] = [];
|
|
229
|
+
|
|
230
|
+
for (const [index, ruleData] of records.entries()) {
|
|
231
|
+
const isRulePresent = Object.keys(ruleData).length > 0;
|
|
232
|
+
|
|
233
|
+
if (isRulePresent) {
|
|
234
|
+
const rule = parseRedisRecords(
|
|
235
|
+
[ruleData],
|
|
236
|
+
accessRuleInput,
|
|
237
|
+
this.logger,
|
|
238
|
+
)[0];
|
|
239
|
+
|
|
240
|
+
if (rule) {
|
|
241
|
+
entries.push({
|
|
242
|
+
rule: rule,
|
|
243
|
+
expiresUnixTimestamp: expirations[index],
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
return entries;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
protected getRuleKeys(ruleIds: string[]): string[] {
|
|
253
|
+
return ruleIds.map((id) => `${ACCESS_RULE_REDIS_KEY_PREFIX}${id}`);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
export class DummyRedisRulesReader implements AccessRulesReader {
|
|
258
|
+
constructor(private readonly logger: Logger) {}
|
|
259
|
+
|
|
260
|
+
async getMissingRuleIds(ruleIds: string[]): Promise<string[]> {
|
|
261
|
+
this.logger.info(() => ({
|
|
262
|
+
msg: "Dummy getMissingRuleIds() has no effect (redis is not ready)",
|
|
263
|
+
data: {
|
|
264
|
+
ruleIds,
|
|
265
|
+
},
|
|
266
|
+
}));
|
|
267
|
+
|
|
268
|
+
return [];
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
async fetchRules(ruleIds: string[]): Promise<AccessRuleEntry[]> {
|
|
272
|
+
this.logger.info(() => ({
|
|
273
|
+
msg: "Dummy fetchRule() has no effect (redis is not ready)",
|
|
274
|
+
data: {
|
|
275
|
+
ruleIds,
|
|
276
|
+
},
|
|
277
|
+
}));
|
|
278
|
+
|
|
279
|
+
return [];
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
async findRules(
|
|
283
|
+
filter: AccessRulesFilter,
|
|
284
|
+
matchingFieldsOnly = false,
|
|
285
|
+
skipEmptyUserScopes = true,
|
|
286
|
+
): Promise<AccessRule[]> {
|
|
287
|
+
this.logger.info(() => ({
|
|
288
|
+
msg: "Dummy findRules() has no effect (redis is not ready)",
|
|
289
|
+
data: {
|
|
290
|
+
filter,
|
|
291
|
+
},
|
|
292
|
+
}));
|
|
293
|
+
|
|
294
|
+
return [];
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
async findRuleIds(
|
|
298
|
+
filter: AccessRulesFilter,
|
|
299
|
+
matchingFieldsOnly = false,
|
|
300
|
+
): Promise<string[]> {
|
|
301
|
+
this.logger.info(() => ({
|
|
302
|
+
msg: "Dummy findRuleIds() has no effect (redis is not ready)",
|
|
303
|
+
data: {
|
|
304
|
+
filter,
|
|
305
|
+
},
|
|
306
|
+
}));
|
|
307
|
+
|
|
308
|
+
return [];
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
async fetchAllRuleIds(
|
|
312
|
+
batchHandler: (ruleIds: string[]) => Promise<void>,
|
|
313
|
+
): Promise<void> {
|
|
314
|
+
this.logger.info(() => ({
|
|
315
|
+
msg: "Dummy fetchAllRuleIds() has no effect (redis is not ready)",
|
|
316
|
+
}));
|
|
317
|
+
}
|
|
318
|
+
}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
// Copyright 2021-2026 Prosopo (UK) Ltd.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
|
|
15
|
+
import type { Logger } from "@prosopo/logger";
|
|
16
|
+
import type { RedisClientType } from "redis";
|
|
17
|
+
import { type ZodType, z } from "zod";
|
|
18
|
+
|
|
19
|
+
export const REDIS_BATCH_SIZE = 1_000;
|
|
20
|
+
|
|
21
|
+
export const getMissingRedisKeys = async (
|
|
22
|
+
client: RedisClientType,
|
|
23
|
+
keys: string[],
|
|
24
|
+
): Promise<string[]> => {
|
|
25
|
+
const queries = client.multi();
|
|
26
|
+
|
|
27
|
+
keys.map((key) => {
|
|
28
|
+
queries.exists(key);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
const records: unknown[] = await queries.exec();
|
|
32
|
+
|
|
33
|
+
const missingKeys: string[] = [];
|
|
34
|
+
|
|
35
|
+
records.map((exists, recordIndex) => {
|
|
36
|
+
if ("0" === String(exists)) {
|
|
37
|
+
const key = keys[recordIndex];
|
|
38
|
+
|
|
39
|
+
if (key) {
|
|
40
|
+
missingKeys.push(key);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
return missingKeys;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export const fetchRedisHashRecords = async (
|
|
49
|
+
client: RedisClientType,
|
|
50
|
+
keys: string[],
|
|
51
|
+
logger: Logger,
|
|
52
|
+
): Promise<{ records: object[]; expirations: (number | undefined)[] }> => {
|
|
53
|
+
const rulesPipe = client.multi();
|
|
54
|
+
const expirationPipe = client.multi();
|
|
55
|
+
|
|
56
|
+
for (const key of keys) {
|
|
57
|
+
rulesPipe.hGetAll(key);
|
|
58
|
+
expirationPipe.expireTime(key);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const records = (await rulesPipe.exec()) as object[];
|
|
62
|
+
const expirationRecords = (await expirationPipe.exec()) as unknown[];
|
|
63
|
+
|
|
64
|
+
return {
|
|
65
|
+
records: records,
|
|
66
|
+
expirations: parseExpirationRecords(expirationRecords, logger),
|
|
67
|
+
};
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
export const parseRedisRecords = <T>(
|
|
71
|
+
records: unknown[],
|
|
72
|
+
recordSchema: ZodType<T>,
|
|
73
|
+
logger: Logger,
|
|
74
|
+
): T[] =>
|
|
75
|
+
records.flatMap((record) => {
|
|
76
|
+
const parseResult = recordSchema.safeParse(record);
|
|
77
|
+
|
|
78
|
+
if (parseResult.success) {
|
|
79
|
+
return [parseResult.data];
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
logger.error(() => ({
|
|
83
|
+
msg: "Failed to parse Redis record",
|
|
84
|
+
data: { record, error: parseResult.error },
|
|
85
|
+
}));
|
|
86
|
+
|
|
87
|
+
return [];
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
const expirationRecordSchema = z.coerce.number();
|
|
91
|
+
// Redis returns -1 when expiration is not set
|
|
92
|
+
const UNSET_EXPIRATION_VALUE = -1;
|
|
93
|
+
|
|
94
|
+
const parseExpirationRecords = <T>(
|
|
95
|
+
records: unknown[],
|
|
96
|
+
logger: Logger,
|
|
97
|
+
): (number | undefined)[] =>
|
|
98
|
+
records.flatMap((record) => {
|
|
99
|
+
const parseResult = expirationRecordSchema.safeParse(record);
|
|
100
|
+
|
|
101
|
+
if (parseResult.success) {
|
|
102
|
+
const expiration =
|
|
103
|
+
UNSET_EXPIRATION_VALUE === parseResult.data
|
|
104
|
+
? undefined
|
|
105
|
+
: parseResult.data;
|
|
106
|
+
|
|
107
|
+
return [expiration];
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
logger.error(() => ({
|
|
111
|
+
msg: "Failed to parse Redis expiration record",
|
|
112
|
+
data: {
|
|
113
|
+
record,
|
|
114
|
+
error: parseResult.error,
|
|
115
|
+
},
|
|
116
|
+
}));
|
|
117
|
+
|
|
118
|
+
// ensure consistent output length
|
|
119
|
+
return [undefined];
|
|
120
|
+
});
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// Copyright 2021-2026 Prosopo (UK) Ltd.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
|
|
15
|
+
import type { AllKeys, Keys } from "@prosopo/common";
|
|
16
|
+
import type { RedisIndex } from "@prosopo/redis-client";
|
|
17
|
+
import { type RediSearchSchema, SCHEMA_FIELD_TYPE } from "@redis/search";
|
|
18
|
+
import type {
|
|
19
|
+
AccessRule,
|
|
20
|
+
PolicyScope,
|
|
21
|
+
UserAttributes,
|
|
22
|
+
UserIp,
|
|
23
|
+
UserScope,
|
|
24
|
+
} from "#policy/rule.js";
|
|
25
|
+
import { makeAccessRuleHash } from "#policy/transformRule.js";
|
|
26
|
+
|
|
27
|
+
export const userIpRedisSchema: RediSearchSchema = {
|
|
28
|
+
numericIpMaskMin: { type: SCHEMA_FIELD_TYPE.NUMERIC, INDEXMISSING: true },
|
|
29
|
+
numericIpMaskMax: { type: SCHEMA_FIELD_TYPE.NUMERIC, INDEXMISSING: true },
|
|
30
|
+
numericIp: { type: SCHEMA_FIELD_TYPE.NUMERIC, INDEXMISSING: true },
|
|
31
|
+
} satisfies AllKeys<UserIp>;
|
|
32
|
+
|
|
33
|
+
export const userAttributesRedisSchema: RediSearchSchema = {
|
|
34
|
+
userId: { type: SCHEMA_FIELD_TYPE.TAG, INDEXMISSING: true },
|
|
35
|
+
ja4Hash: { type: SCHEMA_FIELD_TYPE.TAG, INDEXMISSING: true },
|
|
36
|
+
headersHash: { type: SCHEMA_FIELD_TYPE.TAG, INDEXMISSING: true },
|
|
37
|
+
userAgentHash: { type: SCHEMA_FIELD_TYPE.TAG, INDEXMISSING: true },
|
|
38
|
+
headHash: { type: SCHEMA_FIELD_TYPE.TAG, INDEXMISSING: true },
|
|
39
|
+
// Use pipe separator for coords since JSON strings contain commas
|
|
40
|
+
coords: { type: SCHEMA_FIELD_TYPE.TAG, INDEXMISSING: true, SEPARATOR: "|" },
|
|
41
|
+
countryCode: { type: SCHEMA_FIELD_TYPE.TAG, INDEXMISSING: true },
|
|
42
|
+
} satisfies AllKeys<UserAttributes>;
|
|
43
|
+
|
|
44
|
+
export const userScopeRedisSchema: RediSearchSchema = {
|
|
45
|
+
...userAttributesRedisSchema,
|
|
46
|
+
...userIpRedisSchema,
|
|
47
|
+
} satisfies Keys<UserScope>;
|
|
48
|
+
|
|
49
|
+
export const policyScopeRedisSchema: RediSearchSchema = {
|
|
50
|
+
clientId: {
|
|
51
|
+
type: SCHEMA_FIELD_TYPE.TAG,
|
|
52
|
+
INDEXMISSING: true,
|
|
53
|
+
},
|
|
54
|
+
} satisfies AllKeys<PolicyScope>;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Note on the field type decision
|
|
58
|
+
*
|
|
59
|
+
* TAG is designed for the exact value matching
|
|
60
|
+
* TEXT is designed for the word-based and pattern matching
|
|
61
|
+
*
|
|
62
|
+
* For our goal TAG fits perfectly and, more performant
|
|
63
|
+
*/
|
|
64
|
+
export const accessRuleRedisSchema: RediSearchSchema = {
|
|
65
|
+
...policyScopeRedisSchema,
|
|
66
|
+
...userScopeRedisSchema,
|
|
67
|
+
groupId: { type: SCHEMA_FIELD_TYPE.TAG, INDEXMISSING: true },
|
|
68
|
+
} satisfies Keys<AccessRule>;
|
|
69
|
+
|
|
70
|
+
export const ACCESS_RULES_REDIS_INDEX_NAME = "index:user-access-rules";
|
|
71
|
+
|
|
72
|
+
// names take space, so we use an acronym instead of the long-tailed one
|
|
73
|
+
export const ACCESS_RULE_REDIS_KEY_PREFIX = "uar:";
|
|
74
|
+
|
|
75
|
+
export const accessRulesRedisIndex: RedisIndex = {
|
|
76
|
+
name: ACCESS_RULES_REDIS_INDEX_NAME,
|
|
77
|
+
schema: accessRuleRedisSchema,
|
|
78
|
+
options: {
|
|
79
|
+
ON: "HASH" as const,
|
|
80
|
+
PREFIX: [ACCESS_RULE_REDIS_KEY_PREFIX],
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
export const getAccessRuleRedisKey = (rule: AccessRule): string =>
|
|
85
|
+
ACCESS_RULE_REDIS_KEY_PREFIX + makeAccessRuleHash(rule);
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// Copyright 2021-2026 Prosopo (UK) Ltd.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
|
|
15
|
+
import type { Logger } from "@prosopo/logger";
|
|
16
|
+
import type { RedisConnection } from "@prosopo/redis-client";
|
|
17
|
+
import type {
|
|
18
|
+
AccessRulesReader,
|
|
19
|
+
AccessRulesStorage,
|
|
20
|
+
AccessRulesWriter,
|
|
21
|
+
} from "#policy/rulesStorage.js";
|
|
22
|
+
import {
|
|
23
|
+
DummyRedisRulesReader,
|
|
24
|
+
RedisRulesReader,
|
|
25
|
+
} from "./reader/redisRulesReader.js";
|
|
26
|
+
import { DummyRedisRulesWriter, RedisRulesWriter } from "./redisRulesWriter.js";
|
|
27
|
+
|
|
28
|
+
export const createRedisAccessRulesStorage = (
|
|
29
|
+
connection: RedisConnection,
|
|
30
|
+
logger: Logger,
|
|
31
|
+
): AccessRulesStorage => {
|
|
32
|
+
const storage: AccessRulesStorage = composeStorage(
|
|
33
|
+
new DummyRedisRulesReader(logger),
|
|
34
|
+
new DummyRedisRulesWriter(logger),
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
connection.getClient().then((client) => {
|
|
38
|
+
const realStorage = composeStorage(
|
|
39
|
+
new RedisRulesReader(client, logger),
|
|
40
|
+
new RedisRulesWriter(client, logger),
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
// use assigning instead of var overwriting to keep the object reference
|
|
44
|
+
Object.assign(storage, realStorage);
|
|
45
|
+
|
|
46
|
+
logger.info(() => ({
|
|
47
|
+
msg: "RedisAccessRules storage got a ready Redis client",
|
|
48
|
+
}));
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
return storage;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const composeStorage = (
|
|
55
|
+
reader: AccessRulesReader,
|
|
56
|
+
writer: AccessRulesWriter,
|
|
57
|
+
): AccessRulesStorage => ({
|
|
58
|
+
// reader
|
|
59
|
+
fetchRules: reader.fetchRules.bind(reader),
|
|
60
|
+
getMissingRuleIds: reader.getMissingRuleIds.bind(reader),
|
|
61
|
+
findRules: reader.findRules.bind(reader),
|
|
62
|
+
findRuleIds: reader.findRuleIds.bind(reader),
|
|
63
|
+
fetchAllRuleIds: reader.fetchAllRuleIds.bind(reader),
|
|
64
|
+
// writer
|
|
65
|
+
insertRules: writer.insertRules.bind(writer),
|
|
66
|
+
deleteRules: writer.deleteRules.bind(writer),
|
|
67
|
+
deleteAllRules: writer.deleteAllRules.bind(writer),
|
|
68
|
+
});
|