Search in sources :

Example 6 with RequirePOST

use of org.kohsuke.stapler.interceptor.RequirePOST in project sonar-scanner-jenkins by SonarSource.

the class SonarQubeWebHook method doIndex.

@RequirePOST
public void doIndex(StaplerRequest req, StaplerResponse rsp) throws IOException {
    String payload = IOUtils.toString(req.getReader());
    LOGGER.info("Received POST from " + req.getRemoteHost());
    try {
        JSONObject o = JSONObject.fromObject(payload);
        LOGGER.fine(() -> "Full details of the POST was " + o.toString());
        String taskId = o.getString("taskId");
        String status = o.getString("status");
        String qgStatus = null;
        if (CETask.STATUS_SUCCESS.equals(status)) {
            qgStatus = o.has("qualityGate") ? o.getJSONObject("qualityGate").getString("status") : "NONE";
        }
        for (Listener listener : listeners) {
            listener.onTaskCompleted(taskId, status, qgStatus);
        }
    } catch (JSONException e) {
        LOGGER.log(Level.WARNING, e, () -> "Invalid payload " + payload);
        rsp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid JSON Payload");
    }
    rsp.setStatus(HttpServletResponse.SC_OK);
}
Also used : JSONObject(net.sf.json.JSONObject) JSONException(net.sf.json.JSONException) RequirePOST(org.kohsuke.stapler.interceptor.RequirePOST)

Example 7 with RequirePOST

use of org.kohsuke.stapler.interceptor.RequirePOST in project configuration-as-code-plugin by jenkinsci.

the class ConfigurationAsCode method doReplace.

@RequirePOST
@Restricted(NoExternalUse.class)
public void doReplace(StaplerRequest request, StaplerResponse response) throws Exception {
    if (!Jenkins.get().hasPermission(Jenkins.ADMINISTER)) {
        response.sendError(HttpServletResponse.SC_FORBIDDEN);
        return;
    }
    String newSource = request.getParameter("_.newSource");
    String normalizedSource = Util.fixEmptyAndTrim(newSource);
    List<String> candidateSources = new ArrayList<>();
    for (String candidateSource : inputToCandidateSources(normalizedSource)) {
        File file = new File(candidateSource);
        if (file.exists() || ConfigurationAsCode.isSupportedURI(candidateSource)) {
            candidateSources.add(candidateSource);
        } else {
            LOGGER.log(Level.WARNING, "Source {0} could not be applied", candidateSource);
        // todo: show message in UI
        }
    }
    if (!candidateSources.isEmpty()) {
        List<YamlSource> candidates = getConfigFromSources(candidateSources);
        if (canApplyFrom(candidates)) {
            sources = candidateSources;
            configureWith(getConfigFromSources(getSources()));
            CasCGlobalConfig config = GlobalConfiguration.all().get(CasCGlobalConfig.class);
            if (config != null) {
                config.setConfigurationPath(normalizedSource);
                config.save();
            }
            LOGGER.log(Level.FINE, "Replace configuration with: " + normalizedSource);
        } else {
            LOGGER.log(Level.WARNING, "Provided sources could not be applied");
        // todo: show message in UI
        }
    } else {
        LOGGER.log(Level.FINE, "No such source exists, applying default");
        // May be do nothing instead?
        configure();
    }
    response.sendRedirect("");
}
Also used : ArrayList(java.util.ArrayList) File(java.io.File) YamlSource(io.jenkins.plugins.casc.yaml.YamlSource) RequirePOST(org.kohsuke.stapler.interceptor.RequirePOST) Restricted(org.kohsuke.accmod.Restricted)

Example 8 with RequirePOST

use of org.kohsuke.stapler.interceptor.RequirePOST in project configuration-as-code-plugin by jenkinsci.

the class TokenReloadAction method doIndex.

