2025年9月1日月曜日

Oracle Database 19c EEにEntra IDでユーザー認証してReal Application Securityを適用する

先日の記事「Oracle Database 19c EEをTLS有効にしてEntra IDでユーザー認証できるように設定する」で作成したデータベースに、Real Application Securityによる保護を構成します。記事「Entra IDで認証したMCPサーバーにReal Application Securityを適用する」と全く同じ保護を適用しますが、対象のデータベースはAutonomous Databaseの23aiではなく、手元の仮想マシンで実行しているOracle Database 19c EEです。そのため、セクション「管理ユーザーRASADMINの作成と各種権限の付与」の作業をオンプレミス向けに更新します。続くセクション「Real Application Securityの構成」は全く同じ作業になります。

以下よりオンプレミスのOracle Database 19c EEを対象として、Real Application Securityを構成する手順を確認します。確認する手順は、RASの管理ユーザーRASADMINを作成するまでになります。

Oracle Database 19c EEはVirtualBoxの仮想マシンで動作しています。ネットワークのポートフォワーディングを構成することにより、ホスト・マシンからポート1521および1522を宛先としてSQL*Net接続できるようにしています。SIDはORCL、PDBとしてORCLPDB1が作成されています。

作業はホスト・コンピュータ上で実施します。データベースへの接続にはSQLclを使用します。

ウォレットやtnsnames.oraが保存されているディレクトリを環境変数TNS_ADMINに設定したのち、作業を進めます。

export TNS_ADMIN=$PWD/tns_admin

orcl19c % export TNS_ADMIN=$PWD/tns_admin 

orcl19c % 


最初に、Real Application Securityの確認に使用するHRスキーマをインストールします。サンプル・データセットをダウンロードしたのち、解凍します。

curl -OL https://github.com/oracle-samples/db-sample-schemas/archive/refs/tags/v23.3.zip
unzip v23.3.zip


orcl19c % curl -OL https://github.com/oracle-samples/db-sample-schemas/archive/refs/tags/v23.3.zip

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current

                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0

100 11.6M    0 11.6M    0     0  3600k      0 --:--:--  0:00:03 --:--:-- 4046k

orcl19c % unzip v23.3.zip

Archive:  v23.3.zip

e3325a83e56c516815844025418a96ecaf219751

   creating: db-sample-schemas-23.3

 extracting: db-sample-schemas-23.3/.gitignore  

  inflating: db-sample-schemas-23.3/LICENSE.txt  

  inflating: db-sample-schemas-23.3/README.md  

  inflating: db-sample-schemas-23.3/README.txt  

  inflating: db-sample-schemas-23.3/SECURITY.md  

   creating: db-sample-schemas-23.3/customer_orders


[中略]


  inflating: db-sample-schemas-23.3/sales_history/sh_populate.sql  

  inflating: db-sample-schemas-23.3/sales_history/sh_uninstall.sql  

  inflating: db-sample-schemas-23.3/sales_history/supplementary_demographics.csv 

  inflating: db-sample-schemas-23.3/sales_history/times.csv  

orcl19c %                


サンプル・データセットのhuman resourcesを、PDBのORCLPDB1にインストールします。human resourceのディレクトリへ移動し、インストール・スクリプトhr_install.sqlを実行します。

cd db-sample-schemas-23.3/human_resources
@hr_install


orcl19c % cd db-sample-schemas-23.3/human_resources

human_resources % sql sys@localhost/orclpdb1 as sysdba



SQLcl: 月 9月 01 13:03:39 2025のリリース25.2 Production


Copyright (c) 1982, 2025, Oracle.  All rights reserved.


パスワード (**********?) ******

接続先:

Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production

Version 19.19.0.0.0


SQL> @hr_install


Thank you for installing the Oracle Human Resources Sample Schema.

This installation script will automatically exit your database session

at the end of the installation or if any error is encountered.

The entire installation will be logged into the 'hr_install.log' log file.


Enter a password for the user HR: ******



Enter a tablespace for HR [USERS]: 

Do you want to overwrite the schema, if it already exists? [YES|no]: 

******  Creating REGIONS table ....


Table REGIONSは作成されました。



INDEX REG_ID_PKは作成されました。



Table REGIONSが変更されました。


******  Creating COUNTRIES table ....


Table COUNTRIESは作成されました。


[中略]


コミットが完了しました。



Installation verification    

____________________________ 

Verification:                


Table             provided    actual 

______________ ___________ _________ 

regions                  5         5 

countries               25        25 

