forked from apache/jena
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
jena-fuseki-access - Dynamic Graph selection at query time
- "#pragma acl.graphs" preamble can be used to specify one or more graphs to restriction query exectution to - The functionality is only enabled if an access:entry for a specific user contains just <urn:jena:accessGraphsDynamic> - Requires context propagation patch (see apache#1291), since query is stored in request-specific context
- Loading branch information
Showing
4 changed files
with
126 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
...ena-fuseki-access/src/main/java/org/apache/jena/fuseki/access/SecurityContextDynamic.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.jena.fuseki.access; | ||
|
||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.function.Predicate; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import org.apache.jena.graph.Node; | ||
import org.apache.jena.graph.NodeFactory; | ||
import org.apache.jena.query.QueryExecution; | ||
import org.apache.jena.sparql.core.DatasetGraph; | ||
import org.apache.jena.sparql.core.Quad; | ||
|
||
/** A {@link SecurityContext} that provides no access itself but indicates that the set of visible graphs should be | ||
* constructed from a set embedded in the query. Use {@link SecurityContextDynamic#instantiateFromQuery} to create the | ||
* query-specific set of graphs to filter. | ||
* This context is not meant to be used directly by end users: The initial query has a list of visible graphs prepended | ||
* before execution based on use-case specific criteria, with the following format: | ||
* | ||
* #pragma acl.graphs <graph1>|<graph2>|..|<graphN> | ||
* | ||
*/ | ||
public class SecurityContextDynamic implements SecurityContext { | ||
|
||
public static final Node dynamicAccess = NodeFactory.createURI("urn:jena:accessGraphsDynamic"); | ||
|
||
private static final Pattern GRAPH_PRAGMA_DELIMITER_PATTERN = Pattern.compile("\\|"); | ||
private static final String GRAPH_PRAGMA_PATTERN_GROUP = "graphs"; | ||
private static final Pattern GRAPH_PRAGMA_PATTERN = Pattern.compile( | ||
"^" + | ||
// Leading whitespace | ||
"\\s*" + | ||
// For simplicity, expect pragma on first non-whitespace line | ||
"#pragma acl\\.graphs\\h+(?<" + GRAPH_PRAGMA_PATTERN_GROUP + ">\\S*)\\h*\\v" + | ||
// Anything else | ||
".*", | ||
Pattern.DOTALL | ||
); | ||
|
||
public SecurityContextDynamic() {} | ||
|
||
@Override | ||
public Collection<Node> visibleGraphs() { | ||
return Collections.emptyList(); | ||
} | ||
|
||
@Override | ||
public boolean visableDefaultGraph() { return false; } | ||
|
||
@Override | ||
public Predicate<Quad> predicateQuad() { return q -> false; } | ||
|
||
@Override | ||
public void filterTDB(DatasetGraph dsg, QueryExecution qExec) { | ||
Predicate<?> pred = tuple->false; | ||
qExec.getContext().set(GraphFilter.getContextKey(dsg), pred); | ||
} | ||
|
||
public SecurityContext instantiateFromQuery(String queryString) { | ||
if ( queryString != null ) { | ||
Matcher m = GRAPH_PRAGMA_PATTERN.matcher(queryString); | ||
if ( m.matches() ) { | ||
String[] graphs = GRAPH_PRAGMA_DELIMITER_PATTERN | ||
.splitAsStream(m.group(GRAPH_PRAGMA_PATTERN_GROUP)) | ||
.map(String::trim) | ||
.toArray(String[]::new); | ||
return new SecurityContextView(graphs); | ||
} | ||
} | ||
return new SecurityContextAllowNone(); | ||
} | ||
} |