nodedb 0.0.0-beta.1

Local-first, real-time, edge-to-cloud hybrid database for multi-modal workloads
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
//! Scope management DDL commands.
//!
//! ```sql
//! DEFINE SCOPE 'profile:read' AS READ ON user_profiles, READ ON user_settings
//! DEFINE SCOPE 'customer' AS INCLUDE 'profile:read', INCLUDE 'orders:write'
//! DROP SCOPE 'profile:read'
//! GRANT SCOPE 'pro:all' TO ORG 'acme'
//! GRANT SCOPE 'profile:read' TO USER 'user_42'
//! REVOKE SCOPE 'pro:all' FROM ORG 'acme'
//! SHOW SCOPES
//! SHOW SCOPE 'profile:read'
//! SHOW MY SCOPES
//! SHOW SCOPES FOR USER 'user_42'
//! SHOW SCOPES FOR ORG 'acme'
//! ```

use std::sync::Arc;

use futures::stream;
use pgwire::api::results::{DataRowEncoder, QueryResponse, Response, Tag};
use pgwire::error::PgWireResult;

use crate::control::security::identity::AuthenticatedIdentity;
use crate::control::state::SharedState;

use super::super::types::{sqlstate_error, text_field};

/// DEFINE SCOPE '<name>' AS <perm> ON <coll> [, <perm> ON <coll>] [INCLUDE '<scope>']
pub fn define_scope(
    state: &SharedState,
    identity: &AuthenticatedIdentity,
    parts: &[&str],
) -> PgWireResult<Vec<Response>> {
    if !identity.is_superuser {
        return Err(sqlstate_error(
            "42501",
            "permission denied: requires superuser",
        ));
    }
    // DEFINE SCOPE '<name>' AS ...
    if parts.len() < 4 {
        return Err(sqlstate_error(
            "42601",
            "syntax: DEFINE SCOPE '<name>' AS <perm> ON <coll> [, ...] [INCLUDE '<scope>']",
        ));
    }

    let scope_name = parts[2].trim_matches('\'');
    // Everything after "AS" is the definition.
    let as_idx = parts
        .iter()
        .position(|p| p.to_uppercase() == "AS")
        .ok_or_else(|| sqlstate_error("42601", "missing AS keyword"))?;

    let def_parts = &parts[as_idx + 1..];

    let mut grants = Vec::new();
    let mut includes = Vec::new();

    let mut i = 0;
    while i < def_parts.len() {
        let token = def_parts[i].to_uppercase();
        match token.as_str() {
            "INCLUDE" => {
                if i + 1 < def_parts.len() {
                    let inc = def_parts[i + 1].trim_matches('\'').trim_end_matches(',');
                    includes.push(inc.to_string());
                    i += 2;
                } else {
                    return Err(sqlstate_error("42601", "INCLUDE requires a scope name"));
                }
            }
            "READ" | "WRITE" | "CREATE" | "DROP" | "ALTER" | "ADMIN" => {
                // <perm> ON <collection>
                if i + 2 < def_parts.len() && def_parts[i + 1].to_uppercase() == "ON" {
                    let coll = def_parts[i + 2].trim_matches('\'').trim_end_matches(',');
                    grants.push((token.to_lowercase(), coll.to_string()));
                    i += 3;
                } else {
                    return Err(sqlstate_error("42601", "expected <perm> ON <collection>"));
                }
            }
            _ => {
                // Skip commas and unknown tokens.
                i += 1;
            }
        }
    }

    state
        .scope_defs
        .define(scope_name, grants, includes, &identity.username)
        .map_err(|e| sqlstate_error("42601", &e.to_string()))?;

    state.audit_record(
        crate::control::security::audit::AuditEvent::AdminAction,
        Some(identity.tenant_id),
        &identity.username,
        &format!("defined scope '{scope_name}'"),
    );

    Ok(vec![Response::Execution(Tag::new("DEFINE SCOPE"))])
}

