Search in sources :

Example 6 with UsernamePasswordCredentialsImpl

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

the class AbstractBitbucketScm method validateAndCreate.

/**
 * Request payload:
 * {
 *     "userName": "joe",
 *     "password":"****",
 *     "apiUrl":"mybitbucketserver.com"
 * }
 * @param request userName and password of bitbucket server
 *
 * @return credential id
 */
@Override
public HttpResponse validateAndCreate(@JsonBody JSONObject request) {
    User authenticatedUser = User.current();
    if (authenticatedUser == null) {
        throw new ServiceException.UnauthorizedException("No logged in user found");
    }
    String userName = (String) request.get("userName");
    String password = (String) request.get("password");
    String apiUrl = (String) request.get("apiUrl");
    validate(userName, password, apiUrl);
    final StandardUsernamePasswordCredentials credential = new UsernamePasswordCredentialsImpl(CredentialsScope.USER, createCredentialId(apiUrl), "Bitbucket server credentials", userName, password);
    // if credentials are wrong, this call will fail with 401 error
    validateCredential(apiUrl, credential);
    StandardUsernamePasswordCredentials bbCredentials = CredentialsUtils.findCredential(createCredentialId(apiUrl), StandardUsernamePasswordCredentials.class, new BlueOceanDomainRequirement());
    try {
        if (bbCredentials == null) {
            CredentialsUtils.createCredentialsInUserStore(credential, authenticatedUser, getDomainId(), Collections.singletonList(new BlueOceanDomainSpecification()));
        } else {
            CredentialsUtils.updateCredentialsInUserStore(bbCredentials, credential, authenticatedUser, getDomainId(), Collections.singletonList(new BlueOceanDomainSpecification()));
        }
        return createResponse(credential.getId());
    } catch (IOException e) {
        throw new ServiceException.UnexpectedErrorException(e.getMessage());
    }
}
Also used : StandardUsernamePasswordCredentials(com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials) User(hudson.model.User) 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 7 with UsernamePasswordCredentialsImpl

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

the class GithubScm method validateAndCreate.

