Scripts for the Security HR Demo

HR Demo Scripts and Log Files lists the scripts and generated log files with links to the content of each file.

HR Demo Scripts and Log Files

Scripts Log Files
hrdemo_setup.sql hrdemo_setup.log
hrdemo.sql hrdemo.log
hrdemo_session.sql hrdemo_session.log
hrdemo.java hrdemo.log
hrdemo_clean.sql hrdemo_clean.log

This section includes the following script files:

hrdemo_setup.sql

The source file for the set up script hrdemo_setup.sql.

SET ECHO OFF
SET FEEDBACK 1
SET NUMWIDTH 10
SET LINESIZE 80
SET TRIMSPOOL ON
SET TAB OFF
SET PAGESIZE 100
SET ECHO ON

define passwd=&1

----------------------------------------------------------------------
--  Introduction
----------------------------------------------------------------------
-- The HR Demo shows how to use basic Real Application Security features.
-- The demo secures HR.EMPLOYEES table by creating a data security
-- policy that grants the table access to:
-- Data Security Policy
--
--(1) An employee can view their own record including SALARY column.
--(2) An IT engineer can view all employee records in IT department,
--    but cannot view employee's salaries.
--(3) An HR representative can view and update all employee records.
--
--
--Sample Users and Their Role Grants:
-- 1) DAUSTIN, an application user in IT department. He has role employee
--    and it_engineer. He can view employee records in IT department, but he
--    cannot view the salary column except for his own.
-- 2) SMAVRIS, an application user in HR department. She has role employee
--    and hr_representative. She can view and update all the employee records.

----------------------------------------------------------------------
-- 1. SETUP - User and Roles
----------------------------------------------------------------------

connect sys/&passwd as sysdba
-- Create an application role employee for common employees.
exec sys.xs_principal.create_role(name => 'employee', enabled => true);

-- Create an application role it_engineer for IT department.
exec sys.xs_principal.create_role(name => 'it_engineer', enabled => true);

-- Create an application role hr_representative for HR department.
exec sys.xs_principal.create_role(name => 'hr_representative', enabled => true);

-- create a database role for object privilege grants
create role db_emp;

-- Grant DB_EMP to the three application roles, so they have the required
-- object privileges to access the table.
grant db_emp to employee;
grant db_emp to it_engineer;
grant db_emp to hr_representative;

-- Create two application users:
-- DAUSTIN (in IT department), granted employee and it_engineer.
exec  sys.xs_principal.create_user(name => 'daustin', schema => 'hr');
exec  sys.xs_principal.set_password('daustin', 'welcome1');
exec  sys.xs_principal.grant_roles('daustin', 'XSCONNECT');
exec  sys.xs_principal.grant_roles('daustin', 'employee');
exec  sys.xs_principal.grant_roles('daustin', 'it_engineer');

-- SMAVRIS (in HR department), granted employee and hr_representative.
exec  sys.xs_principal.create_user(name => 'smavris', schema => 'hr');
exec  sys.xs_principal.set_password('smavris', 'welcome1');
exec  sys.xs_principal.grant_roles('smavris', 'XSCONNECT');
exec  sys.xs_principal.grant_roles('smavris', 'employee');
exec  sys.xs_principal.grant_roles('smavris', 'hr_representative');

-- Grant HR user policy adminisration privilege
exec sys.xs_admin_util.grant_system_privilege('ADMIN_ANY_SEC_POLICY','HR');

----------------------------------------------------------------------
-- 2. SETUP - Security class and ACL
----------------------------------------------------------------------

-- Connect as HR
connect hr/hr;

-- Grant necessary object privileges to db_emp role
-- This role will be used to grant the required object privileges to
-- application users.

grant select, insert, update, delete on hr.employees to db_emp;

-- Create a security class hr_privileges and include privileges from the predefined DML security class.
-- hr_privileges has a new privilege VIEW_SALARY, which is used to control the
-- access to SALARY column.
declare
begin
  sys.xs_security_class.create_security_class(
    name        => 'hr_privileges',
    parent_list => xs$name_list('sys.dml'),
    priv_list   => xs$privilege_list(xs$privilege('view_salary')));
