Closed11
CloudSQLのIAM認証を試す

以下のドキュメントを参考にCloudSQLのIam認証を試す。

デフォルトの設定でCloudSQLを作成する。
インスタンスのカスタマイズでフラグだけ追加。
cloudsql.iam_authentication
をオンにする。

Cloud-SQL-Auth-Proxy経由で接続する。
M1 Macだと以下コマンドでダウンロードして起動する。
curl -o cloud-sql-proxy https://storage.googleapis.com/cloud-sql-connectors/cloud-sql-proxy/v2.14.0/cloud-sql-proxy.darwin.arm64
chmod +x cloud-sql-proxy
./cloud-sql-proxy 接続名 # ここはいつもならProject-Id:Region:Instance-Name
別ターミナルでpsqlで接続する。
psql --host localhost --username=postgres --password
Password:
psql (17.4, server 16.8)
Type "help" for help.
postgres=> \l
List of databases
Name | Owner | Encoding | Locale Provider | Collate | Ctype | Locale | ICU Rules | Access privileges
---------------+-------------------+----------+-----------------+------------+------------+--------+-----------+-----------------------------------------
cloudsqladmin | cloudsqladmin | UTF8 | libc | en_US.UTF8 | en_US.UTF8 | | |
postgres | cloudsqlsuperuser | UTF8 | libc | en_US.UTF8 | en_US.UTF8 | | |
template0 | cloudsqladmin | UTF8 | libc | en_US.UTF8 | en_US.UTF8 | | | =c/cloudsqladmin +
| | | | | | | | cloudsqladmin=CTc/cloudsqladmin
template1 | cloudsqlsuperuser | UTF8 | libc | en_US.UTF8 | en_US.UTF8 | | | =c/cloudsqlsuperuser +
| | | | | | | | cloudsqlsuperuser=CTc/cloudsqlsuperuser
(4 rows)

Databaseをコンソールで作成する。
作成されたのでターミナルで確認する。
postgres=> \l
List of databases
Name | Owner | Encoding | Locale Provider | Collate | Ctype | Locale | ICU Rules | Access privileges
---------------+-------------------+----------+-----------------+------------+------------+--------+-----------+-----------------------------------------
cloudsqladmin | cloudsqladmin | UTF8 | libc | en_US.UTF8 | en_US.UTF8 | | |
eishow | cloudsqlsuperuser | UTF8 | libc | en_US.UTF8 | en_US.UTF8 | | |
postgres | cloudsqlsuperuser | UTF8 | libc | en_US.UTF8 | en_US.UTF8 | | |
template0 | cloudsqladmin | UTF8 | libc | en_US.UTF8 | en_US.UTF8 | | | =c/cloudsqladmin +
| | | | | | | | cloudsqladmin=CTc/cloudsqladmin
template1 | cloudsqlsuperuser | UTF8 | libc | en_US.UTF8 | en_US.UTF8 | | | =c/cloudsqlsuperuser +
| | | | | | | | cloudsqlsuperuser=CTc/cloudsqlsuperuser
(5 rows)

組み込みユーザーでテーブルを作成してみる。
postgres=> \c eishow
Password:
psql (17.4, server 16.8)
You are now connected to database "eishow" as user "postgres".
eishow=> \dt
Did not find any relations.
eishow=> CREATE TABLE sample_table (
eishow(> id SERIAL PRIMARY KEY,
eishow(> name VARCHAR(255) NOT NULL,
eishow(> age INTEGER,
eishow(> created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
eishow(> );
CREATE TABLE
eishow=> \dt
List of relations
Schema | Name | Type | Owner
--------+--------------+-------+----------
public | sample_table | table | postgres
(1 row)

IAM認証ユーザーをコンソールで作成して、そのユーザーでテーブルを作成してみる。
IAM認証に利用するユーザーにはcloudsql.instanceUser
ロールを付与していること。
IAM認証ユーザーの作成
デフォルトではデータベース権限がありません。
との表記あり。
必要な権限がcloudsql.instances.login
になる。
ターミナルで確認してみる。
postgres=> \du
List of roles
Role name | Attributes
---------------------------------+------------------------------------------------------------
cloudsqladmin | Superuser, Create role, Create DB, Replication, Bypass RLS
cloudsqlagent | Create role, Create DB
cloudsqlconnpooladmin |
cloudsqliamgroup | Cannot login
cloudsqliamgroupserviceaccount | Cannot login
cloudsqliamgroupuser | Cannot login
cloudsqliamserviceaccount | Cannot login
cloudsqliamuser | Cannot login
cloudsqlimportexport | Create role, Create DB
cloudsqllogical | Cannot login, Replication
cloudsqlobservability |
cloudsqlreplica | Replication
cloudsqlsuperuser | Create role, Create DB
IAM認証ユーザー |
postgres | Create role, Create DB

IAM認証ユーザーでログイン
cloud-sql-auth-proxy実行時に--auto-iam-authn
が必要。
別ターミナルでproxyを実行する。
./cloud-sql-proxy --auto-iam-authn 接続名 --port 6543 # portは組み込みユーザーのプロセスと重複するため。
別ターミナルで接続する。
失敗
- proxy実行時に
--port
を指定すると--host
はデフォルト127.0.0.1になる -
--dbname
を指定しないとき、--username
のDBを探しにいく
psql --host localhost --port 6543 --username=IAM認証ユーザー
psql: error: connection to server at "localhost" (::1), port 6543 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
connection to server at "localhost" (127.0.0.1), port 6543 failed: FATAL: database "IAM認証ユーザー" does not exist
成功
psql --host 127.0.0.1 --port 6543 --username=IAM認証ユーザー --dbname=eishow
psql (17.4, server 16.8)
Type "help" for help.

テーブルが見れるか確認する。
eishow=> \dt
List of relations
Schema | Name | Type | Owner
--------+--------------+-------+----------
public | sample_table | table | postgres
(1 row)
一覧は取得できそう。
テーブルが作成できるか確認する。
eishow=> CREATE TABLE sample_table2 (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
age INTEGER,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
ERROR: permission denied for schema public
LINE 1: CREATE TABLE sample_table2 (
^
できない。権限がないと言われる。

組み込みユーザーで権限付与してから再度テーブルを作成してみる。
eishow=> GRANT USAGE, CREATE ON SCHEMA public TO "IAM認証ユーザー";
GRANT
別ターミナルで確認してみる。
eishow=> CREATE TABLE sample_table2 ( id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
age INTEGER,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE
eishow=> \dt
List of relations
Schema | Name | Type | Owner
--------+---------------+-------+---------------------------------
public | sample_table | table | postgres
public | sample_table2 | table | IAM認証ユーザー
(2 rows)

Grantが必要なので組み込みユーザーは必須。
ただし、以下の使い分けができる。
考察
次の役割分担がいいのか?
ユーザー | 役割 | 権限 |
---|---|---|
組み込みユーザー | migration | Create、Insert、Update |
IAM認証ユーザー | workload、operation | Select、Insert、Update、Delete |

terraformでどこまで管理するか。
- DB
- ユーザー
DB内の管理は、Grant含めMigration側に責任をもたせるかが焦点。
このスクラップは2025/03/11にクローズされました