departments             27        27 

locations               23        23 

employees              107       107 

jobs                    19        19 

job_history             10        10 


Thank you!                                                  

___________________________________________________________ 

The installation of the sample schema is now finished.      

Please check the installation verification output above.    

                                                            

You will now be disconnected from the database.             

                                                            

Thank you for using Oracle Database!                        

                                                            

Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production

Version 19.19.0.0.0から切断されました

human_resources % 


データベース・ユーザーRASADMINを作成し、最低限必要な権限を与えます。作業はユーザーSYSで実施します。

create user rasadmin identified by <パスワード>;
alter user rasadmin quota 25m on users;
grant create session to rasadmin;
grant create table, create procedure to rasadmin;


orcl19c % sql sys@localhost/orclpdb1 as sysdba 



SQLcl: 月 9月 01 13:07:58 2025のリリース25.2 Production


Copyright (c) 1982, 2025, Oracle.  All rights reserved.


パスワード (**********?) ******

接続先:

Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production

Version 19.19.0.0.0


SQL> create user rasadmin identified by ********;


User RASADMINは作成されました。


SQL> alter user rasadmin quota 25m on users;


User RASADMINが変更されました。


SQL> grant create session to rasadmin;


Grantが正常に実行されました。


SQL> grant create table, create procedure to rasadmin;


Grantが正常に実行されました。


SQL> 


ユーザーRASADMINに、Real Application Securityを構成するために必要な権限を与えます。パッケージXS_ADMIN_CLOUD_UTILの代わりにXS_ADMIN_UTILを呼び出します。
begin
    sys.xs_admin_util.grant_system_privilege('PROVISION','RASADMIN');
    sys.xs_admin_util.grant_system_privilege('ADMIN_ANY_SEC_POLICY','RASADMIN');
end;
/

SQL> begin

  2      sys.xs_admin_util.grant_system_privilege('PROVISION','RASADMIN');

  3      sys.xs_admin_util.grant_system_privilege('ADMIN_ANY_SEC_POLICY','RASADMIN');

  4  end;

  5* /


PL/SQLプロシージャが正常に完了しました。


SQL> 


ロールHR_ROLEを作成し、ユーザーRASADMINに割り当てます。ロールHR_ROLEは後ほど、ユーザーRASADMINが作成したアプリケーション・ロールEMPLOYEEに割り当てます。
create role hr_role;
grant select on hr.departments to hr_role;
grant select on hr.employees to hr_role;
grant select on hr.jobs to hr_role;
grant select on hr.job_history to hr_role;
grant select on hr.locations to hr_role;
grant select on hr.regions to hr_role;
grant select on hr.countries to hr_role;
grant select on hr.emp_details_view to hr_role;
grant hr_role to rasadmin with admin option;

SQL> create role hr_role;


Role HR_ROLEは作成されました。


SQL> grant select on hr.departments to hr_role;


Grantが正常に実行されました。


SQL> grant select on hr.employees to hr_role;


Grantが正常に実行されました。


SQL> grant select on hr.jobs to hr_role;


Grantが正常に実行されました。


SQL> grant select on hr.job_history to hr_role;


Grantが正常に実行されました。


SQL> grant select on hr.locations to hr_role;


Grantが正常に実行されました。


SQL> grant select on hr.regions to hr_role;


Grantが正常に実行されました。


SQL> grant select on hr.countries to hr_role;


Grantが正常に実行されました。


SQL> grant select on hr.emp_details_view to hr_role;


Grantが正常に実行されました。


SQL> grant hr_role to rasadmin with admin option;


Grantが正常に実行されました。


SQL> 


MCPサーバーの接続ユーザーMCPUSERに、RASセッションを操作する権限を与えます。呼び出すパッケージはXS_ADMIN_UTILになります。
begin
    sys.xs_admin_util.grant_system_privilege('ADMINISTER_SESSION','MCPUSER');
end;
/

SQL> begin

  2      sys.xs_admin_util.grant_system_privilege('ADMINISTER_SESSION','MCPUSER');

  3  end;

  4* /


PL/SQLプロシージャが正常に完了しました。


SQL> 


Autonomous DatabaseでRASを構成したときは、ユーザーMCPUSERにスキーマHR以下のオブジェクトを検索する権限を与えていました。

grant select any table on schema hr to mcpuser;

今回はこの権限は与えません。ロールHR_ROLEはRASの動的ロールEMPLOYEEとしてRASユーザーに割り当てられるため、必ずしもユーザーMCPUSERが同じ権限を持つ必要はありません。

