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());
}
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());
}
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 "));
}
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}"));
}
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}"));
}
Aggregations