Search in sources :

Example 1 with CredentialsStore

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

the class BlueOceanCredentialsProvider method getCredentials.

@Nonnull
public <C extends Credentials> List<C> getCredentials(@Nonnull final Class<C> type, @Nullable ItemGroup itemGroup, @Nullable Authentication authentication, @Nonnull List<DomainRequirement> domainRequirements) {
    final List<C> result = new ArrayList<>();
    final FolderPropertyImpl prop = propertyOf(itemGroup);
    if (prop != null && prop.domain.test(domainRequirements)) {
        final User proxyUser = User.get(prop.getUser(), false, Collections.emptyMap());
        Authentication proxyAuth = proxyUser == null ? null : proxyUser.impersonate();
        if (proxyAuth != null) {
            ACL.impersonate(proxyAuth, new Runnable() {

                @Override
                public void run() {
                    for (CredentialsStore s : CredentialsProvider.lookupStores(proxyUser)) {
                        for (Domain d : s.getDomains()) {
                            if (d.test(PROXY_REQUIREMENT)) {
                                for (Credentials c : filter(s.getCredentials(d), withId(prop.getId()))) {
                                    if (type.isInstance(c)) {
                                        result.add((C) c);
                                    }
                                }
                            }
                        }
                    }
                }
            });
        }
    }
    return result;
}
Also used : User(hudson.model.User) Authentication(org.acegisecurity.Authentication) ArrayList(java.util.ArrayList) CredentialsStore(com.cloudbees.plugins.credentials.CredentialsStore) Domain(com.cloudbees.plugins.credentials.domains.Domain) Credentials(com.cloudbees.plugins.credentials.Credentials) IdCredentials(com.cloudbees.plugins.credentials.common.IdCredentials) Nonnull(javax.annotation.Nonnull)

Example 2 with CredentialsStore

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

the class CredentialContainer method iterator.

@Override
public Iterator<CredentialApi> iterator() {
    List<CredentialApi> apis = new ArrayList<>();
    User user = User.current();
    if (user != null) {
        for (CredentialsStore store : CredentialsProvider.lookupStores(user)) {
            if (store.getStoreAction() != null) {
                apis.add(new CredentialApi(store.getStoreAction(), this));
            }
        }
    } else {
        for (CredentialsStore store : CredentialsProvider.lookupStores(Jenkins.getInstance())) {
            if (store.getStoreAction() != null) {
                apis.add(new CredentialApi(store.getStoreAction(), this));
            }
        }
    }
    return apis.iterator();
}
Also used : User(hudson.model.User) ArrayList(java.util.ArrayList) CredentialsStore(com.cloudbees.plugins.credentials.CredentialsStore)

Example 3 with CredentialsStore

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

the class CredentialApiTest method createSshCredentialUsingDirectSsh.

@Test
public void createSshCredentialUsingDirectSsh() 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));
    Map<String, Object> resp = post("/organizations/jenkins/credentials/system/domains/domain1/credentials/", 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", "GLOBAL").put("description", "ssh2 desc").put("$class", "com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey").put("username", "ssh2").build()), 201);
    Assert.assertEquals("SSH Username with private key", resp.get("typeName"));
    Assert.assertEquals("domain1", resp.get("domain"));
}
Also used : 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) Test(org.junit.Test)

Example 4 with CredentialsStore

use of com.cloudbees.plugins.credentials.CredentialsStore 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) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) Test(org.junit.Test)

Example 5 with CredentialsStore

use of com.cloudbees.plugins.credentials.CredentialsStore 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", 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;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) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) Test(org.junit.Test)

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