Closed11

CloudSQLのIAM認証を試す

ei-showei-show

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

ei-showei-show

Cloud-SQL-Auth-Proxy経由で接続する。
https://cloud.google.com/sql/docs/mysql/connect-auth-proxy?hl=ja#install

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)
ei-showei-show

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)
ei-showei-show

組み込みユーザーでテーブルを作成してみる。

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)
ei-showei-show

IAM認証ユーザーをコンソールで作成して、そのユーザーでテーブルを作成してみる。
https://cloud.google.com/sql/docs/postgres/add-manage-iam-users?hl=ja#creating-a-database-user

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
ei-showei-show

IAM認証ユーザーでログイン
cloud-sql-auth-proxy実行時に--auto-iam-authnが必要。
https://cloud.google.com/sql/docs/postgres/iam-logins?hl=ja#log-in-with-automatic

別ターミナルで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.
ei-showei-show

テーブルが見れるか確認する。

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 (
                     ^

できない。権限がないと言われる。

ei-showei-show

組み込みユーザーで権限付与してから再度テーブルを作成してみる。

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)
ei-showei-show

Grantが必要なので組み込みユーザーは必須。
ただし、以下の使い分けができる。

考察
次の役割分担がいいのか?

ユーザー 役割 権限
組み込みユーザー migration Create、Insert、Update
IAM認証ユーザー workload、operation Select、Insert、Update、Delete
ei-showei-show

terraformでどこまで管理するか。

  • DB
  • ユーザー

DB内の管理は、Grant含めMigration側に責任をもたせるかが焦点。

このスクラップは2025/03/11にクローズされました