Backport for https://gitlab.com/gitlab-org/gitlab-ee/merge_requests/9642
https://gitlab.com/gitlab-org/gitlab-ee/issues/4925