ユーザーMCPUSERがスキーマHRのオブジェクトを検索する権限を持たない場合、RASポリシーが適用される前は、スキーマHRのオブジェクト自体が見えません。そのため、表HR.EMPLOYEESを検索すると「ORA-00942: 表またはビューが存在しません。」が発生します。検索する権限がある場合は「行が選択されていません」が返されます。

以上でRASの管理ユーザーRASADMINが作成できました。

これ以降は記事「Entra IDで認証したMCPサーバーにReal Application Securityを適用する」の「Real Application Securityの構成」に記載されている作業を行ないます。

パッケージEMP_DEPT_CTX_PKGを作成し、データベース・ユーザーMCPUSERに実行権限を与えるところまでを実施します。


動作確認



SQLclのMCPサーバーからOracle Database 19c EEのデータベースに接続し、Real Application Securityによる保護を確認します。

MCPサーバーからのデータベース接続をEntra IDで認証するには、TOKEN_AUTHにAZURE_DEVICE_CODEは使用できません。OAUTHかAZURE_INTERACTIVEのどちらかを設定することになりますが、AZURE_INTERACTIVEでユーザー認証するにはURLの書き換えが必要なため、今回はTOKEN_AUTH=OAUTHで接続することにします。

tnsnames.oraにTNS名として、以下のORCLPDB1_TLS_OAUTHを追記します。TOKEN_LOCATIONは、コマンドaz account get-access-tokenを実行して取り出すアクセス・トークンの保存先となるファイルを指定します。
ORCLPDB1_TLS_OAUTH =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCPS)(HOST = 0.0.0.0)(PORT = 1522))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = ORCLPDB1)
    )
    (SECURITY =
      (SSL_SERVER_DN_MATCH=TRUE)
      (SSL_SERVER_CERT_DN="CN=orcl19c")
      (TOKEN_AUTH=OAUTH)
      (TOKEN_LOCATION=/Users/________/Documents/orcl19c/token.txt)
    )
  )
azure-cli(azコマンド)を実行して、データベースへの認証に使用するアクセス・トークンを取得します。

Entra IDにサインインします。

az login --tenant "テナントID" --scope "アプリケーションIDのURI/session:scope:connect"

ブラウザのサインイン画面が開きます。以下はすでにサインイン済みだったので、アカウントの選択のみを求められています。サインインするユーザーを選択します。


サインインが完了します。


コマンドaz loginを実行したターミナルでは、サブスクリプションの選択を求められます。サブスクリプションは1つしかないので、デフォルトのサブスクリプションを選択しました。

以上で、Entra IDにサインインできました。

orcl19c % az login --tenant "********-****-****-****-************" --scope "https://_________outlook.onmicrosoft.com/********-****-****-****-************/session:scope:connect"

A web browser has been opened at https://login.microsoftonline.com/********-****-****-****-************/oauth2/v2.0/authorize. Please continue the login in the web browser. If no web browser is available or if the web browser fails to open, use device code flow with `az login --use-device-code`.


Retrieving subscriptions for the selection...


[Tenant and subscription selection]


No     Subscription name     Subscription ID                       Tenant

-----  --------------------  ------------------------------------  ------------------------------------

[1] *  Azure subscription 1  ********-****-****-****-************  ********-****-****-****-************


The default is marked with an *; the default tenant is '********-****-****-****-************' and subscription is 'Azure subscription 1' (********-****-****-****-************).


Select a subscription and tenant (Type a number or Enter for no changes): 


Tenant: ********-****-****-****-************

Subscription: Azure subscription 1 (********-****-****-****-************)


[Announcements]

With the new Azure CLI login experience, you can select the subscription you want to use more easily. Learn more about it and its configuration at https://go.microsoft.com/fwlink/?linkid=2271236


If you encounter any problem, please open an issue at https://aka.ms/azclibug


[Warning] The login output has been updated. Please be aware that it no longer displays the full list of available subscriptions by default.


ynakakoshi@Ns-Macbook orcl19c % 


アクセス・トークンを取得します。token.txtの出力先は、ORCLPDB1_TLS_OAUTHTOKEN_LOCATIONとして指定した位置です。

az account get-access-token --scope "アプリケーションIDのURI/session:scope:connect" --query accessToken -o tsv > token.txt

orcl19c % az account get-access-token --scope "https://________outlook.onmicrosoft.com/********-****-****-****-************/session:scope:connect" --query accessToken -o tsv > token.txt

