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
Plug & WAF
Search
ohr486
February 27, 2022
Programming
560
2
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Plug & WAF
https://beam-lang.connpass.com/event/240399/
ohr486
February 27, 2022
More Decks by ohr486
See All by ohr486
負荷試験Night#1 負荷試験2023年トレンド
ohr486
17
4.9k
Elixir/PhoenixによるWeb開発の現場から
ohr486
1
670
Hacking Phoenix Performance
ohr486
1
420
elixirをプロダクションに導入する
ohr486
1
750
IEx maniacs
ohr486
4
690
Hack and Read Elixir
ohr486
2
820
Running App on AppRunner
ohr486
0
880
sponsor-talk-drecom-heisei-ruby-kaigi
ohr486
0
920
ex-app-on-k8s
ohr486
0
270
Other Decks in Programming
See All in Programming
承認済みなのに差戻しできてしまうバグ、型で潰せます
shinchit
0
130
PHPプロジェクトの結合バランスを可視化する #php_night
kajitack
0
190
【DroidKaigi 2026】「アクセシビリティを利用するとき、 アクセシビリティもまたこちらを利用している」 〜マルウェアによる攻撃と防衛について〜
halunoyo
0
200
LL言語やWebフレームワークのPostgreSQL対応 〜DBの機能がユーザーに届くまで〜
kentaroutakeda
0
130
Vibes Containers 〜AIで変わるコンテナ設計と運用〜
tkikuc
0
260
Loosening the Reins: Go Generics Get More Flexible
kuro_kurorrr
0
360
AIを上手に使っていこうとしたら越境せざるを得なくなった話 〜実践1年で見えた境界を越えなければならない理由と進め方〜 / Crossing borders with AI
tomoyakitaura
2
960
[DroidKaigi 26] How We Moved from Android Studio to Slack
thisaay
0
160
XHTMLが残したもの
yosuke_furukawa
PRO
1
370
まだ間に合う!今年の夏こそSchemeのマクロ展開器を完全理解!
omasanori
0
610
Oxlintはいいぞ(続)
yug1224
1
530
Cloudflare is Agents
chimame
0
190
Featured
See All Featured
Sam Torres - BigQuery for SEOs
techseoconnect
PRO
0
510
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.9k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
16
2.1k
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
320
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
6.1k
A Guide to Academic Writing Using Generative AI - A Workshop
ks91
PRO
1
410
Making the Leap to Tech Lead
cromwellryan
135
10k
16th Malabo Montpellier Forum Presentation
akademiya2063
PRO
0
370
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.8k
Getting science done with accelerated Python computing platforms
jacobtomlinson
2
460
Applied NLP in the Age of Generative AI
inesmontani
PRO
4
2.4k
WENDY [Excerpt]
tessaabrams
12
39k
Transcript
Plug & WAF 2022-02-27 tokyo.ex #16
agenda • About Me • ゴール • WAFとは何か • Webアプリ(MVC)の構造
• 何故Plugが必要なのか • Plugの構造 • Plugのインターフェース • Plugのデータフロー • DEMO • まとめ
About Me • おーはら / Twitter: @ohrdev / Github: ohr486
• 株式会社ドリコム SRE部 部長 ◦ Work: ▪ エンジニアマネージャ • 技術戦略の策定/推進 • エンジニア採用/採用戦略策定 ▪ サーバー/インフラエンジニア • 開発現場でゲームのバックエンド (Rails/Phoenix/Go/HCL)のコード書いてます ▪ 新規事業/ディレクター • 負荷試験支援/DevOps推進支援/設計コンサル • Community ◦ tokyo.ex / Japan Elixir Association / Erlang&Elixir Fest • Hobby ◦ 仏像制作 ◦ 自転車
ゴール • ターゲット ◦ Plugが何をしているか理解したい人 ◦ 何故Plugがあるかを理解したい人 ◦ PhoenixとPlugの関係について理解したい人 •
ゴール ◦ (Phoenixを含む)WAFの構造について理解する ◦ Plugの構造について理解する ◦ PhoenixとPlugの関係について理解する
WAFとは何か • WAF = Web Application Framework ◦ elixir ->
phoenix ◦ ruby -> rails ◦ python -> django • Webアプリを作るためのフレームワーク ◦ webアプリとは? ▪ インターネット/ネットワークを介して利用するアプリケーション ▪ ここでは、webブラウザ上で動作するという前提で進めます ◦ ex) wiki, blog, 掲示板, ECサイト, etc
Webアプリ(MVC)の構造 Webサーバー Webアプリ データベース Nginx Apache Cowboy etc プロトコル HTTP
WebSocket Phoenix Rails Django etc MySQL PostgreSQL Spanner etc DB I/F Ecto ActiveRecord etc WAF I/F Plug Rack WSGI etc cowboy Plug Phoenix Endpoint Router Pipeline Controller View Template Ecto DB View Controller Model
何故Plugが必要なのか • 歴史的背景 ◦ WAFの乱立とWebサーバーの多様化により、 WAFとWebサーバーの接続部分の組み合わせ爆発 が発生し、全ての接続を網羅するのが難しくなった ◦ この問題を解決する為、 WAFとWebサーバーの統一的な
I/Fを定めた仕様が策定された ◦ WAF,Webサーバー共に、この I/Fに対応すればどの組み合わせの接続も担保できる WAF1 WAF2 WAF3 フレームワーク Webサーバー Apache nginx lighttpd … … WAF1 WAF2 WAF3 フレームワーク Webサーバー Apache nginx lighttpd 共通のI/F Phoenix cowboy Plug Rails nginx Rack
Plugの構造 cowboy Plug(I/F) Phoenix Endpoint Router Pipeline Controller View Template
HTTPコネクションのリク エスト/レスポンスを抽象 化した構造体 %Plug.Conn %Plug.Conn Plug %Plug.Conn HTTP Request HTTP Response Plug Plug Plug Plug PhoenixではPlugとして実 装されている Plug Plug Conn Conn’ Conn’’ Plugとは Connを受け取り、Connの内 部情報を更新して返却するモ ジュール/関数 pipeline
Plugのインターフェース • Plugは以下の2つに分類される ◦ Function Plugs ◦ Module Plugs ◦
これらのPlugはplugマクロを使って、コントローラー層/Routerに差し込まれる ▪ https://github.com/ohr486/tokyoex_16_sample/blob/main/plug_sample/lib/plug_sample/router.ex#L8-L12 • Function Plugs ◦ Plug.Connとオプションを引数にとり、Plug.Connを返却する関数 ▪ (Plug.Conn.t, Plug.opts) :: Plug.Conn.t ◦ https://github.com/ohr486/tokyoex_16_sample/blob/main/plug_sample/lib/plug_sample/router.ex#L31 • Module Plugs ◦ 以下の2つの関数をexportするモジュール ▪ init/1 : 引数のオプションを使って自身を初期化する関数、 コンパイル時に実行される ▪ call/2 : Plug.Connとオプションを引数にとり、Plug.Connの内部情報を更新して返却する関数 ◦ https://github.com/ohr486/tokyoex_16_sample/blob/main/plug_sample/lib/plug_sample/module_plug.ex
Plugのデータフロー Func Plug Module Plug Conn Conn’ Conn’’ HTTP Request
HTTP Response func_plug(conn, opts) ModulePlug.call(conn’, opts) ModulePlug.init(opts) compile時に呼出 defmodule MyAppMod.Router do use Plug.Router … plug :func_plug plug ModulePlug … end Plug.Routerによる定義 plugを通過する際に呼出 plugを通過する際に呼出
DEMO サンプルコード https://github.com/ohr486/tokyoex_16_sample/tree/main/plug_sample
まとめ • WAFとPlugの構造について紹介しました • 何故Plugが必要なのかについて紹介しました • PhoenixのRoutingやHTTP層の実装のCodeReadingをする際、%Plug.Connの データ構造を追っていくと理解しやすいです • 独自実装のカスタムPlugは簡単に作成できます、PlugはHTTPリクエスト/レスポン
スのデータ構造をダイレクトに操作できるので非常に強力で有用です