Search in sources :

Example 1 with EnvVars

use of hudson.EnvVars in project blueocean-plugin by jenkinsci.

the class GitCloneReadSaveRequest method cloneRepo.

GitClient cloneRepo() throws InterruptedException, IOException {
    EnvVars environment = new EnvVars();
    TaskListener taskListener = new LogTaskListener(Logger.getAnonymousLogger(), Level.ALL);
    String gitExe = gitTool.getGitExe();
    GitClient git = Git.with(taskListener, environment).in(repositoryPath).using(gitExe).getClient();
    git.addCredentials(gitSource.getRemote(), getCredential());
    try {
        git.clone(gitSource.getRemote(), "origin", true, null);
        log.fine("Repository " + gitSource.getRemote() + " cloned to: " + repositoryPath.getCanonicalPath());
    } catch (GitException e) {
        // check if this is an empty repository
        boolean isEmptyRepo = false;
        try {
            if (git.getRemoteReferences(gitSource.getRemote(), null, true, false).isEmpty()) {
                isEmptyRepo = true;
            }
        } catch (GitException ge) {
            // *sigh* @ this necessary hack; {@link org.jenkinsci.plugins.gitclient.CliGitAPIImpl#getRemoteReferences}
            if ("unexpected ls-remote output ".equals(ge.getMessage())) {
                // blank line, command succeeded
                isEmptyRepo = true;
            }
        // ignore other reasons
        }
        if (isEmptyRepo) {
            git.init();
            git.addRemoteUrl("origin", gitSource.getRemote());
            log.fine("Repository " + gitSource.getRemote() + " not found, created new to: " + repositoryPath.getCanonicalPath());
        } else {
            throw e;
        }
    }
    return git;
}
Also used : EnvVars(hudson.EnvVars) GitException(hudson.plugins.git.GitException) TaskListener(hudson.model.TaskListener) LogTaskListener(hudson.util.LogTaskListener) LogTaskListener(hudson.util.LogTaskListener) GitClient(org.jenkinsci.plugins.gitclient.GitClient)

Example 2 with EnvVars

use of hudson.EnvVars in project workflow-cps-plugin by jenkinsci.

the class DSL method invokeStep.

/**
 * When {@link #invokeMethod(String, Object)} is calling a {@link StepDescriptor}
 */
