Search in sources :

Example 11 with UsernamePasswordCredentialsImpl

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

the class RegistryHelperTest method setUp.

@Before
public void setUp() throws Exception {
    user = new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, "privateId", "dummy desc", "myuser", "mypassword");
    CredentialsStore store = CredentialsProvider.lookupStores(j.getInstance()).iterator().next();
    store.addCredentials(Domain.global(), user);
}
Also used : CredentialsStore(com.cloudbees.plugins.credentials.CredentialsStore) UsernamePasswordCredentialsImpl(com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl) Before(org.junit.Before)

Example 12 with UsernamePasswordCredentialsImpl

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

the class S3UploadStepIntegrationTest method smokes.

@Issue("JENKINS-49025")
@Test
public void smokes() throws Exception {
    String globalCredentialsId = "x";
    StandardUsernamePasswordCredentials key = new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, globalCredentialsId, "x", "x", "x");
    SystemCredentialsProvider.getInstance().getCredentials().add(key);
    WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
    p.setDefinition(new CpsFlowDefinition("node('" + r.createSlave().getNodeName() + "') {\n" + "  withAWS (credentials: '" + globalCredentialsId + "') {\n" + "    writeFile file: 'x', text: ''\n" + "    try {\n" + "      s3Upload bucket: 'x', file: 'x', path: 'x'\n" + "      fail 'should not have worked'\n" + "    } catch (com.amazonaws.services.s3.model.AmazonS3Exception x) {\n" + "      echo(/got $x as expected/)\n" + "    }\n" + "  }\n" + "}\n", true));
    r.assertBuildStatusSuccess(p.scheduleBuild2(0));
}
Also used : StandardUsernamePasswordCredentials(com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials) CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) UsernamePasswordCredentialsImpl(com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) Issue(org.jvnet.hudson.test.Issue) Test(org.junit.Test)

Example 13 with UsernamePasswordCredentialsImpl

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

the class GitScm method createPWCredentials.

private void createPWCredentials(String credentialId, User user, @JsonBody JSONObject request, String repositoryUrl) {
    StandardUsernamePasswordCredentials existingCredential = CredentialsUtils.findCredential(credentialId, StandardUsernamePasswordCredentials.class, new BlueOceanDomainRequirement());
    String requestUsername = request.getString("userName");
    String requestPassword = request.getString("password");
    // Un-normalized repositoryUrl so the description matches user input.
    String description = String.format("%s for %s", CREDENTIAL_DESCRIPTION_PW, repositoryUrl);
    final StandardUsernamePasswordCredentials newCredential = new UsernamePasswordCredentialsImpl(CredentialsScope.USER, credentialId, description, requestUsername, requestPassword);
    try {
        if (existingCredential == null) {
            CredentialsUtils.createCredentialsInUserStore(newCredential, user, CREDENTIAL_DOMAIN_NAME, Collections.singletonList(new BlueOceanDomainSpecification()));
        } else {
            CredentialsUtils.updateCredentialsInUserStore(existingCredential, newCredential, user, CREDENTIAL_DOMAIN_NAME, Collections.singletonList(new BlueOceanDomainSpecification()));
        }
    } catch (IOException e) {
        throw new ServiceException.UnexpectedErrorException("Could not persist credential", e);
    }
}
Also used : StandardUsernamePasswordCredentials(com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials) ServiceException(io.jenkins.blueocean.commons.ServiceException) BlueOceanDomainRequirement(io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanDomainRequirement) BlueOceanDomainSpecification(io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanDomainSpecification) IOException(java.io.IOException) UsernamePasswordCredentialsImpl(com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl)

Example 14 with UsernamePasswordCredentialsImpl

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

the class GithubPipelineCreateRequestTest method shouldFindUserStoreCredential.

