Commit 6e3e171e authored by Dmitry Gruzd's avatar Dmitry Gruzd Committed by Imre Farkas

Fix elasticsearch guest results permissions

Fix pendings guest searches for project/group/global
searches.
parent 05506e32
---
title: Fix advanced global search permissions for guest users
merge_request: 23177
author:
type: fixed
...@@ -211,7 +211,10 @@ module Gitlab ...@@ -211,7 +211,10 @@ module Gitlab
def merge_requests def merge_requests
strong_memoize(:merge_requests) do strong_memoize(:merge_requests) do
options = base_options.merge(project_ids: non_guest_project_ids) options = base_options.merge(
project_ids: filter_project_ids_by_feature(:merge_requests, limit_project_ids)
)
MergeRequest.elastic_search(query, options: options) MergeRequest.elastic_search(query, options: options)
end end
end end
...@@ -226,17 +229,14 @@ module Gitlab ...@@ -226,17 +229,14 @@ module Gitlab
return Kaminari.paginate_array([]) if query.blank? return Kaminari.paginate_array([]) if query.blank?
strong_memoize(:blobs) do strong_memoize(:blobs) do
project_ids = visible_project_ids options = base_options.merge(
additional_filter: repository_filter(limit_project_ids)
opt = base_options.merge(
additional_filter: repository_filter(project_ids),
project_ids: project_ids
) )
Repository.elastic_search( Repository.elastic_search(
query, query,
type: :blob, type: :blob,
options: opt.merge({ highlight: true }) options: options.merge({ highlight: true })
)[:blobs][:results].response )[:blobs][:results].response
end end
end end
...@@ -245,17 +245,14 @@ module Gitlab ...@@ -245,17 +245,14 @@ module Gitlab
return Kaminari.paginate_array([]) if query.blank? return Kaminari.paginate_array([]) if query.blank?
strong_memoize(:wiki_blobs) do strong_memoize(:wiki_blobs) do
project_ids = visible_project_ids(visible_for_guests: true) options = base_options.merge(
additional_filter: wiki_filter(limit_project_ids)
opt = base_options.merge(
additional_filter: wiki_filter(project_ids),
project_ids: project_ids
) )
ProjectWiki.elastic_search( ProjectWiki.elastic_search(
query, query,
type: :wiki_blob, type: :wiki_blob,
options: opt.merge({ highlight: true }) options: options.merge({ highlight: true })
)[:wiki_blobs][:results].response )[:wiki_blobs][:results].response
end end
end end
...@@ -270,11 +267,8 @@ module Gitlab ...@@ -270,11 +267,8 @@ module Gitlab
return Kaminari.paginate_array([]) if query.blank? return Kaminari.paginate_array([]) if query.blank?
strong_memoize(:commits) do strong_memoize(:commits) do
project_ids = visible_project_ids
options = base_options.merge( options = base_options.merge(
additional_filter: repository_filter(project_ids), additional_filter: repository_filter(limit_project_ids)
project_ids: project_ids
) )
Repository.find_commits_by_message_with_elastic( Repository.find_commits_by_message_with_elastic(
...@@ -294,22 +288,24 @@ module Gitlab ...@@ -294,22 +288,24 @@ module Gitlab
blob_filter(:repository, project_ids) blob_filter(:repository, project_ids)
end end
def visible_project_ids(visible_for_guests: false) def filter_project_ids_by_feature(feature, project_ids)
visible_for_guests ? limit_project_ids : non_guest_project_ids return project_ids if project_ids == :any
Project
.id_in(project_ids)
.filter_by_feature_visibility(feature, current_user)
.pluck_primary_key
end end
def blob_filter(feature, project_ids) def blob_filter(feature, project_ids)
key_name = "#{feature}_access_level" key_name = "#{feature}_access_level"
project_ids = filter_project_ids_by_feature(feature, project_ids)
conditions = conditions =
if project_ids == :any if project_ids == :any
[{ exists: { field: "id" } }] [{ exists: { field: "id" } }]
else else
project_ids = Project
.id_in(project_ids)
.filter_by_feature_visibility(feature, current_user)
.pluck_primary_key
[{ terms: { id: project_ids } }] [{ terms: { id: project_ids } }]
end end
......
...@@ -45,7 +45,7 @@ describe 'GlobalSearch', :elastic do ...@@ -45,7 +45,7 @@ describe 'GlobalSearch', :elastic do
expect_items_to_be_found(auditor) expect_items_to_be_found(auditor)
expect_items_to_be_found(member) expect_items_to_be_found(member)
expect_items_to_be_found(external_member) expect_items_to_be_found(external_member)
expect_non_code_items_to_be_found(guest) expect_items_to_be_found(guest, except: [:merge_requests, :blobs, :commits])
expect_no_items_to_be_found(non_member) expect_no_items_to_be_found(non_member)
expect_no_items_to_be_found(external_non_member) expect_no_items_to_be_found(external_non_member)
expect_no_items_to_be_found(nil) expect_no_items_to_be_found(nil)
...@@ -89,7 +89,7 @@ describe 'GlobalSearch', :elastic do ...@@ -89,7 +89,7 @@ describe 'GlobalSearch', :elastic do
expect_items_to_be_found(auditor) expect_items_to_be_found(auditor)
expect_items_to_be_found(member) expect_items_to_be_found(member)
expect_items_to_be_found(external_member) expect_items_to_be_found(external_member)
expect_non_code_items_to_be_found(guest) expect_items_to_be_found(guest, except: :merge_requests)
expect_no_items_to_be_found(non_member) expect_no_items_to_be_found(non_member)
expect_no_items_to_be_found(external_non_member) expect_no_items_to_be_found(external_non_member)
expect_no_items_to_be_found(nil) expect_no_items_to_be_found(nil)
...@@ -133,7 +133,7 @@ describe 'GlobalSearch', :elastic do ...@@ -133,7 +133,7 @@ describe 'GlobalSearch', :elastic do
expect_items_to_be_found(auditor) expect_items_to_be_found(auditor)
expect_items_to_be_found(member) expect_items_to_be_found(member)
expect_items_to_be_found(external_member) expect_items_to_be_found(external_member)
expect_non_code_items_to_be_found(guest) expect_items_to_be_found(guest, except: :merge_requests)
expect_no_items_to_be_found(non_member) expect_no_items_to_be_found(non_member)
expect_no_items_to_be_found(external_non_member) expect_no_items_to_be_found(external_non_member)
expect_no_items_to_be_found(nil) expect_no_items_to_be_found(nil)
...@@ -163,30 +163,35 @@ describe 'GlobalSearch', :elastic do ...@@ -163,30 +163,35 @@ describe 'GlobalSearch', :elastic do
end end
def expect_no_items_to_be_found(user) def expect_no_items_to_be_found(user)
results = search(user, 'term') expect_items_to_be_found(user, except: :all)
expect(results.issues_count).to eq(0)
expect(results.merge_requests_count).to eq(0)
expect(results.wiki_blobs_count).to eq(0)
expect(search(user, 'def').blobs_count).to eq(0)
expect(search(user, 'add').commits_count).to eq(0)
end end
def expect_items_to_be_found(user) POSSIBLE_FEATURES = %i(issues merge_requests wiki_blobs blobs commits).freeze
results = search(user, 'term')
expect(results.issues_count).not_to eq(0) def expect_items_to_be_found(user, only: nil, except: nil)
expect(results.merge_requests_count).not_to eq(0) arr = if only
expect(results.wiki_blobs_count).not_to eq(0) [only].flatten.compact
expect(search(user, 'def').blobs_count).not_to eq(0) elsif except == :all
expect(search(user, 'add').commits_count).not_to eq(0) []
else
POSSIBLE_FEATURES - [except].flatten.compact
end
check_count = lambda do |feature, c|
if arr.include?(feature)
expect(c).to be > 0
else
expect(c).to eq(0)
end
end end
def expect_non_code_items_to_be_found(user)
results = search(user, 'term') results = search(user, 'term')
expect(results.issues_count).not_to eq(0)
expect(results.wiki_blobs_count).not_to eq(0) check_count[:issues, results.issues_count]
expect(results.merge_requests_count).to eq(0) check_count[:merge_requests, results.merge_requests_count]
expect(search(user, 'def').blobs_count).to eq(0) check_count[:wiki_blobs, results.wiki_blobs_count]
expect(search(user, 'add').commits_count).to eq(0) check_count[:blobs, search(user, 'def').blobs_count]
check_count[:commits, search(user, 'add').commits_count]
end end
def search(user, search, snippets: false) def search(user, search, snippets: false)
......
...@@ -38,7 +38,7 @@ describe Search::GlobalService do ...@@ -38,7 +38,7 @@ describe Search::GlobalService do
update_feature_access_level(project, feature_access_level) update_feature_access_level(project, feature_access_level)
Gitlab::Elastic::Helper.refresh_index Gitlab::Elastic::Helper.refresh_index
expect_search_results(user, 'merge_requests', expected_count: expected_count, pending: pending?) do |user| expect_search_results(user, 'merge_requests', expected_count: expected_count) do |user|
described_class.new(user, search: merge_request.title).execute described_class.new(user, search: merge_request.title).execute
end end
...@@ -52,12 +52,6 @@ describe Search::GlobalService do ...@@ -52,12 +52,6 @@ describe Search::GlobalService do
context 'code' do context 'code' do
let!(:project) { create(:project, project_level, :repository, namespace: group ) } let!(:project) { create(:project, project_level, :repository, namespace: group ) }
let!(:note) { create :note_on_commit, project: project } let!(:note) { create :note_on_commit, project: project }
let(:pendings) do
[
{ project_level: :public, feature_access_level: :private, membership: :guest, expected_count: 1 },
{ project_level: :internal, feature_access_level: :private, membership: :guest, expected_count: 1 }
]
end
where(:project_level, :feature_access_level, :membership, :expected_count) do where(:project_level, :feature_access_level, :membership, :expected_count) do
permission_table_for_guest_feature_access_and_non_private_project_only permission_table_for_guest_feature_access_and_non_private_project_only
...@@ -69,7 +63,7 @@ describe Search::GlobalService do ...@@ -69,7 +63,7 @@ describe Search::GlobalService do
ElasticCommitIndexerWorker.new.perform(project.id) ElasticCommitIndexerWorker.new.perform(project.id)
Gitlab::Elastic::Helper.refresh_index Gitlab::Elastic::Helper.refresh_index
expect_search_results(user, 'commits', expected_count: expected_count, pending: pending?) do |user| expect_search_results(user, 'commits', expected_count: expected_count) do |user|
described_class.new(user, search: 'initial').execute described_class.new(user, search: 'initial').execute
end end
......
...@@ -80,12 +80,6 @@ describe Search::GroupService, :elastic do ...@@ -80,12 +80,6 @@ describe Search::GroupService, :elastic do
let!(:merge_request2) { create :merge_request, target_project: project2, source_project: project2, title: merge_request.title } let!(:merge_request2) { create :merge_request, target_project: project2, source_project: project2, title: merge_request.title }
let!(:note) { create :note, project: project, noteable: merge_request } let!(:note) { create :note, project: project, noteable: merge_request }
let!(:note2) { create :note, project: project2, noteable: merge_request2, note: note.note } let!(:note2) { create :note, project: project2, noteable: merge_request2, note: note.note }
let(:pendings) do
[
{ project_level: :public, feature_access_level: :enabled, membership: :guest, expected_count: 1 },
{ project_level: :internal, feature_access_level: :enabled, membership: :guest, expected_count: 1 }
]
end
where(:project_level, :feature_access_level, :membership, :expected_count) do where(:project_level, :feature_access_level, :membership, :expected_count) do
permission_table_for_reporter_feature_access permission_table_for_reporter_feature_access
...@@ -98,7 +92,7 @@ describe Search::GroupService, :elastic do ...@@ -98,7 +92,7 @@ describe Search::GroupService, :elastic do
end end
Gitlab::Elastic::Helper.refresh_index Gitlab::Elastic::Helper.refresh_index
expect_search_results(user, 'merge_requests', expected_count: expected_count, pending: pending?) do |user| expect_search_results(user, 'merge_requests', expected_count: expected_count) do |user|
described_class.new(user, group, search: merge_request.title).execute described_class.new(user, group, search: merge_request.title).execute
end end
...@@ -112,14 +106,6 @@ describe Search::GroupService, :elastic do ...@@ -112,14 +106,6 @@ describe Search::GroupService, :elastic do
context 'code' do context 'code' do
let!(:project) { create(:project, project_level, :repository, namespace: group ) } let!(:project) { create(:project, project_level, :repository, namespace: group ) }
let!(:note) { create :note_on_commit, project: project } let!(:note) { create :note_on_commit, project: project }
let(:pendings) do
[
{ project_level: :public, feature_access_level: :enabled, membership: :guest, expected_count: 1 },
{ project_level: :public, feature_access_level: :private, membership: :guest, expected_count: 1 },
{ project_level: :internal, feature_access_level: :enabled, membership: :guest, expected_count: 1 },
{ project_level: :internal, feature_access_level: :private, membership: :guest, expected_count: 1 }
]
end
where(:project_level, :feature_access_level, :membership, :expected_count) do where(:project_level, :feature_access_level, :membership, :expected_count) do
permission_table_for_guest_feature_access_and_non_private_project_only permission_table_for_guest_feature_access_and_non_private_project_only
...@@ -134,15 +120,15 @@ describe Search::GroupService, :elastic do ...@@ -134,15 +120,15 @@ describe Search::GroupService, :elastic do
ElasticCommitIndexerWorker.new.perform(project.id) ElasticCommitIndexerWorker.new.perform(project.id)
Gitlab::Elastic::Helper.refresh_index Gitlab::Elastic::Helper.refresh_index
expect_search_results(user, 'commits', expected_count: expected_count, pending: pending?) do |user| expect_search_results(user, 'commits', expected_count: expected_count) do |user|
described_class.new(user, group, search: 'initial').execute described_class.new(user, group, search: 'initial').execute
end end
expect_search_results(user, 'blobs', expected_count: expected_count, pending: pending?) do |user| expect_search_results(user, 'blobs', expected_count: expected_count) do |user|
described_class.new(user, group, search: '.gitmodules').execute described_class.new(user, group, search: '.gitmodules').execute
end end
expect_search_results(user, 'notes', expected_count: expected_count, pending: pending?) do |user| expect_search_results(user, 'notes', expected_count: expected_count) do |user|
described_class.new(user, group, search: note.note).execute described_class.new(user, group, search: note.note).execute
end end
end end
......
...@@ -30,12 +30,6 @@ describe Search::ProjectService do ...@@ -30,12 +30,6 @@ describe Search::ProjectService do
let!(:merge_request2) { create :merge_request, target_project: project2, source_project: project2, title: merge_request.title } let!(:merge_request2) { create :merge_request, target_project: project2, source_project: project2, title: merge_request.title }
let!(:note) { create :note, project: project, noteable: merge_request } let!(:note) { create :note, project: project, noteable: merge_request }
let!(:note2) { create :note, project: project2, noteable: merge_request2, note: note.note } let!(:note2) { create :note, project: project2, noteable: merge_request2, note: note.note }
let(:pendings) do
[
{ project_level: :public, feature_access_level: :enabled, membership: :guest, expected_count: 1 },
{ project_level: :internal, feature_access_level: :enabled, membership: :guest, expected_count: 1 }
]
end
where(:project_level, :feature_access_level, :membership, :expected_count) do where(:project_level, :feature_access_level, :membership, :expected_count) do
permission_table_for_reporter_feature_access permission_table_for_reporter_feature_access
...@@ -48,7 +42,7 @@ describe Search::ProjectService do ...@@ -48,7 +42,7 @@ describe Search::ProjectService do
end end
Gitlab::Elastic::Helper.refresh_index Gitlab::Elastic::Helper.refresh_index
expect_search_results(user, 'merge_requests', expected_count: expected_count, pending: pending?) do |user| expect_search_results(user, 'merge_requests', expected_count: expected_count) do |user|
described_class.new(project, user, search: merge_request.title).execute described_class.new(project, user, search: merge_request.title).execute
end end
...@@ -77,7 +71,7 @@ describe Search::ProjectService do ...@@ -77,7 +71,7 @@ describe Search::ProjectService do
end end
Gitlab::Elastic::Helper.refresh_index Gitlab::Elastic::Helper.refresh_index
expect_search_results(user, 'commits', expected_count: expected_count, pending: pending?) do |user| expect_search_results(user, 'commits', expected_count: expected_count) do |user|
described_class.new(project, user, search: 'initial').execute described_class.new(project, user, search: 'initial').execute
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment