Skip to content

Commit 58da503

Browse files
committed
feat: support context_to_roles in Python configuration
1 parent ce68576 commit 58da503

File tree

5 files changed

+72
-51
lines changed

5 files changed

+72
-51
lines changed

packages/cubejs-backend-native/js/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ export interface PyConfiguration {
354354
checkAuth?: (req: unknown, authorization: string) => Promise<{ 'security_context'?: unknown }>
355355
queryRewrite?: (query: unknown, ctx: unknown) => Promise<unknown>
356356
contextToApiScopes?: () => Promise<string[]>
357+
contextToRoles?: (ctx: unknown) => Promise<string[]>
357358
}
358359

359360
function simplifyExpressRequest(req: ExpressRequest) {

packages/cubejs-backend-native/python/cube/src/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,9 @@ class Configuration:
7474
repository_factory: Callable
7575
schema_version: Union[str, Callable[[RequestContext], str]]
7676
semantic_layer_sync: Union[Dict, Callable[[], Dict]]
77-
pre_aggregations_schema: Union[Callable[[RequestContext], str]]
77+
pre_aggregations_schema: Union[Callable[[RequestContext], str], str]
7878
orchestrator_options: Union[Dict, Callable[[RequestContext], Dict]]
79+
context_to_roles: Callable[[RequestContext], list[str]]
7980

8081
def __init__(self):
8182
self.web_sockets = None
@@ -123,6 +124,7 @@ def __init__(self):
123124
self.semantic_layer_sync = None
124125
self.pre_aggregations_schema = None
125126
self.orchestrator_options = None
127+
self.context_to_roles = None
126128

127129
def __call__(self, func):
128130
if isinstance(func, str):

packages/cubejs-backend-native/src/python/cube_config.rs

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,51 +17,52 @@ impl CubeConfigPy {
1717

1818
pub fn get_attrs(&self) -> Vec<&'static str> {
1919
vec![
20-
"web_sockets",
21-
"http",
22-
"graceful_shutdown",
23-
"process_subscriptions_interval",
24-
"web_sockets_base_path",
25-
"schema_path",
26-
"base_path",
27-
"dev_server",
20+
"allow_js_duplicate_props_in_schema",
2821
"api_secret",
22+
"base_path",
2923
"cache_and_queue_driver",
30-
"allow_js_duplicate_props_in_schema",
24+
"compiler_cache_size",
25+
"dev_server",
26+
"graceful_shutdown",
27+
"http",
3128
"jwt",
29+
"live_preview",
30+
"max_compiler_cache_keep_alive",
31+
"pg_sql_port",
32+
"process_subscriptions_interval",
33+
"scheduled_refresh_batch_size",
34+
"scheduled_refresh_concurrency",
3235
"scheduled_refresh_timer",
3336
"scheduled_refresh_timezones",
34-
"scheduled_refresh_concurrency",
35-
"scheduled_refresh_batch_size",
36-
"compiler_cache_size",
37-
"update_compiler_cache_keep_alive",
38-
"max_compiler_cache_keep_alive",
39-
"telemetry",
37+
"schema_path",
4038
"sql_cache",
41-
"live_preview",
42-
"pg_sql_port",
39+
"sql_password",
4340
"sql_super_user",
4441
"sql_user",
45-
"sql_password",
42+
"telemetry",
43+
"update_compiler_cache_keep_alive",
44+
"web_sockets",
45+
"web_sockets_base_path",
4646
// functions
47-
"logger",
47+
"can_switch_sql_user",
48+
"check_auth",
49+
"check_sql_auth",
50+
"context_to_api_scopes",
4851
"context_to_app_id",
4952
"context_to_orchestrator_id",
53+
"context_to_roles",
54+
"db_type",
5055
"driver_factory",
56+
"extend_context",
5157
"external_driver_factory",
52-
"db_type",
53-
"check_auth",
54-
"check_sql_auth",
55-
"can_switch_sql_user",
58+
"logger",
59+
"orchestrator_options",
60+
"pre_aggregations_schema",
5661
"query_rewrite",
57-
"extend_context",
58-
"scheduled_refresh_contexts",
59-
"context_to_api_scopes",
6062
"repository_factory",
61-
"semantic_layer_sync",
63+
"scheduled_refresh_contexts",
6264
"schema_version",
63-
"pre_aggregations_schema",
64-
"orchestrator_options",
65+
"semantic_layer_sync",
6566
]
6667
}
6768

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,61 @@
1-
from cube import (
2-
config,
3-
file_repository
4-
)
1+
from cube import config, file_repository
52

63
config.schema_path = "models"
74
config.pg_sql_port = 5555
85
config.telemetry = False
96

7+
108
@config
119
def query_rewrite(query, ctx):
12-
print('[python] query_rewrite query=', query, ' ctx=', ctx)
10+
print("[python] query_rewrite query=", query, " ctx=", ctx)
1311
return query
1412

13+
1514
@config
1615
async def check_auth(req, authorization):
17-
print('[python] check_auth req=', req, ' authorization=', authorization)
16+
print("[python] check_auth req=", req, " authorization=", authorization)
1817
return {
19-
'security_context': {
20-
'sub': '1234567890',
21-
'iat': 1516239022,
22-
'user_id': 42
23-
},
24-
'ignoredField': 'should not be visible'
18+
"security_context": {"sub": "1234567890", "iat": 1516239022, "user_id": 42},
19+
"ignoredField": "should not be visible",
2520
}
2621

22+
2723
@config
2824
async def repository_factory(ctx):
29-
print('[python] repository_factory ctx=', ctx)
25+
print("[python] repository_factory ctx=", ctx)
26+
27+
return file_repository(ctx["securityContext"]["schemaPath"])
3028

31-
return file_repository(ctx['securityContext']['schemaPath'])
3229

3330
@config
3431
async def context_to_api_scopes():
35-
print('[python] context_to_api_scopes')
36-
return ['meta', 'data', 'jobs']
32+
print("[python] context_to_api_scopes")
33+
return ["meta", "data", "jobs"]
34+
3735

3836
@config
3937
def schema_version(ctx):
40-
print('[python] schema_version', ctx)
38+
print("[python] schema_version", ctx)
39+
40+
return "1"
4141

42-
return '1'
4342

4443
@config
4544
def pre_aggregations_schema(ctx):
46-
print('[python] pre_aggregations_schema', ctx)
45+
print("[python] pre_aggregations_schema", ctx)
46+
47+
return "schema"
4748

48-
return 'schema'
4949

5050
@config
5151
def logger(msg, params):
52-
print('[python] logger msg', msg, 'params=', params)
52+
print("[python] logger msg", msg, "params=", params)
53+
54+
55+
@config
56+
def context_to_roles(ctx):
57+
print("[python] context_to_roles", ctx)
58+
59+
return [
60+
"admin",
61+
]

packages/cubejs-backend-native/test/python.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ suite('Python Config', () => {
6666
});
6767
});
6868

69+
test('context_to_roles', async () => {
70+
if (!config.contextToRoles) {
71+
throw new Error('contextToRoles was not defined in config.py');
72+
}
73+
74+
expect(await config.contextToRoles({})).toEqual(['admin']);
75+
});
76+
6977
test('context_to_api_scopes', async () => {
7078
if (!config.contextToApiScopes) {
7179
throw new Error('contextToApiScopes was not defined in config.py');

0 commit comments

Comments
 (0)