1
1

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.

[SQLAlchemy] 神速の爆裂理解!!!コンソールログ上のパラメータをマスキングする方法!!!

Last updated at Posted at 2022-11-26

実装の目的

  • AWSのCloudWatchなどのコンソールログ上でお客様の個人情報を開発者の目に触れるのを防ぐため
    • SQLAlchemyではデフォルトでマスキングの設定はされていないため

説明しないこと

  • DBへの接続方法

DBセッティングファイルに以下の記述を追記

↓最初のログはこんな感じ

2022-11-02 16:37:03,292 INFO sqlalchemy.engine.Engine [generated in 0.00014s] (2151, 1, 99, '20220101', 1, 20)
INFO:sqlalchemy.engine.Engine:[generated in 0.00014s] (2151, 1, 99, '20220101', 1, 20)
2022-11-02 16:37:03,297 INFO sqlalchemy.engine.Engine INSERT INTO workplace (customer_id, company_name, kana_company_name, business_type, company_size, company_phone_number) VALUES (%s, %s, %s, %s, %s, %s)
INFO:sqlalchemy.engine.Engine:INSERT INTO workplace (customer_id, company_name, kana_company_name, business_type, company_size, company_phone_number) VALUES (%s, %s, %s, %s, %s, %s)
2022-11-02 16:37:03,297 INFO sqlalchemy.engine.Engine [generated in 0.00014s] (2151, 'ミミドン株式会社', 'ミミドンカブシキガイシャ', 30, 1, '08012345678')
INFO:sqlalchemy.engine.Engine:[generated in 0.00014s] (2151, 'ミミドン株式会社', 'ミミドンカブシキガイシャ', 30, 1, '08012345678')
2022-11-02 16:37:03,298 INFO sqlalchemy.engine.Engine COMMIT
INFO:sqlalchemy.engine.Engine:COMMIT
INFO:     127.0.0.1:55543 - "POST /application HTTP/1.1" 200 OK

DBとの接続設定ファイルに記載している
create_engine()の引数にhide_parameters=Trueを渡す

db_setting.py
# DBとの接続
ENGINE = create_engine(
    connection_url,
    # 文字コードを指定
    encoding="utf8mb4",
    # 自動生成されたSQLを吐き出すようにする
    echo=True,
    # これだね↓
    hide_parameters=True,
)

↓するとこんな感じでDBに格納するデータをマスキングすることが可能

INFO:sqlalchemy.engine.Engine:[generated in 0.00028s] [SQL parameters hidden due to hide_parameters=True]
2022-11-02 16:35:56,971 INFO sqlalchemy.engine.Engine INSERT INTO workplace (customer_id, company_name, kana_company_name, business_type, company_size, company_phone_number) VALUES (%s, %s, %s, %s, %s, %s)
INFO:sqlalchemy.engine.Engine:INSERT INTO workplace (customer_id, company_name, kana_company_name, business_type, company_size, company_phone_number) VALUES (%s, %s, %s, %s, %s, %s)
2022-11-02 16:35:56,971 INFO sqlalchemy.engine.Engine [generated in 0.00015s] [SQL parameters hidden due to hide_parameters=True]
INFO:sqlalchemy.engine.Engine:[generated in 0.00015s] [SQL parameters hidden due to hide_parameters=True]
2022-11-02 16:35:56,975 INFO sqlalchemy.engine.Engine COMMIT
INFO:sqlalchemy.engine.Engine:COMMIT
INFO:     127.0.0.1:55269 - "POST /application HTTP/1.1" 200 OK

参考

SQLAlchemy Engine Configuration

この記事が誰かの力になっていれば幸いです。

P.S. SQLAlchemyのドキュメント見辛すぎる・・・

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?