end;
/

-- Create three ACLs to grant privileges for the policy defined later.
declare
  aces xs$ace_list := xs$ace_list();
begin
  aces.extend(1);

  -- EMP_ACL: This ACL grants employee the privileges to view an employee's
  --          own record including SALARY column.
  aces(1) := xs$ace_type(privilege_list => xs$name_list('select','view_salary'),
                         principal_name => 'employee');

  sys.xs_acl.create_acl(name      => 'emp_acl',
                    ace_list  => aces,
                    sec_class => 'hr_privileges');

  -- IT_ACL:  This ACL grants it_engineer the privilege to view the employee
  --          records in IT department, but it does not grant the VIEW_SALARY
  --          privilege that is required for access to SALARY column.
  aces(1) := xs$ace_type(privilege_list => xs$name_list('select'),
                         principal_name => 'it_engineer');

  sys.xs_acl.create_acl(name      => 'it_acl',
                    ace_list  => aces,
                    sec_class => 'hr_privileges');

  -- HR_ACL:  This ACL grants hr_representative the privileges to view and update all
  --          employees' records including SALARY column.
  aces(1):= xs$ace_type(privilege_list => xs$name_list('select', 'insert',
                                        'update', 'delete', 'view_salary'),
                        principal_name => 'hr_representative');

  sys.xs_acl.create_acl(name      => 'hr_acl',
                    ace_list  => aces,
                    sec_class => 'hr_privileges');
end;
/

----------------------------------------------------------------------
-- 3. SETUP - Data security policy
----------------------------------------------------------------------
-- Create data security policy for EMPLOYEE table. The policy defines three
-- realm constraints and a column constraint that protects SALARY column.
declare
  realms   xs$realm_constraint_list := xs$realm_constraint_list();
  cols     xs$column_constraint_list := xs$column_constraint_list();
begin
  realms.extend(3);

  -- Realm #1: Only the employee's own record.
  --           employee can view the realm including SALARY column.
  realms(1) := xs$realm_constraint_type(
    realm    => 'email = xs_sys_context(''xs$session'',''username'')',
    acl_list => xs$name_list('emp_acl'));

  -- Realm #2: The records in the IT department.
  --           it_engineer can view the realm excluding SALARY column.
  realms(2) := xs$realm_constraint_type(
    realm    => 'department_id = 60',
    acl_list => xs$name_list('it_acl'));

  -- Realm #3: All the records.
  --           hr_representative can view and update the realm including SALARY column.
  realms(3) := xs$realm_constraint_type(
    realm    => '1 = 1',
    acl_list => xs$name_list('hr_acl'));

  -- Column constraint protects SALARY column by requiring VIEW_SALARY
  -- privilege.
  cols.extend(1);
  cols(1) := xs$column_constraint_type(
    column_list => xs$list('salary'),
    privilege   => 'view_salary');

  sys.xs_data_security.create_policy(
    name                   => 'employees_ds',
    realm_constraint_list  => realms,
    column_constraint_list => cols);
end;
/

-- Apply the data security policy to the table.
begin
  sys.xs_data_security.apply_object_policy(
    policy => 'employees_ds',
    schema => 'hr',
    object =>'employees');
end;
/

----------------------------------------------------------------------
-- 4. SETUP - Validate the objects we have set up.
----------------------------------------------------------------------
set serveroutput on;
begin
  if (sys.xs_diag.validate_workspace()) then
    dbms_output.put_line('All configurations are correct.');
  else
    dbms_output.put_line('Some configurations are incorrect.');
  end if;
end;
/
-- XS$VALIDATION_TABLE contains validation errors if any.
-- Expect no rows selected.
select * from xs$validation_table order by 1, 2, 3, 4;

----------------------------------------------------------------------
-- 5. SETUP - Mid-Tier related configuration.
----------------------------------------------------------------------

connect sys/&passwd as sysdba

-- create a session administrator who has only
-- RAS session administration privilege (no data privilege),
-- and is responsible to manage RAS session for each application user.
grant xs_session_admin, create session to hr_session identified by hr_session;
grant create session to hr_common identified by hr_common;

