Atlas now manages Oracle Virtual Private Database (VPD) policies, policy groups, driving contexts, and application contexts, and plans the DBMS_RLS calls and CREATE CONTEXT statements that create, change, disable, and drop them.
Atlas now manages Oracle Virtual Private Database (VPD): row-level security policies on tables, views, and synonyms, policy groups, driving contexts, and application contexts. Declare them in HCL and Atlas plans the DBMS_RLS calls and CREATE CONTEXT statements that create, change, disable, and drop them.
Enabling Policies
Policies are opt-in. Turn them on with policies in the mode "oracle" block of the schema:
env "vpd" { schema { mode "oracle" { policies = true } }}
Policies, Policy Groups, and Driving Contexts
Declare the policies of a table, its policy group, and its driving context (policy_context). The predicate stays in the policy function; Atlas manages only its binding to the table:
table "ORDERS" { schema = schema.APP column "ID" { null = false type = NUMBER } column "AMOUNT" { null = false type = NUMBER } column "SSN" { null = false type = NUMBER }}schema "APP" {}function "TENANT_PRED" { schema = schema.APP arg "SCHEMA_NAME" { type = VARCHAR2 } arg "OBJECT_NAME" { type = VARCHAR2 } return = VARCHAR2 as = "BEGIN RETURN '1=1'; END;"}policy "ORDERS_MASK" { on = table.ORDERS function = function.TENANT_PRED group = policy_group.SENSITIVE for = [SELECT] sec_relevant_columns = ["AMOUNT", "SSN"] sec_relevant_option = ALL_ROWS}policy "ORDERS_TENANT" { on = table.ORDERS function = function.TENANT_PRED for = [SELECT, UPDATE, DELETE] type = CONTEXT_SENSITIVE update_check = true}policy_group "SENSITIVE" { on = table.ORDERS}policy_context "APP_CTX_POLICY_GRP" { on = table.ORDERS namespace = "APP_CTX" attribute = "POLICY_GRP"}
Atlas plans:
-- Add new table named "ORDERS"CREATE TABLE "ORDERS" ( "ID" NUMBER NOT NULL, "AMOUNT" NUMBER NOT NULL, "SSN" NUMBER NOT NULL);-- Create policy group "SENSITIVE" on "ORDERS"BEGIN "SYS"."DBMS_RLS"."CREATE_POLICY_GROUP"( object_name => '"ORDERS"', policy_group => '"SENSITIVE"' );END;;-- Create "TENANT_PRED" functionCREATE FUNCTION "TENANT_PRED" ( "SCHEMA_NAME" VARCHAR2, "OBJECT_NAME" VARCHAR2)RETURN VARCHAR2 ASBEGIN RETURN '1=1'; END;;-- Create policy "ORDERS_MASK" on "ORDERS"BEGIN "SYS"."DBMS_RLS"."ADD_GROUPED_POLICY"( object_name => '"ORDERS"', policy_group => '"SENSITIVE"', policy_name => '"ORDERS_MASK"', policy_function => '"TENANT_PRED"', statement_types => 'SELECT', sec_relevant_cols => '"AMOUNT","SSN"', sec_relevant_cols_opt => SYS.DBMS_RLS.ALL_ROWS );END;;-- Create policy "ORDERS_TENANT" on "ORDERS"BEGIN "SYS"."DBMS_RLS"."ADD_POLICY"( object_name => '"ORDERS"', policy_name => '"ORDERS_TENANT"', policy_function => '"TENANT_PRED"', statement_types => 'SELECT,UPDATE,DELETE', update_check => TRUE, policy_type => SYS.DBMS_RLS.CONTEXT_SENSITIVE );END;;-- Add policy context "APP_CTX"."POLICY_GRP" on "ORDERS"BEGIN "SYS"."DBMS_RLS"."ADD_POLICY_CONTEXT"( object_name => '"ORDERS"', namespace => '"APP_CTX"', attribute => '"POLICY_GRP"' );END;;
Disabling a Policy
Setting enabled = false disables a policy in place, without dropping it:
policy "ORDERS_TENANT" { on = table.ORDERS function = function.TENANT_PRED for = [SELECT, UPDATE, DELETE] type = CONTEXT_SENSITIVE update_check = true enabled = false}
-- Disable policy "ORDERS_TENANT"BEGIN "SYS"."DBMS_RLS"."ENABLE_POLICY"( object_name => '"ORDERS"', policy_name => '"ORDERS_TENANT"', enable => FALSE );END;;
Application Contexts
In database mode, with mode=database on both URLs, the contexts mode adds application contexts:
env "vpd" { schema { mode "oracle" { policies = true contexts = true } }}
A context block manages the application context of a context-sensitive policy, and an attribute_association block binds the two. The table and the function, already in the database, are left out of this excerpt:
policy "ORDERS_TENANT" { on = table.ORDERS function = function.TENANT_PRED for = [SELECT] type = CONTEXT_SENSITIVE attribute_association { namespace = context.RLS_CTX attribute = "TENANT_ID" }}context "RLS_CTX" { package = "PDBADMIN.RLS_PKG"}
-- Create context "RLS_CTX"CREATE CONTEXT "RLS_CTX" USING "PDBADMIN"."RLS_PKG";-- Create policy "ORDERS_TENANT" on "ORDERS"BEGIN "SYS"."DBMS_RLS"."ADD_POLICY"( object_schema => '"PDBADMIN"', object_name => '"ORDERS"', policy_name => '"ORDERS_TENANT"', function_schema => '"PDBADMIN"', policy_function => '"TENANT_PRED"', statement_types => 'SELECT', policy_type => SYS.DBMS_RLS.CONTEXT_SENSITIVE );END;;-- Add attribute association "RLS_CTX"."TENANT_ID" to policy "ORDERS_TENANT"BEGIN "SYS"."DBMS_RLS"."ALTER_POLICY"( object_schema => '"PDBADMIN"', object_name => '"ORDERS"', policy_name => '"ORDERS_TENANT"', alter_option => SYS.DBMS_RLS.ADD_ATTRIBUTE_ASSOCIATION, namespace => '"RLS_CTX"', attribute => '"TENANT_ID"' );END;;
- Without the policies mode, Atlas neither inspects nor drops the policies, policy groups, and driving contexts of a database, so a schema written before turning it on leaves them alone. A schema that declares one is refused: managing policy_group "SENSITIVE" requires enabling "policies" in the schema mode. The contexts mode gates context blocks the same way.
- A context is database-global, so a schema-scope run refuses a context block: add mode=database to the URLs. Its package is written as <schema>.<package>, and type takes ACCESSED_GLOBALLY, INITIALIZED_EXTERNALLY, or INITIALIZED_GLOBALLY. Changing either is planned as CREATE OR REPLACE CONTEXT.
- Oracle drops a policy with the last of the columns it guards, and ADD_POLICY rejects a column a view lacks. So when a change drops a guarded column (a column swap on a table or a view, or the drop and add Oracle needs to change a virtual, identity, or VARRAY column), Atlas drops the policy before the change and adds it back after.
- A SQL desired state declares policies, policy groups, and driving contexts as anonymous blocks, each followed by a / line, such as BEGIN DBMS_RLS.CREATE_POLICY_GROUP(object_name => 'ORDERS', policy_group => 'SENSITIVE'); END;. Atlas plans the same calls as for the HCL form.
- With the policies mode, when an enabled SELECT policy guards a table, the emptiness checks of migrate down fail unless the session holds EXEMPT ACCESS POLICY, as the policy can hide rows from the table owner too.
Getting Started
Oracle support is part of Atlas Pro. Run atlas login, enable policies in the mode "oracle" block, and run atlas migrate diff or atlas schema apply.