Search in sources :

Example 6 with CredentialsStore

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

the class GitScmTest method shouldGetForbiddenForBadCredentialIdOnUpdate2.

@Test
public void shouldGetForbiddenForBadCredentialIdOnUpdate2() throws IOException, UnirestException {
    User user = login();
    String mbp = createMbp(user);
    SystemCredentialsProvider.ProviderImpl system = ExtensionList.lookup(CredentialsProvider.class).get(SystemCredentialsProvider.ProviderImpl.class);
    CredentialsStore systemStore = system.getStore(j.getInstance());
    systemStore.addDomain(new Domain("domain1", null, null));
    Map<String, Object> resp = createCredentials(user, ImmutableMap.of("credentials", new ImmutableMap.Builder<String, Object>().put("password", "abcd").put("stapler-class", "com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl").put("scope", "USER").put("domain", "blueocean-git-domain").put("description", "joe desc").put("$class", "com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl").put("username", "joe").build()));
    String credentialId = (String) resp.get("id");
    Assert.assertNotNull(credentialId);
    put("/organizations/jenkins/pipelines/" + mbp + "/", ImmutableMap.of("name", mbp, "$class", "io.jenkins.blueocean.blueocean_git_pipeline.GitPipelineUpdateRequest", "scmConfig", ImmutableMap.of("uri", "git@github.com:vivek/capability-annotation.git", "credentialId", credentialId)), 400);
}
Also used : User(hudson.model.User) SystemCredentialsProvider(com.cloudbees.plugins.credentials.SystemCredentialsProvider) 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) PipelineBaseTest(io.jenkins.blueocean.rest.impl.pipeline.PipelineBaseTest) Test(org.junit.Test)

Example 7 with CredentialsStore

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

the class GitScmTest method shouldGetForbiddenForBadCredentialIdOnUpdate1.

@Test
public void shouldGetForbiddenForBadCredentialIdOnUpdate1() throws IOException, UnirestException {
    User user = login();
    String mbp = createMbp(user);
    SystemCredentialsProvider.ProviderImpl system = ExtensionList.lookup(CredentialsProvider.class).get(SystemCredentialsProvider.ProviderImpl.class);
    CredentialsStore systemStore = system.getStore(j.getInstance());
    systemStore.addDomain(new Domain("domain1", null, null));
    Map resp = createCredentials(user, ImmutableMap.of("credentials", new ImmutableMap.Builder<String, Object>().put("privateKeySource", ImmutableMap.of("privateKey", "abcabc1212", "stapler-class", "com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey$DirectEntryPrivateKeySource")).put("passphrase", "ssh2").put("scope", "USER").put("domain", "blueocean-git-domain").put("description", "ssh2 desc").put("$class", "com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey").put("username", "ssh2").build()));
    String credentialId = (String) resp.get("id");
    Assert.assertNotNull(credentialId);
    put("/organizations/jenkins/pipelines/" + mbp + "/", ImmutableMap.of("name", mbp, "$class", "io.jenkins.blueocean.blueocean_git_pipeline.GitPipelineUpdateRequest", "scmConfig", ImmutableMap.of("uri", "git@github.com:vivek/capability-annotation.git", "credentialId", credentialId)), 400);
}
Also used : User(hudson.model.User) SystemCredentialsProvider(com.cloudbees.plugins.credentials.SystemCredentialsProvider) 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) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) PipelineBaseTest(io.jenkins.blueocean.rest.impl.pipeline.PipelineBaseTest) Test(org.junit.Test)

Example 8 with CredentialsStore

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

Example 9 with CredentialsStore

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

the class CredentialsUtils method updateCredentialsInUserStore.

public static void updateCredentialsInUserStore(@Nonnull Credentials current, @Nonnull Credentials replacement, @Nonnull User user, @Nonnull String domainName, @Nonnull List<DomainSpecification> domainSpecifications) throws IOException {
    CredentialsStore store = findUserStoreFirstOrNull(user);
    if (store == null) {
        throw new ServiceException.ForbiddenException(String.format("Logged in user: %s doesn't have writable credentials store", user.getId()));
    }
    Domain domain = findOrCreateDomain(store, domainName, domainSpecifications);
    if (!store.updateCredentials(domain, current, replacement)) {
        throw new ServiceException.UnexpectedErrorException("Failed to update credential to domain");
    }
}
Also used : CredentialsStore(com.cloudbees.plugins.credentials.CredentialsStore) Domain(com.cloudbees.plugins.credentials.domains.Domain)

Example 10 with CredentialsStore

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

the class CredentialsUtils method createCredentialsInUserStore.

public static void createCredentialsInUserStore(@Nonnull Credentials credential, @Nonnull User user, @Nonnull String domainName, @Nonnull List<DomainSpecification> domainSpecifications) throws IOException {
    CredentialsStore store = findUserStoreFirstOrNull(user);
    if (store == null) {
        throw new ServiceException.ForbiddenException(String.format("Logged in user: %s doesn't have writable credentials store", user.getId()));
    }
    Domain domain = findOrCreateDomain(store, domainName, domainSpecifications);
    if (!store.addCredentials(domain, credential)) {
        throw new ServiceException.UnexpectedErrorException("Failed to add credential to domain");
    }
}
Also used : CredentialsStore(com.cloudbees.plugins.credentials.CredentialsStore) Domain(com.cloudbees.plugins.credentials.domains.Domain)

Aggregations

CredentialsStore (com.cloudbees.plugins.credentials.CredentialsStore)13 Domain (com.cloudbees.plugins.credentials.domains.Domain)12 Test (org.junit.Test)9 CredentialsProvider (com.cloudbees.plugins.credentials.CredentialsProvider)8 SystemCredentialsProvider (com.cloudbees.plugins.credentials.SystemCredentialsProvider)8 ImmutableMap (com.google.common.collect.ImmutableMap)7 User (hudson.model.User)5 UsernamePasswordCredentialsImpl (com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl)3 PipelineBaseTest (io.jenkins.blueocean.rest.impl.pipeline.PipelineBaseTest)3 Map (java.util.Map)3 CredentialsStoreAction (com.cloudbees.plugins.credentials.CredentialsStoreAction)2 ArrayList (java.util.ArrayList)2 AbstractFolderProperty (com.cloudbees.hudson.plugins.folder.AbstractFolderProperty)1 Credentials (com.cloudbees.plugins.credentials.Credentials)1 IdCredentials (com.cloudbees.plugins.credentials.common.IdCredentials)1 StandardCredentials (com.cloudbees.plugins.credentials.common.StandardCredentials)1 StandardUsernamePasswordCredentials (com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials)1 DomainSpecification (com.cloudbees.plugins.credentials.domains.DomainSpecification)1 ExtensionList (hudson.ExtensionList)1 BlueOceanDomainRequirement (io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanDomainRequirement)1