@Test
public void shouldFindUserStoreCredential() throws IOException {
    // add username password credential to user's credential store in user domain and in USER scope
    User user = login();
    CredentialsStore store = null;
    for (CredentialsStore s : CredentialsProvider.lookupStores(user)) {
        if (s.hasPermission(CredentialsProvider.CREATE) && s.hasPermission(CredentialsProvider.UPDATE)) {
            store = s;
            break;
        }
    }
    assertNotNull(store);
    store.addDomain(new Domain("github-domain", "GitHub Domain to store personal access token", Collections.<DomainSpecification>singletonList(new BlueOceanDomainSpecification())));
    Domain domain = store.getDomainByName("github-domain");
    StandardUsernamePasswordCredentials credential = new UsernamePasswordCredentialsImpl(CredentialsScope.USER, "github", "GitHub Access Token", user.getId(), "12345");
    store.addCredentials(domain, credential);
    // create another credentials with same id in system store with different description
    for (CredentialsStore s : CredentialsProvider.lookupStores(Jenkins.get())) {
        s.addCredentials(Domain.global(), new UsernamePasswordCredentialsImpl(CredentialsScope.USER, "github", "System GitHub Access Token", user.getId(), "12345"));
    }
    WorkflowMultiBranchProject mp = j.jenkins.createProject(WorkflowMultiBranchProject.class, "demo");
    AbstractFolderProperty prop = new BlueOceanCredentialsProvider.FolderPropertyImpl(user.getId(), credential.getId(), BlueOceanCredentialsProvider.createDomain("https://api.github.com"));
    mp.addProperty(prop);
    // lookup for created credential id in system store, it should resolve to previously created user store credential
    StandardCredentials c = Connector.lookupScanCredentials((Item) mp, "https://api.github.com", credential.getId());
    assertEquals("GitHub Access Token", c.getDescription());
    assertNotNull(c);
    assertTrue(c instanceof StandardUsernamePasswordCredentials);
    StandardUsernamePasswordCredentials usernamePasswordCredentials = (StandardUsernamePasswordCredentials) c;
    assertEquals(credential.getId(), usernamePasswordCredentials.getId());
    assertEquals(credential.getPassword().getPlainText(), usernamePasswordCredentials.getPassword().getPlainText());
    assertEquals(credential.getUsername(), usernamePasswordCredentials.getUsername());
    // check the domain
    Domain d = CredentialsUtils.findDomain(credential.getId(), user);
    assertNotNull(d);
    assertTrue(d.test(new BlueOceanDomainRequirement()));
    // now remove this property
    mp.getProperties().remove(prop);
    // it must resolve to system credential
    c = Connector.lookupScanCredentials((Item) mp, null, credential.getId());
    assertEquals("System GitHub Access Token", c.getDescription());
}
Also used : User(hudson.model.User) BlueOceanDomainSpecification(io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanDomainSpecification) UsernamePasswordCredentialsImpl(com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl) DomainSpecification(com.cloudbees.plugins.credentials.domains.DomainSpecification) BlueOceanDomainSpecification(io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanDomainSpecification) AbstractFolderProperty(com.cloudbees.hudson.plugins.folder.AbstractFolderProperty) StandardUsernamePasswordCredentials(com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials) WorkflowMultiBranchProject(org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject) Item(hudson.model.Item) BlueOceanDomainRequirement(io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanDomainRequirement) CredentialsStore(com.cloudbees.plugins.credentials.CredentialsStore) Domain(com.cloudbees.plugins.credentials.domains.Domain) StandardCredentials(com.cloudbees.plugins.credentials.common.StandardCredentials) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PipelineBaseTest(io.jenkins.blueocean.rest.impl.pipeline.PipelineBaseTest) Test(org.junit.Test)

Example 15 with UsernamePasswordCredentialsImpl

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

the class GithubScmTest method mockCredentials.

void mockCredentials(String userId, String accessToken, String credentialId, String domainName) throws Exception {
    // Mock Credentials
    UsernamePasswordCredentialsImpl credentials = mock(UsernamePasswordCredentialsImpl.class);
    whenNew(UsernamePasswordCredentialsImpl.class).withAnyArguments().thenReturn(credentials);
    when(credentials.getId()).thenReturn(credentialId);
    when(credentials.getUsername()).thenReturn(userId);
    Secret secret = mock(Secret.class);
    when(secret.getPlainText()).thenReturn(accessToken);
    when(credentials.getPassword()).thenReturn(secret);
    CredentialsMatcher credentialsMatcher = mock(CredentialsMatcher.class);
    mockStatic(CredentialsMatchers.class);
    mockStatic(CredentialsProvider.class);
    when(CredentialsMatchers.withId(credentialId)).thenReturn(credentialsMatcher);
    BlueOceanDomainRequirement blueOceanDomainRequirement = mock(BlueOceanDomainRequirement.class);
    whenNew(BlueOceanDomainRequirement.class).withNoArguments().thenReturn(blueOceanDomainRequirement);
    when(CredentialsProvider.class, "lookupCredentials", StandardUsernamePasswordCredentials.class, jenkins, authentication, blueOceanDomainRequirement).thenReturn(Collections.singletonList(credentials));
    when(CredentialsMatchers.class, "firstOrNull", Collections.singletonList(credentials), credentialsMatcher).thenReturn(credentials);
    when(CredentialsMatchers.allOf(credentialsMatcher)).thenReturn(credentialsMatcher);
    // Mock credentials Domain
    Domain domain = mock(Domain.class);
    when(domain.getName()).thenReturn(domainName);
    // Mock credentials Store
    CredentialsStore credentialsStore = mock(CredentialsStore.class);
    when(credentialsStore.hasPermission(CredentialsProvider.CREATE)).thenReturn(true);
    when(credentialsStore.hasPermission(CredentialsProvider.UPDATE)).thenReturn(true);
    when(credentialsStore.getDomainByName(domainName)).thenReturn(domain);
    when(CredentialsProvider.class, "lookupStores", user).thenReturn(Collections.singletonList(credentialsStore));
    when(credentialsStore.addCredentials(domain, credentials)).thenReturn(true);
    when(credentialsStore.updateCredentials(domain, credentials, credentials)).thenReturn(true);
}
Also used : Secret(hudson.util.Secret) StandardUsernamePasswordCredentials(com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials) BlueOceanDomainRequirement(io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanDomainRequirement) CredentialsMatchers(com.cloudbees.plugins.credentials.CredentialsMatchers) CredentialsStore(com.cloudbees.plugins.credentials.CredentialsStore) CredentialsMatcher(com.cloudbees.plugins.credentials.CredentialsMatcher) CredentialsProvider(com.cloudbees.plugins.credentials.CredentialsProvider) UsernamePasswordCredentialsImpl(com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl) Domain(com.cloudbees.plugins.credentials.domains.Domain)

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