Search in sources :

Example 1 with LinearScanner

use of org.jenkinsci.plugins.workflow.graphanalysis.LinearScanner in project workflow-cps-plugin by jenkinsci.

the class ArgumentsActionImplTest method testArgumentDescriptions.

@Test
public void testArgumentDescriptions() throws Exception {
    WorkflowJob job = r.createProject(WorkflowJob.class);
    job.setDefinition(new CpsFlowDefinition("echo 'test' \n " + " node('" + r.jenkins.getSelfLabel().getName() + "') { \n" + "   retry(3) {\n" + "     if (isUnix()) { \n" + "       sh 'whoami' \n" + "     } else { \n" + "       bat 'echo %USERNAME%' \n" + "     }\n" + "   } \n" + "}", false));
    WorkflowRun run = r.buildAndAssertSuccess(job);
    LinearScanner scan = new LinearScanner();
    // Argument test
    FlowNode echoNode = scan.findFirstMatch(run.getExecution().getCurrentHeads().get(0), new NodeStepTypePredicate("echo"));
    Assert.assertEquals("test", echoNode.getPersistentAction(ArgumentsAction.class).getArguments().values().iterator().next());
    Assert.assertEquals("test", ArgumentsAction.getStepArgumentsAsString(echoNode));
    if (Functions.isWindows()) {
        FlowNode batchNode = scan.findFirstMatch(run.getExecution().getCurrentHeads().get(0), new NodeStepTypePredicate("bat"));
        Assert.assertEquals("echo %USERNAME%", batchNode.getPersistentAction(ArgumentsAction.class).getArguments().values().iterator().next());
        Assert.assertEquals("echo %USERNAME%", ArgumentsAction.getStepArgumentsAsString(batchNode));
    } else {
        // Unix
        FlowNode shellNode = scan.findFirstMatch(run.getExecution().getCurrentHeads().get(0), new NodeStepTypePredicate("sh"));
        Assert.assertEquals("whoami", shellNode.getPersistentAction(ArgumentsAction.class).getArguments().values().iterator().next());
        Assert.assertEquals("whoami", ArgumentsAction.getStepArgumentsAsString(shellNode));
    }
    FlowNode nodeNode = scan.findFirstMatch(run.getExecution().getCurrentHeads().get(0), Predicates.and(Predicates.instanceOf(StepStartNode.class), new NodeStepTypePredicate("node"), FlowScanningUtils.hasActionPredicate(ArgumentsActionImpl.class)));
    Assert.assertEquals(r.jenkins.getSelfLabel().getName(), nodeNode.getPersistentAction(ArgumentsAction.class).getArguments().values().iterator().next());
    Assert.assertEquals(r.jenkins.getSelfLabel().getName(), ArgumentsAction.getStepArgumentsAsString(nodeNode));
    testDeserialize(run.getExecution());
}
Also used : ArgumentsAction(org.jenkinsci.plugins.workflow.actions.ArgumentsAction) NodeStepTypePredicate(org.jenkinsci.plugins.workflow.graphanalysis.NodeStepTypePredicate) CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) LinearScanner(org.jenkinsci.plugins.workflow.graphanalysis.LinearScanner) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) FlowNode(org.jenkinsci.plugins.workflow.graph.FlowNode) Test(org.junit.Test)

Example 2 with LinearScanner

use of org.jenkinsci.plugins.workflow.graphanalysis.LinearScanner in project workflow-cps-plugin by jenkinsci.

the class ArgumentsActionImplTest method testReallyUnusualStepInstantiations.