-- craete a dispatcher user for java demo, to set up session for application user
exec sys.xs_principal.create_user(name=>'dispatcher', schema=>'HR');
exec sys.xs_principal.set_password('dispatcher', 'welcome1');
exec sys.xs_principal.grant_roles('dispatcher', 'XSCONNECT');
exec sys.xs_principal.grant_roles('dispatcher', 'xsdispatcher');

exit

hrdemo.sql

The source file for the hrdemo.sql script. This script runs the demo with direct logon.

SET ECHO OFF
SET FEEDBACK 1
SET NUMWIDTH 10
SET LINESIZE 80
SET TRIMSPOOL ON
SET TAB OFF
SET PAGESIZE 100
COLUMN EMAIL FORMAT A10
COLUMN FIRST_NAME FORMAT A15
COLUMN LAST_NAME FORMAT A15
COLUMN DEPARTMENT_ID FORMAT 9999
COLUMN MANAGER_ID FORMAT 9999
COLUMN SALARY FORMAT 999999
SET ECHO ON


----------------------------------------------------------------------
--  HR Demo - PL/SQL with RAS direct logon user
----------------------------------------------------------------------
-- This demo shows RAS runtime, using RAS direct logon user.
-- Each user directly connects to database and accesses employee table.
-- RAS policy is automatically enforced.
---------------------------------------------------------------------

-- Connect as DAUSTIN, who has only employee and it_engineer role
conn daustin/welcome1;

SET SECUREDCOL ON UNAUTH *******

-- DAUSTIN can view the records in IT department, but can only view his own
-- SALARY column.
select email, first_name, last_name, department_id, manager_id, salary
from employees order by email;


SET SECUREDCOL OFF

-- DAUSTIN cannot update the record.
update employees set manager_id = 102 where email = 'DAUSTIN';

-- Record is not changed.
select email, first_name, last_name, department_id, manager_id, salary
from employees where email = 'DAUSTIN';


-- Connect as SMAVRIS, who has both employee and hr_representative role.
conn smavris/welcome1;

-- SMAVRIS can view all the records including SALARY column.
select email, first_name, last_name, department_id, manager_id, salary
from employees where department_id = 60 or department_id = 40
order by department_id, email;

-- EMPLOYEES table has 107 rows, we expect to see all of them.
select count(*) from employees;

-- SMAVRIS can update the record.
update employees set manager_id = 102 where email = 'DAUSTIN';

-- Record is changed.
select email, first_name, last_name, department_id, manager_id, salary
from employees where email = 'DAUSTIN';

-- change the record back to the original.
update employees set manager_id = 103 where email = 'DAUSTIN';

exit

hrdemo_session.sql

The source file for the hrdemo_session.sql script. This script explicitly creates and attaches a Real Application Security session.

SET ECHO OFF
SET FEEDBACK 1
SET NUMWIDTH 10
SET LINESIZE 80
SET TRIMSPOOL ON
SET TAB OFF
SET PAGESIZE 100
COLUMN EMAIL FORMAT A10
COLUMN FIRST_NAME FORMAT A15
COLUMN LAST_NAME FORMAT A15
COLUMN DEPARTMENT_ID FORMAT 9999
COLUMN MANAGER_ID FORMAT 9999
COLUMN SALARY FORMAT 999999
SET ECHO ON

----------------------------------------------------------------------
--  HR Demo - PL/SQL with Session API
----------------------------------------------------------------------
-- This demo shows RAS runtime, using RAS user as application user.
-- The user does not logon to database, but a RAS session is created
-- and attached for each user before accessing employee table.
---------------------------------------------------------------------

-- Connect as RAS session administrator.
connect hr_session/hr_session;

-- Variable used to remember the session ID.
var gsessionid varchar2(32);

-- Create an application session for SMARVIS and attach to it.
declare
  sessionid raw(16);
begin
  sys.dbms_xs_sessions.create_session('SMAVRIS', sessionid);
  :gsessionid := rawtohex(sessionid);
  sys.dbms_xs_sessions.attach_session(sessionid, null);
