Search in sources :

Example 11 with Domain

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

the class CredentialsUtils method findOrCreateDomain.

@Nonnull
private static Domain findOrCreateDomain(@Nonnull CredentialsStore store, @Nonnull String domainName, @Nonnull List<DomainSpecification> domainSpecifications) throws IOException {
    Domain domain = store.getDomainByName(domainName);
    if (domain == null) {
        // create new one
        boolean result = store.addDomain(new Domain(domainName, domainName + " to store credentials by BlueOcean", domainSpecifications));
        if (!result) {
            throw new ServiceException.BadRequestException("Failed to create credential domain: " + domainName);
        }
        domain = store.getDomainByName(domainName);
        if (domain == null) {
            throw new ServiceException.UnexpectedErrorException("Domain %s created but not found");
        }
    }
    return domain;
}
Also used : Domain(com.cloudbees.plugins.credentials.domains.Domain) Nonnull(javax.annotation.Nonnull)

Example 12 with Domain

use of com.cloudbees.plugins.credentials.domains.Domain 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());
        if (proxyUser != null) {
            try (ACLContext ignored = ACL.as(proxyUser.impersonate())) {
                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);
                                }
                            }
                        }
                    }
                }
            } catch (UsernameNotFoundException ex) {
                logger.warn("BlueOceanCredentialsProvider#getCredentials(): Username attached to credentials can not be found");
            }
        }
    }
    return result;
}
Also used : UsernameNotFoundException(org.acegisecurity.userdetails.UsernameNotFoundException) ACLContext(hudson.security.ACLContext) User(hudson.model.User) 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 13 with Domain

use of com.cloudbees.plugins.credentials.domains.Domain 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 14 with Domain

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

Example 15 with Domain

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

the class CredentialApiTest method createSshCredentialUsingSshFileOnMaster.

@Test
public void createSshCredentialUsingSshFileOnMaster() 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("privateKeyFile", "~/.ssh/blah", "stapler-class", "com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey$FileOnMasterPrivateKeySource")).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)

Aggregations

Domain (com.cloudbees.plugins.credentials.domains.Domain)30 CredentialsStore (com.cloudbees.plugins.credentials.CredentialsStore)16 Test (org.junit.Test)14 CredentialsProvider (com.cloudbees.plugins.credentials.CredentialsProvider)9 SystemCredentialsProvider (com.cloudbees.plugins.credentials.SystemCredentialsProvider)8 User (hudson.model.User)8 UsernamePasswordCredentialsImpl (com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl)6 BlueOceanCredentialsProvider (io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanCredentialsProvider)6 BlueOceanDomainRequirement (io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanDomainRequirement)6 AbstractFolderProperty (com.cloudbees.hudson.plugins.folder.AbstractFolderProperty)5 Map (java.util.Map)5 Credentials (com.cloudbees.plugins.credentials.Credentials)4 PipelineBaseTest (io.jenkins.blueocean.rest.impl.pipeline.PipelineBaseTest)4 List (java.util.List)4 AbstractFolderPropertyDescriptor (com.cloudbees.hudson.plugins.folder.AbstractFolderPropertyDescriptor)3 StandardUsernamePasswordCredentials (com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 DescribableList (hudson.util.DescribableList)3 ErrorMessage (io.jenkins.blueocean.commons.ErrorMessage)3 ServiceException (io.jenkins.blueocean.commons.ServiceException)3