Search in sources :

Example 16 with JSONObject

use of net.sf.json.JSONObject in project blueocean-plugin by jenkinsci.

the class JenkinsJSExtensionsTest method test.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void test() {
    // Simple test of JenkinsJSExtensions. It should find the "blueocean-dashboard"
    // and "blueocean-personalization" plugin ExtensionPoint contributions.
    JSONArray extensionData = JenkinsJSExtensions.getExtensionsData();
    Assert.assertTrue(extensionData.size() > 2);
    Iterator pluginIterator = extensionData.iterator();
    while (pluginIterator.hasNext()) {
        JSONObject plugin = (JSONObject) pluginIterator.next();
        JSONArray extensionPoints = (JSONArray) plugin.get("extensions");
        String pluginId = plugin.getString("hpiPluginId");
        Assert.assertNotNull(plugin.get("hpiPluginVer"));
        Assert.assertNotNull(extensionPoints);
        if ("blueocean-dashboard".equals(pluginId)) {
            Assert.assertEquals(7, extensionPoints.size());
            Assert.assertEquals("AdminNavLink", extensionPoints.getJSONObject(0).get("component"));
            Assert.assertEquals("jenkins.logo.top", extensionPoints.getJSONObject(0).get("extensionPoint"));
        } else if ("blueocean-personalization".equals(pluginId)) {
            Assert.assertEquals(5, extensionPoints.size());
            Assert.assertEquals("redux/FavoritesStore", extensionPoints.getJSONObject(0).get("component"));
            Assert.assertEquals("jenkins.main.stores", extensionPoints.getJSONObject(0).get("extensionPoint"));
            Assert.assertEquals("components/DashboardCards", extensionPoints.getJSONObject(1).get("component"));
            Assert.assertEquals("jenkins.pipeline.list.top", extensionPoints.getJSONObject(1).get("extensionPoint"));
            Assert.assertEquals("components/FavoritePipeline", extensionPoints.getJSONObject(2).get("component"));
            Assert.assertEquals("jenkins.pipeline.list.action", extensionPoints.getJSONObject(2).get("extensionPoint"));
            Assert.assertEquals("components/FavoritePipelineHeader", extensionPoints.getJSONObject(3).get("component"));
            Assert.assertEquals("jenkins.pipeline.detail.header.action", extensionPoints.getJSONObject(3).get("extensionPoint"));
            Assert.assertEquals("components/FavoritePipeline", extensionPoints.getJSONObject(4).get("component"));
            Assert.assertEquals("jenkins.pipeline.branches.list.action", extensionPoints.getJSONObject(4).get("extensionPoint"));
        }
    }
    // Calling JenkinsJSExtensions.getJenkinsJSExtensionData() multiple times should
    // result in the same object instance being returned because the list of plugin
    // has not changed i.e. we have a simple optimization in there where we only scan
    // the classpath if the active plugin lust has changed.
    Assert.assertEquals(JenkinsJSExtensions.getJenkinsJSExtensionData(), JenkinsJSExtensions.getJenkinsJSExtensionData());
}
Also used : JSONObject(net.sf.json.JSONObject) JSONArray(net.sf.json.JSONArray) Iterator(java.util.Iterator) Test(org.junit.Test) BaseTest(io.jenkins.blueocean.service.embedded.BaseTest)

Example 17 with JSONObject

use of net.sf.json.JSONObject in project zaproxy by zaproxy.

the class AuthenticationAPI method handleApiAction.

