Search in sources :

Example 1 with UsernamePasswordCredentialsImpl

use of com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl 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)

Example 2 with UsernamePasswordCredentialsImpl

use of com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl in project pipeline-aws-plugin by jenkinsci.

the class WithAWSStepTest method testStepWithGlobalCredentials.

@Test
public void testStepWithGlobalCredentials() throws Exception {
    String globalCredentialsId = "global-aws-creds";
    List<String> credentialIds = new ArrayList<>();
    credentialIds.add(globalCredentialsId);
    StandardUsernamePasswordCredentials key = new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, globalCredentialsId, "test-global-creds", "global-aws-access-key-id", "global-aws-secret-access-key");
    SystemCredentialsProvider.getInstance().getCredentials().add(key);
    SystemCredentialsProvider.getInstance().save();
    WorkflowJob job = jenkinsRule.jenkins.createProject(WorkflowJob.class, "testStepWithGlobalCredentials");
    job.setDefinition(new CpsFlowDefinition("" + "node {\n" + "  withAWS (credentials: '" + globalCredentialsId + "') {\n" + "    echo 'It works!'\n" + "  }\n" + "}\n", true));
    jenkinsRule.assertBuildStatusSuccess(job.scheduleBuild2(0));
}
Also used : StandardUsernamePasswordCredentials(com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials) CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) ArrayList(java.util.ArrayList) UsernamePasswordCredentialsImpl(com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) Test(org.junit.Test)

Example 3 with UsernamePasswordCredentialsImpl

use of com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl in project pipeline-aws-plugin by jenkinsci.

the class WithAWSStepTest method testListCredentials.

@Test
public void testListCredentials() throws Exception {
    Folder folder = jenkinsRule.jenkins.createProject(Folder.class, "folder" + jenkinsRule.jenkins.getItems().size());
    CredentialsStore folderStore = this.getFolderStore(folder);
    StandardUsernamePasswordCredentials folderCredentials = new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, "folder-creds", "test-creds", "aws-access-key-id", "aws-secret-access-key");
    StandardUsernamePasswordCredentials globalCredentials = new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, "global-creds", "test-creds", "aws-access-key-id", "aws-secret-access-key");
    folderStore.addCredentials(Domain.global(), folderCredentials);
    SystemCredentialsProvider.getInstance().getCredentials().add(globalCredentials);
    SystemCredentialsProvider.getInstance().save();
    WorkflowJob job = folder.createProject(WorkflowJob.class, "testStepWithFolderCredentials");
    final WithAWSStep.DescriptorImpl descriptor = jenkinsRule.jenkins.getDescriptorByType(WithAWSStep.DescriptorImpl.class);
    // 3 options: Root credentials, folder credentials and "none"
    ListBoxModel list = descriptor.doFillCredentialsItems(job);
    Assert.assertEquals(3, list.size());
    StandardUsernamePasswordCredentials systemCredentials = new UsernamePasswordCredentialsImpl(CredentialsScope.SYSTEM, "system-creds", "test-creds", "aws-access-key-id", "aws-secret-access-key");
    SystemCredentialsProvider.getInstance().getCredentials().add(systemCredentials);
    // Still 3 options: Root credentials, folder credentials and "none"
    list = descriptor.doFillCredentialsItems(job);
    Assert.assertEquals(3, list.size());
}
Also used : StandardUsernamePasswordCredentials(com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials) CredentialsStore(com.cloudbees.plugins.credentials.CredentialsStore) AbstractFolder(com.cloudbees.hudson.plugins.folder.AbstractFolder) Folder(com.cloudbees.hudson.plugins.folder.Folder) UsernamePasswordCredentialsImpl(com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) ListBoxModel(hudson.util.ListBoxModel) Test(org.junit.Test)

Example 4 with UsernamePasswordCredentialsImpl

use of com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl in project pipeline-aws-plugin by jenkinsci.

the class WithAWSStepTest method testStepWithFolderCredentials.

