Search in sources :

Example 1 with ForkScanner

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

the class ArgumentsActionImplTest method testBasicCredentials.

@Test
public void testBasicCredentials() throws Exception {
    String username = "bob";
    String password = "s3cr3t";
    UsernamePasswordCredentialsImpl c = new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, "test", "sample", username, password);
    CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), c);
    WorkflowJob job = r.jenkins.createProject(WorkflowJob.class, "credentialed");
    job.setDefinition(new CpsFlowDefinition("node{ withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'test',\n" + "                usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) {\n" + "    //available as an env variable, but will be masked if you try to print it out any which way\n" + "    echo \"$PASSWORD'\" \n" + "    echo \"${env.USERNAME}\"\n" + "    echo \"bob\"\n" + "} }\n" + "withCredentials([usernamePassword(credentialsId: 'test', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {\n" + "  echo \"${env.USERNAME} ${env.PASSWORD}\"\n" + "}"));
    WorkflowRun run = job.scheduleBuild2(0).getStartCondition().get();
    r.waitForCompletion(run);
    FlowExecution exec = run.getExecution();
    String log = r.getLog(run);
    ForkScanner scanner = new ForkScanner();
    List<FlowNode> filtered = scanner.filteredNodes(exec, new DescriptorMatchPredicate(BindingStep.DescriptorImpl.class));
    // Check the binding step is OK
    Assert.assertEquals(8, filtered.size());
    FlowNode node = Collections2.filter(filtered, FlowScanningUtils.hasActionPredicate(ArgumentsActionImpl.class)).iterator().next();
    ArgumentsActionImpl act = node.getPersistentAction(ArgumentsActionImpl.class);
    Assert.assertNotNull(act.getArgumentValue("bindings"));
    Assert.assertNotNull(act.getArguments().get("bindings"));
    // Test that masking really does mask bound credentials appropriately
    filtered = scanner.filteredNodes(exec, new DescriptorMatchPredicate(EchoStep.DescriptorImpl.class));
    for (FlowNode f : filtered) {
        act = f.getPersistentAction(ArgumentsActionImpl.class);
        Assert.assertEquals(ArgumentsAction.NotStoredReason.MASKED_VALUE, act.getArguments().get("message"));
        Assert.assertNull(ArgumentsAction.getStepArgumentsAsString(f));
    }
    List<FlowNode> allStepped = scanner.filteredNodes(run.getExecution().getCurrentHeads(), FlowScanningUtils.hasActionPredicate(ArgumentsActionImpl.class));
    // One ArgumentsActionImpl per block or atomic step
    Assert.assertEquals(6, allStepped.size());
    testDeserialize(exec);
}
Also used : DescriptorMatchPredicate(org.jenkinsci.plugins.workflow.cps.DescriptorMatchPredicate) EchoStep(org.jenkinsci.plugins.workflow.steps.EchoStep) ForkScanner(org.jenkinsci.plugins.workflow.graphanalysis.ForkScanner) UsernamePasswordCredentialsImpl(com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) FlowExecution(org.jenkinsci.plugins.workflow.flow.FlowExecution) CpsFlowExecution(org.jenkinsci.plugins.workflow.cps.CpsFlowExecution) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) FlowNode(org.jenkinsci.plugins.workflow.graph.FlowNode) Test(org.junit.Test)

Aggregations

UsernamePasswordCredentialsImpl (com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl)1 CpsFlowDefinition (org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition)1 CpsFlowExecution (org.jenkinsci.plugins.workflow.cps.CpsFlowExecution)1 DescriptorMatchPredicate (org.jenkinsci.plugins.workflow.cps.DescriptorMatchPredicate)1 FlowExecution (org.jenkinsci.plugins.workflow.flow.FlowExecution)1 FlowNode (org.jenkinsci.plugins.workflow.graph.FlowNode)1 ForkScanner (org.jenkinsci.plugins.workflow.graphanalysis.ForkScanner)1 WorkflowJob (org.jenkinsci.plugins.workflow.job.WorkflowJob)1 WorkflowRun (org.jenkinsci.plugins.workflow.job.WorkflowRun)1 EchoStep (org.jenkinsci.plugins.workflow.steps.EchoStep)1 Test (org.junit.Test)1