@Override
public ApiResponse handleApiAction(String name, JSONObject params) throws ApiException {
    log.debug("handleApiAction " + name + " " + params.toString());
    Context context;
    switch(name) {
        case ACTION_SET_LOGGED_IN_INDICATOR:
            String loggedInIndicator = params.getString(PARAM_LOGGED_IN_INDICATOR);
            if (loggedInIndicator == null || loggedInIndicator.isEmpty())
                throw new ApiException(Type.MISSING_PARAMETER, PARAM_LOGGED_IN_INDICATOR);
            context = getContext(params);
            context.getAuthenticationMethod().setLoggedInIndicatorPattern(loggedInIndicator);
            context.save();
            return ApiResponseElement.OK;
        case ACTION_SET_LOGGED_OUT_INDICATOR:
            String loggedOutIndicator = params.getString(PARAM_LOGGED_OUT_INDICATOR);
            if (loggedOutIndicator == null || loggedOutIndicator.isEmpty())
                throw new ApiException(Type.MISSING_PARAMETER, PARAM_LOGGED_OUT_INDICATOR);
            context = getContext(params);
            context.getAuthenticationMethod().setLoggedOutIndicatorPattern(loggedOutIndicator);
            context.save();
            return ApiResponseElement.OK;
        case ACTION_SET_METHOD:
            // Prepare the params
            JSONObject actionParams;
            if (params.has(PARAM_METHOD_CONFIG_PARAMS))
                actionParams = API.getParams(params.getString(PARAM_METHOD_CONFIG_PARAMS));
            else
                actionParams = new JSONObject();
            context = getContext(params);
            actionParams.put(PARAM_CONTEXT_ID, context.getIndex());
            // Run the method
            getSetMethodActionImplementor(params).handleAction(actionParams);
            context.save();
            return ApiResponseElement.OK;
        default:
            throw new ApiException(Type.BAD_ACTION);
    }
}
Also used : Context(org.zaproxy.zap.model.Context) JSONObject(net.sf.json.JSONObject) ApiException(org.zaproxy.zap.extension.api.ApiException)

Example 18 with JSONObject

use of net.sf.json.JSONObject in project zaproxy by zaproxy.

the class FormBasedAuthenticationMethodType method getSetMethodForContextApiAction.

