pgroles-inspect 0.9.0

Database introspection, version detection, and privilege checks for pgroles
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
450
451
452
//! Query PostgreSQL role attributes from `pg_roles` / `pg_shdescription`.

use sqlx::PgPool;

use pgroles_core::model::RoleState;

/// A row from our role-attributes query.
#[derive(Debug, sqlx::FromRow)]
pub struct RoleRow {
    pub rolname: String,
    pub rolsuper: bool,
    pub rolinherit: bool,
    pub rolcreaterole: bool,
    pub rolcreatedb: bool,
    pub rolcanlogin: bool,
    pub rolreplication: bool,
    pub rolbypassrls: bool,
    pub rolconnlimit: i32,
    /// Comment from pg_shdescription (NULL if none).
    pub comment: Option<String>,
    /// Password expiration from pg_roles.rolvaliduntil (NULL if no expiration).
    pub rolvaliduntil: Option<String>,
    /// Cluster-wide role config defaults from pg_roles.rolconfig, as
    /// `parameter=value` strings (NULL if none). Per-database settings
    /// (`ALTER ROLE ... IN DATABASE`) do not appear here.
    pub rolconfig: Option<Vec<String>>,
}

impl RoleRow {
    /// Convert to the core model's `RoleState`.
    pub fn to_role_state(&self) -> RoleState {
        RoleState {
            login: self.rolcanlogin,
            superuser: self.rolsuper,
            createdb: self.rolcreatedb,
            createrole: self.rolcreaterole,
            inherit: self.rolinherit,
            replication: self.rolreplication,
            bypassrls: self.rolbypassrls,
            connection_limit: self.rolconnlimit,
            comment: self.comment.clone(),
            password_valid_until: self.rolvaliduntil.clone(),
            config: self
                .rolconfig
                .as_deref()
                .unwrap_or_default()
                .iter()
                .filter_map(|entry| parse_rolconfig_entry(entry))
                .collect(),
        }
    }
}

/// Parse one `pg_roles.rolconfig` entry (`parameter=value`) into a
/// `(parameter, value)` pair.
///
/// PostgreSQL serializes `GUC_LIST_QUOTE` parameters (search_path, ...) with
/// each list element individually quoted as needed (e.g.
/// `search_path="$user", public`). Those values are canonicalized element-wise
/// so they compare equal to the manifest's desired value, which passes through
/// the same canonicalization. All other parameters are stored verbatim.
fn parse_rolconfig_entry(entry: &str) -> Option<(String, String)> {
    let (parameter, raw_value) = entry.split_once('=')?;
    let parameter = parameter.to_ascii_lowercase();
    let value = if pgroles_core::guc::is_list_quote_parameter(&parameter) {
        pgroles_core::guc::canonicalize_list_guc_value(raw_value)
    } else {
        raw_value.to_string()
    };
    Some((parameter, value))
}