/// DROP SCOPE '<name>'
pub fn drop_scope(
    state: &SharedState,
    identity: &AuthenticatedIdentity,
    parts: &[&str],
) -> PgWireResult<Vec<Response>> {
    if !identity.is_superuser {
        return Err(sqlstate_error(
            "42501",
            "permission denied: requires superuser",
        ));
    }
    if parts.len() < 3 {
        return Err(sqlstate_error("42601", "syntax: DROP SCOPE '<name>'"));
    }
    let name = parts[2].trim_matches('\'');

    let found = state
        .scope_defs
        .drop_scope(name)
        .map_err(|e| sqlstate_error("XX000", &e.to_string()))?;
    if !found {
        return Err(sqlstate_error(
            "42704",
            &format!("scope '{name}' not found"),
        ));
    }

    state.audit_record(
        crate::control::security::audit::AuditEvent::AdminAction,
        Some(identity.tenant_id),
        &identity.username,
        &format!("dropped scope '{name}'"),
    );

    Ok(vec![Response::Execution(Tag::new("DROP SCOPE"))])
}

/// GRANT SCOPE '<scope>' TO <ORG|USER|ROLE> '<id>'
pub fn grant_scope(
    state: &SharedState,
    identity: &AuthenticatedIdentity,
    parts: &[&str],
) -> PgWireResult<Vec<Response>> {
    if !identity.is_superuser {
        return Err(sqlstate_error(
            "42501",
            "permission denied: requires superuser",
        ));
    }
    // GRANT SCOPE '<scope>' TO <type> '<id>'
    if parts.len() < 6 {
        return Err(sqlstate_error(
            "42601",
            "syntax: GRANT SCOPE '<scope>' TO <ORG|USER|ROLE> '<id>'",
        ));
    }
    let scope_name = parts[2].trim_matches('\'');
    let grantee_type = parts[4].to_lowercase();
    let grantee_id = parts[5].trim_matches('\'');

    if !matches!(grantee_type.as_str(), "org" | "user" | "role" | "team") {
        return Err(sqlstate_error(
            "42601",
            "grantee type must be ORG, USER, ROLE, or TEAM",
        ));
    }

    // Parse optional EXPIRES, GRACE PERIOD, ON EXPIRE clauses.
    let expires_at = parse_expires(parts);
    let grace_period_secs = parse_grace_period(parts);
    let on_expire_action = parse_on_expire(parts);

    state
        .scope_grants
        .grant(
            scope_name,
            &grantee_type,
            grantee_id,
            &identity.username,
            expires_at,
            grace_period_secs,
            &on_expire_action,
        )
        .map_err(|e| sqlstate_error("XX000", &e.to_string()))?;

    state.audit_record(
        crate::control::security::audit::AuditEvent::AdminAction,
        Some(identity.tenant_id),
        &identity.username,
        &format!("granted scope '{scope_name}' to {grantee_type} '{grantee_id}'"),
    );

    Ok(vec![Response::Execution(Tag::new("GRANT SCOPE"))])
}

/// REVOKE SCOPE '<scope>' FROM <ORG|USER|ROLE> '<id>'
pub fn revoke_scope(
    state: &SharedState,
    identity: &AuthenticatedIdentity,
    parts: &[&str],
) -> PgWireResult<Vec<Response>> {
    if !identity.is_superuser {
        return Err(sqlstate_error(
            "42501",
            "permission denied: requires superuser",
        ));
    }
    if parts.len() < 6 {
        return Err(sqlstate_error(
            "42601",
            "syntax: REVOKE SCOPE '<scope>' FROM <ORG|USER|ROLE> '<id>'",
        ));
    }
    let scope_name = parts[2].trim_matches('\'');
    let grantee_type = parts[4].to_lowercase();
    let grantee_id = parts[5].trim_matches('\'');

    state
        .scope_grants
        .revoke(scope_name, &grantee_type, grantee_id)
        .map_err(|e| sqlstate_error("XX000", &e.to_string()))?;

    state.audit_record(
        crate::control::security::audit::AuditEvent::AdminAction,
        Some(identity.tenant_id),
        &identity.username,
        &format!("revoked scope '{scope_name}' from {grantee_type} '{grantee_id}'"),
    );

    Ok(vec![Response::Execution(Tag::new("REVOKE SCOPE"))])
}

