Security roles least privilege

From CarlosCaPe/octorato · View interactive version

Security roles least privilege is an AI agent skill from the GitHub repository CarlosCaPe/octorato. Security Roles & Least-Privilege Grants

How to use this skill

Skill file

Security Roles & Least-Privilege Grants

Source: PostgreSQL Best Practices

-- "Security essentials", "Roles & grants"

What

Implementing role-based access control with three standard roles

(app_owner, app_user, app_readonly) and schema-level GRANT/REVOKE

to enforce least-privilege access. This ensures applications and users

can only perform the operations they need.

Why

By default, PostgreSQL grants generous permissions:

Without role separation:

The Best Practices mandate three roles:

| Role | Purpose | Permissions |

|------|---------|-------------|

| app_owner | Schema management, migrations | ALL on schema + objects |

| app_user | Application runtime | SELECT, INSERT, UPDATE, DELETE on tables |

| app_readonly | Dashboards, reporting | SELECT only |

How

Create the three standard roles

-- Roles are cluster-wide (not per-database)
CREATE ROLE app_owner NOLOGIN;
CREATE ROLE app_user NOLOGIN;
CREATE ROLE app_readonly NOLOGIN;

-- Grant hierarchy: app_owner > app_user > app_readonly
GRANT app_readonly TO app_user;
GRANT app_user TO app_owner;

Grant schema-level permissions

-- Revoke default PUBLIC access
REVOKE CREATE ON SCHEMA public FROM PUBLIC;

-- app_owner: full control
GRANT ALL ON SCHEMA public TO app_owner;
GRANT ALL ON ALL TABLES IN SCHEMA public TO app_owner;
GRANT ALL ON ALL SEQUENCES IN SCHEMA public TO app_owner;
GRANT ALL ON ALL FUNCTIONS IN SCHEMA public TO app_owner;

-- app_user: DML only
GRANT USAGE ON SCHEMA public TO app_user;
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO app_user;
GRANT USAGE ON ALL SEQUENCES IN SCHEMA public TO app_user;

-- app_readonly: read only
GRANT USAGE ON SCHEMA public TO app_readonly;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO app_readonly;

Set default privileges for future objects

-- When app_owner creates new tables, app_user and app_readonly
-- automatically get the right permissions
ALTER DEFAULT PRIVILEGES FOR ROLE app_owner IN SCHEMA public
    GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO app_user;

ALTER DEFAULT PRIVILEGES FOR ROLE app_owner IN SCHEMA public
    GRANT SELECT ON TABLES TO app_readonly;

ALTER DEFAULT PRIVILEGES FOR ROLE app_owner IN SCHEMA public
    GRANT USAGE ON SEQUENCES TO app_user;

Create login users assigned to roles

-- Migration runner (uses app_owner)
CREATE USER migration_user WITH PASSWORD '***' IN ROLE app_owner;

-- Application service account (uses app_user)
CREATE USER appservice_user WITH PASSWORD '***' IN ROLE app_user;

-- Reporting/dashboard account (uses app_readonly)
CREATE USER report_user WITH PASSWORD '***' IN ROLE app_readonly;

Audit current permissions

-- Check table-level grants
SELECT
    grantee,
    table_schema,
    table_name,
    privilege_type
FROM information_schema.table_privileges
WHERE table_schema = 'public'
ORDER BY grantee, table_name, privilege_type;
-- Check schema-level grants
SELECT
    nspname AS schema,
    pg_catalog.has_schema_privilege(r.rolname, n.nspname, 'CREATE') AS can_create,
    pg_catalog.has_schema_privilege(r.rolname, n.nspname, 'USAGE')  AS can_use
FROM pg_namespace n
CROSS JOIN pg_roles r
WHERE nspname = 'public'
  AND r.rolname IN ('app_owner', 'app_user', 'app_readonly')
ORDER BY r.rolname;

Revoke dangerous defaults

-- Prevent PUBLIC from creating objects in public schema
REVOKE CREATE ON SCHEMA public FROM PUBLIC;

-- Prevent PUBLIC from connecting to sensitive databases (optional)
REVOKE CONNECT ON DATABASE "myapp" FROM PUBLIC;
GRANT CONNECT ON DATABASE "myapp" TO app_owner, app_user, app_readonly;

Schema Isolation Pattern

For databases with multiple schemas (e.g., operations, staging):

-- Create isolated schema
CREATE SCHEMA operations AUTHORIZATION app_owner;

-- Grant per-schema access
GRANT USAGE ON SCHEMA operations TO app_user;
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA operations TO app_user;

-- Readonly sees public but not operations
GRANT USAGE ON SCHEMA public TO app_readonly;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO app_readonly;
-- (no grant on operations schema for app_readonly)

Azure Flexible Server Notes

azure_ad_admin configuration

When to Use

Where We Applied It

Related Skills

References

-- "Security essentials" and "Roles & grants" sections

-- Permission audit findings

Gotchas

permissions on tables within the schema (those are separate)

command runs -- existing tables need explicit GRANT

psqladmin creates a table, app_owner's default privileges do not apply

previous per-schema GRANT -- it cannot revoke global defaults

(PG 16 docs: sql-alterdefaultprivileges.html)

on a table does NOT extend to sequences used by that table. Always grant

USAGE ON ALL SEQUENCES explicitly for roles that INSERT

(PG 16 docs: sql-grant.html)

create objects in public -- test after revoking

for administrative tasks

you want explicit SET ROLE activation

extreme caution during role cleanup


Category: Safety | Origin: Best Practices

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.