Search in sources :

Example 1 with BaseExternalType

use of org.gluu.model.custom.script.type.BaseExternalType in project oxCore by GluuFederation.

the class CustomScriptManager method createExternalTypeFromStringWithPythonException.

public BaseExternalType createExternalTypeFromStringWithPythonException(CustomScript customScript, Map<String, SimpleCustomProperty> configurationAttributes) throws Exception {
    String script = customScript.getScript();
    String scriptName = StringHelper.toLowerCase(customScript.getName()) + ".py";
    if (script == null) {
        return null;
    }
    CustomScriptType customScriptType = customScript.getScriptType();
    BaseExternalType externalType = null;
    InputStream bis = null;
    try {
        bis = new ByteArrayInputStream(script.getBytes("UTF-8"));
        externalType = pythonService.loadPythonScript(bis, scriptName, customScriptType.getPythonClass(), customScriptType.getCustomScriptType(), new PyObject[] { new PyLong(System.currentTimeMillis()) });
    } catch (UnsupportedEncodingException e) {
        log.error(String.format("%s. Script inum: %s", e.getMessage(), customScript.getInum()), e);
    } finally {
        IOUtils.closeQuietly(bis);
    }
    if (externalType == null) {
        return null;
    }
    boolean initialized = false;
    try {
        if (externalType.getApiVersion() > 10) {
            initialized = externalType.init(customScript, configurationAttributes);
        } else {
            initialized = externalType.init(configurationAttributes);
            log.warn(" Update the script's init method to init(self, customScript, configurationAttributes)", customScript.getName());
        }
    } catch (Exception ex) {
        log.error("Failed to initialize custom script: '{}'", ex, customScript.getName());
    }
    if (initialized) {
        return externalType;
    }
    return null;
}
Also used : PyLong(org.python.core.PyLong) ByteArrayInputStream(java.io.ByteArrayInputStream) CustomScriptType(org.gluu.model.custom.script.CustomScriptType) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) BaseExternalType(org.gluu.model.custom.script.type.BaseExternalType) UnsupportedEncodingException(java.io.UnsupportedEncodingException) PyObject(org.python.core.PyObject) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 2 with BaseExternalType

use of org.gluu.model.custom.script.type.BaseExternalType in project oxCore by GluuFederation.

the class CustomScriptManager method executeCustomScriptDestroy.

public boolean executeCustomScriptDestroy(CustomScriptConfiguration customScriptConfiguration) {
    try {
        log.debug("Executing python 'destroy' custom script method");
        BaseExternalType externalType = customScriptConfiguration.getExternalType();
        Map<String, SimpleCustomProperty> configurationAttributes = customScriptConfiguration.getConfigurationAttributes();
        return externalType.destroy(configurationAttributes);
    } catch (Exception ex) {
        log.error(ex.getMessage(), ex);
        saveScriptError(customScriptConfiguration.getCustomScript(), ex);
    }
    return false;
}
Also used : BaseExternalType(org.gluu.model.custom.script.type.BaseExternalType) SimpleCustomProperty(org.gluu.model.SimpleCustomProperty) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 3 with BaseExternalType

use of org.gluu.model.custom.script.type.BaseExternalType in project oxAuth by GluuFederation.

the class ExternalAuthenticationService method getAuthModesByAcrValues.