@Override
public ApiDynamicActionImplementor getSetMethodForContextApiAction() {
    return new ApiDynamicActionImplementor(API_METHOD_NAME, new String[] { PARAM_LOGIN_URL }, new String[] { PARAM_LOGIN_REQUEST_DATA }) {

        @Override
        public void handleAction(JSONObject params) throws ApiException {
            Context context = ApiUtils.getContextByParamId(params, AuthenticationAPI.PARAM_CONTEXT_ID);
            String loginUrl = ApiUtils.getNonEmptyStringParam(params, PARAM_LOGIN_URL);
            try {
                new URL(loginUrl);
            } catch (Exception ex) {
                throw new ApiException(ApiException.Type.ILLEGAL_PARAMETER, PARAM_LOGIN_URL);
            }
            String postData = "";
            if (params.containsKey(PARAM_LOGIN_REQUEST_DATA)) {
                postData = params.getString(PARAM_LOGIN_REQUEST_DATA);
            }
            // Set the method
            FormBasedAuthenticationMethod method = createAuthenticationMethod(context.getIndex());
            try {
                method.setLoginRequest(loginUrl, postData);
            } catch (Exception e) {
                throw new ApiException(ApiException.Type.INTERNAL_ERROR, e.getMessage());
            }
            if (!context.getAuthenticationMethod().isSameType(method))
                apiChangedAuthenticationMethodForContext(context.getIndex());
            context.setAuthenticationMethod(method);
        }
    };
}
Also used : ApiDynamicActionImplementor(org.zaproxy.zap.extension.api.ApiDynamicActionImplementor) Context(org.zaproxy.zap.model.Context) PopupMenuItemContext(org.zaproxy.zap.view.popup.PopupMenuItemContext) RecordContext(org.parosproxy.paros.db.RecordContext) JSONObject(net.sf.json.JSONObject) URL(java.net.URL) URIException(org.apache.commons.httpclient.URIException) HttpMalformedHeaderException(org.parosproxy.paros.network.HttpMalformedHeaderException) ApiException(org.zaproxy.zap.extension.api.ApiException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ConfigurationException(org.apache.commons.configuration.ConfigurationException) IOException(java.io.IOException) DatabaseException(org.parosproxy.paros.db.DatabaseException) ApiException(org.zaproxy.zap.extension.api.ApiException)

Example 19 with JSONObject

use of net.sf.json.JSONObject in project zaproxy by zaproxy.

the class HttpAuthenticationMethodType method getSetMethodForContextApiAction.

@Override
public ApiDynamicActionImplementor getSetMethodForContextApiAction() {
    return new ApiDynamicActionImplementor(API_METHOD_NAME, new String[] { PARAM_HOSTNAME, PARAM_REALM }, new String[] { PARAM_PORT }) {

        @Override
        public void handleAction(JSONObject params) throws ApiException {
            Context context = ApiUtils.getContextByParamId(params, AuthenticationAPI.PARAM_CONTEXT_ID);
            HttpAuthenticationMethod method = createAuthenticationMethod(context.getIndex());
            method.hostname = ApiUtils.getNonEmptyStringParam(params, PARAM_HOSTNAME);
            try {
                new URI(method.hostname);
            } catch (Exception ex) {
                throw new ApiException(ApiException.Type.ILLEGAL_PARAMETER, PARAM_HOSTNAME);
            }
            if (params.containsKey(PARAM_REALM))
                method.realm = params.getString(PARAM_REALM);
            if (params.containsKey(PARAM_PORT))
                try {
                    String portString = params.getString(PARAM_PORT);
                    method.port = Integer.parseInt(portString);
                } catch (Exception ex) {
                    throw new ApiException(ApiException.Type.ILLEGAL_PARAMETER, PARAM_PORT);
                }
            if (!context.getAuthenticationMethod().isSameType(method))
                apiChangedAuthenticationMethodForContext(context.getIndex());
            context.setAuthenticationMethod(method);
        }
    };
}
Also used : ApiDynamicActionImplementor(org.zaproxy.zap.extension.api.ApiDynamicActionImplementor) Context(org.zaproxy.zap.model.Context) RecordContext(org.parosproxy.paros.db.RecordContext) JSONObject(net.sf.json.JSONObject) URI(java.net.URI) ApiException(org.zaproxy.zap.extension.api.ApiException) DatabaseException(org.parosproxy.paros.db.DatabaseException) UnknownHostException(java.net.UnknownHostException) ConfigurationException(org.apache.commons.configuration.ConfigurationException) ApiException(org.zaproxy.zap.extension.api.ApiException)

Example 20 with JSONObject

use of net.sf.json.JSONObject in project zaproxy by zaproxy.

the class ScriptBasedAuthenticationMethodType method getSetMethodForContextApiAction.

@Override
public ApiDynamicActionImplementor getSetMethodForContextApiAction() {
    return new ApiDynamicActionImplementor(API_METHOD_NAME, new String[] { PARAM_SCRIPT_NAME }, new String[] { PARAM_SCRIPT_CONFIG_PARAMS }) {

        @Override
        public void handleAction(JSONObject params) throws ApiException {
            Context context = ApiUtils.getContextByParamId(params, AuthenticationAPI.PARAM_CONTEXT_ID);
            String scriptName = ApiUtils.getNonEmptyStringParam(params, PARAM_SCRIPT_NAME);
            // Prepare the method
            ScriptBasedAuthenticationMethod method = createAuthenticationMethod(context.getIndex());
            // Load the script and make sure it exists and follows the required interface
            ScriptWrapper script = getScriptsExtension().getScript(scriptName);
            if (script == null) {
                log.error("Unable to find script while loading Script Based Authentication Method for name: " + scriptName);
                throw new ApiException(ApiException.Type.SCRIPT_NOT_FOUND, scriptName);
            } else
                log.info("Loaded script for API:" + script.getName());
            method.script = script;
            // Check script interface and make sure we load the credentials parameter names
            AuthenticationScript s = getScriptInterfaceV2(script);
            if (s == null) {
                s = getScriptInterface(script);
            }
            if (s == null) {
                log.error("Unable to load Script Based Authentication method. The script " + script.getName() + " does not properly implement the Authentication Script interface.");
                throw new ApiException(ApiException.Type.BAD_SCRIPT_FORMAT, "Does not follow Authentication script interface");
            }
            try {
                if (s instanceof AuthenticationScriptV2) {
                    AuthenticationScriptV2 sV2 = (AuthenticationScriptV2) s;
                    method.setLoggedInIndicatorPattern(sV2.getLoggedInIndicator());
                    method.setLoggedOutIndicatorPattern(sV2.getLoggedOutIndicator());
                }
                method.credentialsParamNames = s.getCredentialsParamsNames();
                // Load config param names + values and make sure all of the required ones
                // are there
                String[] requiredParams = s.getRequiredParamsNames();
                String[] optionalParams = s.getOptionalParamsNames();
                if (log.isDebugEnabled()) {
                    log.debug("Loaded authentication script - required parameters: " + Arrays.toString(requiredParams) + " - optional parameters: " + Arrays.toString(optionalParams));
                }
                Map<String, String> paramValues = new HashMap<String, String>();
                for (String rp : requiredParams) {
                    // If one of the required parameters is not present, it will throw
                    // an exception
                    String val = ApiUtils.getNonEmptyStringParam(params, rp);
                    paramValues.put(rp, val);
                }
                for (String op : optionalParams) paramValues.put(op, ApiUtils.getOptionalStringParam(params, op));
                method.paramValues = paramValues;
                if (log.isDebugEnabled())
                    log.debug("Loaded authentication script parameters:" + paramValues);
            } catch (ApiException e) {
                throw e;
            } catch (Exception e) {
                getScriptsExtension().handleScriptException(script, e);
                log.error("Unable to load Script Based Authentication method. The script " + script.getName() + " contains errors.");
                throw new ApiException(ApiException.Type.BAD_SCRIPT_FORMAT, e.getMessage());
            }
            // accordingly
            if (!context.getAuthenticationMethod().isSameType(method))
                apiChangedAuthenticationMethodForContext(context.getIndex());
            context.setAuthenticationMethod(method);
        }
    };
}
Also used : ApiDynamicActionImplementor(org.zaproxy.zap.extension.api.ApiDynamicActionImplementor) Context(org.zaproxy.zap.model.Context) RecordContext(org.parosproxy.paros.db.RecordContext) HashMap(java.util.HashMap) ScriptException(javax.script.ScriptException) ApiException(org.zaproxy.zap.extension.api.ApiException) ConfigurationException(org.apache.commons.configuration.ConfigurationException) DatabaseException(org.parosproxy.paros.db.DatabaseException) JSONObject(net.sf.json.JSONObject) ScriptWrapper(org.zaproxy.zap.extension.script.ScriptWrapper) ApiException(org.zaproxy.zap.extension.api.ApiException)

Aggregations

JSONObject (net.sf.json.JSONObject)493 Test (org.junit.Test)99 JSONArray (net.sf.json.JSONArray)94 IOException (java.io.IOException)49 HashMap (java.util.HashMap)48 ArrayList (java.util.ArrayList)36 JSON (net.sf.json.JSON)26 PrintWriter (java.io.PrintWriter)25 Map (java.util.Map)23 File (java.io.File)21 InputStream (java.io.InputStream)18 URISyntaxException (java.net.URISyntaxException)15 UnsupportedEncodingException (java.io.UnsupportedEncodingException)14 JsonConfig (net.sf.json.JsonConfig)14 FreeStyleBuild (hudson.model.FreeStyleBuild)13 URI (java.net.URI)13 URL (java.net.URL)13 JSONException (net.sf.json.JSONException)13 Context (org.zaproxy.zap.model.Context)12 Transactional (org.springframework.transaction.annotation.Transactional)11