Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Active Record Encryption と AWS KMS でエンベロープ暗号化
Search
gedorinku
December 18, 2024
Programming
0
360
Active Record Encryption と AWS KMS でエンベロープ暗号化
gedorinku
December 18, 2024
Tweet
Share
More Decks by gedorinku
See All by gedorinku
Wantedly のバックエンドの将来に向けた取り組みと課題 - Wantedly Tech Night 2024/5
gedorinku
0
100
Porting mruby/c for the SNES (Super Famicom) - RubyKaigi 2024
gedorinku
0
4.2k
N+1 問題の解決と computed_model
gedorinku
0
69
部内での競プロ用ジャッジシステム
gedorinku
0
1.6k
部内ジャッジを作る話
gedorinku
1
98
プロラボ年度末報告会 HackDay / Hack U 福岡
gedorinku
0
160
Kotlin入門しました
gedorinku
0
270
Other Decks in Programming
See All in Programming
コンポーネントライブラリで実現する、アクセシビリティの正しい実装パターン
schktjm
1
600
Investigating Multithreaded PostgreSQL
macdice
0
130
データと事例で振り返るDevin導入の"リアル" / The Realities of Devin Reflected in Data and Case Studies
rkaga
3
3.4k
ソフトウェア品質特性、意識してますか?AIの真の力を引き出す活用事例 / ai-and-software-quality
minodriven
19
6.3k
型付け力を強化するための Hoogle のすゝめ / Boosting Your Type Mastery with Hoogle
guvalif
1
210
MLOps Japan 勉強会 #52 - 特徴量を言語を越えて一貫して管理する, 『特徴量ドリブン』な MLOps の実現への試み
taniiicom
2
350
推論された型の移植性エラーTS2742に挑む
teamlab
PRO
0
130
少数精鋭エンジニアがフルスタック力を磨く理由 -そしてAI時代へ-
rebase_engineering
0
110
TypeScript エンジニアが Android 開発の世界に飛び込んだ話
yuisakamoto
6
820
抽象データ型について学んだ
ryounasso
0
200
コードに語らせよう――自己ドキュメント化が内包する楽しさについて / Let the Code Speak
nrslib
5
570
テスト分析入門/Test Analysis Tutorial
goyoki
8
2.5k
Featured
See All Featured
Agile that works and the tools we love
rasmusluckow
329
21k
How STYLIGHT went responsive
nonsquared
100
5.6k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
Build your cross-platform service in a week with App Engine
jlugia
231
18k
Rebuilding a faster, lazier Slack
samanthasiow
81
9k
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
1
59
GitHub's CSS Performance
jonrohan
1031
460k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
26k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
Why Our Code Smells
bkeepers
PRO
336
57k
We Have a Design System, Now What?
morganepeng
52
7.6k
Fireside Chat
paigeccino
37
3.5k
Transcript
© 2024 Wantedly, Inc. Active Record Encryption と AWS KMS
でエンベロープ暗号化 Dec. 18 2024 - Ryota Egusa Wantedly Tech Night #6
© 2024 Wantedly, Inc. Ryota Egusa @gedorinku Quality Control Squad
https://www.wantedly.com/companies/ wantedly/post_articles/459966
© 2024 Wantedly, Inc. モチベーション データにアクセスできる範囲を制限したい • 万が一の漏洩時の被害を抑える 1. アクセスに対して監査ログを残したい
• KMS のようなキーマネージメントサービスの機能 • DB レベルで行うこともできる (例: pgAudit) 2.
© 2024 Wantedly, Inc. エンベロープ暗号化 Root Key Data Key Data
暗号化 暗号化 データごとに⽣成
© 2024 Wantedly, Inc. AWS KMS + エンベロープ暗号化 Root Key
Data Key Data 暗号化 暗号化 AWS KMS Data Key の暗号化‧復号は KMS の中で⾏う ここは各アプリケーション 内で⾏う
© 2024 Wantedly, Inc. AWS KMS のメリット キーの使用時にログが残る • Root
key が AWS KMS の外に出ないので必ずログが残る 1. Root key 自体は AWS が保護してくれる • AWS のアクセスキーが漏れることはあっても Root key が漏れる心配はなさそう 2.
© 2024 Wantedly, Inc. Active Record Encryption class User encrypts
:foo end user = User.create(foo: "hoge") user.foo # => "hoge"
© 2024 Wantedly, Inc. 標準のKeyProvider DerivedSecretKeyProvider • デフォルト • アプリケーション全体で単一のキーを使って暗号化
1. EnvelopeEncryptionKeyProvider • 暗号化毎にデータキーを生成、指定した Root key で暗号化 • 暗号化されたデータキーと暗号化されたデータを一緒に保存 2.
© 2024 Wantedly, Inc. Active Record Encryption class CustomKeyProvider def
encryption_key end def decryption_keys(encrypted_message) end end 暗号化⽤のData Keyを返す
© 2024 Wantedly, Inc. Active Record Encryption class CustomKeyProvider def
encryption_key end def decryption_keys(encrypted_message) end end 復号⽤のData Keyを返す
© 2024 Wantedly, Inc. Active Record Encryption class CustomKeyProvider def
encryption_key end def decryption_keys(encrypted_message) end end ドキュメントに詳しい説明はないが デフォルトの実装が参考になる https://github.com/rails/rails/blob/main/activerecord/lib/ active_record/encryption/envelope_encryption_key_prov ider.rb
© 2024 Wantedly, Inc. CustomKeyProvider#encryption_key resp = kms_client.generate_data_key( key_id: ENV.fetch('AWS_KMS_KEY_ID'),
key_spec: 'AES_256', ) ActiveRecord::Encryption::Key.new(resp.plaintext).tap do |key| key.public_tags.encrypted_data_key = resp.ciphertext_blob end 暗号化されずに保存される
© 2024 Wantedly, Inc. CustomKeyProvider#decryption_keys encrypted_data_key = encrypted_message.headers.encrypted_data_key response =
kms_client.decrypt({ ciphertext_blob: encrypted_data_key, key_id: ENV.fetch('AWS_KMS_KEY_ID') }) [ActiveRecord::Encryption::Key.new(response.plaintext)]
© 2024 Wantedly, Inc. まとめ アプリケーションレベルの暗号化によって被害を抑えログを残す • データにアクセスできる範囲を限定し、ログを残すことができる 1. Rails
と AWS KMS などを組み合わせて透過的に暗号化できる • 暗号化キーをRailsに渡す部分だけ作る必要がある • 暗号化処理はRailsやKMSがやってくれる 2.