Showing posts with label 23ai. Show all posts
Showing posts with label 23ai. Show all posts

Saturday, June 8, 2024

Auditing Enhancements in Oracle Database 23ai

Auditing Enhancements in Oracle Database 23ai

This post describes some of the auditing enhancements in Oracle database 23ai.

◉ Desupport of Traditional Auditing


Traditional auditing was deprecated in Oracle 21c, and has been desupported in Oracle 23ai. Make sure you are using Unified Auditing. 

◉ Audit Individual Columns for Tables and Views


In Oracle 23ai we can create audit policies on individual columns of tables and views, which allows us to thin out the contents of the audit trail by ignoring actions that don't affect the columns of interest. For a table or view column we can audit the following actions, as described here.

ALL, ALTER, AUDIT, COMMENT, DELETE, GRANT, INDEX, INSERT, SELECT, UPDATE

To demonstrate this we create a test table.

conn testuser1/testuser1@//localhost:1521/freepdb1

drop table if exists audit_test_tab purge;

create table audit_test_tab (
  id  number generated always as identity,
  col1 varchar2(10),
  col2 varchar2(10),
  col3 varchar2(10)
);

insert into audit_test_tab (col1, col2) values ('apple', 'banana');
commit;

We connect to a privileged user and create a new audit policy. We want to audit updates on COL1 or COL2, and queries of COL2. Notice we supply a comma-separated list of columns the audited action applies to.

conn sys/SysPassword1@//localhost:1521/freepdb1 as sysdba

noaudit policy test_audit_policy;
drop audit policy test_audit_policy;

create audit policy test_audit_policy
  actions update(col1, col2) on testuser1.audit_test_tab,
          select(col2) on testuser1.audit_test_tab
  container = current;

audit policy test_audit_policy;

We check the audit trail for actions against the table, and we can see there are no actions audited.

column event_timestamp format a30
column dbusername format a10
column action_name format a20
column object_schema format a10
column object_name format a20
column sql_text format a40

select event_timestamp,
       dbusername,
       action_name,
       object_schema,
       object_name,
       sql_text
from   unified_audit_trail
where  object_name = 'AUDIT_TEST_TAB'
order BY event_timestamp;

no rows selected

SQL>

We perform some operations against the test table, some of which are auditable actions.

conn testuser1/testuser1@//localhost:1521/freepdb1

-- Not audited.
insert into audit_test_tab (col1, col2) values ('apple2', 'banana2');

update audit_test_tab
set    col3 = 'pear'
where  col3 is null;

commit;

select id from audit_test_tab;

        ID
----------
         1
         2

SQL>


-- Audited.
update audit_test_tab
set    col1 = 'apple1'
where  col1 = 'apple';

update audit_test_tab
set    col2 = 'banana1'
where  col2 = 'banana';

select col2 from audit_test_tab;

COL2
----------
banana1
banana2

SQL>

We check the audit trail.

conn sys/SysPassword1@//localhost:1521/freepdb1 as sysdba

column event_timestamp format a30
column dbusername format a10
column action_name format a20
column object_schema format a10
column object_name format a20
column sql_text format a40

select event_timestamp,
       dbusername,
       action_name,
       object_schema,
       object_name,
       sql_text
from   unified_audit_trail
where  object_name = 'AUDIT_TEST_TAB'
order BY event_timestamp;

EVENT_TIMESTAMP                DBUSERNAME ACTION_NAME          OBJECT_SCH OBJECT_NAME          SQL_TEXT
------------------------------ ---------- -------------------- ---------- -------------------- ----------------------------------------
14-JUN-23 19.31.17.231940 PM   TESTUSER1  UPDATE               TESTUSER1  AUDIT_TEST_TAB       update audit_test_tab
                                                                                               set    col1 = 'apple1'
                                                                                               where  col1 = 'apple'

14-JUN-23 19.31.17.248862 PM   TESTUSER1  UPDATE               TESTUSER1  AUDIT_TEST_TAB       update audit_test_tab
                                                                                               set    col2 = 'banana1'
                                                                                               where  col2 = 'banana'

14-JUN-23 19.31.17.252646 PM   TESTUSER1  SELECT               TESTUSER1  AUDIT_TEST_TAB       select col2 from audit_test_tab

SQL>

Notice only those actions on the specified columns were audited. The query of the ID column, and the updates to the COL3 column were not audited.

Source: oracle-base.com

Friday, June 7, 2024