end ;
/

-- Display the current user, it should be SMAVRIS now.
select xs_sys_context('xs$session','username') from dual;

-- Display the enabled application roles and database roles.
select role_name from v$xs_session_roles union
select role from session_roles order by 1;

-- SMAVRIS can view all the records including SALARY column.
select email, first_name, last_name, department_id, manager_id, salary
from employees where department_id = 60 or department_id = 40
order by department_id, email;

-- EMPLOYEES table has 107 rows, we expect to see all of them.
select count(*) from employees;

-- Disable hr_representative role.
exec dbms_xs_sessions.disable_role('hr_representative');

-- SMAVRIS should only be able to see her own record.
select email, first_name, last_name, department_id, manager_id, salary
from employees where department_id = 60 or department_id = 40
order by department_id, email;

-- Enable hr_representative role.
exec sys.dbms_xs_sessions.enable_role('hr_representative');

-- SMAVRIS can view all the records again.
select email, first_name, last_name, department_id, manager_id, salary
from employees where department_id = 60 or department_id = 40
order by department_id, email;

-- EMPLOYEES table has 107 rows, we expect to see all of them.
select count(*) from employees;

-- Detach and destroy the application session.
declare
  sessionid raw(16);
begin
  sessionid := hextoraw(:gsessionid);
  sys.dbms_xs_sessions.detach_session;
  sys.dbms_xs_sessions.destroy_session(sessionid);
end;
/

exit

hrdemo.java

The source file for the Java demo is hrdemo.java.

import java.security.GeneralSecurityException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import java.util.ArrayList;
import java.util.List;
import oracle.jdbc.OracleDriver;
import oracle.jdbc.OracleResultSet;
import oracle.jdbc.OracleResultSet.AuthorizationIndicator;

import oracle.security.xs.AccessDeniedException;
import oracle.security.xs.InvalidXSNamespaceException;
import oracle.security.xs.InvalidXSUserException;
import oracle.security.xs.Role;
import oracle.security.xs.Session;
import oracle.security.xs.XSAccessController;
import oracle.security.xs.XSException;
import oracle.security.xs.XSSessionManager;

/**
 * A simple java application implemented using RAS.
 * It shows:
 *  -  How to setup RAS session manager
 *  -  How to manage RAS sessions
 *  -  How to use Column authorization indicator
 *  -  How to check privileges using "checkAcl" function
*/
public class hrdemo {

  // application connection, should be created with unprivileged user
  // in RAS case, the user only needs DB connection privilege
  private Connection appConnection = null;

  // RAS dispatcher's connection, should be create with a RAS dispatcher user
  private Connection mgrConnection = null;
  // RAS session manager, to manage session for application user
  // Must be instanciated with disptcher's connection
  private XSSessionManager manager = null;

  public static void main(String[] args)  {

    try {
      DriverManager.registerDriver(new OracleDriver());


      if (args.length != 1) {
        System.out.println("Usage hrdemo dbURL");
        System.exit(1);
      }
      hrdemo demo = new hrdemo();
      demo.setupConnection(args[0]);

      demo.queryAsUser("DAUSTIN");
      demo.queryAsUser("SMAVRIS");

      demo.cleanupConnection();

    } catch (Exception e) {
      // we don't handle exception for now
      e.printStackTrace();
    }
  }

  private void queryAsUser(String user) throws SQLException, XSException {

    System.out.println("\nQuery HR.EMPLOYEES table as user \"" + user + "\"");

      Session lws = manager.createSession(appConnection, user, null,null);
      manager.attachSession(appConnection, lws, null, null, null, null, null);

      queryEmployees(lws);

      manager.detachSession(lws);
      manager.destroySession(appConnection, lws);

  }

  public void setupConnection(String url) throws SQLException, XSException, GeneralSecurityException {
    // dispatcher's connection
    mgrConnection =
        DriverManager.getConnection(url, "dispatcher", "welcome1");

    // RAS session manager
    manager = XSSessionManager.getSessionManager(mgrConnection, 30, 2048000);

    // connection used for application query
    appConnection = DriverManager.getConnection(url, "hr_common", "hr_common");
  }