public List<String> getAuthModesByAcrValues(List<String> acrValues) {
    List<String> authModes = new ArrayList<String>();
    for (String acrValue : acrValues) {
        if (StringHelper.isNotEmpty(acrValue)) {
            String customScriptName = StringHelper.toLowerCase(scriptName(acrValue));
            if (customScriptConfigurationsNameMap.containsKey(customScriptName)) {
                CustomScriptConfiguration customScriptConfiguration = customScriptConfigurationsNameMap.get(customScriptName);
                CustomScript customScript = customScriptConfiguration.getCustomScript();
                // Handle internal authentication method
                if (customScript.isInternal()) {
                    authModes.add(scriptName(acrValue));
                    continue;
                }
                CustomScriptType customScriptType = customScriptConfiguration.getCustomScript().getScriptType();
                BaseExternalType defaultImplementation = customScriptType.getDefaultImplementation();
                BaseExternalType pythonImplementation = customScriptConfiguration.getExternalType();
                if ((pythonImplementation != null) && (defaultImplementation != pythonImplementation)) {
                    authModes.add(scriptName(acrValue));
                }
            }
        }
    }
    return authModes;
}
Also used : CustomScript(org.gluu.model.custom.script.model.CustomScript) AuthenticationCustomScript(org.gluu.model.custom.script.model.auth.AuthenticationCustomScript) CustomScriptType(org.gluu.model.custom.script.CustomScriptType) BaseExternalType(org.gluu.model.custom.script.type.BaseExternalType) ArrayList(java.util.ArrayList) CustomScriptConfiguration(org.gluu.model.custom.script.conf.CustomScriptConfiguration)

Example 4 with BaseExternalType

use of org.gluu.model.custom.script.type.BaseExternalType in project oxCore by GluuFederation.

the class CustomScriptManager method createExternalType.

private BaseExternalType createExternalType(CustomScript customScript, Map<String, SimpleCustomProperty> configurationAttributes) {
    String customScriptInum = customScript.getInum();
    BaseExternalType externalType;
    try {
        externalType = createExternalTypeFromStringWithPythonException(customScript, configurationAttributes);
    } catch (Exception ex) {
        log.error("Failed to prepare external type '{}'", ex, customScriptInum);
        saveScriptError(customScript, ex, true);
        return null;
    }
    if (externalType == null) {
        log.debug("Using default external type class");
        saveScriptError(customScript, new Exception("Using default external type class"), true);
        externalType = customScript.getScriptType().getDefaultImplementation();
    } else {
        clearScriptError(customScript);
    }
    return externalType;
}
Also used : BaseExternalType(org.gluu.model.custom.script.type.BaseExternalType) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 5 with BaseExternalType

use of org.gluu.model.custom.script.type.BaseExternalType in project oxCore by GluuFederation.

the class CustomScriptManager method reloadCustomScriptConfigurations.

