Skip to content

Commit 8ea9c2a

Browse files
Robert Marshallngala
andcommitted
Merge branch 'ngala/pages_namespace_in_path_custom_auth_redirect_uri' into 'master'
Support custom auth_redirect_uri when namespace_in_path is enabled See merge request https://gitlab.com/gitlab-org/omnibus-gitlab/-/merge_requests/7516 Merged-by: Robert Marshall <[email protected]> Approved-by: Mitchell Nielsen <[email protected]> Approved-by: Robert Marshall <[email protected]> Co-authored-by: ngala <[email protected]>
2 parents f96ce28 + 5bc1b0d commit 8ea9c2a

File tree

2 files changed

+48
-7
lines changed

2 files changed

+48
-7
lines changed

files/gitlab-cookbooks/gitlab-pages/libraries/gitlab_pages.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,18 +145,19 @@ def parse_namespace_in_path
145145
url_scheme = Gitlab['gitlab_rails']['pages_https'] ? 'https' : 'http'
146146

147147
pages_port = Gitlab['gitlab_rails']['pages_port']
148+
redirect_uri = Gitlab['gitlab_pages']['auth_redirect_uri'].gsub('.', '\.')
148149
# Add the following when pages_port is not 80 or 443
149150
Gitlab['pages_nginx']['proxy_redirect'] =
150151
if [80, 443].include?(pages_port)
151152
{
152-
"~^#{url_scheme}://(projects\\.#{Gitlab['pages_nginx']['fqdn_regex']})/(.*)$" => "#{url_scheme}://$1/$2",
153+
"~^(#{redirect_uri})(.*)$" => "$1$2",
153154
"~^#{url_scheme}://([^/]*)\\.(#{Gitlab['pages_nginx']['fqdn_regex']})/(.*)$" => "#{url_scheme}://$2/$1/$3",
154155
"~^//([^/]*)\\.(#{Gitlab['pages_nginx']['fqdn_regex']})/(.*)$" => "/$1/$3",
155156
"~^/(.*)$" => "/$namespace/$1",
156157
}
157158
else
158159
{
159-
"~^#{url_scheme}://(projects\\.#{Gitlab['pages_nginx']['fqdn_regex']}:#{pages_port})/(.*)$" => "#{url_scheme}://$1/$2",
160+
"~^(#{redirect_uri})(.*)$" => "$1$2",
160161
"~^#{url_scheme}://([^/]*)\\.(#{Gitlab['pages_nginx']['fqdn_regex']}:#{pages_port})/(.*)$" => "#{url_scheme}://$2/$1/$3",
161162
"~^//([^/]*)\\.(#{Gitlab['pages_nginx']['fqdn_regex']}:#{pages_port})/(.*)$" => "/$1/$3",
162163
"~^/(.*)$" => "/$namespace/$1",

spec/chef/cookbooks/gitlab/recipes/nginx_spec.rb

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -904,7 +904,10 @@
904904
context 'when namespace_in_path is enabled in gitlab_pages' do
905905
before do
906906
stub_gitlab_rb(
907-
gitlab_pages: { namespace_in_path: true }
907+
gitlab_pages: {
908+
namespace_in_path: true,
909+
auth_redirect_uri: 'https://projects.pages.localhost/auth',
910+
}
908911
)
909912
end
910913

@@ -918,7 +921,7 @@
918921
expect(content).to include('rewrite ^/([^/]+)/(.*)$ /$2 break;')
919922
expect(content).to include('proxy_set_header Host $1.$http_host;')
920923
expect(content).to include('proxy_set_header X-Gitlab-Namespace-In-Path $namespace;')
921-
expect(content).to include('proxy_redirect ~^https://(projects\.pages\.localhost)/(.*)$ https://$1/$2;')
924+
expect(content).to include('proxy_redirect ~^(https://projects\.pages\.localhost/auth)(.*)$ $1$2;')
922925
expect(content).to include('proxy_redirect ~^https://([^/]*)\.(pages\.localhost)/(.*)$ https://$2/$1/$3;')
923926
expect(content).to include('proxy_redirect ~^//([^/]*)\.(pages\.localhost)/(.*)$ /$1/$3;')
924927
expect(content).to include('proxy_redirect ~^/(.*)$ /$namespace/$1;')
@@ -935,8 +938,11 @@
935938
context 'when namespace_in_path is enabled and pages_external_url has custom port in gitlab_pages' do
936939
before do
937940
stub_gitlab_rb(
938-
gitlab_pages: { namespace_in_path: true },
939-
pages_external_url: 'https://pages.localhost:25800'
941+
pages_external_url: 'https://pages.localhost:25800',
942+
gitlab_pages: {
943+
namespace_in_path: true,
944+
auth_redirect_uri: 'https://projects.pages.localhost/auth',
945+
}
940946
)
941947
end
942948

@@ -951,7 +957,7 @@
951957
expect(content).to include('rewrite ^/([^/]+)/(.*)$ /$2 break;')
952958
expect(content).to include('proxy_set_header Host $1.$http_host;')
953959
expect(content).to include('proxy_set_header X-Gitlab-Namespace-In-Path $namespace;')
954-
expect(content).to include('proxy_redirect ~^https://(projects\.pages\.localhost:25800)/(.*)$ https://$1/$2;')
960+
expect(content).to include('proxy_redirect ~^(https://projects\.pages\.localhost/auth)(.*)$ $1$2;')
955961
expect(content).to include('proxy_redirect ~^https://([^/]*)\.(pages\.localhost:25800)/(.*)$ https://$2/$1/$3;')
956962
expect(content).to include('proxy_redirect ~^//([^/]*)\.(pages\.localhost:25800)/(.*)$ /$1/$3;')
957963
expect(content).to include('proxy_redirect ~^/(.*)$ /$namespace/$1;')
@@ -995,6 +1001,40 @@
9951001
}
9961002
end
9971003
end
1004+
1005+
context 'when namespace_in_path is enabled in gitlab_pages and auth_redirect_uri has custom url' do
1006+
before do
1007+
stub_gitlab_rb(
1008+
gitlab_pages: {
1009+
namespace_in_path: true,
1010+
auth_redirect_uri: 'https://customauth.pages.localhost/auth',
1011+
}
1012+
)
1013+
end
1014+
1015+
it 'applies nginx namespace_in_path settings for gitlab-pages' do
1016+
expect(chef_run).to render_file(http_conf['pages']).with_content { |content|
1017+
expect(content).to include('server {').twice
1018+
expect(content).to include('server_name ~^pages\.localhost$;')
1019+
expect(content).to include('location ~ ^/(?<namespace>[^/]+)$ {')
1020+
expect(content).to include('return 301 $scheme://$http_host$request_uri/;')
1021+
expect(content).to include('location ~ ^/(?<namespace>[^/]+)/(?<project>.*)$ {')
1022+
expect(content).to include('rewrite ^/([^/]+)/(.*)$ /$2 break;')
1023+
expect(content).to include('proxy_set_header Host $1.$http_host;')
1024+
expect(content).to include('proxy_set_header X-Gitlab-Namespace-In-Path $namespace;')
1025+
expect(content).to include('proxy_redirect ~^(https://customauth\.pages\.localhost/auth)(.*)$ $1$2;')
1026+
expect(content).to include('proxy_redirect ~^https://([^/]*)\.(pages\.localhost)/(.*)$ https://$2/$1/$3;')
1027+
expect(content).to include('proxy_redirect ~^//([^/]*)\.(pages\.localhost)/(.*)$ /$1/$3;')
1028+
expect(content).to include('proxy_redirect ~^/(.*)$ /$namespace/$1;')
1029+
expect(content).to include('proxy_hide_header X-Gitlab-Namespace-In-Path;')
1030+
# Below checks are to verify proper render entries are made
1031+
expect(content).to include('proxy_http_version 1.1;').twice
1032+
expect(content).to include('proxy_pass').twice
1033+
expect(content).to include('disable_symlinks on;').twice
1034+
expect(content).to include('server_tokens off;').twice
1035+
}
1036+
end
1037+
end
9981038
end
9991039

10001040
include_examples "consul service discovery", "nginx", "nginx"

0 commit comments

Comments
 (0)