protected Object invokeStep(StepDescriptor d, Object args) {
    final NamedArgsAndClosure ps = parseArgs(args, d);
    CpsThread thread = CpsThread.current();
    FlowNode an;
    // TODO: generalize the notion of Step taking over the FlowNode creation.
    boolean hack = d instanceof ParallelStep.DescriptorImpl || d instanceof LoadStep.DescriptorImpl;
    if (ps.body == null && !hack) {
        an = new StepAtomNode(exec, d, thread.head.get());
        // TODO: use CPS call stack to obtain the current call site source location. See JENKINS-23013
        thread.head.setNewHead(an);
    } else {
        an = new StepStartNode(exec, d, thread.head.get());
        thread.head.setNewHead(an);
    }
    final CpsStepContext context = new CpsStepContext(d, thread, handle, an, ps.body);
    Step s;
    boolean sync;
    ClassLoader originalLoader = Thread.currentThread().getContextClassLoader();
    try {
        d.checkContextAvailability(context);
        Thread.currentThread().setContextClassLoader(CpsVmExecutorService.ORIGINAL_CONTEXT_CLASS_LOADER.get());
        s = d.newInstance(ps.namedArgs);
        try {
            // No point storing empty arguments, and ParallelStep is a special case where we can't store its closure arguments
            if (ps.namedArgs != null && !(ps.namedArgs.isEmpty()) && isKeepStepArguments() && !(s instanceof ParallelStep)) {
                // Get the environment variables to find ones that might be credentials bindings
                Computer comp = context.get(Computer.class);
                EnvVars allEnv = new EnvVars(context.get(EnvVars.class));
                if (comp != null && allEnv != null) {
                    allEnv.entrySet().removeAll(comp.getEnvironment().entrySet());
                }
                an.addAction(new ArgumentsActionImpl(ps.namedArgs, allEnv));
            }
        } catch (Exception e) {
            // Avoid breaking execution because we can't store some sort of crazy Step argument
            LOGGER.log(Level.WARNING, "Error storing the arguments for step: " + d.getFunctionName(), e);
        }
        // Persist the node - block start and end nodes do their own persistence.
        CpsFlowExecution.maybeAutoPersistNode(an);
        StepExecution e = s.start(context);
        thread.setStep(e);
        sync = e.start();
    } catch (Exception e) {
        if (e instanceof MissingContextVariableException)
            reportMissingContextVariableException(context, (MissingContextVariableException) e);
        context.onFailure(e);
        s = null;
        sync = true;
    } finally {
        Thread.currentThread().setContextClassLoader(originalLoader);
    }
    if (sync) {
        assert context.bodyInvokers.isEmpty() : "If a step claims synchronous completion, it shouldn't invoke body";
        if (context.getOutcome() == null) {
            context.onFailure(new AssertionError("Step " + s + " claimed to have ended synchronously, but didn't set the result via StepContext.onSuccess/onFailure"));
        }
        thread.setStep(null);
        // we just move on accordingly
        if (an instanceof StepStartNode) {
            // no body invoked, so EndNode follows StartNode immediately.
            thread.head.setNewHead(new StepEndNode(exec, (StepStartNode) an, an));
        }
        thread.head.markIfFail(context.getOutcome());
        return context.replay();
    } else {
        // if it's in progress, suspend it until we get invoked later.
        // when it resumes, the CPS caller behaves as if this method returned with the resume value
        Continuable.suspend(new ThreadTaskImpl(context));
        // so the execution will never reach here.
        throw new AssertionError();
    }
}
Also used : StepStartNode(org.jenkinsci.plugins.workflow.cps.nodes.StepStartNode) LoadStep(org.jenkinsci.plugins.workflow.cps.steps.LoadStep) Step(org.jenkinsci.plugins.workflow.steps.Step) LoadStep(org.jenkinsci.plugins.workflow.cps.steps.LoadStep) ParallelStep(org.jenkinsci.plugins.workflow.cps.steps.ParallelStep) StepEndNode(org.jenkinsci.plugins.workflow.cps.nodes.StepEndNode) StepAtomNode(org.jenkinsci.plugins.workflow.cps.nodes.StepAtomNode) StepExecution(org.jenkinsci.plugins.workflow.steps.StepExecution) NoStaplerConstructorException(org.kohsuke.stapler.NoStaplerConstructorException) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) IOException(java.io.IOException) MissingContextVariableException(org.jenkinsci.plugins.workflow.steps.MissingContextVariableException) ParallelStep(org.jenkinsci.plugins.workflow.cps.steps.ParallelStep) EnvVars(hudson.EnvVars) ArgumentsActionImpl(org.jenkinsci.plugins.workflow.cps.actions.ArgumentsActionImpl) Computer(hudson.model.Computer) MissingContextVariableException(org.jenkinsci.plugins.workflow.steps.MissingContextVariableException) FlowNode(org.jenkinsci.plugins.workflow.graph.FlowNode)

Example 3 with EnvVars

use of hudson.EnvVars in project workflow-cps-plugin by jenkinsci.

the class EnvActionImpl method getEnvironment.

@Override
public EnvVars getEnvironment() throws IOException, InterruptedException {
    TaskListener listener;
    if (owner instanceof FlowExecutionOwner.Executable) {
        FlowExecutionOwner executionOwner = ((FlowExecutionOwner.Executable) owner).asFlowExecutionOwner();
        if (executionOwner != null) {
            listener = executionOwner.getListener();
        } else {
            listener = new LogTaskListener(LOGGER, Level.INFO);
        }
    } else {
        listener = new LogTaskListener(LOGGER, Level.INFO);
    }
    EnvVars e = owner.getEnvironment(listener);
    e.putAll(env);
    return e;
}
Also used : EnvVars(hudson.EnvVars) FlowExecutionOwner(org.jenkinsci.plugins.workflow.flow.FlowExecutionOwner) LogTaskListener(hudson.util.LogTaskListener) TaskListener(hudson.model.TaskListener) LogTaskListener(hudson.util.LogTaskListener)

