ã¤ã³ãã©ã¹ãã©ã¯ãã£ã¼é¨ id:sora_h ã§ããã¯ãã¯ãããã§ã¯ã社å åãã® Web ã¢ã㪠(以é â社å ãã¼ã«â) ã社å¤ã®ãããã¯ã¼ã¯ããå©ç¨ããéãã¢ããªã±ã¼ã·ã§ã³ã¬ãã«ã§ã®ã¢ã¯ã»ã¹å¶å¾¡ã¨ã¯å¥ã«ããªãã¼ã¹ãããã·ã§ãã¢ã¯ã»ã¹å¶å¾¡ãå®æ½ãã¦ãã¾ãã*1
ããã¾ã§ BASIC èªè¨¼ããã㯠VPN ã«ãã社å ãããã¯ã¼ã¯ãçµç±ããæ¥ç¶ã¨ããå½¢ã§è¨±å¯ãã¦ãã¾ãããããããiOS ã® Safari ãªã©ã§ã¯ BASIC èªè¨¼æã®ãã¹ã¯ã¼ããä¿åã§ããªãä¸ãé »ç¹ã«å ¥åãæ±ãããã¦ãã¾ãã¾ãããVPN ã¯ãªã³ã¯ãéãåã«æ¥ç¶ããã¦ããå¿ è¦ãããã¾ããããã«ããã社å ãã¼ã«ã社å¤ã§éãæã«æéãããã£ã¦ãã¾ãåé¡ãããã¾ããã
ããã«å¯¾ããä¸é¨ã§ã¯ typester/gate ãªã©ãå°å ¥ã Google Apps ã§ã®èªè¨¼ãè¡ãªã£ã¦ãã¾ããããããããã¤ãåé¡ããããéã¢ãããã¯ãªå¯¾å¿ã§ã¯ PR ãéãã¤ã¤ãç¬èªã«ããããå½ã¦ã¦ã¡ã³ããã³ã¹ãã¦ããç¶æ³ã§ãããã¾ããæ°ãã社å ãã¼ã«ã§å©ç¨ããéãæ°è¦ã«è¨å®ãã¦èµ·åã»ç£è¦è¨å®ãããå¿ è¦ããããªã©ãä»ã®ç¤¾å ãã¼ã«ã¸ç°¡åã«é©ç¨ããã®ãé£ããç¶æ ã§ããã
æè¿ã«ãªã Microservices åãé²ãã¦ããä¸ã社å ãã¼ã«ããã¡ãã¡ã§å®è£ ã»åé¢ãããããã«ãªã£ã¦ãã¾ãããä¸é¨ç¤¾å ãã¼ã«ã§å©ç¨ããã¦ãã Google Apps èªè¨¼ãä»ã§ãæ°è»½ã«å©ç¨ããããã¨æãä»å nginx_omniauth_adapter ãéçºããã®ã§ãç´¹ä»ãã¾ãã
nginx_omniauth_adapter ã¨ã¯
https://github.com/sorah/nginx_omniauth_adapter
nginx_omniauth_adapter 㯠nginx ã® ngx_http_auth_request_module ã¨çµåãã¦ãRuby ã® omniauth gem ãå©ç¨ã㦠nginx ã®ã¢ã¯ã»ã¹èªè¨¼ã»èªå¯ãè¡ãããã®å°ã㪠Rack ã¢ããªã«ãªã£ã¦ãã¾ããomniauth ã¨ãã®ãã©ã°ã¤ã³ãæã¤è±å¯ãªèªè¨¼æ段ããã®ã¾ã¾ nginx ã§æ´»ç¨ã§ãã¾ãã
ã¯ãã¯ãããã§ã¯åºæ¬çã«ããã Google OAuth2 ã¨çµã¿åããã¦å©ç¨ãã¦ãã¾ãããã°ã¤ã³ãããã¢ã«ã¦ã³ãã社ç¨ã® Google Apps ã¢ã«ã¦ã³ãã§ããã®ãæ¤è¨¼ããè¨å®ã«ãã¦ãã¾ãã
使ãæ¹
Rack ã¢ããªã®ãããè¨å®ã¯ config.ru
ãã¡ã¤ã«ã§è¡ãã¾ããomniauth å´ã¨ãnginx_omniauth_adapter å´ã®è¨å®ã config.ru
ã«æ¸ãã¦èµ·åããã°å©ç¨ã§ãã¾ããåèã¾ã§ã«ãã¯ãã¯ãããã§ã¯ Gemfile
㨠config.ru
ãç½®ãããªãã¸ããªãä½æããããã capistrano ã§ãããã¤ãã¦ãã¾ãã
ã¾ããGitHub ã Google OAuth2 ã§ããã°ãæ·»ä»ã® Dockerfile 㨠config.ru
ã«ç°å¢å¤æ°ã渡ãã¦ç°¡åã«å©ç¨ã§ãã¾ãã詳細㯠README ãããããã ããã
nginx å´ã®è¨å®ä¾ã¯ examples ãã£ã¬ã¯ããªãåèã«ããã¨è¯ãã§ãããã
ä»çµã¿
ãã¦ãä¸è¨ nginx ã®è¨å®ãè¦ãã¨ãè¥å¹²ããªããã¼ãªå 容ã«ãªã£ã¦ãããã¨ãåããã¨æãã¾ããæ¬ç¯ã§ã¯ãããè¸ã¾ãã¦ãnginx_omniauth_adapter ãã©ã®ããã« ngx_http_auth_request_module ã¨é£æºãã¦ãããã解説ãã¾ãã
ngx_http_auth_request_module ã¨ã¯
ã¾ããèå¿ã® ngx_http_auth_request_module ã«ã¤ãã¦è»½ãç´¹ä»ãã¾ãããã®ã¢ã¸ã¥ã¼ã«ã¯ nginx ã§ãªã¯ã¨ã¹ãã¯å¦çããã¤ã¤ãã¢ã¯ã»ã¹èªå¯å¦çã¯ã©ããã¸ç§»è²ãããã¨ããæã«å©ç¨ã§ãã¾ãã
å
·ä½çã«ã¯ãauth_request
directive ãè¨å®ããã¦ãããã¹ã¸ã®ãªã¯ã¨ã¹ããåä¿¡ããæãã¯ã©ã¤ã¢ã³ãã«ã¬ã¹ãã³ã¹ãè¿ãåã« nginx ãå
é¨ãªã¯ã¨ã¹ãã¨ã㦠auth_request
ã§æå®ããããã¹ã¸ãªã¯ã¨ã¹ããéä¿¡ãã¾ãã
ãã®å
é¨ãªã¯ã¨ã¹ãã®ã¬ã¹ãã³ã¹ã 200
ã§ããã°ãã¼ã¸ã表示ã§ãã¾ããã401
ã 403
ã®å ´åã¢ã¯ã»ã¹æå¦ã¨ã¿ãªããã¾ãããã®æãå
ã®ãªã¯ã¨ã¹ãã«ã¯ 401
ã 403
ãè¿çããããªã¯ã¨ã¹ãã®å¦çãä¸æããã¾ãã
å®éã«å©ç¨ããæã¯ããã®ã¢ã¸ã¥ã¼ã«ã«ããèªå¯ãã§ããªãã£ãå ´åãå¥ã®è¨å®ã§èªè¨¼å¦çã¸é·ç§»ããã¾ãããã®ã¢ã¸ã¥ã¼ã«èªä½ã¯ã¢ã¯ã»ã¹ã許å¯ãããã©ããã®å¤å®ããã§ããªãããã§ãã詳細ã¯å¾è¿°ãã¾ãã
auth_request
ã§çºçããå
é¨ãªã¯ã¨ã¹ãã§ã¯å
ã®ãªã¯ã¨ã¹ãã¨åããããã»ããã£ãéä¿¡ãããã®ã§ããããå©ç¨ãã¦èªå¯ãè¡ãã¾ããå
é¨ãªã¯ã¨ã¹ãå
ã« proxy_pass
ãä»æãã¦ãããã¨ã§å¤é¨ããã»ã¹ã¸å¦çã移è²ã§ãã¾ãã
location / { auth_request /_auth/challenge; } location = /_auth/challenge { internal; proxy_pass_request_body off; proxy_set_header Content-Length ""; proxy_set_header Host $http_host; proxy_pass http://auth_adapter/test; }
ãã®ã¢ã¸ã¥ã¼ã«ããã¾ãæ´»ç¨ã§ããã¨ãåè¿°ã® typester/gate ã bitly/oauth2_proxy ã¨éããèªè¨¼ã»èªå¯ã®ããã®ããã«ã¦ã§ã¢ã§ãªãã¼ã¹ãããã·ãå®è£ ããå¿ è¦ããªããªãã¨ããç¹ã便å©ã§ãã
auth_request ã«ããèªè¨¼ã失æããæã«èªè¨¼ãã¼ã¸ã¸ãªãã¤ã¬ã¯ããããã
omniauth ã«ããèªè¨¼ãéå§ããããã«ã¯ /auth/â¦
ã®ãã¹ã¸ãªãã¤ã¬ã¯ããããå¿
è¦ãããã¾ããããããngx_http_auth_request_module ã§ã¯ auth_request
ã®ã¬ã¹ãã³ã¹ããã®ã¾ã¾ãã©ã¦ã¶ã«è¿ãã¾ãããauth_request
å
ã«å±ããããã¼ãã¯ããã¼ã§èªè¨¼ãèªå¯ãè¡ããã¨ã¯ã§ãã¾ããã失ææã«èªè¨¼ãã¼ã¸ã¸ãªãã¤ã¬ã¯ããããã¨ãã£ãäºãã©ãããã°è¯ãã®ãããã¥ã¡ã³ããè¦ã¦ããã¾ãã¡åããã¾ããã
ããã§ãnginx_omniauth_adapter ã§ã¯ error_page
directive ãå©ç¨ãã¦ãªãã¤ã¬ã¯ãããã¦ãã¾ããerror_page
directive ã¯ã¹ãã¼ã¿ã¹ã³ã¼ãã«å¿ãã¦å
é¨ãªã¯ã¨ã¹ããçºçããããããã¬ã¹ãã³ã¹ã¨ããäºãã§ãã¾ããã¾ãã =
ãªãã·ã§ã³ãå©ç¨ããã¨ããã®å
é¨ãªã¯ã¨ã¹ãã®ã¬ã¹ãã³ã¹ã³ã¼ããå
ã®ã¨ã©ã¼ã®ãããã«ã¯ã©ã¤ã¢ã³ãã¸è¿çã§ãã¾ãã
ã¤ã¾ããauth_request
ã«ããã¢ã¯ã»ã¹èªå¯ã失æããã¨ã401
ããã㯠403
ãã¨ã©ã¼ã¨ãã¦ã¯ã©ã¤ã¢ã³ãã«è¿çããã¾ãããã㯠nginx èªä½ãéä¿¡ããã¨ã©ã¼ãã¼ã¸ã®ãããerror_page 401 = â¦
ãå©ç¨ãã¦å
é¨ãªã¯ã¨ã¹ããçºçãããããã§ãªãã¤ã¬ã¯ããè¡ãªãã¾ãããªããnginx_omniauth_adapter ã® auth_request
ç¨ã¨ã³ããã¤ã³ãã¯ãèªè¨¼ãããã¦ããªãã¨ã 401
ãè¿çãã¾ãã *2
location / { auth_request /_auth/challenge; error_page 401 = /_auth/initiate; } location = /_auth/initiate { internal; proxy_pass_request_body off; proxy_set_header Content-Length ""; proxy_set_header Host $http_host; proxy_set_header x-ngx-omniauth-initiate-back-to http://$http_host$request_uri; proxy_set_header x-ngx-omniauth-initiate-callback http://$http_host/_auth/callback; # âãå¿ è¦ãªå¦çãè¡ãããªãã¤ã¬ã¯ãããã proxy_pass http://auth_adapter/initiate; }
nginx_omniauth_adapter ã§ã¯å®éã«ã¯æ»ãå
URL å¨ãã®å¦çããããããããã§ãä¸åº¦ nginx_omniauth_adapter 㸠proxy_pass
ããã¦ãã¾ãã
ãã ãããã®æåãå©ç¨ãã注æç¹ã¨ã㦠proxy_intercept_errors
directive 㯠off ã«è¨å®ãã¦ããå¿
è¦ãããã¾ããon ã®å ´åãproxy_pass
ã§ãããã·ããå
ã®ã¬ã¹ãã³ã¹ã 401
, 403
ã®æã« error_page
ã®è¨å®ãä½åãã¦ãå¿
ãèªè¨¼ãã¼ã¸ã¸é£ã°ããã¦ãã¾ãããã§ãã
èªè¨¼å¾ãå ã®ãã¼ã¸ã¸ãªãã¤ã¬ã¯ãããã
nginx_omniauth_adapter ã«ãã£ã¦ãªãã¤ã¬ã¯ããããå 㯠nginx_omniauth_adapter èªä½ã® FQDN ã«ãªãã¤ã¬ã¯ãããããããã omniauth ã®å¦çãéå§ããã¾ãããã㯠OAuth 2 ã®ãããã¤ããªã©ã§ã³ã¼ã«ãã㯠URL ãåºå®ã ã£ãããããããnginx_omniauth_adapter èªä½ã« FQDN ãå²ãå½ã¦ãå¿ è¦ãããã¾ãã
omniauth ã«ããèªè¨¼å¦çãçµãã£ãå¾ãnginx_omniauth_adapter å´ã® domain ã«ã»ãã·ã§ã³ã¯ããã¼ãã»ããããã¾ããå ã®ã¢ããªã±ã¼ã·ã§ã³ãåã URL ã«ãªãã¤ã¬ã¯ããã¦æ»ãå¿ è¦ãããã¾ãããdomain ãéãããã»ãã·ã§ã³ãã¢ããªã±ã¼ã·ã§ã³ãåã domain ã¸å¼ãç¶ãå¿ è¦ãããã¾ãã
ãã®ãããnginx_omniauth_adapter ã»ãã·ã§ã³ã®ä¸èº«ãèªè¨¼ä»ãæå· aes-256-gcm ã§æå·åãã¦ã¯ã¨ãªãã©ã¡ã¼ã¿ã«è¼ãå
ã® domain ã¸ãªãã¤ã¬ã¯ãããã¦ãã¾ãã
ãªãã¤ã¬ã¯ãå
ã§ãä¸åº¦ nginx_omniauth_adapter ã« proxy_pass
ãã¦ããããããã§å
容ãæ¤è¨¼ã»å¾©å·ããä¸ã§ã¢ããªå´ã® domain ã«å度ã»ãã·ã§ã³ãã»ããããããã«ãã¦ãã¾ãããã®å¾ãå®éã«å
ã®ãã¼ã¸ã¸ã®ãªãã¤ã¬ã¯ããçºçãã¾ããå
ã®ãã¼ã¸ãæ»ã£ãæãæ£ããã»ãã·ã§ã³ã¯ããã¼ããªã¯ã¨ã¹ãã«å«ã¾ãããããauth_request
ãæåãã¦ãªã¯ã¨ã¹ããç¶ç¶ããç¡äºã«ãã¼ã¸ã表示ããã¾ãã
# ããã¸æ»ã£ã¦ãã location = /_auth/callback { auth_request off; proxy_set_header Host $http_host; proxy_pass http://auth_adapter/callback; }
ã»ãã·ã§ã³ã®å¼ã渡ãã«ã¤ãã¦ã¯ãå®éã®ã¨ãã Redis, Memcached ãªã© KVS ããã¾ãå©ç¨ãã¦ã»ãã·ã§ã³ãå¼ãç¶ãã¹ããªæ°ããã¦ãã¾ããç¾ç¶ã®å®è£ ã ã¨å¤§ããªã»ãã·ã§ã³æ å ±ã®å¼ã渡ããã§ããªãã£ãããç¡é§ãªãã©ãã£ãã¯ãçºçãã¦ããããã§ããä»å¾ã®æ¹åç¹ã®ä¸ã¤ã«ãªãã¾ãã
ã¡ãªã¿ã«ãã¢ããªå´ã® domain ã§æã¤ã»ãã·ã§ã³ã¯ nginx_omniauth_adapter å´ domain ã®ã»ãã·ã§ã³ããæå¹æéãçãè¨å®ãã¦ãããå®æçã«æ¤è¨¼ãããããã«ãã¦ãã¾ããnginx_omniauth_adapter å´ã®èªè¨¼å¦çã¯ãnginx_omniauth_adapter å´ã®ã»ãã·ã§ã³ã失å¹ãã¦ããªãéãã¹ããããããåã«ã¢ããªå´ã®ã»ãã·ã§ã³ãæ´æ°ãããããªæåã«ãªãã¾ãã ããã«ããã»ãã¥ãªãã£ã¬ãã«ãä½ããããé »ç¹ã«èªè¨¼ãã¼ã¸ã¸ãªãã¤ã¬ã¯ããããã¿ã³ãæ¼ãå¿ è¦ããªããã¦ãã¾ãã
å®éã®ããã¼
以ä¸ãå³ã«èµ·ããã¨ãã®ãããªå½¢ã«ãªãã¾ããç®æ¡æ¸ãã§é çªã«è§£èª¬ãã¦ããã®ã¯ README ã«ããã¾ãã
å®éã®å¹æ
ä¸é¨ã®ç¤¾å ãã¼ã«ã§æ¤è¨¼ãã¦ãããåé¡ãªãéç¨ã§ããã¨å¤æãã¦ãä»ã®ç¤¾å ãã¼ã«ã (æ¢ã« typester/gate ãå©ç¨ãã¦ãã社å ãã¼ã«ãå«ã) BASIC èªè¨¼ããç½®ãæããè¡ãã¾ããã社å ã§ã¯ã¡ã¼ã«ã®ãªã³ã¯ãå¤ã§éããæã«ç ©ããããªããªã£ãã¨é«è©ä¾¡ãè²°ã£ã¦ãã¾ãã
ã¾ããnginx_omniauth_adapter 㯠nginx ã¨åããµã¼ãã¼ä¸ã§åä½ãããå¿ è¦ã¯ãªãããã¤ãã¢ããªæ¯ã«ç´°ãã nginx_omniauth_adapter ãè¨å®ããå¿ è¦ã¯ããã¾ããã ã¤ã¾ãåã« nginx ã®è¨å®ã ãæ¿å ¥ããã°å©ç¨ã§ãããããnginx ããå°å ¥ããã¦ããã°æ°è¦ã«ä½ããã¤ã³ã¹ãã¼ã«ã㦠consumer key çãè¨å®ãã¦â¦ã¨ããæéãªãã« omniauth ã«ããèªè¨¼ãå°å ¥ã§ããã®ã§ãã¤ã³ãã©å´ã®æéãããªãä½ããªãã¾ããã
FAQ: ãªã社å¤ãããã¯ã¼ã¯ããã®ç´æ¥ã¢ã¯ã»ã¹ã許å¯ãã¦ããã®ã
ã¾ããã¯ãã¯ãããã®ç¤¾å ãããã¯ã¼ã¯ã« VPN ãå°å ¥ãããã®ã¯ããæ°å¹´ã§ã®åºæ¥äºã«ãªãã¾ããå°å ¥ä»¥åãã BASIC èªè¨¼ã§ç¤¾å¤ã¢ã¯ã»ã¹ã許å¯ãã¦ãã社å ãã¼ã«ãåå¨ãã¾ããã
ã§ã¯ãä»ç¾å¨ VPN ãå©ç¨ã§ããä¸ããªã VPN ã®ã¿ã«ããä»ã®ã¢ã¯ã»ã¹æ段ãæä¾ãã¦ããã®ããåè¿°ãããã㪠VPN æ¥ç¶ã®æéãçãå©ä¾¿æ§ãããã§ãããã¯ãã¯ãããã§ç¾å¨é²ãããã¦ããã°ãã¼ãã«å±éã«é¢ä¿ãã¦åä¼ç¤¾ã®æ ç¹ãä¸çä¸ã«åå¨ããå¢ãã¦ããç¶æ³ã«ããã¾ãããã®ä¸ã§æ ç¹é VPNãæµ·å¤ã§å¤åããã¹ã¿ããåãã® VPN ã®æ´åã追ãä»ãã¦ããªãã¨ããã®ãä¸ã¤ã®çç±ã«ãªã£ã¦ãã¾ãã
ã¾ãããã®ä»£æ¿æ段㫠Google Apps ãå©ç¨ãã¦ããçç±ã§ãããGoogle Apps ã®ã¢ã«ã¦ã³ãã¯å ¨ã¹ã¿ããã«ä»ä¸ããã¦ããäºã¨ã2 段éèªè¨¼ãå ¨ã¹ã¿ããã«å¯¾ãã¦å¿ é ã¨ãã¦ããããã§ããä¸æ㪠BASIC èªè¨¼ã§ãªã¼ãã³ã«ãããããå®å ¨ã ã¨å¤æãã¦ãã¾ãã
ã¾ã¨ã
ã¯ãã¯ãããã®ç¤¾å ãã¼ã«ã§å©ç¨ããã¦ããã¢ã¯ã»ã¹å¶å¾¡ã®ä»çµã¿ã nginx_omniauth_adapter ãç´¹ä»ãã¾ãããã©ãããå©ç¨ãã ããã
*1:ã¢ããªã±ã¼ã·ã§ã³ã¬ãã«ã§ã®ã¢ã¯ã»ã¹å¶å¾¡ã«å ããã¢ããªã±ã¼ã·ã§ã³ã®æåã§åºæ¬çãªã¢ã¯ã»ã¹å¶å¾¡ãå®æ½ãããã¨ã«ãªãã¾ããããã«ããä¸ãä¸ã¢ããªã±ã¼ã·ã§ã³å´ã«ãã°ãæ··å ¥ããã¤ã³ã¿ã¼ãããããèªè¨¼ç¡ãã§è¦ããã¨ãã£ãäºæ ãé²ãã§ãã¾ãã
*2:ä½è«ã§ãããnginx_omniauth_adapter ã§ã¯ 403 ãå©ç¨ãã¦ãã¾ãããªã¯ã¨ã¹ãæ å ±ã¨ã¦ã¼ã¶ã¼æ å ±ã使ããªã¯ã¨ã¹ãæ¯ã«ã¢ã¯ã»ã¹è¨±å¯ãããã®å¤å®å¦çãè¨å®ã§ããããã§æå¦ãããå ´å 403 ãè¿ãããèªè¨¼å¦çã¸ã®é·ç§»ã¯çºçãã¾ããã