@Test
public void testReallyUnusualStepInstantiations() throws Exception {
    WorkflowJob job = r.createProject(WorkflowJob.class);
    job.setDefinition(new CpsFlowDefinition(" node() {\n" + "   writeFile text: 'hello world', file: 'msg.out'\n" + // note, not whitelisted
    "   step(new hudson.tasks.ArtifactArchiver('msg.out'))\n" + "}", false));
    WorkflowRun run = r.buildAndAssertSuccess(job);
    LinearScanner scan = new LinearScanner();
    FlowNode testNode = scan.findFirstMatch(run.getExecution().getCurrentHeads().get(0), new NodeStepTypePredicate("step"));
    ArgumentsAction act = testNode.getPersistentAction(ArgumentsAction.class);
    Assert.assertNotNull(act);
    Object delegate = act.getArgumentValue("delegate");
    // Test that for a raw Describable we explode it into its arguments Map
    Assert.assertThat(delegate, instanceOf(UninstantiatedDescribable.class));
    UninstantiatedDescribable ud = (UninstantiatedDescribable) delegate;
    Map<String, ?> args = (Map<String, ?>) (((UninstantiatedDescribable) delegate).getArguments());
    Assert.assertThat(args, IsMapContaining.hasEntry("artifacts", "msg.out"));
    Assert.assertEquals(ArtifactArchiver.class.getName(), ud.getModel().getType().getName());
}
Also used : ArgumentsAction(org.jenkinsci.plugins.workflow.actions.ArgumentsAction) UninstantiatedDescribable(org.jenkinsci.plugins.structs.describable.UninstantiatedDescribable) ArtifactArchiver(hudson.tasks.ArtifactArchiver) LinearScanner(org.jenkinsci.plugins.workflow.graphanalysis.LinearScanner) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) NodeStepTypePredicate(org.jenkinsci.plugins.workflow.graphanalysis.NodeStepTypePredicate) CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) Map(java.util.Map) HashMap(java.util.HashMap) FlowNode(org.jenkinsci.plugins.workflow.graph.FlowNode) Test(org.junit.Test)

Example 3 with LinearScanner

use of org.jenkinsci.plugins.workflow.graphanalysis.LinearScanner in project workflow-cps-plugin by jenkinsci.

the class DSLTest method emptyPasswordParametersIgnored.

@Issue("JENKINS-64282")
@Test
public void emptyPasswordParametersIgnored() throws Exception {
    String shellStep = Functions.isWindows() ? "bat" : "sh";
    p.setDefinition(new CpsFlowDefinition("" + "node {\n" + shellStep + " \"echo ${params.TEXT} ${params.PASSWORD}\"\n" + "}", true));
    p.addProperty(new ParametersDefinitionProperty(new StringParameterDefinition("TEXT", ""), new PasswordParameterDefinition("PASSWORD", "", null)));
    WorkflowRun run = r.assertBuildStatusSuccess(p.scheduleBuild2(0, new ParametersAction(new StringParameterValue("TEXT", "hello"), new PasswordParameterValue("PASSWORD", ""))));
    r.assertLogNotContains("Warning: A secret was passed", run);
    r.assertLogNotContains("Affected argument(s) used the following", run);
    LinearScanner scan = new LinearScanner();
    FlowNode node = scan.findFirstMatch(run.getExecution().getCurrentHeads().get(0), new NodeStepTypePredicate(shellStep));
    ArgumentsAction argAction = node.getPersistentAction(ArgumentsAction.class);
    Assert.assertTrue(argAction.isUnmodifiedArguments());
    MatcherAssert.assertThat(argAction.getArguments().values().iterator().next(), is("echo hello "));
}
Also used : ArgumentsAction(org.jenkinsci.plugins.workflow.actions.ArgumentsAction) StringParameterDefinition(hudson.model.StringParameterDefinition) StringParameterValue(hudson.model.StringParameterValue) PasswordParameterValue(hudson.model.PasswordParameterValue) Matchers.containsString(org.hamcrest.Matchers.containsString) ParametersAction(hudson.model.ParametersAction) LinearScanner(org.jenkinsci.plugins.workflow.graphanalysis.LinearScanner) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) NodeStepTypePredicate(org.jenkinsci.plugins.workflow.graphanalysis.NodeStepTypePredicate) ParametersDefinitionProperty(hudson.model.ParametersDefinitionProperty) PasswordParameterDefinition(hudson.model.PasswordParameterDefinition) FlowNode(org.jenkinsci.plugins.workflow.graph.FlowNode) Issue(org.jvnet.hudson.test.Issue) Test(org.junit.Test)

