Skip to main content

VPD Policy Implementation - ORACLE Database




VPD Policy Implementation - ORACLE Database

 VPD policies Oracle


CREATE TEST USERS in DB


-- Owner schema for objects

CREATE USER app_owner IDENTIFIED BY AppOwner#123

DEFAULT TABLESPACE users

TEMPORARY TABLESPACE temp

QUOTA UNLIMITED ON users;


GRANT CREATE SESSION, CREATE TABLE, CREATE PROCEDURE, CREATE ANY CONTEXT TO app_owner;


-- Test application user

CREATE USER app_user IDENTIFIED BY AppUser#123

DEFAULT TABLESPACE users

TEMPORARY TABLESPACE temp;


GRANT CREATE SESSION TO app_user;


CONNECT TO USER


CONN app_owner/AppOwner#123 


CREATE THE SAMPLE TABLE AND DATA


CREATE TABLE employees (

    emp_id         NUMBER PRIMARY KEY,

    emp_name       VARCHAR2(100),

    department_id  NUMBER,

    salary         NUMBER

);


INSERT TEST DATA

 

INSERT INTO employees VALUES (1, 'John',  10, 1000);

INSERT INTO employees VALUES (2, 'Mary',  20, 1200);

INSERT INTO employees VALUES (3, 'David', 10, 1500);

INSERT INTO employees VALUES (4, 'Sarah', 30, 1800);

COMMIT; 


CREATE PACKAGE

CREATE OR REPLACE PACKAGE sec_ctx_pkg AS

    PROCEDURE set_dept_context(p_dept_id NUMBER);

END sec_ctx_pkg;

/


CREATE PACKAGE BODY


CREATE OR REPLACE PACKAGE BODY sec_ctx_pkg AS

    PROCEDURE set_dept_context(p_dept_id NUMBER) IS

    BEGIN

        DBMS_SESSION.SET_CONTEXT(

            namespace => 'APP_CTX',

            attribute => 'DEPT_ID',

            value     => p_dept_id

        );

    END;

END sec_ctx_pkg;


CREATE CONTEXT

CREATE CONTEXT app_ctx USING sec_ctx_pkg; 


CREATE THE VPD POLICY FUNCTION.


CREATE OR REPLACE FUNCTION emp_policy_fn (

    p_schema IN VARCHAR2,

    p_object IN VARCHAR2

) RETURN VARCHAR2

AS

    l_dept_id VARCHAR2(30);

BEGIN

    l_dept_id := SYS_CONTEXT('APP_CTX', 'DEPT_ID');


    IF l_dept_id IS NULL THEN

        RETURN '1=2';

    END IF;


    RETURN 'department_id = ' || l_dept_id;

END;

/


ATTACH THE VPD POLICY TO THE TABLE CREATED.


BEGIN

    DBMS_RLS.ADD_POLICY(

        object_schema   => 'APP_OWNER',

        object_name     => 'EMPLOYEES',

        policy_name     => 'EMP_DEPT_VPD',

        function_schema => 'APP_OWNER',

        policy_function => 'EMP_POLICY_FN',

        statement_types => 'SELECT,UPDATE,DELETE',

        update_check    => TRUE

    );

END;


GRANT ACCESS TO THE USER

 

GRANT SELECT, UPDATE, DELETE ON employees TO app_user;

GRANT EXECUTE ON sec_ctx_pkg TO app_user; 



 

Comments

Popular posts from this blog

Warning: long redo log write elapsed times detected, the LG* process tracefiles have more details

Warning: long redo log write elapsed times detected, the LG* process tracefiles have more details This warning means LGWR / LGnn background processes are taking too long to write redo to the online redo logs. It is usually related to one of these: - Slow storage / high I/O latency on redo log disks - Redo logs placed on busy disks together with datafiles, FRA, archive logs, backups, or OS files - Too many commits from the application, causing frequent LGWR flushes - Redo log size too small, causing frequent log switches - CPU scheduling issue, where LGWR is not getting CPU quickly - Data Guard synchronous transport delay, if using SYNC/AFFIRM - In newer 19c RU versions, especially around 19.28, this warning can appear more visibly because of diagnostic changes, so first confirm whether there is a real performance impact before changing anything. - Oracle’s own wait-event documentation says log file sync is the foreground wait for redo write confirmation after commit, and log file paral...

Convert TIMESTAMP to SCN and SCN to TIMESTAMP in Oracle

  Convert TIMESTAMP to SCN and SCN to TIMESTAMP in Oracle In many recovery scenario we need to know our SCN and timestamps. We can convert this by using the following function SCN_TO_TIMESTAMP TIMESTAMP_TO_SCN We can use this function with help of dual functions. Example of using this function as follows: 1. Convert the SCN to Timestamp SQL> select scn_to_timestamp(2011955) from dual; SCN_TO_TIMESTAMP(2011955) ----------------------------------------------------- 05-SEP-18 12.46.20.000000000 PM 2. Convert the Timestamp to SCN SQL> select timestamp_to_scn(to_timestamp('05-09-2018 12:46:21','dd-mm-yyyy hh24:mi:ss')) scn from dual; SCN ---------- 2011955

How To Enable Flash Recovery Area In Oracle Database

  The flash recovery area(FRA) is an Oracle-managed destination( either FILE SYSTEM or ASM ) for centralized backup and recovery files. It simplifies the backup management. The following recovery-related files are stored in the flash recovery area: — Current control file — Online redo logs — Archived redo logs — Flashback logs — Control file auto backups — Datafile and control file copies — Backup pieces — Foreign archived redo log Below are the steps for enabling  flash  recovery area. DB_RECOVERY_FILE_DEST_SIZE   and  DB_RECOVERY_FILE_DEST   initial parameters are required for enabling FRA. DB_RECOVERY_FILE_DEST_SIZE   -> It is the disk quota size for the flash recovery area. DB_RECOVERY_FILE_DEST   – > This initialization parameter is a valid destination for the Flash Recovery Area. It can be a directory, file system, or ASM disk group. NOTE  : DB_RECOVERY_FILE_DEST_SIZE must be set before DB_RECOVERY_FILE_DEST. 1. Check whether FRA ...