Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Go×RLSで複数テナントのデータを安全に扱う/secure-data-access-with...
Search
Sh0He1666
October 02, 2023
Programming
1.1k
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Go×RLSで複数テナントのデータを安全に扱う/secure-data-access-with-go-rls-in-multi-tenant-environment
Sh0He1666
October 02, 2023
More Decks by Sh0He1666
See All by Sh0He1666
ログ調査で分からなかった原因が、New Relicのトランザクションで一瞬で解決した話
sh0he1666
0
36
Other Decks in Programming
See All in Programming
[2026-10-14](C)Re:ゼロから始める異世界生活〜始まりの終わりと終わりの始まり〜
tosite
0
150
ChromeでローカルLLM
ryu1013
0
160
TanStack Vue Query と Pinia Colada から学ぶ非同期状態の型設計
aoshi
0
190
Groovy as a human and AI friendly programming language
paulk
0
110
SFCで実現する病院・医療システムの動的組み立て式UI
koukimiura
1
110
當我們開始不讀 Code - AI Agent 時代,Code Review 與 Clean Code 還剩下什麼?
jame2408
1
310
Next.js: The final boss of Web Performance
guaca
0
340
AI主導のプログラミング学習アプリを作ってみた
econanringo
0
190
Agentic Software Factoryに、すごく賢いIF文を。 / super smart IF statement into the Agentic Software Factory.
rkaga
8
2.4k
contenteditable と日本語入力に向き合う
colorful12
0
220
あなたの await、OS まで届いてる? strace で覗く JavaScript の非同期
hasutosasaki
1
1.4k
Coding with AI: How to trust the storyteller (AWS Community Day Adria, Ljubljana 2026)
slobodan
0
120
Featured
See All Featured
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.9k
BBQ
matthewcrist
90
10k
The agentic SEO stack - context over prompts
schlessera
0
980
AI Search: Where Are We & What Can We Do About It?
aleyda
0
8.1k
Organizational Design Perspectives: An Ontology of Organizational Design Elements
kimpetersen
PRO
1
860
Odyssey Design
rkendrick25
PRO
2
890
We Have a Design System, Now What?
morganepeng
55
8.4k
Data-driven link building: lessons from a $708K investment (BrightonSEO talk)
szymonslowik
1
1.4k
Imperfection Machines: The Place of Print at Facebook
scottboms
270
14k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.7k
Money Talks: Using Revenue to Get Sh*t Done
nikkihalliwell
0
540
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
20k
Transcript
Go×RLSで複数テナントのデータを安全に扱う 2023/09/28 golang.Tokyo#33 スプリームシステム株式会社 プロダクトディベロップメント部 徳丸 翔平
自己紹介 • 徳丸 翔平(@shohei36) • 普段の業務では主にJavaやGoを使ってバックエンド開発をやってます • Go歴はもう少しで1年といったところ • 低レイヤーに興味あり
モノリスからマイクロサービスへ https://acropolium.com/blog/migrating-monolith-to-microservices/
マルチテナントのデータベース戦略 https://medium.com/one9-tech/which-database-structure-to- use-in-multi-tenant-application-6f1b9af09634
マルチテナントのデータベース戦略 https://medium.com/one9-tech/which-database-structure-to- use-in-multi-tenant-application-6f1b9af09634
データベース の Row-Level Security(RLS)機能 データの読み込み時にテーブルの行レベルでアクセスを制御するデータベースの機能 テナントID ID NAME tenant01 1000
Bob tenant02 1000 Alice tenant03 1000 Taro tenant03 1000 Hanako SELECT ID, NAME FROM SOME_TABLE WHERE ID = ‘1000’ 「tenant02」が接続
プログラム側も工夫が必要… 🤔 RLSが効いたコネクションを正しく制御するには? 🤔 DBに依存せずにトランザクションをどのように表現するか? 🤔 テナント追加時の作業をゼロにしたい、、
プログラム設計方針 以下のアプローチによって実現 • データベースセッションの環境変数にセットしたテナントIDでアク セス権を制御 • Context 経由で sql.Tx を
リポジトリ にパスする
プログラム設計方針 以下のアプローチによって実現 • データベースセッションの環境変数にセットしたテナントIDでアク セス権を制御 • Context 経由で sql.Tx を
リポジトリ にパスする こちらのサンプルコードを使用して説明します https://github.com/shohei36/go-rls
環境 • WSL2 Ubuntu 20.04 on Windows • PostgreSQL 13.2
• Go 1.20
サンプルコードの構成 // アプリケーション固有のビジネスルール // トランザクション // DBなど外部システムとのアダプター // ドメインモデル //
Main関数(controllerも兼ねる) ※サンプルコードの構成 Clean Architecture
例)会員情報を更新するユースケース
登場人物
DoInTxを実行 usecase.go
sql.DB からコネクション(sql.Conn)を取得 transaction.go db.go
DBセッションの環境変数にテナントIDをセット db.go
補足:テーブルにRLSを適用するDDL pgsql/init/001_ddl.sql
sql.DBのコネクションはスレッドセーフ sql.DB の仕組み https://please-sleep.cou929.nu/go-sql-db-connection-pool.html 公式ドキュメント https://pkg.go.dev/database/sql#DB
トランザクション(sql.Tx)開始 transaction.go DoInTx
Context に sql.Tx をセット transaction.go DoInTx Sql.Tx を取り出すときに使うKey
トランザクション内の処理を実行 transaction.go DoInTx usecase.go
リポジトリのUpdateメソッドをCall usecase.go
DoInTx内でContextにセットしたsql.Txを取得 transaction.go repository.go
Update文を発行 repository.go ※RLSが効いているので、SQLのWhereの条件にtenant_idは不要
トランザクションをコミット transaction.go DoInTx
コネクションをsql.DBに返却 transaction.go DoInTx https://pkg.go.dev/database/sql#Conn.Close
まとめ • Context経由でsql.Txを渡すことで、DBに依存しない形でユースケースで トランザクションの定義が可能に • 参考:https://qiita.com/arkuchy/items/659a11767912c2ec266d • データベースセッションの環境変数にセットしたテナントIDでアクセス権 を制御 •
テナントのロールを追加する必要がなく、基本的にはテナント追加時の作業が不要 • sql.DBで管理するコネクションがスレッドセーフ • (感想)Goはマイクロサービス向きの言語であることを実感
Thank you for listening!