LoginSignup
7
3

More than 3 years have passed since last update.

CloudSQLのSQL監査ログを取得してみた!

Last updated at Posted at 2019-06-04

はじめに

CloudSQLのSQLクエリの実行内容を監査出来ないか検証したので、備忘録で記事にしてみました!
※CloudSQLは、MySQLまたはPostgreSQLのマネージドDBサービスです。(AWSでいうところのRDSですね)

【参考】
CloudSQLとは

利用環境

product version region database_id table name
PostgreSQL 9.6 us-west1 test testtable

やりたいこと

CloudSQLに格納されている機密情報に対して、不正なSQLクエリが実行されていないか監査したい。

設定方法

  • GCPのWeb管理コンソールにログイン後、[SQL]をクリックし、インスタンスIDをクリックします。
    image.png

  • [設定] > [設定の編集]をクリックします。
    image.png

  • [設定オプション] > [データベース フラグの追加]で[+項目を追加]をクリックします。
    image.png

  • [フラグを選択]をクリックし、[log_statement]を選択します。
    image.png

  • 全てのログを取得するように[all]を指定します。
    image.png

  • [保存]します。
    image.png

  • [設定]で内容に追加されていればOKです。
    image.png

【参考】
データベース フラグを構成する
log_statement

動作確認

  • [Cloud Shellを使用して接続]をクリックします。
    image.png

  • Cloud ShellでDB接続後、以下のように①テーブル作成②データ投入③データを参照を実施します。

Welcome to Cloud Shell! Type "help" to get started.
Your Cloud Platform project in this session is set to [PROJECT_ID].
Use “gcloud config set project [PROJECT_ID]” to change to a different project.
user@cloudshell:~ ([PROJECT_ID])$ gcloud sql connect test --user=postgres --quiet #作成したDBインスタンスに接続
Whitelisting your IP for incoming connection for 5 minutes...done.
Connecting to database with SQL user [postgres].Password for user postgres: #postgresユーザのパスワードを入力
psql (9.6.13, server 9.6.11)
SSL connection (protocol: TLSv1.2, cipher: ECDHE-RSA-AES128-GCM-SHA256, bits: 128, compression: off)
Type "help" for help.

postgres=> CREATE TABLE testtable (Name VARCHAR(255), Text VARCHAR(255)); #①testtableというテーブルを新規作成
CREATE TABLE
postgres=> INSERT INTO testtable (Name, Text) values ('Guest01', 'Test01'); #②データを適当に投入
INSERT 0 1
postgres=> SELECT * FROM testtable ; #③作成したtesttableから全データを参照
  name   |  text
---------+--------
 Guest01 | Test01
(1 row)
  • [SQL]から抜けて、[Logging]をクリックします。postgres.logを選択します。
    image.png

  • テーブル名(text:testtable)で検索します。(叩いたSQL文が3件出てきましたね)
    image.png

  • 上記のログのうち最後に実施したSELECT * FROM testtable;の中身は以下の通りです。

{
 insertId:  "s=80fd93ba8bdf46e28a8625aba4d6d6aa;i=256b;b=c048e1bb5f6044f9875dd774b312af0f;m=3fc50c74;t=58a802863540d;x=2d634c26fcfd4ebc-0@a4"  
 logName:  "projects/[PROJECT_ID]/logs/cloudsql.googleapis.com%2Fpostgres.log"  
 receiveTimestamp:  "2019-06-04T14:18:48.536405622Z"  
 resource: {
  labels: {
   database_id:  "[PROJECT_ID]:test"    
   project_id:  "[PROJECT_ID]"    
   region:  "us-west1"    
  }
  type:  "cloudsql_database"   
 }
 severity:  "INFO"  
 textPayload:  "[162]: [5-1] db=postgres,user=postgres LOG:  statement: SELECT * FROM testtable ;"  
 timestamp:  "2019-06-04T14:18:42.109868Z"  
}

まとめ

簡単でしたが、GCP CloudSQL(PostgreSQL)のSQL監査は無事取得できました!
MySQLも同様に取得できるようです。

Stackdriverに溜めたSQL監査ログをBigqueryにLoadして、監査業務に活用出来ると良い感じですね^^

7
3
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
7
3