/// SHOW SCOPES / SHOW SCOPE '<name>' / SHOW SCOPES FOR <type> '<id>'
pub fn show_scopes(
    state: &SharedState,
    _identity: &AuthenticatedIdentity,
    parts: &[&str],
) -> PgWireResult<Vec<Response>> {
    // SHOW SCOPE '<name>' — resolve a single scope.
    if parts.len() >= 3 && parts[1].to_uppercase() == "SCOPE" && parts[2].to_uppercase() != "GRANTS"
    {
        let name = parts[2].trim_matches('\'');
        let resolved = state.scope_defs.resolve(name);
        let schema = Arc::new(vec![text_field("permission"), text_field("collection")]);
        let rows: Vec<_> = resolved
            .iter()
            .map(|(perm, coll)| {
                let mut enc = DataRowEncoder::new(schema.clone());
                let _ = enc.encode_field(perm);
                let _ = enc.encode_field(coll);
                Ok(enc.take_row())
            })
            .collect();
        return Ok(vec![Response::Query(QueryResponse::new(
            schema,
            stream::iter(rows),
        ))]);
    }

    // SHOW SCOPES — list all scope definitions.
    let scopes = state.scope_defs.list();
    let schema = Arc::new(vec![
        text_field("name"),
        text_field("grants"),
        text_field("includes"),
        text_field("created_by"),
    ]);

    let rows: Vec<_> = scopes
        .iter()
        .map(|s| {
            let grants_str: Vec<String> = s
                .grants
                .iter()
                .map(|(p, c)| format!("{p} ON {c}"))
                .collect();
            let mut enc = DataRowEncoder::new(schema.clone());
            let _ = enc.encode_field(&s.name);
            let _ = enc.encode_field(&grants_str.join(", "));
            let _ = enc.encode_field(&s.includes.join(", "));
            let _ = enc.encode_field(&s.created_by);
            Ok(enc.take_row())
        })
        .collect();

    Ok(vec![Response::Query(QueryResponse::new(
        schema,
        stream::iter(rows),
    ))])
}

/// RENEW SCOPE '<scope>' FOR <ORG|USER> '<id>' EXTEND BY <duration>
pub fn renew_scope(
    state: &SharedState,
    identity: &AuthenticatedIdentity,
    parts: &[&str],
) -> PgWireResult<Vec<Response>> {
    if !identity.is_superuser {
        return Err(sqlstate_error(
            "42501",
            "permission denied: requires superuser",
        ));
    }
    // RENEW SCOPE '<scope>' FOR <type> '<id>' EXTEND BY <duration>
    if parts.len() < 8 {
        return Err(sqlstate_error(
            "42601",
            "syntax: RENEW SCOPE '<scope>' FOR <ORG|USER> '<id>' EXTEND BY <duration>",
        ));
    }
    let scope_name = parts[2].trim_matches('\'');
    let grantee_type = parts[4].to_lowercase();
    let grantee_id = parts[5].trim_matches('\'');
    let duration_str = parts[7];
    let extend_secs =
        crate::control::server::pgwire::ddl::auth_user_ddl::parse_duration_public(duration_str)
            .ok_or_else(|| {
                sqlstate_error("42601", &format!("invalid duration: '{duration_str}'"))
            })?;

    let found = state
        .scope_grants
        .renew(scope_name, &grantee_type, grantee_id, extend_secs)
        .map_err(|e| sqlstate_error("XX000", &e.to_string()))?;
    if !found {
        return Err(sqlstate_error("42704", "scope grant not found"));
    }

    state.audit_record(
        crate::control::security::audit::AuditEvent::AdminAction,
        Some(identity.tenant_id),
        &identity.username,
        &format!(
            "renewed scope '{scope_name}' for {grantee_type} '{grantee_id}' by {duration_str}"
        ),
    );

    Ok(vec![Response::Execution(Tag::new("RENEW SCOPE"))])
}

