Upgrade to PRO for Only $50/YearโLimited-Time Offer! ๐ฅ
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
SQLAlchemy CustomTypes
Search
AhnSeongHyun
August 24, 2019
Programming
0
970
SQLAlchemyย CustomTypes
DB Encryption/Decryption using SQLAlchemy Customtypes
AhnSeongHyun
August 24, 2019
Tweet
Share
More Decks by AhnSeongHyun
See All by AhnSeongHyun
Django ORM์์๋ ์ด๋ป๊ฒ SQL Where์ ์กฐ๊ฑด ์์๋ฅผ ๊ณ ์ ํ ์ ์์๊น?
ahnseonghyun
0
310
๊ฐ๋ฐ์ ์ปค๋ฆฌ์ด : ๋๋ ค์์ด ์ด๋๋ ๋๋ก
ahnseonghyun
0
1.6k
๊ฐ๋ฐ์๋ ์ด๋ค ์ผ์ ํ๋ ๊ฑธ๊น?
ahnseonghyun
0
510
5๋ฒ์งธ ํ์ฌ ๊ทธ๋ฆฌ๊ณ ๋ ์ด๋์คํธ
ahnseonghyun
0
250
๋ฆฌ๋ถํธ : ๊ฒฝ๋ ฅ์์๊ฒ๋ ์์ต์ด ํ์ํ ์๊ฐ
ahnseonghyun
0
2.5k
MQTT ๋ฅผ ์ด์ฉํ ์ฃผ๋ฌธ ์์คํ ๊ฐ์
ahnseonghyun
0
1.2k
CNN MNIST Tutorial
ahnseonghyun
0
100
github:์ ์ฉํ ๊ธฐ๋ฅ๋ค
ahnseonghyun
0
1.8k
์นดํผ์บฃ์ผ๋ก ์์ํ๋ ์คํ์์ค
ahnseonghyun
0
1.5k
Other Decks in Programming
See All in Programming
ๅ จๅกใขใผใญใใฏใใงๆใใ ๅทจๅคงใง้ซๅฏๅบฆใชใใกใคใณใฎ็ด่งฃใๆน
agatan
8
20k
Why Kotlin? ้ปๅญใซใซใใ Kotlin ใง้็บใใ็็ฑ / Why Kotlin? at Henry
agatan
2
7k
How Software Deployment tools have changed in the past 20 years
geshan
0
29k
้็บใซๅฏใใใ่ชๅใในใใฎๅฎ็พ
goyoki
1
830
ใใคใใผใกใใฃใข้งๅใขใใชใฑใผใทใงใณใจIslandใขใผใญใใฏใใฃ: htmxใซใใWebใขใใชใฑใผใทใงใณ้็บใจๅ็UIใฎๅฑๆ็้ฉ็จ
nowaki28
0
400
Building AI Agents with TypeScript #TSKaigiHokuriku
izumin5210
6
1.3k
CSC509 Lecture 14
javiergs
PRO
0
220
ๅฎ ๅฎ ่ชไปฅ็บ็ๆตชๆผซ๏ผ่ท AI ไธ่ตท็บ่ชๅทฑ่พฆ็็ ่จๆๅฏซไธๅๅฎ็ฅจ็ณป็ตฑ
eddie
0
500
Context is King๏ผ ใVerifiabilityๆไปฃใจใณใณใใญในใ่จญ่จ / Beyond "Context is King"
rkaga
9
1.1k
็คพๅ ใชใใฌใผใทใงใณๆนๅใฎใใใฎTypeScript / TSKaigi Hokuriku 2025
dachi023
1
580
Go ใณใผใใใผในใฎๆงๆใจ AI ใณใณใใญในใๅฎ็พฉ
andpad
0
120
Integrating WordPress and Symfony
alexandresalome
0
150
Featured
See All Featured
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
359
30k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3.2k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
141
34k
Learning to Love Humans: Emotional Interface Design
aarron
274
41k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
15k
Automating Front-end Workflow
addyosmani
1371
200k
Building an army of robots
kneath
306
46k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
16
1.8k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.3k
Thoughts on Productivity
jonyablonski
73
5k
GitHub's CSS Performance
jonrohan
1032
470k
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
Transcript
SQLAlchemy CustomTypes ์์ฑํ
DB Encryption SQLAlchemy CustomTypes - TypeDecorator - Usage ๋ชฉ์ฐจ
์์ฑํ ๋ ์ด๋์คํธ ์๋ฒ ์์ง๋์ด ๊ฒ์, ๊ฒฐ์ , ํํ ํฌ ๋ฐํ์ ์๊ฐ @sh84ahn
DB Encryption
๊ฐ์ธ์ ๋ณด ๋ณดํธ๋ฒ ์ ์๊ธ์ต์ ๊ฑฐ๋๋ฒ WHY?
๊ณ ์ ์๋ณ์ ๋ณด ์ฌ์ฉ์ ์ด๋ฆ, ์๋ ์์ผ, ํด๋ํฐ ๋ฒํธ, ์ฌ๊ถ ๋ฒํธ, ์ฃผ๋ฏผ๋ฑ๋ก๋ฒํธ ๋ฑ๋ฑ
TARGET
๊ธ์ต๊ด๋ จ ์ ๋ณด ๊ฑฐ๋์ ๋ณด, ๊ฒฐ์ ์ ๋ณด ๋ฑ๋ฑ TARGET
AES256 HOW
AES256 SEED HOW
AES256 - pycrypto - pycryptodome : PyCryptodome is a fork
of PyCrypto HOW
SEED - C/C++, ASP, JAVA, PHP - HOW
์์ฉ ๋ฒค๋์ฌ - Python ์์ฒด๋ก ์์ฑ๋ ๊ฒ์ ์ ๋ฌด - c/c++,
.so ํ์ผ ์ฐ๋์ ์ํ ๊ฐ์ด๋ ์ ์ HOW
API PROJECT STRUCTURE PRESENTATION PERSISTENCE API Endpoint Use Case Data
BUSINESS LOGIC
API PROJECT STRUCTURE PRESENTATION BUSINESS LOGIC PERSISTENCE view.py service.py repo.py
API PROJECT STRUCTURE PRESENTATION BUSINESS LOGIC PERSISTENCE view.py service.py repo.py
์ด๋์ ์๋ณตํธํ๋ฅผ ํด์ผํ ๊น?
๊ฐ๋จํ ์์ ์ฌ์ฉ์์ ์ ๋ณด์ ์ ์ฅ/์กฐํํ๋ ๊ฐ๋จํ ์๋น์ค shorturl.at/QRVW1
๊ฐ๋จํ ์์ Presentation Business Logic Persistence Flask ๊ธฐ๋ฐ์ ์ฑ
๊ฐ๋จํ ์์ ์ฌ์ฉ์์ ์ ๋ณด๋ฅผ์ ์ฅํ๋ ํ ์ด๋ธ
๊ฐ๋จํ ์์ service : ๋น์ง๋์ค ๋ก์ง ์ฒ๋ฆฌ
๊ฐ๋จํ ์์ repo : ORM ์ฝ๋ ์ฒ๋ฆฌ
๋ฐฉ์ 1. service ์์ ์/๋ณตํธํ ์ํ ๋น์ง๋์ค ๋ก์ง + ์๋ณตํธํ
๋ฐฉ์ 1. service ์์ ์/๋ณตํธํ ์ํ Pros : - DB
๊ด๋ จ ๋ถ๋ถ์์ ์ํธํ๋ฅผ ์ ๊ฒฝ ์ธ ํ์๊ฐ ์๋ค. - ORM insert, update ์ฝ๋ ๊ทธ๋๋ก ์ํ.
๋ฐฉ์ 1. service ์์ ์/๋ณตํธํ ์ํ Cons: - ๋น์ง๋์ค ๋ก์ง์
์ฒ๋ฆฌํ๋ ๋ถ๋ถ์์ ์ํ? - ๋ณตํธํ์ ๋ฒ๊ฑฐ๋ก์ : - mapper instance ๋ด ์ปฌ๋ผ ๋ณ์๋ค์ด ์ํธํ ๋์ด ์์ - ์ปฌ๋ผ๋ณ ๋ณตํธํ๊ฐ ๋ถ๊ฐํผ ๊ฐ์ฅ ์์ฝ๊ฒ ์๊ฐํ ์ ์๋ ๋ฐฉ๋ฒ
๋ฐฉ์ 2. ๋ณตํธํ ๋ถ๋ถ์ ORM Mapper ๋ด property ๋ฅผ ํ์ฉ
๋ฐฉ์ 2. ๋ณตํธํ ๋ถ๋ถ์ ORM Mapper ๋ด property ๋ฅผ ํ์ฉ
Pros : - ์ปฌ๋ผ์ ๋ฐ๋ณต์ ์ธ ๋ณตํธํ ๋ถ๋ถ ์ ๊ฑฐ(๋ฐฉ์ 1์ ๋ํ ๋ณด์) - decrypt() ๋ฅผ service ์์ ํธ์ถํ์ง ์์๋ ๋๋ค.
๋ฐฉ์ 2. ๋ณตํธํ ๋ถ๋ถ์ ORM Mapper ๋ด property ๋ฅผ ํ์ฉ
Cons : - encrypt() ๋ ํธ์ถํ๊ณ ์๋๋ฐ? - User instance ์์ฒด๊ฐ ๋ณตํธํ๋ ๊ฐ์ ๋ค๊ณ ์์ง ์์ ์ํ - property ํธ์ถ์ ๋ง๋ค, ๋ณตํธํ ์ํ, ์ํธํ ์๋ฒ๋ผ๋ฉด ๋คํธ์ํฌ ๋น์ฉ ์ฆ๊ฐ ๋ฐ์ดํฐ๋ฅผ ๋ด๋ ํด๋์ค์๋ ๋ก์ง์ ๋ด๋ ๊ฒ์ ์ง์ํ์๋ ์๊ฒฌ encrypt(), decrypt() ๋ฅผ ํธ์ถํ๋ ๊ณณ์ด ๋ค๋ฅธ ํ์ผ/๋ค๋ฅธ ๋ ์ด์ด
๋ฐฉ์ 3. repo ์์ ์ํ
๋ฐฉ์ 3. repo ์์ ์ํ Pros : - DB ์ํธํ์ด๊ธฐ
๋๋ฌธ์ repo ์์ ์ํํ๊ธฐ ์ ์ - ๋น์ง๋์ค ๋ก์ง ๋ถ๋ฆฌ Cons: - insert, update, select ๋ฅผ ์ฌ์ฉํ๋ ํจ์๊ฐ ๋ง์์ง๋ฉด? ๋น์ทํ ๋ถ๋ถ ์ฌ์์ฑ! - ์ํธํ ํด์ผํ๋ Table ์ด ๋ง์์ง๋ฉด? ์ฌ์์ฑ! - ์ฌ์ ํ ์ปฌ๋ผ๋ณ ๋ณตํธํ ์ด์
๋ฐฉ์ 4. SQLAlchemy ์ ORM event ๋ฅผ ํ์ฉ DB ์ํธํ์
๊ด๋ จ๋ QUERY - encrypt : insert, update - decrypt : select ์ด๋ฒคํธ๋ฅผ ์ก์์ ์ฒ๋ฆฌ ํ ์๋ ์์๊น? Insert, update ๊ฐ ๋ฐ์ํ ๋ encrypt() ๋ฅผ ์ํ? Select ๊ฐ ๋ฐ์ํ ๋, decrypt() ๋ฅผ ์ํ?
๋ฐฉ์ 4. SQLAlchemy ์ ORM event ๋ฅผ ํ์ฉ ORM Event
- Attribute Event - Mapper Event - Instance Event - Session Event - Query Event ๋ค์ํ ์ด๋ฒคํธ ์ ๊ณต
๋ฐฉ์ 4. SQLAlchemy ์ ORM event ๋ฅผ ํ์ฉ INSERT, UPDATE
์ ์ํธํ
๋ฐฉ์ 4. SQLAlchemy ์ ORM event ๋ฅผ ํ์ฉ SELECT ์
๋ณตํธํ
๋ฐฉ์ 4. SQLAlchemy ์ ORM event ๋ฅผ ํ์ฉ Pros :
- ์ด๋ ์์ ์ ์๋ณตํธํ๋ฅผ ํ๋์ง ๋ช ํ - service, repo ๋ ์์์ ์ง Cons: - ์ปฌ๋ผ๋ณ ๊ฐ๋ณ ์๋ณตํธํ ์ด์ - Table ์ด ๋์ด๋๋ฉด? Handler ์ฆ๊ฐ? ์ข ๋ ์ข์ ๋ฐฉ๋ฒ์ด ์์๊น?
SQLAlchemy CustomTypes https://docs.sqlalchemy.org/en/13/core/custom_types.html#custom-types
Intro
Getting Started ๊ฐ๋จํ๊ฒ TestField ๋ฅผ ๋ง๋ค์ด์ ํ ์คํธ
Getting Started Apply TestField
Getting Started > repo.get(user_id) process_result_value : 20190101 process_result_value : ํ๊ธธ๋
process_result_value : 01012341234
Getting Started > repo.insert(user_name, birthday, phone_number) process_bind_param : ํ๊ธธ๋ process_bind_param
: 20190101 process_bind_param : 01012341234
SQLAlchemy CustomTypes TypeDecorator Class - ๊ธฐ์กด Type์ ๊ธฐ๋ฅ์ ์ถ๊ฐํ๋ ์ฌ์ฉ์
์ง์ Type ์์ฑ ๊ฐ๋ฅ - Subclassing of SQLAlchemyโs built-in types
TypeDecorator class variable required TypeEngine์ ์ฐธ์กฐ ์ด๋ค built-in type ์
subclass ์ธ์ง ๋ช ์
TypeDecorator process_bind_param - APP => DB - DBAPI ์ execute()
ํจ์๋ก ์ ๋ฌ๋ ๊ฐ(value) ๋ฅผ ์ ๋ฌ ๋ฐ๋๋ค. - value serializing, transformation - ์ฃผ์ : reverse operation - process_result_value()
TypeDecorator process_result_value - APP < = DB - DB ์์
๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ค๋ ๋ถ๋ถ(fetch)์ ํด๋น - a result-row column value to be converted - ์ฃผ์ : reverse operation - process_bind_param()
TypeDecorator bind_processor, result_processor - processor ํจ์๋ฅผ ์ ์ํด์ ๋๊ธธ ์ ์๋
ํํ - process_bind_param, process_result_value ์ฌ์ฉ ๊ถ์ฅ
Usage JSON Encoded
Usage phone_number masking
Usage AmountType https://techspot.zzzeek.org/2011/10/29/value-agnostic-types-part-ii/
TypeDecorator + DB Encrypt/Decrypt ์ปฌ๋ผ๋ณ ๋ณตํธํ ์ด์ ์ ๊ฑฐ
TypeDecorator + DB Encrypt/Decrypt ๋ก์ง ์ถ๊ฐ ์์ด ๊ทธ๋๋ก ์ฌ์ฉ
TypeDecorator + DB Encrypt/Decrypt ORM ๊ทธ๋๋ก ์ฌ์ฉ
TypeDecorator + DB Encrypt/Decrypt ์์ ๋น์ง๋์ค ๋ก์ง
๊ฒฐ๋ก DB์ํธํ๋ ํ์ ์ฌํญ ์ด๋์์ ์ํธํ๋ฅผ ํ ๊ฒ์ธ๊ฐ? - USE
CASE? DATA? ๊ณ ๋ คํด์ผํ ์ฌํญ: - ์๋ณตํธํ ํจ์๋ฅผ ํธ์ถ ํ๋ ๋ถ๋ถ ์ด์ - ORM Mapper ์ปฌ๋ผ๋ณ ๋ณตํธํ ์ด์ - ํ ์คํธ/์ ์ง๋ณด์
๊ฒฐ๋ก SQLAlchemy CustomTypes - TypeDecorator : ๊ธฐ์กด ํ์ + ์ถ๊ฐ๊ธฐ๋ฅ
=> ์ฌ์ฉ์ ์ปค์คํ ํ์ - process_bind_param : execute() ๋ก ์คํ๋๊ธฐ์ ๋ฐ์ธ๋ ๋๋ ๊ฐ์ ์กฐ์ - process_result_value : DB ์์ fetch ํ๋ ๋ฐ์ดํฐ์ ๋ํ ๊ฐ์ ์กฐ์
๊ฒฐ๋ก TypeDecorator + DB ์ํธํ ORM Mapper ํด๋์ค ์์ฒด์์ Column
๋ณ ์ง์ ๋ช ์์ ์ผ๋ก ์ด๋ค ์ปฌ๋ผ์ด ์๋ณตํธํ ๋์์ธ์ง ๋๊ตฌ๋ ์ ์ ์๋ค. ์ ์ง๋ณด์ ์๋ณตํธํ๋ฅผ ํธ์ถํ๋ ๋ถ๋ถ์ด EncryptedField ์์๋ง ์ํ ํ ์คํธ ์ฉ์ด ๋ณต์ก๋ ๊ฐ์ Business Logic ์ ์ฒ๋ฆฌํ๋ service ๋ ์์ Persistence ์ ํด๋นํ๋ repo ๋ ORM ๊ด๋ จ ์ฝ๋๋ง ์กด์ฌ
ั เขเณคืคืฎ.