DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Data Engineering
  3. Databases
  4. How to implement row level access control in Lucene

How to implement row level access control in Lucene

Aaron McCurry user avatar by
Aaron McCurry
·
Oct. 07, 08 · Interview
Like (1)
Save
Tweet
Share
13.99K Views

Join the DZone community and get the full member experience.

Join For Free
Below I have written some fully functionally code that shows how you could implement row level access control in Lucene (2.3.2). Basically you have to index enough information to be able to search (in a single query) and find all documents that a given user has access to read.

In the below example there are two fields:

DATA: Which contains any data that you want your users to be able to search. NOTE: You can have as many data fields as you like.

ACL_FIELD: The field used to determine what users have access to this document. Note: You can have as many access control fields as you like.

All you have to do is built the access control query for each user and submit your user's query unchanged.
public class TestIndexerSearcher {

public static void main(String[] args) throws Exception {
Directory directory = new RAMDirectory();
IndexWriter indexWriter = new IndexWriter(directory, new StandardAnalyzer());
indexWriter.addDocument(buildDocument("DATA:sametoken","ACL_FIELD:access"));
indexWriter.addDocument(buildDocument("DATA:sametoken","ACL_FIELD:noaccess"));
indexWriter.optimize();
indexWriter.close();

IndexSearcher indexSearcher = new IndexSearcher(directory);

QueryParser parser = new QueryParser("DATA", new StandardAnalyzer());
Query query = parser.parse("sametoken");

//This is all you have to add to your existing code.
Filter aclFilter = applyAccessControl(new TermQuery(
new Term("ACL_FIELD","access")));

Hits hits = indexSearcher.search(query, aclFilter);
System.out.println("Hits[" + hits.length() + "]");
for (int i = 0; i < hits.length(); i++) {
Document doc = hits.doc(i);
System.out.println("DATA [" + doc.get("DATA") +
"] ACL_FIELD [" + doc.get("ACL_FIELD") + "]");
}
indexSearcher.close();
}

private static Filter applyAccessControl(Query aclQuery) {
return new CachedQueryFilter(aclQuery.toString(),
new QueryWrapperFilter(aclQuery));
}

private static Document buildDocument(String... fieldInfo) {
Document document = new Document();
for (int i = 0; i < fieldInfo.length; i++) {
String[] split = fieldInfo[i].split(":");
String fieldName = split[0];
String fieldValue = split[1];
document.add(new Field(fieldName,fieldValue,
Field.Store.YES,Field.Index.TOKENIZED));
}
return document;
}
}

After you run this code, you will get a single hit, not the two that you would normally get if the access control filter wasn't in place.
public class CachedQueryFilter extends Filter {
private static final long serialVersionUID = 6797293376134753695L;
private Filter filter;
private String key;
private static transient Map<String, BitSetCache> filterCache =
new ConcurrentHashMap<String, BitSetCache>();

public CachedQueryFilter(String key, Filter filter) {
this.filter = filter;
this.key = key;
}

public BitSet bits(IndexReader reader) throws IOException {
BitSetCache cachedBitSet = (BitSetCache) filterCache.get(key);
if (cachedBitSet != null) {
BitSet bitSet = cachedBitSet.bitSet.get();
if (bitSet != null && cachedBitSet.indexReaderVersion == reader.getVersion()) {
return bitSet;
}
}
BitSet bits = filter.bits(reader);
BitSetCache bitSetCache = new BitSetCache();
bitSetCache.indexReaderVersion = reader.getVersion();
bitSetCache.bitSet = new SoftReference<BitSet>(bits);
filterCache.put(key, bitSetCache);
return bits;
}

private class BitSetCache {
long indexReaderVersion;
SoftReference<BitSet> bitSet;
}
}

There are two additional features that this query filter doesn't implements that you may want to consider.

1st - Provide per query locking around the bitset creation code. This would allow multiple bitset creation calls to occur at once, but the same access control query would block. Therefore we would only have to build it once, even if multiple user queries with the same access control hit the query filter at once.

2nd - Persist the bitsets. In the past I have used the same directory as the index, but you may want to use a database, or something else.

From near @ infinity blog

Database Lucene

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Choosing the Right Framework for Your Project
  • Introduction to Container Orchestration
  • Using GPT-3 in Our Applications
  • Practical Example of Using CSS Layer

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: