Pages

Tuesday, May 22, 2012

On ORA-01031: insufficient privileges error received when selecting from a view

I was asked to give an example on how to solve the ORA-01031: insufficient privileges error received by a user who select a view OWNED by another user (view created on a table owned by another user as well).
I mean: user W is the owner of a table (T1); user V has built a view (VIEW_T1) on that table; user R want to query V.VIEW_T1 view... but he receives the ORA-01031: insufficient privileges error.
I think the easy way is to provide some line codes. First of all the following are the schema and objects settings:
CREATE USER W
  IDENTIFIED BY W
  DEFAULT TABLESPACE USERS
  TEMPORARY TABLESPACE TEMP_GROUP_SYS
  PROFILE DEFAULT
  ACCOUNT UNLOCK
/
  -- 1 Role for W 
  GRANT CONNECT TO W
/
  ALTER USER W DEFAULT ROLE ALL
/
  GRANT CREATE TABLE TO W
/
  GRANT CREATE SESSION TO W
/
  ALTER USER W QUOTA 1M ON USERS
  /
CREATE USER V
  IDENTIFIED BY V
  DEFAULT TABLESPACE USERS
  TEMPORARY TABLESPACE TEMP_GROUP_SYS
  PROFILE DEFAULT
  ACCOUNT UNLOCK
/
  -- 1 Role for R 
  GRANT CONNECT TO V
/
  ALTER USER V DEFAULT ROLE ALL
/
  GRANT CREATE TABLE TO V
/
  GRANT CREATE SESSION TO V
/
  ALTER USER V QUOTA 1M ON USERS
/
CREATE USER R
  IDENTIFIED BY R
  DEFAULT TABLESPACE USERS
  TEMPORARY TABLESPACE TEMP_GROUP_SYS
  PROFILE DEFAULT
  ACCOUNT UNLOCK
/
  -- 1 Role for R 
  GRANT CONNECT TO R
/
  ALTER USER R DEFAULT ROLE ALL
/
  GRANT CREATE TABLE TO R
/
  GRANT CREATE SESSION TO R
/
  ALTER USER R QUOTA 1M ON USERS
/
CREATE TABLE W.T1 (A NUMBER)
/
INSERT INTO W.T1 VALUES (1)
/
INSERT INTO W.T1 VALUES (2)
/
INSERT INTO W.T1 VALUES (3)
/
GRANT SELECT ON W.T1 TO V
/
CREATE OR REPLACE VIEW V.T1_VIEW (A_VIEW) AS SELECT A FROM W.T1
/
Now let's try to select those records from all the 3 users, W, V and R respectively and let's see what we get:
user@hostname:~$ sqlplus w/w
SQL> select * from t1;

  A
----------
  1
  2
  3

SQL> set feed on
SQL> /

  A
----------
  1
  2
  3

3 rows selected.
Now connect using the V user...
SQL> conn v/v
Connected.
SQL> select * from t1_view;

    A_VIEW
----------
  1
  2
  3

3 rows selected.

SQL> select * from w.t1;

  A
----------
  1
  2
  3

3 rows selected.
And now using the R user...
SQL> conn r/r
Connected.
SQL> select * from v.t1_view;
select * from v.t1_view
                *
ERROR at line 1:
ORA-00942: table or view does not exist
Those code lines state: 1) V user has the permissions to query his view and, of course, the table on which his view was built on; 2) R user is trying to query an object that seems it doesn't exist on the V schema; that's because R has no permissions to query that object; So let's try to grant the select permission to R user so he can query the view on V schema:
SQL> conn sys/my_pwd
Connected.
SQL> grant select on v.t1_view to r;

Grant succeeded.
Let's try to query again V.T1_VIEW from R user and see what happens:
SQL> conn r/r
Connected.
SQL> select * from v.t1_view;
select * from v.t1_view
                *
ERROR at line 1:
ORA-01031: insufficient privileges
We are getting finally the ORA-01031: insufficient privileges error. Now the question was: Do I need to grant select permission on the W.T1 table directly to R user ? The answer is: no.
SQL> conn sys/my_pwd
Connected.
SQL> grant select on w.t1 to r;

Grant succeeded.

SQL> conn r/r
Connected.
SQL> select * from v.t1_view;
select * from v.t1_view
                *
ERROR at line 1:
ORA-01031: insufficient privileges


SQL> select * from w.t1;

  A
----------
  1
  2
  3

3 rows selected.
What's happening ? We are able to select rows directly from W user, but not from the view owned by V, built and with the right privileges on W.T1 table ?!?! The error is in the grant option granted to V user to select W.T1 table. It should be like the following command:
SQL> conn sys/my_pwd
Connected.
SQL> grant select on w.t1 to v with grant option;

Grant succeeded.
And now...
SQL> conn r/r
Connected.
SQL> select * from v.t1_view;

    A_VIEW
----------
  1
  2
  3

3 rows selected.
Of course even if we revoke the privilege to select directly the W.T1 table from R user, R user is still able to query the view owned by V user.
SQL> conn sys/my_pwd
Connected.
SQL> revoke select on w.t1 from r;

Revoke succeeded.

SQL> conn r/r
Connected.
SQL> select * from v.t1_view;

    A_VIEW
----------
  1
  2
  3

3 rows selected.
That's all

How create a virtual machine from a previously cloned template on Oracle VM 3.0.3: step 1

I've a golden image of a previously created virtual machine. Now I want to create a new virtual machine starting from that cloned template. The template is based on a Oracle Enterprise Linux 5.7 x86-64 server, including just the installation of the "already patched" Oracle database software (in my case it's 10.2.0.5.0). No database is so installed, but it just takes time to execute your dbca utility and it will.
Select your Server Pools and on the right panel, clicking on the Template tab, select your template. Now click on the "Clone" icon (just on the left of red cross icon).

A new window will appear: simply insert the name of the future new virtual machine and select your target server pool. I have two separate server pool without sharing resources so I will create my new machine on the same server pools where the template was saved. Click the OK button.

A new job will start and it usually completes quickly

As you can see a new virtual machine was created. Now it's time to edit it because it has all configuration files of the cloned virtual machine and you don't want to have two machines with the same MAC address, hostname, ip address on the same network...

Before to start the new virtual machine let's modify some informations: select from the Server Pools the Repositories tab on the right panel. Then choose your repository (in my case it was ovm2reposdevpom): on the bottom panel select the Virtual Disks tab and choose the new cloned disk (vsixx-disk0.img(2) ). Then click on the edit icon (the pencil).

A new window will appear.

Just edit your new Virtual Disk Name and click OK

Your edited virtual disk name is there in your repository.

Now it's time to start your new virtual machine. Right click on your virtual machine and select the start menu.

A new job will be submitted

And when it completes, your virtual machine is ready to be edited.

Right click on your virtual machine and select "Launch Console".

Step 2 is here

How to delete an Oracle VM virtual machine

Its' so easy to delete a Oracle virtual machine. Just expand your Server Pools and select your virtual machine you don't need anymore. Then click on "red cross" icon


As soon as you press that icon a new window will ask you to confirm your action.

Select OK and a new job will be submitted, deleting your virtual machine. Under your Server Pools there's no virtual machine available now...