This MR is an EE port for https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/17411.
https://gitlab.com/gitlab-org/gitlab-ee/issues/3727
https://gitlab.com/gitlab-org/gitlab-ee/issues/4032