0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Snowflakeで参照権限の無いテーブルのデータをビューで表示する

Last updated at Posted at 2023-06-09

概要

Snowflakeではビューが参照しているテーブルの権限が無くても、そのビューを見ることができるとのことなので、実際に試してみたいと思います。

事前準備

DB,スキーマ,ロールを作成し、権限を割り当てていきます。

--DBを作成し、切り替える
create database test_DB;
use database test_DB;

--スキーマを2つ作成
create schema test_Schema_01;
create schema test_Schema_02;

--ロールを2つ作成
create role test_Role_01;
create role test_Role_02;

--ユーザーにロールを割り当てる
grant role test_Role_01 to user <ユーザー名>;
grant role test_Role_02 to user <ユーザー名>;

--ロールにウェアハウス、DBの参照権限を割り当てる
grant usage on warehouse COMPUTE_WH to test_Role_01;
grant usage on warehouse COMPUTE_WH to test_Role_02;
grant usage on database TEST_DB to test_Role_01;
grant usage on database TEST_DB to test_Role_02;

--ロール1にはスキーマ1とスキーマ2の参照権限とcreate権限を、ロール2にはスキーマ2の参照権限を割り当てる
grant usage on schema test_Schema_01 to test_Role_01;
grant usage on schema test_Schema_02 to test_Role_01;
grant create table, create view on schema test_Schema_01 to test_Role_01;
grant create table, create view on schema test_Schema_02 to test_Role_01;

grant usage on schema test_Schema_02 to test_Role_02;

上記実施後は下記の通りに権限が割り振られます。
ロール1はスキーマ1とスキーマ2へアクセス/テーブル・ビュー作成ができる、ロール2はスキーマ2へのみアクセスができる。

スキーマ\ロール test_Role_01 test_Role_02
test_Schema_01へのアクセス ×
test_Schema_01上でのcreate権限 ×
test_Schema_02へのアクセス
test_Schema_02上でのcreate権限 ×

確認

それでは表示されるか確認していきます。
まず、ロール1でスキーマ1にテーブルを作成します。

//ロール1でスキーマ1にテーブルを作成し、データをinsert 

use role test_Role_01;
use schema test_Schema_01;

create table test_Table( C1 VARCHAR(10) );

insert into test_Table values('test_data');

ロール2はスキーマ1へのアクセス権がないので、もちろんこのテーブルを確認することはできません。

use role test_Role_02;
select * from "TEST_SCHEMA_01".test_Table
;

--出力結果
SQLコンパイルエラー:
Schema 「TEST_DB.TEST_SCHEMA_01」は存在しないか、許可されていません。

次にこのテーブルをそのまま出力するビューをスキーマ2に作成し、ビューのselect権限をロール2に割り当てます。

use role test_Role_01;
use schema test_schema_02;

create view test_View
as
select * from "TEST_SCHEMA_01".test_table;

grant select on view test_View to test_Role_02;

このビューをロール2でselectします。

use role test_Role_02;
select * from test_View;

--出力結果

C1
test_data

データを見ることができました。
この時、ロール2は基テーブルの参照権限を保持していません。

show grants to role test_Role_02;
・・・ privilege granted_on name ・・・
・・・ USAGE DATABASE TEST_DB ・・・
・・・ USAGE SCHEMA TEST_DB.TEST_SCHEMA_02 ・・・
・・・ SELECT VIEW TEST_DB.TEST_SCHEMA_02.TEST_VIEW ・・・
・・・ USAGE WAREHOUSE COMPUTE_WH ・・・

まとめ

基テーブルのselect権限が無くてもビューを通じてデータを確認することができました。
スキーマにビューが追加される度にselect権限を与えるFUTUREによって、より効果的に権限の管理ができそうです。

grant select on future views in schema test_Schema_02 to role test_Role_02;

ここまでお読みくださりありがとうございました!

参考
Snowflake公式ドキュメント[アクセス制御の概要]
https://docs.snowflake.com/ja/user-guide/security-access-control-overview

Snowflake公式ドキュメント[オブジェクトに対する将来の付与の割り当て]
https://docs.snowflake.com/ja/user-guide/security-access-control-configure#label-granting-future-privs-on-schema-objects

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?