Example 4 with LinearScanner

use of org.jenkinsci.plugins.workflow.graphanalysis.LinearScanner in project workflow-cps-plugin by jenkinsci.

the class DSLTest method passwordParametersSanitized.

@Issue("JENKINS-47101")
@Test
public void passwordParametersSanitized() throws Exception {
    String shellStep = Functions.isWindows() ? "bat" : "sh";
    p.setDefinition(new CpsFlowDefinition("" + "node {\n" + shellStep + " \"echo ${params.TEXT} ${params.PASSWORD}\"\n" + "}", true));
    p.addProperty(new ParametersDefinitionProperty(new StringParameterDefinition("TEXT", ""), new PasswordParameterDefinition("PASSWORD", "", null)));
    WorkflowRun run = r.assertBuildStatusSuccess(p.scheduleBuild2(0, new ParametersAction(new StringParameterValue("TEXT", "hello"), new PasswordParameterValue("PASSWORD", "s3cr3t"))));
    r.assertLogContains("Warning: A secret was passed to \"" + shellStep + "\"", run);
    r.assertLogContains("Affected argument(s) used the following variable(s): [PASSWORD]", run);
    InterpolatedSecretsAction reportAction = run.getAction(InterpolatedSecretsAction.class);
    Assert.assertNotNull(reportAction);
    List<InterpolatedSecretsAction.InterpolatedWarnings> warnings = reportAction.getWarnings();
    MatcherAssert.assertThat(warnings.size(), is(1));
    InterpolatedSecretsAction.InterpolatedWarnings stepWarning = warnings.get(0);
    MatcherAssert.assertThat(stepWarning.getStepName(), is(shellStep));
    MatcherAssert.assertThat(stepWarning.getInterpolatedVariables(), is(Arrays.asList("PASSWORD")));
    LinearScanner scan = new LinearScanner();
    FlowNode node = scan.findFirstMatch(run.getExecution().getCurrentHeads().get(0), new NodeStepTypePredicate(shellStep));
    ArgumentsAction argAction = node.getPersistentAction(ArgumentsAction.class);
    Assert.assertFalse(argAction.isUnmodifiedArguments());
    MatcherAssert.assertThat(argAction.getArguments().values().iterator().next(), is("echo hello ${PASSWORD}"));
}
Also used : ArgumentsAction(org.jenkinsci.plugins.workflow.actions.ArgumentsAction) StringParameterDefinition(hudson.model.StringParameterDefinition) StringParameterValue(hudson.model.StringParameterValue) PasswordParameterValue(hudson.model.PasswordParameterValue) Matchers.containsString(org.hamcrest.Matchers.containsString) ParametersAction(hudson.model.ParametersAction) LinearScanner(org.jenkinsci.plugins.workflow.graphanalysis.LinearScanner) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) NodeStepTypePredicate(org.jenkinsci.plugins.workflow.graphanalysis.NodeStepTypePredicate) ParametersDefinitionProperty(hudson.model.ParametersDefinitionProperty) PasswordParameterDefinition(hudson.model.PasswordParameterDefinition) InterpolatedSecretsAction(org.jenkinsci.plugins.workflow.cps.view.InterpolatedSecretsAction) FlowNode(org.jenkinsci.plugins.workflow.graph.FlowNode) Issue(org.jvnet.hudson.test.Issue) Test(org.junit.Test)

Example 5 with LinearScanner

use of org.jenkinsci.plugins.workflow.graphanalysis.LinearScanner in project workflow-cps-plugin by jenkinsci.

the class DSLTest method sensitiveVariableInterpolation.

