Skip to content
This repository was archived by the owner on Feb 9, 2021. It is now read-only.

Commit 4a7b47d

Browse files
committed
BZ1192831: User with no privileges for Repository can view and modify Assets in that Repository
(cherry picked from commit e934735)
1 parent d8a7add commit 4a7b47d

File tree

2 files changed

+253
-37
lines changed
  • uberfire-metadata
    • uberfire-metadata-backends/uberfire-metadata-backend-lucene/src/main/java/org/uberfire/ext/metadata/backend/lucene/search
    • uberfire-metadata-commons-io/src/test/java/org/uberfire/ext/metadata/io

2 files changed

+253
-37
lines changed

uberfire-metadata/uberfire-metadata-backends/uberfire-metadata-backend-lucene/src/main/java/org/uberfire/ext/metadata/backend/lucene/search/LuceneSearchIndex.java

Lines changed: 48 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -177,49 +177,60 @@ private String buildTerm( final String term ) {
177177

178178
private Query composeQuery( final Query query,
179179
final ClusterSegment... clusterSegments ) {
180-
if ( clusterSegments != null && clusterSegments.length > 0 ) {
181-
final BooleanQuery booleanQuery = new BooleanQuery();
182-
booleanQuery.add( query, MUST );
183-
184-
final BooleanQuery complement;
180+
if ( clusterSegments == null || clusterSegments.length == 0 ) {
181+
return query;
182+
}
185183

186-
if ( clusterSegments.length == 1 ) {
187-
complement = booleanQuery;
188-
} else {
189-
complement = new BooleanQuery();
190-
}
184+
final BooleanQuery booleanQuery = new BooleanQuery();
185+
booleanQuery.add( query,
186+
MUST );
187+
188+
final BooleanClause.Occur occur = ( clusterSegments.length == 1 ? MUST : SHOULD );
189+
for ( ClusterSegment clusterSegment : clusterSegments ) {
190+
final BooleanQuery clusterSegmentQuery = new BooleanQuery();
191+
addClusterIdTerms( clusterSegmentQuery,
192+
clusterSegment );
193+
addSegmentIdTerms( clusterSegmentQuery,
194+
clusterSegment );
195+
booleanQuery.add( clusterSegmentQuery,
196+
occur );
197+
}
191198

192-
for ( final ClusterSegment clusterSegment : clusterSegments ) {
193-
final BooleanQuery clusterBoolean = new BooleanQuery();
194-
if ( clusterSegment.getClusterId() != null ) {
195-
final Query cluster = new TermQuery( new Term( "cluster.id", clusterSegment.getClusterId() ) );
196-
clusterBoolean.add( cluster, MUST );
197-
}
198-
if ( clusterSegment.segmentIds() != null && clusterSegment.segmentIds().length > 0 ) {
199-
if ( clusterSegment.segmentIds().length == 1 ) {
200-
final Query segment = new TermQuery( new Term( "segment.id", clusterSegment.segmentIds()[ 0 ] ) );
201-
clusterBoolean.add( segment, MUST );
202-
} else {
203-
final BooleanQuery segments = new BooleanQuery();
204-
for ( final String segmentId : clusterSegment.segmentIds() ) {
205-
final Query segment = new TermQuery( new Term( "segment.id", segmentId ) );
206-
segments.add( segment, BooleanClause.Occur.SHOULD );
207-
}
208-
clusterBoolean.add( segments, MUST );
209-
}
210-
}
211-
complement.add( clusterBoolean, MUST );
212-
}
199+
return booleanQuery;
200+
}
213201

214-
if ( clusterSegments.length == 1 ) {
215-
return complement;
216-
}
202+
private void addClusterIdTerms( final BooleanQuery query,
203+
final ClusterSegment clusterSegment ) {
204+
if ( clusterSegment.getClusterId() != null ) {
205+
final Query cluster = new TermQuery( new Term( "cluster.id",
206+
clusterSegment.getClusterId() ) );
207+
query.add( cluster,
208+
MUST );
209+
}
210+
}
217211

218-
booleanQuery.add( complement, MUST );
219-
return booleanQuery;
212+
private void addSegmentIdTerms( final BooleanQuery query,
213+
final ClusterSegment clusterSegment ) {
214+
if ( clusterSegment.segmentIds() == null || clusterSegment.segmentIds().length == 0 ) {
215+
return;
220216
}
221217

222-
return query;
218+
if ( clusterSegment.segmentIds().length == 1 ) {
219+
final Query segment = new TermQuery( new Term( "segment.id",
220+
clusterSegment.segmentIds()[ 0 ] ) );
221+
query.add( segment,
222+
MUST );
223+
} else {
224+
final BooleanQuery segments = new BooleanQuery();
225+
for ( final String segmentId : clusterSegment.segmentIds() ) {
226+
final Query segment = new TermQuery( new Term( "segment.id",
227+
segmentId ) );
228+
segments.add( segment,
229+
SHOULD );
230+
}
231+
query.add( segments,
232+
MUST );
233+
}
223234
}
224235

225236
private String format( final String term ) {
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
/*
2+
* Copyright 2014 JBoss, by Red Hat, Inc
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.uberfire.ext.metadata.io;
18+
19+
import java.io.File;
20+
import java.io.IOException;
21+
import java.net.URI;
22+
import java.util.ArrayList;
23+
import java.util.HashMap;
24+
import java.util.List;
25+
import java.util.Map;
26+
import java.util.Random;
27+
28+
import org.apache.commons.io.FileUtils;
29+
import org.junit.AfterClass;
30+
import org.junit.Before;
31+
import org.junit.BeforeClass;
32+
import org.junit.Test;
33+
import org.uberfire.ext.metadata.backend.lucene.LuceneConfig;
34+
import org.uberfire.ext.metadata.backend.lucene.LuceneConfigBuilder;
35+
import org.uberfire.ext.metadata.search.ClusterSegment;
36+
import org.uberfire.io.IOService;
37+
import org.uberfire.java.nio.base.FileSystemId;
38+
import org.uberfire.java.nio.base.SegmentedPath;
39+
import org.uberfire.java.nio.file.Path;
40+
41+
import static org.junit.Assert.*;
42+
43+
public class LuceneSearchIndexTest {
44+
45+
private int seed = new Random( 10L ).nextInt();
46+
47+
protected boolean created = false;
48+
private Map<String, Path> basePaths = new HashMap<String, Path>();
49+
50+
private LuceneConfig config;
51+
private IOService ioService = null;
52+
53+
protected static final List<File> tempFiles = new ArrayList<File>();
54+
55+
@AfterClass
56+
@BeforeClass
57+
public static void cleanup() {
58+
for ( final File tempFile : tempFiles ) {
59+
FileUtils.deleteQuietly( tempFile );
60+
}
61+
}
62+
63+
protected IOService ioService() {
64+
if ( ioService == null ) {
65+
config = new LuceneConfigBuilder()
66+
.withInMemoryMetaModelStore()
67+
.useDirectoryBasedIndex()
68+
.useInMemoryDirectory()
69+
.build();
70+
71+
ioService = new IOServiceIndexedImpl( config.getIndexEngine() );
72+
}
73+
return ioService;
74+
}
75+
76+
protected static File createTempDirectory() throws IOException {
77+
final File temp = File.createTempFile( "temp", Long.toString( System.nanoTime() ) );
78+
if ( !( temp.delete() ) ) {
79+
throw new IOException( "Could not delete temp file: " + temp.getAbsolutePath() );
80+
}
81+
if ( !( temp.mkdir() ) ) {
82+
throw new IOException( "Could not create temp directory: " + temp.getAbsolutePath() );
83+
}
84+
tempFiles.add( temp );
85+
return temp;
86+
}
87+
88+
@Before
89+
public void setup() throws IOException {
90+
if ( !created ) {
91+
final String path = createTempDirectory().getAbsolutePath();
92+
System.setProperty( "org.uberfire.nio.git.dir",
93+
path );
94+
System.out.println( ".niogit: " + path );
95+
96+
for ( String repositoryName : getRepositoryNames() ) {
97+
98+
final URI newRepo = URI.create( "git://" + repositoryName );
99+
100+
try {
101+
ioService().newFileSystem( newRepo,
102+
new HashMap<String, Object>() );
103+
104+
//Don't ask, but we need to write a single file first in order for indexing to work
105+
final Path basePath = getDirectoryPath( repositoryName ).resolveSibling( "someNewOtherPath" );
106+
ioService().write( basePath.resolve( "dummy" ),
107+
"<none>" );
108+
basePaths.put( repositoryName,
109+
basePath );
110+
111+
} catch ( final Exception ex ) {
112+
ex.fillInStackTrace();
113+
System.out.println( ex.getMessage() );
114+
} finally {
115+
created = true;
116+
}
117+
}
118+
}
119+
}
120+
121+
private String[] getRepositoryNames() {
122+
return new String[]{ this.getClass().getSimpleName() + "_1", this.getClass().getSimpleName() + "_2" };
123+
}
124+
125+
private Path getBasePath( final String repositoryName ) {
126+
return basePaths.get( repositoryName );
127+
}
128+
129+
private Path getDirectoryPath( final String repositoryName ) {
130+
final Path dir = ioService().get( URI.create( "git://" + repositoryName + "/_someDir" + seed ) );
131+
ioService().deleteIfExists( dir );
132+
return dir;
133+
}
134+
135+
@Test
136+
public void testClusterSegments() throws IOException, InterruptedException {
137+
//Add test files
138+
final Path path1 = getBasePath( this.getClass().getSimpleName() + "_1" ).resolve( "indexedFile1.txt" );
139+
ioService().write( path1,
140+
"content1" );
141+
final Path path2 = getBasePath( this.getClass().getSimpleName() + "_2" ).resolve( "indexedFile2.txt" );
142+
ioService().write( path2,
143+
"content2" );
144+
145+
//Setup ClusterSegments
146+
final ClusterSegment cs1 = new ClusterSegment() {
147+
@Override
148+
public String getClusterId() {
149+
return ( (FileSystemId) getBasePath( LuceneSearchIndexTest.this.getClass().getSimpleName() + "_1" ).getFileSystem() ).id();
150+
}
151+
152+
@Override
153+
public String[] segmentIds() {
154+
return new String[]{ ( (SegmentedPath) getBasePath( LuceneSearchIndexTest.this.getClass().getSimpleName() + "_1" ) ).getSegmentId() };
155+
}
156+
};
157+
final ClusterSegment cs2 = new ClusterSegment() {
158+
@Override
159+
public String getClusterId() {
160+
return ( (FileSystemId) getBasePath( LuceneSearchIndexTest.this.getClass().getSimpleName() + "_2" ).getFileSystem() ).id();
161+
}
162+
163+
@Override
164+
public String[] segmentIds() {
165+
return new String[]{ ( (SegmentedPath) getBasePath( LuceneSearchIndexTest.this.getClass().getSimpleName() + "_2" ) ).getSegmentId() };
166+
}
167+
};
168+
169+
Thread.sleep( 5000 ); //wait for events to be consumed from jgit -> (notify changes -> watcher -> index) -> lucene index
170+
171+
final Map<String, Object> attributes = new HashMap<String, Object>() {{
172+
put( "filename",
173+
"*.txt" );
174+
}};
175+
176+
{
177+
final int hits = config.getSearchIndex().searchByAttrsHits( attributes );
178+
assertEquals( 2,
179+
hits );
180+
}
181+
182+
{
183+
final int hits = config.getSearchIndex().searchByAttrsHits( attributes,
184+
cs1 );
185+
assertEquals( 1,
186+
hits );
187+
}
188+
189+
{
190+
final int hits = config.getSearchIndex().searchByAttrsHits( attributes,
191+
cs2 );
192+
assertEquals( 1,
193+
hits );
194+
}
195+
196+
{
197+
final int hits = config.getSearchIndex().searchByAttrsHits( attributes,
198+
cs1,
199+
cs2 );
200+
assertEquals( 2,
201+
hits );
202+
}
203+
}
204+
205+
}

0 commit comments

Comments
 (0)