ARGUMENT Command in SQL*Plus 23ai and SQLcl 22.4

ARGUMENT Command in SQL*Plus 23ai and SQLcl 22.4

The ARGUMENT command in SQL*Plus 23ai and SQLcl 22.4 allows us to manage arguments passed to a script. This includes the prompt or default value if an argument is missing, and whether the user input from a prompt should be echoed to the screen.

Remember, SQLcl is shipped independently of the database, so we can use the ARGUMENT command against any version of the database from 11.2 onward.

1. PROMPT


The PROMPT option of the ARGUMENT command allows us set the prompt text displayed if the argument is missing from the command line. Create a file called "test.sql" with the following contents.

set verify off
argument 1 prompt "Enter a value for the first argument:"

column arguments format a10

select '&1' as arguments;

undefine 1

Let's breakdown what we are doing here.

◉ We use SET VERIFY OFF so we don't display old and new values for the argument.
◉ We use the ARGUMENT command to define the prompt text for argument number 1.
◉ We use the argument in a query to display the value.
◉ We use UNDEFINE to make sure the current value of the argument is not reused. In SQLcl we could use SET PARAMETERPOLICY ISOLATE to achieve this, but using UNDEFINE works for both tools.

We test the script. In the first example we are prompted and provide the value "banana". In the second example we are prompted and provide the value "apple". In the third example we provide the value "pear" on the command line, and we are not prompted to enter a value.

SQL> @test.sql
Enter a value for the first argument:banana

ARGUMENTS
----------
banana

SQL> @test.sql
Enter a value for the first argument:apple

ARGUMENTS
----------
apple

SQL> @test.sql pear

ARGUMENTS
----------
pear

1 row selected.

SQL>

2. PROMPT and HIDE (SQL*Plus Only)


Adding the HIDE keyword to the ARGUMENT command means the text we enter is no longer echoed to the screen. This is useful when entering passwords. We amend the previous script to add the HIDE keyword.

set verify off
argument 1 prompt "Enter a value for the first argument:" hide

column arguments format a10

select '&1' as arguments;

undefine 1

We test the amended script. In the first example we are prompted and provide the value "secret". Notice this is not echoed to the screen. In the second example we provide the value "secret2" on the command line, and we are not prompted to enter a value.

SQL> @test.sql
Enter a value for the first argument:

ARGUMENTS
----------
secret

1 row selected.

SQL> @test.sql secret2

ARGUMENTS
----------
secret2

1 row selected.

SQL>

3. DEFAULT


Not surprisingly the DEFAULT keyword allows us to define a default value to use if an argument is not specified. We amend the previous script to assign a default value.

set verify off
argument 1 default "banana"

column arguments format a10

select '&1' as arguments;

undefine 1

We test the amended script. If we don't provide a command line argument the default value of "banana" is used. If we provide the command line argument, it is used.

SQL> @test.sql

ARGUMENTS
----------
banana

1 row selected.

SQL> @test.sql apple

ARGUMENTS
----------
apple

1 row selected.

SQL>

4. ARGUMENT with DEFINE


In the previous examples we have used the arguments directly, but we could use them in conjunction with a DEFINE command. We amend the test script giving it the following contents. In this example we have used the first argument to define "arg1", which we use the in the subsequent query.

set verify off
argument 1 prompt "Enter a value for arg1:"
define arg1 = '&1';

column arguments format a10

select '&arg1' as arguments;

undefine 1

We test the amended script and it performs as expected.

SQL> @test.sql
Enter a value for arg1:banana

ARGUMENTS
----------
banana

1 row selected.

SQL> @test.sql apple

ARGUMENTS
----------
apple

1 row selected.

SQL>

5. ARGUMENT with VARIABLE


We can also use arguments with variables defined in SQL*Plus and SQLcl. We amend the test script giving it the following contents. In this example we have defined a variable called "var1" and set it to the value if the first argument. We use the variable in the subsequent query.

variable var1 varchar2(10);

set verify off
argument 1 prompt "Enter a value for var1:"
set feedback off
exec :var1:= '&1';
set feedback on

column arguments format a10

select :var1 as arguments;

undefine 1

We test the amended script and it performs as expected.

SQL> @test.sql
Enter a value for var1:banana

ARGUMENTS
----------
banana

1 row selected.

SQL> @test.sql apple

ARGUMENTS
----------
apple

1 row selected.

SQL>

Source: oracle-base.com