Skip to content

Commit ced6344

Browse files
authored
RHOAIENG-34310: support odh gateway auth with adjusting code-server and rstudio nginx (opendatahub-io#2585)
* support odh gateway auth with adjusting code-server and rstudio nginx Signed-off-by: Harshad Reddy Nalla <hnalla@redhat.com> * removed redudant fallback location Signed-off-by: Harshad Reddy Nalla <hnalla@redhat.com> --------- Signed-off-by: Harshad Reddy Nalla <hnalla@redhat.com>
1 parent 9af1da6 commit ced6344

File tree

6 files changed

+51
-45
lines changed

6 files changed

+51
-45
lines changed

codeserver/ubi9-python-3.12/nginx/serverconf/proxy.conf.template_nbprefix

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
# api calls from probes get to code-server /healthz endpoint
33
###############
44
location = ${NB_PREFIX}/api {
5-
return 302 /codeserver/healthz/;
5+
return 302 ${NB_PREFIX}/codeserver/healthz/;
66
access_log off;
77
}
88

99
location ${NB_PREFIX}/api/ {
10-
return 302 /codeserver/healthz/;
10+
return 302 ${NB_PREFIX}/codeserver/healthz/;
1111
access_log off;
1212
}
1313
###############
@@ -16,12 +16,12 @@ location ${NB_PREFIX}/api/ {
1616
# api calls from culler get to CGI processing
1717
###############
1818
location = ${NB_PREFIX}/api/kernels {
19-
return 302 $custom_scheme://$http_host/api/kernels/;
19+
return 302 $custom_scheme://$http_host${NB_PREFIX}/api/kernels/;
2020
access_log off;
2121
}
2222

2323
location ${NB_PREFIX}/api/kernels/ {
24-
return 302 $custom_scheme://$http_host/api/kernels/;
24+
return 302 $custom_scheme://$http_host${NB_PREFIX}/api/kernels/;
2525
access_log off;
2626
}
2727

@@ -39,32 +39,34 @@ location /api/kernels/ {
3939
###############
4040
# root and prefix get to code-server endpoint
4141
###############
42-
location = ${NB_PREFIX} {
43-
return 302 $custom_scheme://$http_host/codeserver/;
44-
}
45-
46-
location ${NB_PREFIX}/ {
47-
return 302 $custom_scheme://$http_host/codeserver/;
42+
location ${NB_PREFIX} {
43+
return 302 $custom_scheme://$http_host${NB_PREFIX}/codeserver/;
4844
}
4945

5046
location = /codeserver {
51-
return 302 $custom_scheme://$http_host/codeserver/;
47+
return 302 $custom_scheme://$http_host${NB_PREFIX}/codeserver/;
5248
}
5349

5450
location = / {
55-
return 302 $custom_scheme://$http_host/codeserver/;
51+
return 302 $custom_scheme://$http_host${NB_PREFIX}/codeserver/;
5652
}
5753

58-
location /codeserver/ {
59-
rewrite ^/codeserver/(.*)$ /$1 break;
60-
# Standard RStudio/NGINX configuration
54+
location ${NB_PREFIX}/codeserver/ {
55+
rewrite ^${NB_PREFIX}/codeserver/(.*)$ /$1 break;
56+
# Standard code-server/NGINX configuration
6157
proxy_pass http://workbench_server/;
6258
proxy_http_version 1.1;
63-
6459
proxy_set_header Upgrade $http_upgrade;
6560
proxy_set_header Connection $connection_upgrade;
6661
proxy_read_timeout 20d;
62+
63+
# Needed to make it work properly
64+
proxy_set_header X-Real-IP $remote_addr;
65+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
6766
proxy_set_header X-Forwarded-Proto $custom_scheme;
67+
proxy_set_header Host $http_host;
68+
proxy_set_header X-NginX-Proxy true;
69+
proxy_redirect off;
6870

6971
access_log /var/log/nginx/codeserver.access.log json if=$loggable;
7072
}

codeserver/ubi9-python-3.12/run-nginx.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ fi
1818
if [ -z "$NB_PREFIX" ]; then
1919
cp /opt/app-root/etc/nginx.default.d/proxy.conf.template /opt/app-root/etc/nginx.default.d/proxy.conf
2020
else
21-
export BASE_URL=$(echo $NB_PREFIX | awk -F/ '{ print $4"-"$3 }')$(echo $NOTEBOOK_ARGS | grep -Po 'hub_host":"\K.*?(?=")' | awk -F/ '{ print $3 }' | awk -F. '{for (i=2; i<=NF; i++) printf ".%s", $i}')
21+
export BASE_URL=$(echo "$NB_PREFIX" | awk -F/ '{ print $4"-"$3 }')$(echo "$NOTEBOOK_ARGS" | grep -Po 'hub_host":"\K.*?(?=")' | awk -F/ '{ print $3 }' | awk -F. '{for (i=2; i<=NF; i++) printf ".%s", $i}')
22+
# If BASE_URL is empty or invalid (missing hub_host), use wildcard server_name
23+
if [ -z "$BASE_URL" ] || [ "$BASE_URL" = "$(echo "$NB_PREFIX" | awk -F/ '{ print $4"-"$3 }')" ]; then
24+
export BASE_URL="_"
25+
fi
2226
envsubst '${NB_PREFIX},${BASE_URL}' < /opt/app-root/etc/nginx.default.d/proxy.conf.template_nbprefix > /opt/app-root/etc/nginx.default.d/proxy.conf
2327
envsubst '${BASE_URL}' < /etc/nginx/nginx.conf | tee /etc/nginx/nginx.conf
2428
fi

rstudio/c9s-python-3.12/nginx/serverconf/proxy.conf.template_nbprefix

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
###############
22
# Fix rstudio-server auth-sign-in redirect bug
33
###############
4-
rewrite ^/auth-sign-in(.*) "$custom_scheme://$http_host/rstudio/auth-sign-in$1?appUri=%2Frstudio";
5-
rewrite ^/auth-sign-out(.*) "$custom_scheme://$http_host/rstudio/auth-sign-out$1?appUri=%2Frstudio";
4+
rewrite ^/auth-sign-in(.*) "$custom_scheme://$http_host${NB_PREFIX}/rstudio/auth-sign-in$1?appUri=%2Frstudio";
5+
rewrite ^/auth-sign-out(.*) "$custom_scheme://$http_host${NB_PREFIX}/rstudio/auth-sign-out$1?appUri=%2Frstudio";
66
###############
77

88
###############
@@ -66,24 +66,20 @@ location /api/kernels/ {
6666
###############
6767
# root and prefix get to RStudio endpoint
6868
###############
69-
location = ${NB_PREFIX} {
70-
return 302 $custom_scheme://$http_host/rstudio/;
71-
}
72-
73-
location ${NB_PREFIX}/ {
74-
return 302 $custom_scheme://$http_host/rstudio/;
69+
location ${NB_PREFIX} {
70+
return 302 $custom_scheme://$http_host${NB_PREFIX}/rstudio/;
7571
}
7672

7773
location = /rstudio {
78-
return 302 $custom_scheme://$http_host/rstudio/;
74+
return 302 $custom_scheme://$http_host${NB_PREFIX}/rstudio/;
7975
}
8076

8177
location = / {
82-
return 302 $custom_scheme://$http_host/rstudio/;
78+
return 302 $custom_scheme://$http_host${NB_PREFIX}/rstudio/;
8379
}
8480

85-
location /rstudio/ {
86-
rewrite ^/rstudio/(.*)$ /$1 break;
81+
location ${NB_PREFIX}/rstudio/ {
82+
rewrite ^${NB_PREFIX}/rstudio/(.*)$ /$1 break;
8783
# Standard RStudio/NGINX configuration
8884
proxy_pass http://workbench_server/;
8985
proxy_http_version 1.1;
@@ -93,7 +89,7 @@ location /rstudio/ {
9389

9490
# Needed to make it work properly
9591
proxy_set_header X-RStudio-Request $custom_scheme://$http_host$request_uri;
96-
proxy_set_header X-RStudio-Root-Path /rstudio;
92+
proxy_set_header X-RStudio-Root-Path ${NB_PREFIX}/rstudio;
9793
proxy_set_header Host $http_host;
9894
proxy_set_header X-Forwarded-Proto $custom_scheme;
9995

rstudio/c9s-python-3.12/run-nginx.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ fi
1818
if [ -z "$NB_PREFIX" ]; then
1919
cp /opt/app-root/etc/nginx.default.d/proxy.conf.template /opt/app-root/etc/nginx.default.d/proxy.conf
2020
else
21-
export BASE_URL=$(echo $NB_PREFIX | awk -F/ '{ print $4"-"$3 }')$(echo $NOTEBOOK_ARGS | grep -Po 'hub_host":"\K.*?(?=")' | awk -F/ '{ print $3 }' | awk -F. '{for (i=2; i<=NF; i++) printf ".%s", $i}')
21+
export BASE_URL=$(echo "$NB_PREFIX" | awk -F/ '{ print $4"-"$3 }')$(echo "$NOTEBOOK_ARGS" | grep -Po 'hub_host":"\K.*?(?=")' | awk -F/ '{ print $3 }' | awk -F. '{for (i=2; i<=NF; i++) printf ".%s", $i}')
22+
# If BASE_URL is empty or invalid (missing hub_host), use wildcard server_name
23+
if [ -z "$BASE_URL" ] || [ "$BASE_URL" = "$(echo "$NB_PREFIX" | awk -F/ '{ print $4"-"$3 }')" ]; then
24+
export BASE_URL="_"
25+
fi
2226
envsubst '${NB_PREFIX},${BASE_URL}' < /opt/app-root/etc/nginx.default.d/proxy.conf.template_nbprefix > /opt/app-root/etc/nginx.default.d/proxy.conf
2327
envsubst '${BASE_URL}' < /etc/nginx/nginx.conf | tee /etc/nginx/nginx.conf
2428
fi

rstudio/rhel9-python-3.12/nginx/serverconf/proxy.conf.template_nbprefix

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
###############
22
# Fix rstudio-server auth-sign-in redirect bug
33
###############
4-
rewrite ^/auth-sign-in(.*) "$custom_scheme://$http_host/rstudio/auth-sign-in$1?appUri=%2Frstudio";
5-
rewrite ^/auth-sign-out(.*) "$custom_scheme://$http_host/rstudio/auth-sign-out$1?appUri=%2Frstudio";
4+
rewrite ^/auth-sign-in(.*) "$custom_scheme://$http_host${NB_PREFIX}/rstudio/auth-sign-in$1?appUri=%2Frstudio";
5+
rewrite ^/auth-sign-out(.*) "$custom_scheme://$http_host${NB_PREFIX}/rstudio/auth-sign-out$1?appUri=%2Frstudio";
66
###############
77

88
###############
@@ -66,24 +66,20 @@ location /api/kernels/ {
6666
###############
6767
# root and prefix get to RStudio endpoint
6868
###############
69-
location = ${NB_PREFIX} {
70-
return 302 $custom_scheme://$http_host/rstudio/;
71-
}
72-
73-
location ${NB_PREFIX}/ {
74-
return 302 $custom_scheme://$http_host/rstudio/;
69+
location ${NB_PREFIX} {
70+
return 302 $custom_scheme://$http_host${NB_PREFIX}/rstudio/;
7571
}
7672

7773
location = /rstudio {
78-
return 302 $custom_scheme://$http_host/rstudio/;
74+
return 302 $custom_scheme://$http_host${NB_PREFIX}/rstudio/;
7975
}
8076

8177
location = / {
82-
return 302 $custom_scheme://$http_host/rstudio/;
78+
return 302 $custom_scheme://$http_host${NB_PREFIX}/rstudio/;
8379
}
8480

85-
location /rstudio/ {
86-
rewrite ^/rstudio/(.*)$ /$1 break;
81+
location ${NB_PREFIX}/rstudio/ {
82+
rewrite ^${NB_PREFIX}/rstudio/(.*)$ /$1 break;
8783
# Standard RStudio/NGINX configuration
8884
proxy_pass http://workbench_server/;
8985
proxy_http_version 1.1;
@@ -93,7 +89,7 @@ location /rstudio/ {
9389

9490
# Needed to make it work properly
9591
proxy_set_header X-RStudio-Request $custom_scheme://$http_host$request_uri;
96-
proxy_set_header X-RStudio-Root-Path /rstudio;
92+
proxy_set_header X-RStudio-Root-Path ${NB_PREFIX}/rstudio;
9793
proxy_set_header Host $http_host;
9894
proxy_set_header X-Forwarded-Proto $custom_scheme;
9995

rstudio/rhel9-python-3.12/run-nginx.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ fi
1818
if [ -z "$NB_PREFIX" ]; then
1919
cp /opt/app-root/etc/nginx.default.d/proxy.conf.template /opt/app-root/etc/nginx.default.d/proxy.conf
2020
else
21-
export BASE_URL=$(echo $NB_PREFIX | awk -F/ '{ print $4"-"$3 }')$(echo $NOTEBOOK_ARGS | grep -Po 'hub_host":"\K.*?(?=")' | awk -F/ '{ print $3 }' | awk -F. '{for (i=2; i<=NF; i++) printf ".%s", $i}')
21+
export BASE_URL=$(echo "$NB_PREFIX" | awk -F/ '{ print $4"-"$3 }')$(echo "$NOTEBOOK_ARGS" | grep -Po 'hub_host":"\K.*?(?=")' | awk -F/ '{ print $3 }' | awk -F. '{for (i=2; i<=NF; i++) printf ".%s", $i}')
22+
# If BASE_URL is empty or invalid (missing hub_host), use wildcard server_name
23+
if [ -z "$BASE_URL" ] || [ "$BASE_URL" = "$(echo "$NB_PREFIX" | awk -F/ '{ print $4"-"$3 }')" ]; then
24+
export BASE_URL="_"
25+
fi
2226
envsubst '${NB_PREFIX},${BASE_URL}' < /opt/app-root/etc/nginx.default.d/proxy.conf.template_nbprefix > /opt/app-root/etc/nginx.default.d/proxy.conf
2327
envsubst '${BASE_URL}' < /etc/nginx/nginx.conf | tee /etc/nginx/nginx.conf
2428
fi

0 commit comments

Comments
 (0)