/// Fetch all non-system roles from the database.
///
/// Excludes PostgreSQL internal roles (those starting with `pg_`) and the
/// `postgres` superuser, since we don't want to manage those.
///
/// The `managed_roles` parameter, if provided, filters to only those role names.
/// If `None`, returns all non-system roles.
pub async fn fetch_roles(
    pool: &PgPool,
    managed_roles: Option<&[&str]>,
) -> Result<Vec<RoleRow>, sqlx::Error> {
    // We use pg_roles (a view over pg_authid) because it doesn't require
    // superuser access. We LEFT JOIN pg_shdescription for role comments.
    //
    // pg_shdescription stores shared object comments. For roles, the
    // classoid is pg_authid's OID and objoid is the role's OID.
    match managed_roles {
        Some(names) => {
            sqlx::query_as::<_, RoleRow>(
                r#"
                SELECT
                    r.rolname,
                    r.rolsuper,
                    r.rolinherit,
                    r.rolcreaterole,
                    r.rolcreatedb,
                    r.rolcanlogin,
                    r.rolreplication,
                    r.rolbypassrls,
                    r.rolconnlimit,
                    d.description AS comment,
                    CASE WHEN r.rolvaliduntil IS NOT NULL
                         THEN to_char(r.rolvaliduntil AT TIME ZONE 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS"Z"')
                         ELSE NULL END AS rolvaliduntil,
                    r.rolconfig
                FROM pg_roles r
                LEFT JOIN pg_shdescription d
                    ON d.objoid = r.oid
                    AND d.classoid = 'pg_authid'::regclass
                WHERE r.rolname = ANY($1)
                ORDER BY r.rolname
                "#,
            )
            .bind(names)
            .fetch_all(pool)
            .await
        }
        None => {
            sqlx::query_as::<_, RoleRow>(
                r#"
                SELECT
                    r.rolname,
                    r.rolsuper,
                    r.rolinherit,
                    r.rolcreaterole,
                    r.rolcreatedb,
                    r.rolcanlogin,
                    r.rolreplication,
                    r.rolbypassrls,
                    r.rolconnlimit,
                    d.description AS comment,
                    CASE WHEN r.rolvaliduntil IS NOT NULL
                         THEN to_char(r.rolvaliduntil AT TIME ZONE 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS"Z"')
                         ELSE NULL END AS rolvaliduntil,
                    r.rolconfig
                FROM pg_roles r
                LEFT JOIN pg_shdescription d
                    ON d.objoid = r.oid
                    AND d.classoid = 'pg_authid'::regclass
                WHERE r.rolname NOT LIKE 'pg_%'
                    AND r.rolname <> 'postgres'
                ORDER BY r.rolname
                "#,
            )
            .fetch_all(pool)
            .await
        }
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn role_row_maps_to_role_state() {
        let row = RoleRow {
            rolname: "analytics".to_string(),
            rolsuper: false,
            rolinherit: false,
            rolcreaterole: true,
            rolcreatedb: false,
            rolcanlogin: true,
            rolreplication: false,
            rolbypassrls: true,
            rolconnlimit: 12,
            comment: Some("analytics login".to_string()),
            rolvaliduntil: Some("2026-12-31T00:00:00Z".to_string()),
            rolconfig: Some(vec![
                "role=combined".to_string(),
                "search_path=\"$user\", public".to_string(),
            ]),
        };

        let state = row.to_role_state();
        assert_eq!(
            state.config.get("role").map(String::as_str),
            Some("combined")
        );
        assert_eq!(
            state.config.get("search_path").map(String::as_str),
            Some("\"$user\", public")
        );
        assert!(state.login);
        assert!(!state.superuser);
        assert!(!state.inherit);
        assert!(state.createrole);
        assert!(!state.createdb);
        assert!(!state.replication);
        assert!(state.bypassrls);
        assert_eq!(state.connection_limit, 12);
        assert_eq!(state.comment.as_deref(), Some("analytics login"));
        assert_eq!(
            state.password_valid_until.as_deref(),
            Some("2026-12-31T00:00:00Z")
        );
    }

    fn with_runtime<T>(future: impl std::future::Future<Output = T>) -> T {
        tokio::runtime::Runtime::new()
            .expect("failed to create tokio runtime")
            .block_on(future)
    }

    fn database_url() -> String {
        std::env::var("DATABASE_URL").expect("DATABASE_URL must be set for live DB tests")
    }

    fn unique_name(prefix: &str) -> String {
        let nanos = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .expect("system clock before unix epoch")
            .as_nanos();
        format!("{prefix}_{nanos}")
    }

    fn execute_sql(sql: &str) {
        use sqlx::Executor;

        with_runtime(async {
            let pool = PgPool::connect(&database_url())
                .await
                .expect("failed to connect to live test database");
            pool.execute(sql)
                .await
                .expect("failed to execute setup SQL");
        });
    }

    struct TestDbCleanup {
        sql: String,
    }

    impl TestDbCleanup {
        fn new(sql: String) -> Self {
            Self { sql }
        }
    }

    impl Drop for TestDbCleanup {
        fn drop(&mut self) {
            execute_sql(&self.sql);
        }
    }

    #[test]
    fn parse_rolconfig_entry_splits_on_first_equals() {
        // GUC values may themselves contain '=' — only the first one is the
        // parameter/value separator. app.motd is not a list parameter, so
        // its value is kept verbatim.
        assert_eq!(
            parse_rolconfig_entry("app.motd=a=b"),
            Some(("app.motd".to_string(), "a=b".to_string()))
        );
        assert_eq!(parse_rolconfig_entry("no_separator"), None);
    }

    #[test]
    fn parse_rolconfig_entry_keeps_non_list_values_verbatim() {
        // Non-list parameters are stored verbatim by PostgreSQL — quotes are
        // part of the value, not serialization.
        assert_eq!(
            parse_rolconfig_entry(r#"app.motd="not unwrapped""#),
            Some(("app.motd".to_string(), r#""not unwrapped""#.to_string()))
        );
    }

    #[test]
    fn parse_rolconfig_entry_canonicalizes_list_parameters() {
        // PostgreSQL serializes GUC_LIST_QUOTE parameters with per-element
        // quoting; parsing canonicalizes so manifest values compare equal.
        assert_eq!(
            parse_rolconfig_entry(r#"search_path="$user", public"#),
            Some(("search_path".to_string(), r#""$user", public"#.to_string()))
        );
        // A single element containing a comma (the single-literal footgun)
        // stays one quoted element — distinct from a two-element list.
        assert_eq!(
            parse_rolconfig_entry(r#"search_path="app, public""#),
            Some(("search_path".to_string(), r#""app, public""#.to_string()))
        );
    }

    #[test]
    fn parse_rolconfig_entry_lowercases_parameter_names() {
        assert_eq!(
            parse_rolconfig_entry("Role=combined"),
            Some(("role".to_string(), "combined".to_string()))
        );
    }

    #[test]
    #[ignore]
    fn fetch_roles_reads_role_config_defaults() {
        let login = unique_name("config_login");
        let group = unique_name("config_group");
        let _cleanup = TestDbCleanup::new(format!(
            r#"
            DROP ROLE IF EXISTS "{login}";
            DROP ROLE IF EXISTS "{group}";
            "#
        ));

        // search_path is a GUC_LIST_QUOTE parameter: PostgreSQL stores each
        // element individually quoted as needed (`search_path="$user", public`),
        // which exercises the element-wise canonicalization.
        execute_sql(&format!(
            r#"
            DROP ROLE IF EXISTS "{login}";
            DROP ROLE IF EXISTS "{group}";
            CREATE ROLE "{group}" NOLOGIN;
            CREATE ROLE "{login}" LOGIN;
            GRANT "{group}" TO "{login}";
            ALTER ROLE "{login}" SET "role" = '{group}';
            ALTER ROLE "{login}" SET "search_path" = '$user', 'public';
            ALTER ROLE "{login}" SET "app.tenant" = 'acme';
            "#
        ));

        let roles = with_runtime(async {
            let pool = PgPool::connect(&database_url())
                .await
                .expect("failed to connect to live test database");
            fetch_roles(&pool, Some(&[login.as_str()]))
                .await
                .expect("failed to fetch scoped roles")
        });

        assert_eq!(roles.len(), 1);
        let config = &roles[0].to_role_state().config;
        assert_eq!(config.get("role").map(String::as_str), Some(group.as_str()));
        assert_eq!(
            config.get("search_path").map(String::as_str),
            Some("\"$user\", public")
        );
        assert_eq!(config.get("app.tenant").map(String::as_str), Some("acme"));
    }

    #[test]
    #[ignore]
    fn fetch_roles_ignores_per_database_config() {
        let login = unique_name("config_dbscoped");
        let _cleanup = TestDbCleanup::new(format!(r#"DROP ROLE IF EXISTS "{login}";"#));

        execute_sql(&format!(
            r#"
            DROP ROLE IF EXISTS "{login}";
            CREATE ROLE "{login}" LOGIN;
            "#
        ));

        let roles = with_runtime(async {
            use sqlx::Executor;

            let pool = PgPool::connect(&database_url())
                .await
                .expect("failed to connect to live test database");
            let (dbname,): (String,) = sqlx::query_as("SELECT current_database()")
                .fetch_one(&pool)
                .await
                .expect("failed to read current database name");
            pool.execute(
                format!(r#"ALTER ROLE "{login}" IN DATABASE "{dbname}" SET "work_mem" = '64MB';"#)
                    .as_str(),
            )
            .await
            .expect("failed to set per-database config");

            fetch_roles(&pool, Some(&[login.as_str()]))
                .await
                .expect("failed to fetch scoped roles")
        });

        assert_eq!(roles.len(), 1);
        assert!(
            roles[0].to_role_state().config.is_empty(),
            "per-database settings must not appear as managed cluster-wide config"
        );
    }

    #[test]
    #[ignore]
    fn fetch_roles_scopes_to_managed_names() {
        let managed = unique_name("managed_role");
        let extra = unique_name("extra_role");
        let _cleanup = TestDbCleanup::new(format!(
            r#"
            DROP ROLE IF EXISTS "{managed}";
            DROP ROLE IF EXISTS "{extra}";
            "#
        ));

        execute_sql(&format!(
            r#"
            DROP ROLE IF EXISTS "{managed}";
            DROP ROLE IF EXISTS "{extra}";
            CREATE ROLE "{managed}" LOGIN;
            CREATE ROLE "{extra}" NOLOGIN;
            "#
        ));

        let roles = with_runtime(async {
            let pool = PgPool::connect(&database_url())
                .await
                .expect("failed to connect to live test database");
            fetch_roles(&pool, Some(&[managed.as_str()]))
                .await
                .expect("failed to fetch scoped roles")
        });

        assert_eq!(roles.len(), 1);
        assert_eq!(roles[0].rolname, managed);
    }

    #[test]
    #[ignore]
    fn fetch_roles_unscoped_excludes_postgres_but_includes_user_roles() {
        let user_role = unique_name("role_inventory");
        let _cleanup = TestDbCleanup::new(format!(r#"DROP ROLE IF EXISTS "{user_role}";"#));

        execute_sql(&format!(
            r#"
            DROP ROLE IF EXISTS "{user_role}";
            CREATE ROLE "{user_role}" LOGIN;
            "#
        ));

        let roles = with_runtime(async {
            let pool = PgPool::connect(&database_url())
                .await
                .expect("failed to connect to live test database");
            fetch_roles(&pool, None)
                .await
                .expect("failed to fetch unscoped roles")
        });

        assert!(
            roles.iter().any(|row| row.rolname == user_role),
            "expected unscoped fetch to include the test user role"
        );
        assert!(
            roles.iter().all(|row| row.rolname != "postgres"),
            "expected unscoped fetch to exclude postgres"
        );
    }
}