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