Example 4 with EnvVars

use of hudson.EnvVars in project workflow-cps-plugin by jenkinsci.

the class ArgumentsActionImplTest method testBasicCreateAndMask.

@Test
public void testBasicCreateAndMask() throws Exception {
    HashMap<String, String> passwordBinding = new HashMap<String, String>();
    passwordBinding.put("mypass", "p4ssw0rd");
    Map<String, Object> arguments = new HashMap<String, Object>();
    arguments.put("message", "I have a secret p4ssw0rd");
    Field maxSizeF = ArgumentsAction.class.getDeclaredField("MAX_RETAINED_LENGTH");
    maxSizeF.setAccessible(true);
    int maxSize = maxSizeF.getInt(null);
    // Same string, unsanitized
    ArgumentsActionImpl argumentsActionImpl = new ArgumentsActionImpl(arguments, new EnvVars());
    Assert.assertEquals(true, argumentsActionImpl.isUnmodifiedArguments());
    Assert.assertEquals(arguments.get("message"), argumentsActionImpl.getArgumentValueOrReason("message"));
    Assert.assertEquals(1, argumentsActionImpl.getArguments().size());
    Assert.assertEquals("I have a secret p4ssw0rd", argumentsActionImpl.getArguments().get("message"));
    // Test sanitizing arguments now
    argumentsActionImpl = new ArgumentsActionImpl(arguments, new EnvVars(passwordBinding));
    Assert.assertEquals(false, argumentsActionImpl.isUnmodifiedArguments());
    Assert.assertEquals(ArgumentsActionImpl.NotStoredReason.MASKED_VALUE, argumentsActionImpl.getArgumentValueOrReason("message"));
    Assert.assertEquals(1, argumentsActionImpl.getArguments().size());
    Assert.assertEquals(ArgumentsAction.NotStoredReason.MASKED_VALUE, argumentsActionImpl.getArguments().get("message"));
    // Mask oversized values
    arguments.clear();
    arguments.put("text", RandomStringUtils.random(maxSize + 1));
    argumentsActionImpl = new ArgumentsActionImpl(arguments);
    Assert.assertEquals(ArgumentsAction.NotStoredReason.OVERSIZE_VALUE, argumentsActionImpl.getArgumentValueOrReason("text"));
}
Also used : Field(java.lang.reflect.Field) EnvVars(hudson.EnvVars) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 5 with EnvVars

use of hudson.EnvVars in project workflow-cps-plugin by jenkinsci.

the class ArgumentsActionImplTest method testRecursiveSanitizationOfContent.

