Virtual Private Databases (VPD) Virtual Private Databases (VPD) allow multiple users to access a single schema whilst preventing them from accessing data that is not relevant to them. Although this type of access can be controlled by the application, access via other methods ( SQL * Plus) Would Leave The Data Open To Abuse. Setting Up A VPD Involves The Following Steps:
Setup Test Environment Create an Application Context Create Login Trigger Create Security Policies Apply Security Policies to Tables Test VPD What Next Setup Test Environment First we must create a user to act as the schema owner for this example. Obviously, you will perform the following tasks using Your Current Schema Owner: Code:
Connect Sys / Password @ service as sysdba; create user schemaowner identified by Schemaowner Default TableSpace Uses Temporary TableSpace Temp; Grant Connect, Resource To schemaowner;
Create User1 Identified by User1 Default TableSpace Users Temporary TableSpace Temp; Grant Connect, Resource To User1;
Create User2 Identified by User2 Default TableSpace User2; Grant Connect, Resource To User2;
Grant Execute on dbms_rls to public;
Conn Schemaowner / Schemawner @ Service
Create Table Users (ID Number (10) Not Null, Ouser Varchar2 (30) Not Null, First_name Varchar2 (50) Not Null, Last_name Varchar2 (50) Not Null;
Create Table User_Data (Column1 Varchar2 (50) Not Null, User_id Number (10) Not null;
INSERT INTO Users Values (1, 'User1', 'User', 'One'); Insert Into Users Values (2, 'User2', 'User', 'Two');
Grant SELECT, INSERT ON USER_DATA to USER1, User2;
Create an Application Context Grant CREATE ANY CONTEXT to the schema owner then create the context and context package: Code: CONNECT sys / password @ service AS SYSDBA; GRANT create any context, create public synonym TO schemaowner;
Connect Schemaowner / Schemaowner @ Service;
Create Context Schemaowner Using Schemaowner.Context_package;
Create Or Replace Package Context_Package As Procedure Set_Context; End; /
Next We create the context_package body which will actually set the user context: Code:
CREATE OR REPLACE PACKAGE BODY Context_Package IS PROCEDURE Set_Context IS v_ouser VARCHAR2 (30); v_id NUMBER; BEGIN DBMS_Session.Set_Context ( 'SCHEMAOWNER', 'SETUP', 'TRUE'); v_ouser: = SYS_CONTEXT ( 'USERENV', 'SESSION_USER') ; BEGIN SELECT id INTO v_id FROM users WHERE ouser = v_ouser; DBMS_Session.Set_Context ( 'SCHEMAOWNER', 'USER_ID', v_id); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_Session.Set_Context ( 'SCHEMAOWNER', 'USER_ID', 0); END; dBMS_Session .Set_context ('schemaowner', 'setup', 'false'); end set_context; end context_package; / show errors
Next We make Sure That Uses Have Access To The Context_package: Code:
Grant Execute on schemaowner.context_package to public; create public synonym context_package for schemaowner.context_package;
To Be Continue
________________If God Had Gifted Me With Some Beauty and Much Wealth, I Should Have Made It As Hard for You ToAve Me, As It Is Now for Me To Leave you.
Back to top
Yikaikai Elf
Registration time: 2002-11-09 Last login: 2004-09-23 Total number: 2654 Essence Post: 8 Original Essence: 1 From: China
Online status: ... Offline ...
Published in: 2003-02-24 11:02 Posted: Create login Trigger
Next We Must Create a Trigger to Fire After The User Logs ONTO The Database: Code:
Connect Sys / Password @ Service As Sysdba; Create or Replace Trigger Schemaowner.SET_SECURITY_CONTEXT AFTER LOGON on DATABASE BEGIN SCHEMAOWNTEXT_PACKAGE.SET_CONTEXT; END; / SHOW ERRORS
CREATE Security Policies:
In order for the context package to have any effect on the users interaction with the database, we need to define a Security_package for use with the security policy This package will tell database how to treat any interactions with the specified table:. Code:
CONNECT schemaowner / schemaowner @ serivce; CREATE OR REPLACE PACKAGE Security_Package AS FUNCTION User_Data_Insert_Security (Owner VARCHAR2, Objname VARCHAR2) return varchar2; FUNCTION User_Data_Select_Security (Owner VARCHAR2, Objname VARCHAR2) RETURN VARCHAR2; END Security_Package; /
Next We create the security_package body: code:
CREATE OR REPLACE PACKAGE BODY Security_Package IS FUNCTION User_Data_Select_Security (Owner VARCHAR2, Objname VARCHAR2) RETURN VARCHAR2 IS Predicate VARCHAR2 (2000); BEGIN Predicate: = '1 = 2'; IF (SYS_CONTEXT ( 'USERENV', 'SESSION_USER'); = ' Schemaowner ') Then Predicate: = NULL; Else Predicate: =' user_id = sys_context ('' Schemaowner '', '' User_ID '') '; Return Predicate; End User_Data_Select_security;
FUNCTION User_Data_Insert_Security (Owner VARCHAR2, Objname VARCHAR2) RETURN VARCHAR2 IS Predicate VARCHAR2 (2000); BEGIN Predicate: = '1 = 2'; IF (SYS_CONTEXT ( 'USERENV', 'SESSION_USER') = 'SCHEMAOWNER') THEN Predicate: = NULL ; ELSE Predicate: = 'USER_ID = SYS_CONTEXT (' 'SCHEMAOWNER' ',' 'USER_ID' ')'; END IF; RETURN Predicate; END User_Data_Insert_Security; END Security_Package; / SHOW ERRORSNext we make sure that all users have access to the Security_Package : Code:
Grant Execute on schemaowner.security_package to public; create public synynym security_package for schemaowner.security_package;
Apply security policies to Tables the dbms_rls package is buy ketalog the security policy, usd by security_package, to the relevant tables: code:
BEGIN DBMS_Rls.Add_Policy ( 'SCHEMAOWNER', 'USER_DATA', 'USER_DATA_INSERT_POLICY', 'SCHEMAOWNER', 'SECURITY_PACKAGE.USER_DATA_INSERT_SECURITY', 'INSERT', TRUE); DBMS_Rls.Add_Policy ( 'SCHEMAOWNER', 'USER_DATA', 'USER_DATA_SELECT_POLICY', 'Schemaowner', 'Security_Package.user_Data_select_security', 'SELECT'); END; /
Test VPD Finally, Test That The VPD IS WORKING CORRECTLY: Code:
Connect User1 / User1 @ Service; Insert Into Schemaowner.user_data (Column1, User_ID) VALUES ('User 1', 1); Insert Into Schemaowner.user_Data (Column1, User_ID) Values ('USER 2', 2); commit;
CONNECT user2 / user2 @ service INSERT INTO schemaowner.user_data (column1, user_id) VALUES ( 'User 1', 1); INSERT INTO schemaowner.user_data (column1, user_id) VALUES ( 'User 2', 2); COMMIT; CONNECT schemaowner / schemaowner @ service SELECT * FROM schemaowner.user_data; CONNECT user1 / user1 @ Service; SELECT * FROM schemaowner.user_data; CONNECT user2 / user2 @ Service SELECT * FROM schemaowner.user_data;
WHEN Connected to User1, Only The First Insert Work. When Connected To User2, ONLY The Second Insert Will Work. The Failing Inserts Produce The Error:
ORA-28115: policy with check option violation Once the inserts are finished, there will be two rows in the table, as seen when connected as SCHEMAOWNER When connected as USER1 or USER2, only the single row they inserted will be visible..
What Next Once you're happy with the basic mechanism you can extend the Security_Package to cover all tables where restricted access is neccessary, remembering to apply all security policies to the relevant tables. (END)
________________If God Had Gifted Me With Some Beauty and Much Wealth, I Should Have Made It As Hard for You ToAve Me, As It Is Now for Me To Leave you.