git.iko.soy/typeable/oauth2-proxy
on master help sign in
Add nginx test environment to demonstrate protecting multiple subdomains
commitafef9c758893ab16cdc9e8ebbaf57009409e208d authorJoel Speed <joel.speed@hotmail.co.uk> dateThu 7 May 2020, 22:59 (6 years ago) parent0ccfc73
7 files changed+157−2
Makefile+5 contrib/local-environment/Makefile+8 contrib/local-environment/dex.yaml+3−1 contrib/local-environment/docker-compose-nginx.yaml+43 contrib/local-environment/nginx.conf+84 contrib/local-environment/oauth2-proxy-nginx.cfg+12 contrib/local-environment/oauth2-proxy.cfg+2−1
Makefile+5
@@ -88,6 +88,11 @@ validate-go-version:
8888 fi
8989
9090# local-env can be used to interact with the local development environment
91+# eg:
92+# make local-env-up # Bring up a basic test environment
93+# make local-env-down # Tear down the basic test environment
94+# make local-env-nginx-up # Bring up an nginx based test environment
95+# make local-env-nginx-down # Tead down the nginx based test environment
9196.PHONY: local-env-%
9297local-env-%:
9398 make -C contrib/local-environment $*
contrib/local-environment/Makefile+8
@@ -5,3 +5,11 @@ up:
55.PHONY: %
66%:
77 docker-compose $*
8+
9+.PHONY: nginx-up
10+nginx-up:
11+ docker-compose -f docker-compose.yaml -f docker-compose-nginx.yaml up -d
12+
13+.PHONY: nginx-%
14+nginx-%:
15+ docker-compose -f docker-compose.yaml -f docker-compose-nginx.yaml $*
contrib/local-environment/dex.yaml+3−1
@@ -18,7 +18,9 @@ expiry:
1818staticClients:
1919- id: oauth2-proxy
2020 redirectURIs:
21− - 'http://localhost:4180/oauth2/callback'
21+ # These redirect URIs point to the `--redirect-url` for OAuth2 proxy.
22+ - 'http://localhost:4180/oauth2/callback' # For basic proxy example.
23+ - 'http://oauth2-proxy.oauth2-proxy.localhost/oauth2/callback' # For nginx example.
2224 name: 'OAuth2 Proxy'
2325 secret: b2F1dGgyLXByb3h5LWNsaWVudC1zZWNyZXQK
2426enablePasswordDB: true
contrib/local-environment/docker-compose-nginx.yamlnew file+43
@@ -0,0 +1,43 @@
1+# This docker-compose file can be used to bring up an example instance of oauth2-proxy
2+# for manual testing and exploration of features.
3+# Alongside OAuth2-Proxy, this file also starts Dex to act as the identity provider,
4+# etcd for storage for Dex, nginx as a reverse proxy and other http services for upstreams
5+#
6+# This file is an extension of the main compose file and must be used with it
7+# docker-compose -f docker-compose.yaml -f docker-compose-nginx.yaml <command>
8+# Alternatively:
9+# make nginx-<command> (eg make nginx-up, make nginx-down)
10+#
11+# Access one of the following URLs to initiate a login flow:
12+# - http://oauth2-proxy.localhost
13+# - http://httpbin.oauth2-proxy.localhost
14+#
15+# The OAuth2 Proxy itself is hosted at http://oauth2-proxy.oauth2-proxy.localhost
16+#
17+# Note, the above URLs should work with Chrome, but you may need to add hosts
18+# entries for other browsers
19+# 127.0.0.1 oauth2-proxy.localhost
20+# 127.0.0.1 httpbin.oauth2-proxy.localhost
21+# 127.0.0.1 oauth2-proxy.oauth2-proxy.localhost
22+version: '3.0'
23+services:
24+ oauth2-proxy:
25+ ports: []
26+ hostname: oauth2-proxy
27+ volumes:
28+ - "./oauth2-proxy-nginx.cfg:/oauth2-proxy.cfg"
29+ networks:
30+ oauth2-proxy: {}
31+ nginx:
32+ container_name: nginx
33+ image: nginx:1.18
34+ ports:
35+ - 80:80/tcp
36+ hostname: nginx
37+ volumes:
38+ - "./nginx.conf:/etc/nginx/conf.d/default.conf"
39+ networks:
40+ oauth2-proxy: {}
41+ httpbin: {}
42+networks:
43+ oauth2-proxy: {}
contrib/local-environment/nginx.confnew file+84
@@ -0,0 +1,84 @@
1+# Reverse proxy to oauth2-proxy
2+server {
3+ listen 80;
4+ server_name oauth2-proxy.oauth2-proxy.localhost;
5+
6+ location / {
7+ proxy_set_header Host $host;
8+ proxy_set_header X-Real-IP $remote_addr;
9+
10+ proxy_pass http://oauth2-proxy:4180/;
11+ }
12+}
13+
14+# Reverse proxy to httpbin
15+server {
16+ listen 80;
17+ server_name httpbin.oauth2-proxy.localhost;
18+
19+ auth_request /internal-auth/oauth2/auth;
20+
21+ # If the auth_request denies the request (401), redirect to the sign_in page
22+ # and include the final rd URL back to the user's original request.
23+ error_page 401 = http://oauth2-proxy.oauth2-proxy.localhost/oauth2/sign_in?rd=$scheme://$host$request_uri;
24+
25+ # Alternatively send the request to `start` to skip the provider button
26+ # error_page 401 = http://oauth2-proxy.oauth2-proxy.localhost/oauth2/start?rd=$scheme://$host$request_uri;
27+
28+ location / {
29+ proxy_pass http://httpbin/;
30+ }
31+
32+ # auth_request must be a URI so this allows an internal path to then proxy to
33+ # the real auth_request path.
34+ # The trailing /'s are required so that nginx strips the prefix before proxying.
35+ location /internal-auth/ {
36+ internal; # Ensure external users can't access this path
37+
38+ # Make sure the OAuth2 Proxy knows where the original request came from.
39+ proxy_set_header Host $host;
40+ proxy_set_header X-Real-IP $remote_addr;
41+
42+ proxy_pass http://oauth2-proxy:4180/;
43+ }
44+}
45+
46+# Statically serve the nginx welcome
47+server {
48+ listen 80;
49+ server_name oauth2-proxy.localhost;
50+
51+ location / {
52+ auth_request /internal-auth/oauth2/auth;
53+
54+ # If the auth_request denies the request (401), redirect to the sign_in page
55+ # and include the final rd URL back to the user's original request.
56+ error_page 401 = http://oauth2-proxy.oauth2-proxy.localhost/oauth2/sign_in?rd=$scheme://$host$request_uri;
57+
58+ # Alternatively send the request to `start` to skip the provider button
59+ # error_page 401 = http://oauth2-proxy.oauth2-proxy.localhost/oauth2/start?rd=$scheme://$host$request_uri;
60+
61+
62+ root /usr/share/nginx/html;
63+ index index.html index.htm;
64+ }
65+
66+ # redirect server error pages to the static page /50x.html
67+ error_page 500 502 503 504 /50x.html;
68+ location = /50x.html {
69+ root /usr/share/nginx/html;
70+ }
71+
72+ # auth_request must be a URI so this allows an internal path to then proxy to
73+ # the real auth_request path.
74+ # The trailing /'s are required so that nginx strips the prefix before proxying.
75+ location /internal-auth/ {
76+ internal; # Ensure external users can't access this path
77+
78+ # Make sure the OAuth2 Proxy knows where the original request came from.
79+ proxy_set_header Host $host;
80+ proxy_set_header X-Real-IP $remote_addr;
81+
82+ proxy_pass http://oauth2-proxy:4180/;
83+ }
84+}
contrib/local-environment/oauth2-proxy-nginx.cfgnew file+12
@@ -0,0 +1,12 @@
1+http_address="0.0.0.0:4180"
2+cookie_secret="OQINaROshtE9TcZkNAm-5Zs2Pv3xaWytBmc5W7sPX7w="
3+provider="oidc"
4+email_domains="example.com"
5+oidc_issuer_url="http://dex.localhost:4190/dex"
6+client_secret="b2F1dGgyLXByb3h5LWNsaWVudC1zZWNyZXQK"
7+client_id="oauth2-proxy"
8+cookie_secure="false"
9+
10+redirect_url="http://oauth2-proxy.oauth2-proxy.localhost/oauth2/callback"
11+cookie_domain=".oauth2-proxy.localhost" # Required so cookie can be read on all subdomains.
12+whitelist_domains=".oauth2-proxy.localhost" # Required to allow redirection back to original requested target.
contrib/local-environment/oauth2-proxy.cfg+2−1
@@ -1,5 +1,4 @@
11http_address="0.0.0.0:4180"
2−redirect_url="http://localhost:4180/oauth2/callback"
32cookie_secret="OQINaROshtE9TcZkNAm-5Zs2Pv3xaWytBmc5W7sPX7w="
43provider="oidc"
54email_domains="example.com"
@@ -7,4 +6,6 @@ oidc_issuer_url="http://dex.localhost:4190/dex"
76client_secret="b2F1dGgyLXByb3h5LWNsaWVudC1zZWNyZXQK"
87client_id="oauth2-proxy"
98cookie_secure="false"
9+
10+redirect_url="http://localhost:4180/oauth2/callback"
1011upstreams="http://httpbin"