@RequirePOST
public void doIndex(StaplerRequest request, StaplerResponse response) throws IOException {
    String token = getReloadToken();
    if (token == null || token.isEmpty()) {
        response.sendError(404);
        LOGGER.warning("Configuration reload via token is not enabled");
    } else {
        String requestToken = getRequestToken(request);
        if (requestToken != null && MessageDigest.isEqual(token.getBytes(StandardCharsets.UTF_8), requestToken.getBytes(StandardCharsets.UTF_8))) {
            LOGGER.info("Configuration reload triggered via token");
            try (ACLContext ignored = ACL.as2(ACL.SYSTEM2)) {
                ConfigurationAsCode.get().configure();
            }
        } else {
            response.sendError(401);
            LOGGER.warning("Invalid token received, not reloading configuration");
        }
    }
}
Also used : ACLContext(hudson.security.ACLContext) RequirePOST(org.kohsuke.stapler.interceptor.RequirePOST)

Example 9 with RequirePOST

use of org.kohsuke.stapler.interceptor.RequirePOST in project workflow-job-plugin by jenkinsci.

the class WorkflowRun method doKill.

/**
 * Immediately kills the build.
 */
@RequirePOST
public void doKill() {
    checkPermission(Item.CANCEL);
    if (!isBuilding() || /* probably redundant, but just to be sure */
    execution == null) {
        return;
    }
    if (listener != null) {
        listener.getLogger().println("Hard kill!");
    }
    // ensures isInProgress returns false
    execution = null;
    FlowInterruptedException suddenDeath = new FlowInterruptedException(Result.ABORTED);
    finish(Result.ABORTED, suddenDeath);
    executionPromise.setException(suddenDeath);
// TODO CpsFlowExecution.onProgramEnd does some cleanup which we cannot access here; perhaps need a FlowExecution.halt(Throwable) API?
}
Also used : FlowInterruptedException(org.jenkinsci.plugins.workflow.steps.FlowInterruptedException) RequirePOST(org.kohsuke.stapler.interceptor.RequirePOST)

Example 10 with RequirePOST

use of org.kohsuke.stapler.interceptor.RequirePOST in project workflow-cps-plugin by jenkinsci.

the class ReplayAction method doRun.

@Restricted(DoNotUse.class)
@RequirePOST
public void doRun(StaplerRequest req, StaplerResponse rsp) throws ServletException, IOException {
    if (!isEnabled() || !(isReplayableSandboxTest())) {
        // AccessDeniedException2 requires us to look up the specific Permission
        throw new AccessDeniedException("not allowed to replay");
    }
    JSONObject form = req.getSubmittedForm();
    // Copy originalLoadedScripts, replacing values with those from the form wherever defined.
    Map<String, String> replacementLoadedScripts = new HashMap<>();
    for (Map.Entry<String, String> entry : getOriginalLoadedScripts().entrySet()) {
        // optString since you might be replaying a running build, which might have loaded a script after the page load but before submission.
        replacementLoadedScripts.put(entry.getKey(), form.optString(entry.getKey().replace('.', '_'), entry.getValue()));
    }
    if (run(form.getString("mainScript"), replacementLoadedScripts) == null) {
        throw HttpResponses.error(SC_CONFLICT, new IOException(run.getParent().getFullName() + " is not buildable"));
    }
    // back to WorkflowJob; new build might not start instantly so cannot redirect to it
    rsp.sendRedirect("../..");
}
Also used : AccessDeniedException(org.acegisecurity.AccessDeniedException) JSONObject(net.sf.json.JSONObject) HashMap(java.util.HashMap) IOException(java.io.IOException) Map(java.util.Map) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) RequirePOST(org.kohsuke.stapler.interceptor.RequirePOST) Restricted(org.kohsuke.accmod.Restricted)

Aggregations

RequirePOST (org.kohsuke.stapler.interceptor.RequirePOST)12 JSONObject (net.sf.json.JSONObject)7 File (java.io.File)4 Restricted (org.kohsuke.accmod.Restricted)4 ACLContext (hudson.security.ACLContext)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 YamlSource (io.jenkins.plugins.casc.yaml.YamlSource)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Component (com.cloudbees.jenkins.support.api.Component)1 ComponentVisitor (com.cloudbees.jenkins.support.api.ComponentVisitor)1 Container (com.cloudbees.jenkins.support.api.Container)1 ObjectComponent (com.cloudbees.jenkins.support.api.ObjectComponent)1 CheckForNull (edu.umd.cs.findbugs.annotations.CheckForNull)1 NonNull (edu.umd.cs.findbugs.annotations.NonNull)1 Extension (hudson.Extension)1 Functions (hudson.Functions)1 PluginManager (hudson.PluginManager)1