orcl19c % 


最初にSQLclからORCLPDB1_TLS_OAUTHに接続し、MCPサーバーから利用できるように接続を保存します。

sql /nolog
conn -save orclpdb1_tls_oauth -savepwd /@orclpdb1_tls_oauth
select sys_context('userenv','authentication_method') from dual;
exit

AUTHENTICATION_METHODTOKEN_GLOBALであることを確認します。

orcl19c % sql /nolog



SQLcl: 月 9月 01 14:01:05 2025のリリース25.2 Production


Copyright (c) 1982, 2025, Oracle.  All rights reserved.


SQL> conn -save orclpdb1_tls_oauth -savepwd /@orclpdb1_tls_oauth

名前: orclpdb1_tls_oauth

接続文字列: orclpdb1_tls_oauth

ユーザー: 

パスワード: 未保存

接続しました.

SQL> select sys_context('userenv','authentication_method') from dual;


SYS_CONTEXT('USERENV','AUTHENTICATION_METHOD')    

_________________________________________________ 

TOKEN_GLOBAL                                      


SQL> exit

Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production

Version 19.19.0.0.0から切断されました

orcl19c % 


SQLclのMCPサーバーの動作確認に、MCP Inspectorを使用します。

MCP Inspectorを起動します。

npx -y @modelcontextprotocol/inspector

ブラウザにMCP Inspectorの画面が開きます。

orcl19c % npx -y @modelcontextprotocol/inspector


Starting MCP inspector...

⚙️ Proxy server listening on localhost:6277

🔑 Session token: c2381f2821873d9179a38ee659ec7ba12796fe14093229ecd912d7ff4022f954

   Use this token to authenticate requests or set DANGEROUSLY_OMIT_AUTH=true to disable auth


🚀 MCP Inspector is up and running at:

   http://localhost:6274/?MCP_PROXY_AUTH_TOKEN=c2381f2821873d9179a38ee659ec7ba12796fe14093229ecd912d7ff4022f954


🌐 Opening browser...



MCP Inspectorの画面で以下の設定を行ないます。

Transport TypeSTDIOを選択し、CommandSQLcl(コマンドとしてはsql)をフルパスで指定します。

次にAdd Environment Variableをクリックし、追加されたEnvironment VariableとしてTNS_ADMINとそのディレクトリを設定します。

以上の設定を実施し、ボタンConnectをクリックしてSQLclのMCPサーバーを起動します。


MCPサーバーの起動を確認し、Toolsタブを開きます。List Toolsをクリックし、呼び出すことができるツールを取得します。


Toolsよりlist-connectionsを選択し、Run Toolをクリックします。Tool Resultに先ほど作成した接続orclpdb1_tls_oauthが含まれていることを確認します。


Toolsよりconnectを選択します。connection_nameorclpdb1_tls_oauthを記述し、Run Toolsをクリックします。

Tool ResultSuccessが返され、データベースに接続できたことを確認します。


Toolsよりrun-sqlを選択します。sqlに以下をを記述し、Run Toolsをクリックします。

select * from hr.employees

まだRASのポリシーが有効化されていず、また、データベース・ユーザーMCPUSERにスキーマHRの持つ表の参照権限を与えていないため、ORA-00942が発生します。


RASポリシーを有効化します。

Toolsよりrun-sqlclを選択します。sqlclに以下をを記述し、Run Toolsをクリックします。

exec rasadmin.emp_dept_ctx_pkg.init;

Tool ResultとしてSuccessが返され、プロシージャが実行されたことを確認します。


Toolsよりrun-sqlを選択します。sqlに以下をを記述し、Run Toolsをクリックします。

select employee_id, first_name, last_name, email, salary, commission_pct, manager_id, department_id from hr.employees;

RASポリシーが適用されて、DEPARTMENT_IDが60の行のみ(Entra IDで認証されたユーザーと同じ部署に所属している従業員)が返されていることを確認します。


以上でオンプレミスのOracle Database 19c EEについても、Entra IDによるユーザー認証およびReal Application Securityによる保護が適用ができることを確認しました。

Toolsよりdisconnectを選択し、Run Toolsをクリックします。


Disconnectをクリックします。


SQLclのMCPサーバーが終了しないという問題があります。これはLLMがいつMCPサーバーを終了させてよいのか判断できないから発生しているのかと考えていましたが、MCP InspectorでdisconnectをしてもSQLclのMCPサーバーは終了しないようです。どうもSQLcl側にも問題がありそうです。

今回の記事は以上になります。