private ReloadResult reloadCustomScriptConfigurations(Map<String, CustomScriptConfiguration> customScriptConfigurations, List<CustomScript> newCustomScripts) {
    Map<String, CustomScriptConfiguration> newCustomScriptConfigurations;
    boolean modified = false;
    if (customScriptConfigurations == null) {
        newCustomScriptConfigurations = new HashMap<String, CustomScriptConfiguration>();
        modified = true;
    } else {
        // Clone old map to avoid reload not changed scripts because it's time and CPU
        // consuming process
        newCustomScriptConfigurations = new HashMap<String, CustomScriptConfiguration>(customScriptConfigurations);
    }
    List<String> newSupportedCustomScriptInums = new ArrayList<String>();
    for (CustomScript newCustomScript : newCustomScripts) {
        if (!newCustomScript.isEnabled()) {
            continue;
        }
        if (ScriptLocationType.FILE == newCustomScript.getLocationType()) {
            // Replace script revision with file modification time. This should allow to
            // reload script automatically after changing location_type
            long fileModifiactionTime = getFileModificationTime(newCustomScript.getLocationPath());
            newCustomScript.setRevision(fileModifiactionTime);
        }
        String newSupportedCustomScriptInum = StringHelper.toLowerCase(newCustomScript.getInum());
        newSupportedCustomScriptInums.add(newSupportedCustomScriptInum);
        CustomScriptConfiguration prevCustomScriptConfiguration = newCustomScriptConfigurations.get(newSupportedCustomScriptInum);
        if (prevCustomScriptConfiguration == null || prevCustomScriptConfiguration.getCustomScript().getRevision() != newCustomScript.getRevision()) {
            // Destroy old version properly before creating new one
            if (prevCustomScriptConfiguration != null) {
                destroyCustomScript(prevCustomScriptConfiguration);
            }
            // Load script entry with all attributes
            CustomScript loadedCustomScript = customScriptService.getCustomScriptByDn(newCustomScript.getScriptType().getCustomScriptModel(), newCustomScript.getDn());
            // Prepare configuration attributes
            Map<String, SimpleCustomProperty> newConfigurationAttributes = new HashMap<String, SimpleCustomProperty>();
            List<SimpleExtendedCustomProperty> simpleCustomProperties = loadedCustomScript.getConfigurationProperties();
            if (simpleCustomProperties == null) {
                simpleCustomProperties = new ArrayList<SimpleExtendedCustomProperty>(0);
            }
            for (SimpleCustomProperty simpleCustomProperty : simpleCustomProperties) {
                newConfigurationAttributes.put(simpleCustomProperty.getValue1(), simpleCustomProperty);
            }
            if (ScriptLocationType.FILE == loadedCustomScript.getLocationType()) {
                // Replace script revision with file modification time. This should allow to
                // reload script automatically after changing location_type
                long fileModifiactionTime = getFileModificationTime(loadedCustomScript.getLocationPath());
                loadedCustomScript.setRevision(fileModifiactionTime);
                if (fileModifiactionTime != 0) {
                    String scriptFromFile = loadFromFile(loadedCustomScript.getLocationPath());
                    if (StringHelper.isNotEmpty(scriptFromFile)) {
                        loadedCustomScript.setScript(scriptFromFile);
                    }
                }
            }
            // Automatic package update '.xdi' --> '.org'
            // TODO: Remove in CE 5.0
            String scriptCode = loadedCustomScript.getScript();
            if (scriptCode != null) {
                scriptCode = scriptCode.replaceAll(".xdi", ".gluu");
                loadedCustomScript.setScript(scriptCode);
            }
            // Load script
            BaseExternalType newCustomScriptExternalType = createExternalType(loadedCustomScript, newConfigurationAttributes);
            CustomScriptConfiguration newCustomScriptConfiguration = new CustomScriptConfiguration(loadedCustomScript, newCustomScriptExternalType, newConfigurationAttributes);
            // Store configuration and script
            newCustomScriptConfigurations.put(newSupportedCustomScriptInum, newCustomScriptConfiguration);
            modified = true;
        }
    }
    // Remove old external scripts configurations
    for (Iterator<Entry<String, CustomScriptConfiguration>> it = newCustomScriptConfigurations.entrySet().iterator(); it.hasNext(); ) {
        Entry<String, CustomScriptConfiguration> externalAuthenticatorConfigurationEntry = it.next();
        String prevSupportedCustomScriptInum = externalAuthenticatorConfigurationEntry.getKey();
        if (!newSupportedCustomScriptInums.contains(prevSupportedCustomScriptInum)) {
            // Destroy old authentication method
            destroyCustomScript(externalAuthenticatorConfigurationEntry.getValue());
            it.remove();
            modified = true;
        }
    }
    return new ReloadResult(newCustomScriptConfigurations, modified);
}
Also used : CustomScript(org.gluu.model.custom.script.model.CustomScript) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SimpleExtendedCustomProperty(org.gluu.model.SimpleExtendedCustomProperty) Entry(java.util.Map.Entry) BaseExternalType(org.gluu.model.custom.script.type.BaseExternalType) SimpleCustomProperty(org.gluu.model.SimpleCustomProperty) CustomScriptConfiguration(org.gluu.model.custom.script.conf.CustomScriptConfiguration)

Aggregations

BaseExternalType (org.gluu.model.custom.script.type.BaseExternalType)6 IOException (java.io.IOException)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 ArrayList (java.util.ArrayList)2 SimpleCustomProperty (org.gluu.model.SimpleCustomProperty)2 CustomScriptType (org.gluu.model.custom.script.CustomScriptType)2 CustomScriptConfiguration (org.gluu.model.custom.script.conf.CustomScriptConfiguration)2 CustomScript (org.gluu.model.custom.script.model.CustomScript)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1 Entry (java.util.Map.Entry)1 SimpleExtendedCustomProperty (org.gluu.model.SimpleExtendedCustomProperty)1 AuthenticationCustomScript (org.gluu.model.custom.script.model.auth.AuthenticationCustomScript)1 PyLong (org.python.core.PyLong)1 PyObject (org.python.core.PyObject)1