  public void cleanupConnection() throws SQLException {
    mgrConnection.close();
    appConnection.close();

  }

  public void queryEmployees(Session lws) throws SQLException, XSException  {
    // using DB connection that has been attached to a RAS session
    Connection conn = lws.getConnection();
    String query = " select email, first_name, last_name, department_id, salary, ora_get_aclids(emp) from hr.employees emp where department_id in (40, 60, 100) order by email";

    Statement stmt = null;
    ResultSet rs = null;

    System.out.printf("  EMAIL  | FIRST_NAME | LAST_NAME  | DEPT | SALARY | UPDATE | VIEW_SALARY\n");

    try {

      stmt = conn.createStatement();
      rs = stmt.executeQuery(query);

      while (rs.next()) {

        String email = rs.getString("EMAIL");
        String first_name = rs.getString("FIRST_NAME");
        String last_name = rs.getString("LAST_NAME");
        String department_id = rs.getString("DEPARTMENT_ID");
        String salary;

        if (((OracleResultSet)rs).getAuthorizationIndicator("SALARY") == AuthorizationIndicator.NONE) {
          salary = rs.getString("SALARY");
        }
        else {
          salary = "*****";
        }

        byte[] aclRaw = rs.getBytes(6);
        String update, viewSalary;


	// call checkAcl to determine whether can update the database record
	if (XSAccessController.checkAcl(lws, aclRaw, "UPDATE")) {
          update = "true";
        }
        else {
          update = "false";
        }

        if (XSAccessController.checkAcl(lws, aclRaw, "VIEW_SALARY")) {
          viewSalary = "true";
        }
        else {
          viewSalary = "false";
        }

        System.out.printf("%9s|%12s|%12s|%6s|%8s|%8s|%8s\n", email,
                          first_name, last_name, department_id,
                          salary, update, viewSalary);
       }
    } finally {
      try { if (rs != null) rs.close(); } catch (Exception e) {};
      try { if (stmt != null) stmt.close(); } catch (Exception e) {};
    }
  }
}

hrdemo_clean.sql

The source file for the cleanup script is hrdemo_clean.sql.

SET ECHO OFF
SET FEEDBACK 1
SET NUMWIDTH 10
SET LINESIZE 80
SET TRIMSPOOL ON
SET TAB OFF
SET PAGESIZE 100
SET ECHO ON

define passwd=&1

connect hr/hr;

-- Remove policy from the table.
begin
  sys.xs_data_security.remove_object_policy(policy=>'employees_ds',
                                        schema=>'hr', object=>'employees');
end;
/
-- Delete security class and ACLs
exec sys.xs_security_class.delete_security_class('hr_privileges', xs_admin_util.cascade_option);
exec sys.xs_acl.delete_acl('emp_acl', xs_admin_util.cascade_option);
exec sys.xs_acl.delete_acl('it_acl', xs_admin_util.cascade_option);
exec sys.xs_acl.delete_acl('hr_acl', xs_admin_util.cascade_option);

-- Delete data security policy
exec sys.xs_data_security.delete_policy('employees_ds', xs_admin_util.cascade_option);

connect sys/&passwd as sysdba
-- Delete application users and roles
exec sys.xs_principal.delete_principal('employee', xs_admin_util.cascade_option);
exec sys.xs_principal.delete_principal('hr_representative', xs_admin_util.cascade_option);
exec sys.xs_principal.delete_principal('it_engineer', xs_admin_util.cascade_option);
exec sys.xs_principal.delete_principal('smavris', xs_admin_util.cascade_option);
exec sys.xs_principal.delete_principal('daustin', xs_admin_util.cascade_option);


-- Delete database role
drop role db_emp;

-- Delete session administrator
drop user hr_session;
-- Delete the common user used to connect to DB
drop user hr_common;

-- Delete dispatcher user used by mid-tier
exec sys.xs_principal.delete_principal('dispatcher', xs_admin_util.cascade_option);

exit