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
670
0
Share
Active Record Encryption と AWS KMS でエンベロープ暗号化
gedorinku
December 18, 2024
More Decks by gedorinku
See All by gedorinku
Wantedly のバックエンドの将来に向けた取り組みと課題 - Wantedly Tech Night 2024/5
gedorinku
0
160
Porting mruby/c for the SNES (Super Famicom) - RubyKaigi 2024
gedorinku
0
4.8k
N+1 問題の解決と computed_model
gedorinku
0
120
部内での競プロ用ジャッジシステム
gedorinku
0
1.8k
部内ジャッジを作る話
gedorinku
1
120
プロラボ年度末報告会 HackDay / Hack U 福岡
gedorinku
0
170
Kotlin入門しました
gedorinku
0
290
Other Decks in Programming
See All in Programming
ReactとSvelteのその先、Ripple-TS / Beyond React and Svelte: Ripple-TS
ssssota
1
320
権限チェックの一貫性を型で守る TypeScript による多層防御
mnch
2
180
運転動画を検索可能にする〜Cosmos-Embed1とDatabricks Vector Searchで〜/cosmos-embed1-databricks-vector-search
studio_graph
3
960
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
420
ソースコード→AST→オペコード、の旅を覗いてみる
o0h
PRO
1
140
Migrations : C'est une question d'hygiène !
vinceamstoutz
0
680
AlarmKitで明後日起きれるアラームアプリを作る
trickart
0
140
AIベース静的検査器の偽陽性率を抑える工夫3選
orgachem
PRO
4
460
Stage 3 Decorators でできること / できないこと / TSKaigi 2026
susisu
0
140
次世代リンターで探る、tsgo 時代における型認識カスタムルールの現実解
ytakahashii
0
220
「OSSがあるなら自作するな」は AI時代も正しいか ── Build vs Adopt の新しい判断基準
kumorn5s
7
2.8k
横断組織出身のQAEがインプロセスQAEでつまずいたこと・活かせたこと
ty89
0
150
Featured
See All Featured
Chasing Engaging Ingredients in Design
codingconduct
0
190
Gemini Prompt Engineering: Practical Techniques for Tangible AI Outcomes
mfonobong
2
400
How Software Deployment tools have changed in the past 20 years
geshan
0
33k
Beyond borders and beyond the search box: How to win the global "messy middle" with AI-driven SEO
davidcarrasco
3
130
The Curse of the Amulet
leimatthew05
1
12k
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
280
Ecommerce SEO: The Keys for Success Now & Beyond - #SERPConf2024
aleyda
1
2k
Evolving SEO for Evolving Search Engines
ryanjones
0
200
Faster Mobile Websites
deanohume
310
31k
What’s in a name? Adding method to the madness
productmarketing
PRO
24
4k
The AI Revolution Will Not Be Monopolized: How open-source beats economies of scale, even for LLMs
inesmontani
PRO
3
3.4k
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
3
570
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.