@Issue("JENKINS-63254")
@Test
public void sensitiveVariableInterpolation() throws Exception {
    final String credentialsId = "creds-sensitiveVariableInterpolation";
    final String username = "bob";
    final String password = "secr3t";
    UsernamePasswordCredentialsImpl c = new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, credentialsId, "sample", username, password);
    CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), c);
    String shellStep = Functions.isWindows() ? "bat" : "sh";
    p.setDefinition(new CpsFlowDefinition("" + "node {\n" + "withCredentials([usernamePassword(credentialsId: '" + credentialsId + "', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {\n" + shellStep + " \"echo $PASSWORD\"\n" + "}\n" + "}", true));
    WorkflowRun run = r.assertBuildStatusSuccess(p.scheduleBuild2(0));
    r.assertLogContains("Warning: A secret was passed to \"" + shellStep + "\"", run);
    r.assertLogContains("Affected argument(s) used the following variable(s): [PASSWORD]", run);
    InterpolatedSecretsAction reportAction = run.getAction(InterpolatedSecretsAction.class);
    Assert.assertNotNull(reportAction);
    List<InterpolatedSecretsAction.InterpolatedWarnings> warnings = reportAction.getWarnings();
    MatcherAssert.assertThat(warnings.size(), is(1));
    InterpolatedSecretsAction.InterpolatedWarnings stepWarning = warnings.get(0);
    MatcherAssert.assertThat(stepWarning.getStepName(), is(shellStep));
    MatcherAssert.assertThat(stepWarning.getInterpolatedVariables(), is(Arrays.asList("PASSWORD")));
    LinearScanner scan = new LinearScanner();
    FlowNode node = scan.findFirstMatch(run.getExecution().getCurrentHeads().get(0), new NodeStepTypePredicate(shellStep));
    ArgumentsAction argAction = node.getPersistentAction(ArgumentsAction.class);
    Assert.assertFalse(argAction.isUnmodifiedArguments());
    MatcherAssert.assertThat(argAction.getArguments().values().iterator().next(), is("echo ${PASSWORD}"));
}
Also used : ArgumentsAction(org.jenkinsci.plugins.workflow.actions.ArgumentsAction) NodeStepTypePredicate(org.jenkinsci.plugins.workflow.graphanalysis.NodeStepTypePredicate) Matchers.containsString(org.hamcrest.Matchers.containsString) InterpolatedSecretsAction(org.jenkinsci.plugins.workflow.cps.view.InterpolatedSecretsAction) UsernamePasswordCredentialsImpl(com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl) LinearScanner(org.jenkinsci.plugins.workflow.graphanalysis.LinearScanner) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) FlowNode(org.jenkinsci.plugins.workflow.graph.FlowNode) Issue(org.jvnet.hudson.test.Issue) Test(org.junit.Test)

Aggregations

FlowNode (org.jenkinsci.plugins.workflow.graph.FlowNode)11 LinearScanner (org.jenkinsci.plugins.workflow.graphanalysis.LinearScanner)11 WorkflowRun (org.jenkinsci.plugins.workflow.job.WorkflowRun)11 Test (org.junit.Test)11 NodeStepTypePredicate (org.jenkinsci.plugins.workflow.graphanalysis.NodeStepTypePredicate)10 ArgumentsAction (org.jenkinsci.plugins.workflow.actions.ArgumentsAction)9 Issue (org.jvnet.hudson.test.Issue)6 Matchers.containsString (org.hamcrest.Matchers.containsString)5 CpsFlowDefinition (org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition)5 WorkflowJob (org.jenkinsci.plugins.workflow.job.WorkflowJob)5 InterpolatedSecretsAction (org.jenkinsci.plugins.workflow.cps.view.InterpolatedSecretsAction)4 UsernamePasswordCredentialsImpl (com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl)3 ParametersAction (hudson.model.ParametersAction)2 ParametersDefinitionProperty (hudson.model.ParametersDefinitionProperty)2 PasswordParameterDefinition (hudson.model.PasswordParameterDefinition)2 PasswordParameterValue (hudson.model.PasswordParameterValue)2 StringParameterDefinition (hudson.model.StringParameterDefinition)2 StringParameterValue (hudson.model.StringParameterValue)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2