Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Upd] Add better authentication for RDF4jModule #143

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package cz.cvut.spipes.engine;

import cz.cvut.spipes.util.CoreConfigProperies;
import org.apache.jena.query.QuerySolution;
import org.apache.jena.query.QuerySolutionMap;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.rdf.model.ResourceFactory;

import java.util.Map;

public class ExecutionContextFactory {

Expand All @@ -11,21 +17,21 @@ public class ExecutionContextFactory {
public static ExecutionContext createEmptyContext() {
ExecutionContextImpl context = new ExecutionContextImpl();
context.setDefaultModel(ModelFactory.createDefaultModel());
context.setVariablesBinding(new VariablesBinding());
context.setVariablesBinding(extendByConfigurationVariables(new VariablesBinding()));
return context;
}

public static ExecutionContext createContext(Model defaultModel, VariablesBinding variablesBinding) {
ExecutionContextImpl context = new ExecutionContextImpl();
context.setDefaultModel(defaultModel);
context.setVariablesBinding(variablesBinding);
context.setVariablesBinding(extendByConfigurationVariables(variablesBinding));
return context;
}

public static ExecutionContext createContext(VariablesBinding variablesBinding) {
ExecutionContextImpl context = new ExecutionContextImpl();
context.setDefaultModel(ModelFactory.createDefaultModel());
context.setVariablesBinding(variablesBinding);
context.setVariablesBinding(extendByConfigurationVariables(variablesBinding));
return context;
}

Expand All @@ -34,8 +40,25 @@ public static ExecutionContext createContext(VariablesBinding variablesBinding)
public static ExecutionContext createContext(Model defaultModel) {
ExecutionContextImpl context = new ExecutionContextImpl();
context.setDefaultModel(defaultModel);
context.setVariablesBinding(new VariablesBinding());
context.setVariablesBinding(extendByConfigurationVariables(new VariablesBinding()));
return context;
}

private static VariablesBinding extendByConfigurationVariables(VariablesBinding variable) {
Map<String,String> map = CoreConfigProperies.getConfigurationVariables();
variable.extendConsistently(new VariablesBinding(transform(map)));
return variable;
}

private static QuerySolution transform(final Map<String,String> parameters) {
final QuerySolutionMap querySolution = new QuerySolutionMap();

parameters.entrySet()
.stream()
.filter(parameter -> parameter.getKey().startsWith(CoreConfigProperies.variableAssignmentPrefix))
.forEach(
e -> querySolution.add(e.getKey(), ResourceFactory.createPlainLiteral(e.getValue()))
);
return querySolution;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cz.cvut.spipes.engine;

import cz.cvut.spipes.util.CoreConfigProperies;
import org.apache.jena.query.QuerySolution;
import org.apache.jena.query.QuerySolutionMap;
import org.apache.jena.rdf.model.*;
Expand Down Expand Up @@ -179,12 +180,14 @@ public void load(final InputStream is, final String lang) throws IOException {

@Override
public String toString() {
return binding.asMap().toString();
return getVariablesWithoutConfigurationOnes().toString();
}

public String toTruncatedString() {
return binding.asMap().entrySet().stream()
.map(e -> new AbstractMap.SimpleEntry<>(e.getKey(), getTruncatedValue(e.getValue().toString()))).
Map<String,String> map = getVariablesWithoutConfigurationOnes();

return map.entrySet().stream()
.map(e -> new AbstractMap.SimpleEntry<>(e.getKey(), getTruncatedValue(e.getValue()))).
collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)).toString();
}

Expand All @@ -194,4 +197,11 @@ private static String getTruncatedValue(@NotNull String value) {
}
return value;
}

private Map<String, String> getVariablesWithoutConfigurationOnes(){
return binding.asMap().entrySet()
.stream()
.filter(entry -> !entry.getKey().startsWith(CoreConfigProperies.variableAssignmentPrefix))
.collect(Collectors.toMap(Map.Entry::getKey, o -> o.getValue().toString(), (x, y) -> y, HashMap::new));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

public class CoreConfigProperies {
public static final String variableAssignmentPrefix = "variable.assignment";
private static final String CONFIG_FILE = "config-core.properties";
private static final java.util.Properties prop = new java.util.Properties();
private static final Logger LOG = LoggerFactory.getLogger(CoreConfigProperies.class);
Expand Down Expand Up @@ -38,6 +42,13 @@ public class CoreConfigProperies {
}
}

public static Map<String,String> getConfigurationVariables(){
Matthew-Kulich marked this conversation as resolved.
Show resolved Hide resolved
Map<String, String> map = new HashMap<>((Map) prop);
return map.entrySet()
.stream()
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (x, y) -> y, HashMap::new));
}