@Test
public void testRecursiveSanitizationOfContent() {
    int maxLen = ArgumentsActionImpl.getMaxRetainedLength();
    ArgumentsActionImpl impl = new ArgumentsActionImpl();
    EnvVars env = new EnvVars();
    String secretUsername = "secretuser";
    // assume secretuser is a bound credential
    env.put("USERVARIABLE", secretUsername);
    char[] oversized = new char[maxLen + 10];
    Arrays.fill(oversized, 'a');
    String oversizedString = new String(oversized);
    // Simplest masking of secret and oversized value
    Assert.assertEquals(ArgumentsAction.NotStoredReason.MASKED_VALUE, impl.sanitizeObjectAndRecordMutation(secretUsername, env));
    Assert.assertFalse(impl.isUnmodifiedArguments());
    impl.isUnmodifiedBySanitization = true;
    Assert.assertEquals(ArgumentsAction.NotStoredReason.OVERSIZE_VALUE, impl.sanitizeObjectAndRecordMutation(oversizedString, env));
    Assert.assertFalse(impl.isUnmodifiedArguments());
    impl.isUnmodifiedBySanitization = true;
    // Test explosion of Step & UninstantiatedDescribable objects
    Step mystep = new EchoStep("I have a " + secretUsername);
    Map<String, ?> singleSanitization = (Map<String, Object>) (impl.sanitizeObjectAndRecordMutation(mystep, env));
    Assert.assertEquals(1, singleSanitization.size());
    Assert.assertEquals(ArgumentsAction.NotStoredReason.MASKED_VALUE, singleSanitization.get("message"));
    Assert.assertFalse(impl.isUnmodifiedArguments());
    impl.isUnmodifiedBySanitization = true;
    singleSanitization = ((UninstantiatedDescribable) (impl.sanitizeObjectAndRecordMutation(mystep.getDescriptor().uninstantiate(mystep), env))).getArguments();
    Assert.assertEquals(1, singleSanitization.size());
    Assert.assertEquals(ArgumentsAction.NotStoredReason.MASKED_VALUE, singleSanitization.get("message"));
    Assert.assertFalse(impl.isUnmodifiedArguments());
    impl.isUnmodifiedBySanitization = true;
    // Maps
    HashMap<String, Object> dangerous = new HashMap<>();
    dangerous.put("name", secretUsername);
    Map<String, Object> sanitizedMap = impl.sanitizeMapAndRecordMutation(dangerous, env);
    Assert.assertNotEquals(sanitizedMap, dangerous);
    Assert.assertEquals(ArgumentsAction.NotStoredReason.MASKED_VALUE, sanitizedMap.get("name"));
    Assert.assertFalse(impl.isUnmodifiedArguments());
    impl.isUnmodifiedBySanitization = true;
    // String is no longer dangerous
    Map<String, Object> identicalMap = impl.sanitizeMapAndRecordMutation(dangerous, new EnvVars());
    Assert.assertEquals(identicalMap, dangerous);
    Assert.assertTrue(impl.isUnmodifiedArguments());
    // Lists
    List unsanitizedList = Arrays.asList("cheese", null, secretUsername);
    List sanitized = (List) impl.sanitizeListAndRecordMutation(unsanitizedList, env);
    Assert.assertEquals(3, sanitized.size());
    Assert.assertFalse(impl.isUnmodifiedArguments());
    Assert.assertEquals(ArgumentsAction.NotStoredReason.MASKED_VALUE, sanitized.get(2));
    impl.isUnmodifiedBySanitization = true;
    Assert.assertEquals(unsanitizedList, impl.sanitizeObjectAndRecordMutation(unsanitizedList, new EnvVars()));
    Assert.assertEquals(unsanitizedList, impl.sanitizeListAndRecordMutation(unsanitizedList, new EnvVars()));
}
Also used : EchoStep(org.jenkinsci.plugins.workflow.steps.EchoStep) HashMap(java.util.HashMap) Step(org.jenkinsci.plugins.workflow.steps.Step) StateMetaStep(org.jenkinsci.plugins.workflow.testMetaStep.StateMetaStep) EchoStep(org.jenkinsci.plugins.workflow.steps.EchoStep) SemaphoreStep(org.jenkinsci.plugins.workflow.test.steps.SemaphoreStep) BindingStep(org.jenkinsci.plugins.credentialsbinding.impl.BindingStep) EnvVars(hudson.EnvVars) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Aggregations

EnvVars (hudson.EnvVars)64 Test (org.junit.Test)23 IOException (java.io.IOException)15 FilePath (hudson.FilePath)11 ClientConfiguration (com.amazonaws.ClientConfiguration)7 ArgumentListBuilder (hudson.util.ArgumentListBuilder)7 TaskListener (hudson.model.TaskListener)6 AbortException (hudson.AbortException)5 HashMap (java.util.HashMap)5 MavenModuleSet (hudson.maven.MavenModuleSet)4 FreeStyleProject (hudson.model.FreeStyleProject)4 ParameterValue (hudson.model.ParameterValue)4 Launcher (hudson.Launcher)3 GitException (hudson.plugins.git.GitException)3 LogTaskListener (hudson.util.LogTaskListener)3 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 NodeJSInstallation (jenkins.plugins.nodejs.tools.NodeJSInstallation)3 ConduitAPIClient (com.uber.jenkins.phabricator.conduit.ConduitAPIClient)2 ConduitAPIException (com.uber.jenkins.phabricator.conduit.ConduitAPIException)2