@Test
public void testStepWithFolderCredentials() throws Exception {
    String folderCredentialsId = "folders-aws-creds";
    // Create a folder with credentials in its store
    Folder folder = jenkinsRule.jenkins.createProject(Folder.class, "folder" + jenkinsRule.jenkins.getItems().size());
    CredentialsStore folderStore = this.getFolderStore(folder);
    StandardUsernamePasswordCredentials inFolderCredentials = new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, folderCredentialsId, "test-folder-creds", "folder-aws-access-key-id", "folder-aws-secret-access-key");
    folderStore.addCredentials(Domain.global(), inFolderCredentials);
    SystemCredentialsProvider.getInstance().save();
    List<String> credentialIds = new ArrayList<>();
    credentialIds.add(folderCredentialsId);
    WorkflowJob job = folder.createProject(WorkflowJob.class, "testStepWithFolderCredentials");
    job.setDefinition(new CpsFlowDefinition("" + "node {\n" + "  withAWS (credentials: '" + folderCredentialsId + "') {\n" + "    echo 'It works!'\n" + "  }\n" + "}\n", true));
    jenkinsRule.assertBuildStatusSuccess(job.scheduleBuild2(0));
}
Also used : StandardUsernamePasswordCredentials(com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials) CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) CredentialsStore(com.cloudbees.plugins.credentials.CredentialsStore) ArrayList(java.util.ArrayList) AbstractFolder(com.cloudbees.hudson.plugins.folder.AbstractFolder) Folder(com.cloudbees.hudson.plugins.folder.Folder) UsernamePasswordCredentialsImpl(com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) Test(org.junit.Test)

Example 5 with UsernamePasswordCredentialsImpl

use of com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl in project nodejs-plugin by jenkinsci.

the class NPMRegistryValidatorTest method test_credentials_ok.

@Test
public void test_credentials_ok() throws Exception {
    String credentialsId = "secret";
    Credentials credentials = new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, credentialsId, "", "user", "password");
    Map<Domain, List<Credentials>> credentialsMap = new HashMap<>();
    credentialsMap.put(Domain.global(), Arrays.asList(credentials));
    SystemCredentialsProvider.getInstance().setDomainCredentialsMap(credentialsMap);
    FreeStyleProject prj = mock(FreeStyleProject.class);
    when(prj.hasPermission(isA(Permission.class))).thenReturn(true);
    DescriptorImpl descriptor = mock(DescriptorImpl.class);
    when(descriptor.doCheckCredentialsId(any(Item.class), (String) any(), anyString())).thenCallRealMethod();
    String serverURL = "http://acme.com";
    FormValidation result = descriptor.doCheckCredentialsId(prj, credentialsId, serverURL);
    assertThat(result.kind, is(Kind.OK));
}
Also used : Item(hudson.model.Item) FormValidation(hudson.util.FormValidation) HashMap(java.util.HashMap) DescriptorImpl(jenkins.plugins.nodejs.configfiles.NPMRegistry.DescriptorImpl) Permission(hudson.security.Permission) List(java.util.List) UsernamePasswordCredentialsImpl(com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl) Domain(com.cloudbees.plugins.credentials.domains.Domain) FreeStyleProject(hudson.model.FreeStyleProject) Credentials(com.cloudbees.plugins.credentials.Credentials) Test(org.junit.Test)

Aggregations

UsernamePasswordCredentialsImpl (com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl)15 StandardUsernamePasswordCredentials (com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials)10 Test (org.junit.Test)10 CredentialsStore (com.cloudbees.plugins.credentials.CredentialsStore)8 Domain (com.cloudbees.plugins.credentials.domains.Domain)6 BlueOceanDomainRequirement (io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanDomainRequirement)6 BlueOceanDomainSpecification (io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanDomainSpecification)5 WorkflowJob (org.jenkinsci.plugins.workflow.job.WorkflowJob)5 User (hudson.model.User)4 CpsFlowDefinition (org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition)4 CredentialsProvider (com.cloudbees.plugins.credentials.CredentialsProvider)3 ServiceException (io.jenkins.blueocean.commons.ServiceException)3 IOException (java.io.IOException)3 AbstractFolder (com.cloudbees.hudson.plugins.folder.AbstractFolder)2 AbstractFolderProperty (com.cloudbees.hudson.plugins.folder.AbstractFolderProperty)2 Folder (com.cloudbees.hudson.plugins.folder.Folder)2 CredentialsStoreAction (com.cloudbees.plugins.credentials.CredentialsStoreAction)2 SystemCredentialsProvider (com.cloudbees.plugins.credentials.SystemCredentialsProvider)2 StandardCredentials (com.cloudbees.plugins.credentials.common.StandardCredentials)2 DomainSpecification (com.cloudbees.plugins.credentials.domains.DomainSpecification)2