public static String get(String name) {
String value = prop.getProperty(name);
if (value == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import cz.cvut.spipes.engine.ExecutionContext;
import cz.cvut.spipes.engine.ExecutionContextFactory;
import cz.cvut.spipes.engine.VariablesBinding;
import cz.cvut.spipes.util.CoreConfigProperies;
import org.apache.jena.rdf.model.Property;
import org.apache.jena.rdf.model.RDFNode;
import org.apache.jena.rdf.model.ResourceFactory;
Expand All @@ -29,12 +30,12 @@
import java.io.StringWriter;
import java.util.Optional;

public class Rdf4jModule extends AbstractModule {
public class Rdf4jModule extends AnnotatedAbstractModule {

private static final Logger LOG = LoggerFactory.getLogger(Rdf4jModule.class);

private static String TYPE_URI = KBSS_MODULE.getURI()+"deploy";
private static String PROPERTY_PREFIX_URI = KBSS_MODULE.getURI()+"rdf4j";
private static final String PROPERTY_PREFIX_URI = KBSS_MODULE.uri + "rdf4j";

private static Property getParameter(final String name) {
return ResourceFactory.createProperty(PROPERTY_PREFIX_URI + "/" + name);
Expand Down Expand Up @@ -64,6 +65,12 @@ private static Property getParameter(final String name) {
static final Property P_IS_REPLACE_CONTEXT_IRI = getParameter("p-is-replace");
private boolean isReplaceContext;

@Parameter(urlPrefix = PROPERTY_PREFIX_URI + "/", name = "p-rdf4j-secured-username-variable")
private String securedUsername;

@Parameter(urlPrefix = PROPERTY_PREFIX_URI + "/", name = "p-rdf4j-secured-password-variable")
private String securedPassword;

public String getRdf4jServerURL() {
return rdf4jServerURL;
}
Expand Down Expand Up @@ -108,12 +115,12 @@ ExecutionContext executeSelf() {
VariablesBinding variablesBinding = getExecutionContext().getVariablesBinding();

String username = Optional
.ofNullable(variablesBinding.getNode("p-username"))
.ofNullable(variablesBinding.getNode(CoreConfigProperies.variableAssignmentPrefix + "." + securedUsername))
.map(RDFNode::toString)
.orElse(null);

String password = Optional
.ofNullable(variablesBinding.getNode("p-password"))
.ofNullable(variablesBinding.getNode(CoreConfigProperies.variableAssignmentPrefix + "." + securedPassword))
.map(RDFNode::toString)
.orElse(null);

Expand Down Expand Up @@ -181,6 +188,7 @@ public String getTypeURI() {

@Override
public void loadConfiguration() {
super.loadConfiguration();
rdf4jServerURL = getEffectiveValue(P_RDF4J_SERVER_URL).asLiteral().getString();
rdf4jRepositoryName = getEffectiveValue(P_RDF4J_REPOSITORY_NAME).asLiteral().getString();
if (this.getPropertyValue(P_RDF4J_CONTEXT_IRI) != null) {
Expand Down