/// SHOW SCOPE GRANTS [EXPIRING WITHIN <duration>]
pub fn show_scope_grants(
    state: &SharedState,
    _identity: &AuthenticatedIdentity,
    parts: &[&str],
) -> PgWireResult<Vec<Response>> {
    let grants = if let Some(within_idx) = parts.iter().position(|p| p.to_uppercase() == "WITHIN") {
        let dur_str = parts.get(within_idx + 1).unwrap_or(&"7d");
        let secs =
            crate::control::server::pgwire::ddl::auth_user_ddl::parse_duration_public(dur_str)
                .unwrap_or(7 * 86_400);
        state.scope_grants.expiring_within(secs)
    } else {
        state.scope_grants.list(None)
    };

    let schema = Arc::new(vec![
        text_field("scope"),
        text_field("grantee_type"),
        text_field("grantee_id"),
        text_field("status"),
        text_field("expires_at"),
        text_field("granted_by"),
    ]);

    let rows: Vec<_> = grants
        .iter()
        .map(|g| {
            let mut enc = DataRowEncoder::new(schema.clone());
            let _ = enc.encode_field(&g.scope_name);
            let _ = enc.encode_field(&g.grantee_type);
            let _ = enc.encode_field(&g.grantee_id);
            let _ = enc.encode_field(&g.status().to_string());
            let _ = enc.encode_field(&if g.expires_at == 0 {
                "permanent".to_string()
            } else {
                g.expires_at.to_string()
            });
            let _ = enc.encode_field(&g.granted_by);
            Ok(enc.take_row())
        })
        .collect();

    Ok(vec![Response::Query(QueryResponse::new(
        schema,
        stream::iter(rows),
    ))])
}

// ── Parse helpers for time-bound GRANT SCOPE syntax ────────────────

/// Parse EXPIRES '<timestamp>' from parts. Returns 0 if not present.
fn parse_expires(parts: &[&str]) -> u64 {
    parts
        .iter()
        .position(|p| p.to_uppercase() == "EXPIRES")
        .and_then(|i| parts.get(i + 1))
        .and_then(|s| s.trim_matches('\'').parse::<u64>().ok())
        .unwrap_or(0)
}

/// Parse GRACE PERIOD <duration> from parts. Returns 0 if not present.
fn parse_grace_period(parts: &[&str]) -> u64 {
    parts
        .iter()
        .position(|p| p.to_uppercase() == "GRACE")
        .and_then(|i| {
            // GRACE PERIOD <duration>
            if parts.get(i + 1).map(|s| s.to_uppercase()) == Some("PERIOD".into()) {
                parts.get(i + 2)
            } else {
                None
            }
        })
        .and_then(|s| crate::control::server::pgwire::ddl::auth_user_ddl::parse_duration_public(s))
        .unwrap_or(0)
}

/// Parse ON EXPIRE action from parts.
fn parse_on_expire(parts: &[&str]) -> String {
    let idx = parts.iter().position(|p| p.to_uppercase() == "EXPIRE");
    let Some(i) = idx else {
        return String::new();
    };
    // Check previous token is "ON".
    if i == 0 || parts[i - 1].to_uppercase() != "ON" {
        return String::new();
    }
    // ON EXPIRE GRANT SCOPE '<name>' → "grant:<name>"
    // ON EXPIRE REVOKE ALL → "revoke_all"
    let action = parts
        .get(i + 1)
        .map(|s| s.to_uppercase())
        .unwrap_or_default();
    match action.as_str() {
        "GRANT" => {
            // ON EXPIRE GRANT SCOPE '<name>'
            let scope = parts.get(i + 3).unwrap_or(&"");
            format!("grant:{}", scope.trim_matches('\''))
        }
        "REVOKE" => "revoke_all".into(),
        _ => String::new(),
    }
}