Search in sources :

Example 11 with LinearScanner

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

the class DSLTest method multipleSensitiveVariables.

@Test
public void multipleSensitiveVariables() throws Exception {
    final String credentialsId = "creds-multipleSensitiveVariables";
    final String username = "bob";
    final String password = "secr3t";
    UsernamePasswordCredentialsImpl c = new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, credentialsId, "sample", username, password);
    c.setUsernameSecret(true);
    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 $USERNAME $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, USERNAME]", 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", "USERNAME")));
    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} ${USERNAME} ${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) 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