Skip to content

Commit

Permalink
feat(jans-core): compile java code on the fly for custom script
Browse files Browse the repository at this point in the history
  • Loading branch information
yuriyz committed May 13, 2022
1 parent 0db5b85 commit 5da6e27
Show file tree
Hide file tree
Showing 10 changed files with 270 additions and 190 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import io.jans.service.LookupService;
import io.jans.service.custom.script.CustomScriptManager;
import io.jans.service.custom.script.ExternalScriptService;
import io.jans.service.custom.script.ExternalTypeCreator;
import io.jans.util.StringHelper;
import org.apache.commons.io.FileUtils;

Expand Down Expand Up @@ -46,6 +47,8 @@ public class ExternalUmaRptPolicyService extends ExternalScriptService {
private LookupService lookupService;
@Inject
private CustomScriptManager scriptManager;
@Inject
private ExternalTypeCreator externalTypeCreator;

protected Map<String, CustomScriptConfiguration> scriptInumMap;

Expand Down Expand Up @@ -83,7 +86,7 @@ public CustomScriptConfiguration getScriptByInum(String inum) {
}

private UmaRptPolicyType policyScript(CustomScriptConfiguration script) {
return HOTSWAP_UMA_SCRIPT ? (UmaRptPolicyType) hotswap(scriptManager, script, true) :
return HOTSWAP_UMA_SCRIPT ? (UmaRptPolicyType) hotswap(externalTypeCreator, script, true) :
(UmaRptPolicyType) script.getExternalType();
}

Expand Down Expand Up @@ -126,7 +129,7 @@ public String getClaimsGatheringScriptName(CustomScriptConfiguration script, Uma
}
}

public static <T> T hotswap(CustomScriptManager scriptManager, CustomScriptConfiguration script, boolean rptPolicyScript) {
public static <T> T hotswap(ExternalTypeCreator externalTypeCreator, CustomScriptConfiguration script, boolean rptPolicyScript) {
if (!HOTSWAP_UMA_SCRIPT) {
throw new RuntimeException("UMA script hotswap is not allowed");
}
Expand All @@ -140,7 +143,7 @@ public static <T> T hotswap(CustomScriptManager scriptManager, CustomScriptConfi
try {
String scriptCode = FileUtils.readFileToString(new File(scriptPath));
script.getCustomScript().setScript(scriptCode);
return (T) scriptManager.createExternalTypeFromStringWithPythonException(script.getCustomScript(), script.getConfigurationAttributes());
return (T) externalTypeCreator.createExternalTypeFromStringWithPythonException(script.getCustomScript());
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand Down
7 changes: 7 additions & 0 deletions jans-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,13 @@
<scope>test</scope>
</dependency>

<!-- java compiler lib -->
<dependency>
<groupId>net.openhft</groupId>
<artifactId>compiler</artifactId>
<version>2.21ea82</version>
</dependency>

<!-- Jetty -->
<dependency>
<groupId>org.eclipse.jetty</groupId>
Expand Down
5 changes: 5 additions & 0 deletions jans-core/script/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@
<artifactId>jans-orm-ldap</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>net.openhft</groupId>
<artifactId>compiler</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,10 @@ public enum CustomScriptType implements AttributeEnum {
private String displayName;
private Class<? extends BaseExternalType> customScriptType;
private Class<? extends CustomScript> customScriptModel;
private String pythonClass;
private String className;
private BaseExternalType defaultImplementation;

private static Map<String, CustomScriptType> MAP_BY_VALUES = new HashMap<String, CustomScriptType>();
private static final Map<String, CustomScriptType> MAP_BY_VALUES = new HashMap<>();

static {
for (CustomScriptType enumType : values()) {
Expand All @@ -124,12 +124,12 @@ public enum CustomScriptType implements AttributeEnum {
}

CustomScriptType(String value, String displayName, Class<? extends BaseExternalType> customScriptType,
Class<? extends CustomScript> customScriptModel, String pythonClass, BaseExternalType defaultImplementation) {
Class<? extends CustomScript> customScriptModel, String className, BaseExternalType defaultImplementation) {
this.displayName = displayName;
this.value = value;
this.customScriptType = customScriptType;
this.customScriptModel = customScriptModel;
this.pythonClass = pythonClass;
this.className = className;
this.defaultImplementation = defaultImplementation;
}

Expand All @@ -153,8 +153,8 @@ public Class<? extends CustomScript> getCustomScriptModel() {
return customScriptModel;
}

public String getPythonClass() {
return pythonClass;
public String getClassName() {
return className;
}

public BaseExternalType getDefaultImplementation() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,24 @@

package io.jans.model.custom.script.model;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import jakarta.persistence.Transient;
import jakarta.validation.constraints.Pattern;
import jakarta.validation.constraints.Size;

import io.jans.model.ProgrammingLanguage;
import io.jans.model.ScriptLocationType;
import io.jans.model.SimpleCustomProperty;
import io.jans.model.SimpleExtendedCustomProperty;
import io.jans.util.StringHelper;
import io.jans.model.custom.script.CustomScriptType;
import io.jans.orm.annotation.AttributeName;
import io.jans.orm.annotation.DataEntry;
import io.jans.orm.annotation.JsonObject;
import io.jans.orm.annotation.ObjectClass;
import io.jans.orm.model.base.BaseEntry;
import io.jans.util.StringHelper;
import jakarta.persistence.Transient;
import jakarta.validation.constraints.Pattern;
import jakarta.validation.constraints.Size;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
* Custom script configuration
Expand Down Expand Up @@ -151,12 +150,10 @@ public void setDescription(String description) {
}

public String getScript() {

if(script==null && scriptType==CustomScriptType.PERSON_AUTHENTICATION){
script=ScriptTemplate.AUTHEN.getValue();
}
else if(script==null && scriptType!=CustomScriptType.PERSON_AUTHENTICATION){
script=ScriptTemplate.NO_AUTHEN.getValue();
if (script == null) {
script = scriptType == CustomScriptType.PERSON_AUTHENTICATION ?
ScriptTemplate.AUTHEN.getValue() :
ScriptTemplate.NO_AUTHEN.getValue();
}
return script;
}
Expand Down Expand Up @@ -305,7 +302,7 @@ public void removeModuleProperty(final String modulePropertyName) {
}

for (Iterator<SimpleCustomProperty> it = modulePropertiesList.iterator(); it.hasNext();) {
SimpleCustomProperty moduleProperty = (SimpleCustomProperty) it.next();
SimpleCustomProperty moduleProperty = it.next();
if (StringHelper.equalsIgnoreCase(moduleProperty.getValue1(), modulePropertyName)) {
it.remove();
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@

package io.jans.model.custom.script.model;

import java.io.Serializable;
import java.util.Date;

/**
* Custom script error
*
* @author Yuriy Movchan Date: 02/27/2018
*/
public class ScriptError {
public class ScriptError implements Serializable {

private Date raisedAt;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public <T> T loadPythonScript(InputStream scriptFile, String scriptName, String
try {
currentPythonInterpreter.execfile(scriptFile, scriptName);
} catch (Exception ex) {
log.error("Failed to load python file", ex.getMessage(), ex);
log.error("Failed to load python file" + ex.getMessage(), ex);
throw new PythonException(String.format("Failed to load python file '%s'", scriptFile), ex);
}

Expand Down
Loading

0 comments on commit 5da6e27

Please sign in to comment.