@Override
public HttpResponse validateAndCreate(@JsonBody JSONObject request) {
    String accessToken = (String) request.get("accessToken");
    if (accessToken == null) {
        throw new ServiceException.BadRequestException("accessToken is required");
    }
    accessToken = accessToken.trim();
    try {
        User authenticatedUser = getAuthenticatedUser();
        HttpURLConnection connection = connect(String.format("%s/%s", getUri(), "user"), accessToken);
        validateAccessTokenScopes(connection);
        String data = IOUtils.toString(HttpRequest.getInputStream(connection), Charset.defaultCharset());
        GHUser user = GithubScm.getMappingObjectReader().forType(GHUser.class).readValue(data);
        if (user.getEmail() != null) {
            Mailer.UserProperty p = authenticatedUser.getProperty(Mailer.UserProperty.class);
            // the one from Github?
            if (p == null) {
                authenticatedUser.addProperty(new Mailer.UserProperty(user.getEmail()));
            }
        }
        // Now we know the token is valid. Lets find credential
        String credentialId = createCredentialId(getUri());
        StandardUsernamePasswordCredentials githubCredential = CredentialsUtils.findCredential(credentialId, StandardUsernamePasswordCredentials.class, new BlueOceanDomainRequirement());
        final StandardUsernamePasswordCredentials credential = new UsernamePasswordCredentialsImpl(CredentialsScope.USER, credentialId, getCredentialDescription(), authenticatedUser.getId(), accessToken);
        if (githubCredential == null) {
            CredentialsUtils.createCredentialsInUserStore(credential, authenticatedUser, getCredentialDomainName(), Collections.singletonList(new BlueOceanDomainSpecification()));
        } else {
            CredentialsUtils.updateCredentialsInUserStore(githubCredential, credential, authenticatedUser, getCredentialDomainName(), Collections.singletonList(new BlueOceanDomainSpecification()));
        }
        return createResponse(credential.getId());
    } catch (IOException e) {
        if (e instanceof MalformedURLException || e instanceof UnknownHostException) {
            throw new ServiceException.BadRequestException(new ErrorMessage(400, "Invalid apiUrl").add(new ErrorMessage.Error("apiUrl", ErrorMessage.Error.ErrorCodes.INVALID.toString(), e.getMessage())));
        }
        throw new ServiceException.UnexpectedErrorException(e.getMessage());
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) GHUser(org.kohsuke.github.GHUser) User(hudson.model.User) UnknownHostException(java.net.UnknownHostException) BlueOceanDomainSpecification(io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanDomainSpecification) Mailer(hudson.tasks.Mailer) GHUser(org.kohsuke.github.GHUser) IOException(java.io.IOException) UsernamePasswordCredentialsImpl(com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl) StandardUsernamePasswordCredentials(com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials) HttpURLConnection(java.net.HttpURLConnection) ServiceException(io.jenkins.blueocean.commons.ServiceException) BlueOceanDomainRequirement(io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanDomainRequirement) ErrorMessage(io.jenkins.blueocean.commons.ErrorMessage)

Example 8 with UsernamePasswordCredentialsImpl

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

the class CredentialApiTest method listCredentials.

@Test
public void listCredentials() throws IOException {
    SystemCredentialsProvider.ProviderImpl system = ExtensionList.lookup(CredentialsProvider.class).get(SystemCredentialsProvider.ProviderImpl.class);
    CredentialsStore systemStore = system.getStore(j.getInstance());
    systemStore.addDomain(new Domain("domain1", null, null));
    systemStore.addCredentials(systemStore.getDomainByName("domain1"), new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, null, null, "admin", "pass$wd"));
    CredentialsStoreAction credentialsStoreAction = ExtensionList.lookup(ViewCredentialsAction.class).get(0).getStore("system");
    CredentialsStoreAction.DomainWrapper domainWrapper = credentialsStoreAction.getDomain("domain1");
    CredentialsStoreAction.CredentialsWrapper credentialsWrapper = domainWrapper.getCredentialsList().get(0);
    List<Map> creds = get("/organizations/jenkins/credentials/system/domains/domain1/credentials/", List.class);
    Assert.assertEquals(1, creds.size());
    Map cred = creds.get(0);
    Assert.assertNotNull(cred.get("id"));
    Map cred1 = get("/organizations/jenkins/credentials/system/domains/domain1/credentials/" + cred.get("id") + "/");
    Assert.assertEquals(credentialsWrapper.getId(), cred1.get("id"));
    Assert.assertEquals(credentialsWrapper.getTypeName(), cred1.get("typeName"));
    Assert.assertEquals(credentialsWrapper.getDisplayName(), cred1.get("displayName"));
    Assert.assertEquals(credentialsWrapper.getFullName(), cred1.get("fullName"));
    Assert.assertEquals(String.format("%s:%s:%s", credentialsWrapper.getDisplayName(), credentialsWrapper.getDomain().getUrlName(), credentialsWrapper.getTypeName()), cred1.get("description"));
    Assert.assertEquals(credentialsWrapper.getDomain().getUrlName(), cred1.get("domain"));
}
Also used : SystemCredentialsProvider(com.cloudbees.plugins.credentials.SystemCredentialsProvider) CredentialsStoreAction(com.cloudbees.plugins.credentials.CredentialsStoreAction) CredentialsStore(com.cloudbees.plugins.credentials.CredentialsStore) SystemCredentialsProvider(com.cloudbees.plugins.credentials.SystemCredentialsProvider) CredentialsProvider(com.cloudbees.plugins.credentials.CredentialsProvider) Domain(com.cloudbees.plugins.credentials.domains.Domain) UsernamePasswordCredentialsImpl(com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl) Map(java.util.Map) Test(org.junit.Test)

Example 9 with UsernamePasswordCredentialsImpl

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

the class CredentialApiTest method listAllCredentials.

@Test
public void listAllCredentials() throws IOException {
    SystemCredentialsProvider.ProviderImpl system = ExtensionList.lookup(CredentialsProvider.class).get(SystemCredentialsProvider.ProviderImpl.class);
    CredentialsStore systemStore = system.getStore(j.getInstance());
    systemStore.addDomain(new Domain("domain1", null, null));
    systemStore.addDomain(new Domain("domain2", null, null));
    systemStore.addCredentials(systemStore.getDomainByName("domain1"), new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, null, null, "admin", "pass$wd"));
    systemStore.addCredentials(systemStore.getDomainByName("domain2"), new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, null, null, "joe", "pass$wd"));
    CredentialsStoreAction credentialsStoreAction = ExtensionList.lookup(ViewCredentialsAction.class).get(0).getStore("system");
    CredentialsStoreAction.DomainWrapper domain1 = credentialsStoreAction.getDomain("domain1");
    CredentialsStoreAction.DomainWrapper domain2 = credentialsStoreAction.getDomain("domain2");
    CredentialsStoreAction.CredentialsWrapper credentials1 = domain1.getCredentialsList().get(0);
    CredentialsStoreAction.CredentialsWrapper credentials2 = domain2.getCredentialsList().get(0);
    List<Map> creds = get("/search?q=type:credential;organization:jenkins", List.class);
    Assert.assertEquals(2, creds.size());
    Assert.assertEquals(credentials1.getId(), creds.get(0).get("id"));
    Assert.assertEquals(credentials2.getId(), creds.get(1).get("id"));
    creds = get("/search?q=type:credential;organization:jenkins;domain:domain2", List.class);
    Assert.assertEquals(1, creds.size());
    Assert.assertEquals(credentials2.getId(), creds.get(0).get("id"));
}
Also used : SystemCredentialsProvider(com.cloudbees.plugins.credentials.SystemCredentialsProvider) CredentialsStoreAction(com.cloudbees.plugins.credentials.CredentialsStoreAction) CredentialsStore(com.cloudbees.plugins.credentials.CredentialsStore) List(java.util.List) ExtensionList(hudson.ExtensionList) SystemCredentialsProvider(com.cloudbees.plugins.credentials.SystemCredentialsProvider) CredentialsProvider(com.cloudbees.plugins.credentials.CredentialsProvider) Domain(com.cloudbees.plugins.credentials.domains.Domain) UsernamePasswordCredentialsImpl(com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl) Map(java.util.Map) Test(org.junit.Test)

Example 10 with UsernamePasswordCredentialsImpl

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

the class GithubOrgFolderTest 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.getInstance())) {
        s.addCredentials(Domain.global(), new UsernamePasswordCredentialsImpl(CredentialsScope.USER, "github", "System Github Access Token", user.getId(), "12345"));
    }
    //create org folder and attach user and credential id to it
    OrganizationFolder organizationFolder = j.createProject(OrganizationFolder.class, "demo");
    AbstractFolderProperty prop = new BlueOceanCredentialsProvider.FolderPropertyImpl(user.getId(), credential.getId(), BlueOceanCredentialsProvider.createDomain("https://api.github.com"));
    organizationFolder.addProperty(prop);
    // lookup for created credential id in system store, it should resolve to previously created user store credential
    StandardCredentials c = Connector.lookupScanCredentials(organizationFolder, "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
    organizationFolder.getProperties().remove(prop);
    //it must resolve to system credential
    c = Connector.lookupScanCredentials(organizationFolder, null, credential.getId());
    assertEquals("System Github Access Token", c.getDescription());
}
Also used : StandardUsernamePasswordCredentials(com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials) User(hudson.model.User) OrganizationFolder(jenkins.branch.OrganizationFolder) BlueOceanDomainRequirement(io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanDomainRequirement) CredentialsStore(com.cloudbees.plugins.credentials.CredentialsStore) BlueOceanDomainSpecification(io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanDomainSpecification) Domain(com.cloudbees.plugins.credentials.domains.Domain) UsernamePasswordCredentialsImpl(com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl) StandardCredentials(com.cloudbees.plugins.credentials.common.StandardCredentials) BlueOceanDomainSpecification(io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanDomainSpecification) DomainSpecification(com.cloudbees.plugins.credentials.domains.DomainSpecification) AbstractFolderProperty(com.cloudbees.hudson.plugins.folder.AbstractFolderProperty) PipelineBaseTest(io.jenkins.blueocean.rest.impl.pipeline.PipelineBaseTest) 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