Search in sources :

Example 21 with Domain

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

the class GitPipelineCreateRequest method create.

@Override
public BluePipeline create(Reachable parent) throws IOException {
    User authenticatedUser = User.current();
    if (authenticatedUser == null) {
        throw new ServiceException.UnauthorizedException("Must login to create a pipeline");
    }
    String sourceUri = scmConfig.getUri();
    if (sourceUri == null) {
        throw new ServiceException.BadRequestExpception(new ErrorMessage(400, "Failed to create Git pipeline:" + getName()).add(new ErrorMessage.Error("scmConfig.uri", ErrorMessage.Error.ErrorCodes.MISSING.toString(), "uri is required")));
    }
    TopLevelItem item = create(Jenkins.getInstance(), getName(), MODE, MultiBranchProjectDescriptor.class);
    if (item instanceof WorkflowMultiBranchProject) {
        WorkflowMultiBranchProject project = (WorkflowMultiBranchProject) item;
        if (StringUtils.isNotBlank(scmConfig.getCredentialId())) {
            Domain domain = CredentialsUtils.findDomain(scmConfig.getCredentialId(), authenticatedUser);
            if (domain == null) {
                throw new ServiceException.BadRequestExpception(new ErrorMessage(400, "Failed to create pipeline").add(new ErrorMessage.Error("scm.credentialId", ErrorMessage.Error.ErrorCodes.INVALID.toString(), "No domain in user credentials found for credentialId: " + scmConfig.getCredentialId())));
            }
            if (domain.test(new BlueOceanDomainRequirement())) {
                //this is blueocean specific domain
                project.addProperty(new BlueOceanCredentialsProvider.FolderPropertyImpl(authenticatedUser.getId(), scmConfig.getCredentialId(), BlueOceanCredentialsProvider.createDomain(sourceUri)));
            }
        }
        String credentialId = StringUtils.defaultString(scmConfig.getCredentialId());
        project.getSourcesList().add(new BranchSource(new GitSCMSource(null, sourceUri, credentialId, "*", "", false)));
        project.scheduleBuild(new Cause.UserIdCause());
        return new MultiBranchPipelineImpl(project);
    } else {
        try {
            // we don't know about this project type
            item.delete();
        } catch (InterruptedException e) {
            throw new ServiceException.UnexpectedErrorException("Failed to delete pipeline: " + getName());
        }
    }
    return null;
}
Also used : MultiBranchPipelineImpl(io.jenkins.blueocean.rest.impl.pipeline.MultiBranchPipelineImpl) User(hudson.model.User) BlueOceanCredentialsProvider(io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanCredentialsProvider) TopLevelItem(hudson.model.TopLevelItem) GitSCMSource(jenkins.plugins.git.GitSCMSource) BranchSource(jenkins.branch.BranchSource) WorkflowMultiBranchProject(org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject) ServiceException(io.jenkins.blueocean.commons.ServiceException) BlueOceanDomainRequirement(io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanDomainRequirement) Cause(hudson.model.Cause) ErrorMessage(io.jenkins.blueocean.commons.ErrorMessage) Domain(com.cloudbees.plugins.credentials.domains.Domain)

Example 22 with Domain

use of com.cloudbees.plugins.credentials.domains.Domain in project configuration-as-code-plugin by jenkinsci.

the class CredentialsRootConfigurator method configure.

@Override
public CredentialsStore configure(Object config) throws Exception {
    Map map = (Map) config;
    final Map<?, ?> system = (Map) map.get("system");
    final Map<Domain, List<Credentials>> target = SystemCredentialsProvider.getInstance().getDomainCredentialsMap();
    target.clear();
    final Configurator<Domain> domainConfigurator = Configurator.lookup(Domain.class);
    final Configurator<Credentials> credentialsConfigurator = Configurator.lookup(Credentials.class);
    for (Map.Entry dc : system.entrySet()) {
        final Domain domain = domainConfigurator.configure(dc.getKey());
        List values = (List) dc.getValue();
        final List<Credentials> credentials = new ArrayList<>();
        for (Object value : values) {
            credentials.add(credentialsConfigurator.configure(value));
        }
        logger.info("Setting " + target.getClass().getCanonicalName() + "#system[" + domain.toString() + "] = " + credentials);
        target.put(domain, credentials);
    }
    return null;
}
Also used : ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Domain(com.cloudbees.plugins.credentials.domains.Domain) HashMap(java.util.HashMap) Map(java.util.Map) Credentials(com.cloudbees.plugins.credentials.Credentials)

Example 23 with Domain

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

the class UserSSHKeyManager method getDomain.

private static Domain getDomain(CredentialsStore store) {
    Domain domain = store.getDomainByName(BLUEOCEAN_DOMAIN_NAME);
    if (domain == null) {
        try {
            // create new one
            boolean result = store.addDomain(new Domain(BLUEOCEAN_DOMAIN_NAME, null, null));
            if (!result) {
                throw new ServiceException.UnexpectedErrorException(String.format("Failed to create credential domain: %s", BLUEOCEAN_DOMAIN_NAME));
            }
            domain = store.getDomainByName(BLUEOCEAN_DOMAIN_NAME);
            if (domain == null) {
                throw new ServiceException.UnexpectedErrorException(String.format("Domain %s created but not found", BLUEOCEAN_DOMAIN_NAME));
            }
        } catch (IOException ex) {
            throw new ServiceException.UnexpectedErrorException("Failed to save the Blue Ocean domain.", ex);
        }
    }
    return domain;
}
Also used : ServiceException(io.jenkins.blueocean.commons.ServiceException) IOException(java.io.IOException) Domain(com.cloudbees.plugins.credentials.domains.Domain)

Example 24 with Domain

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

the class BitbucketServerScmContentProviderTest method mockMbp.

private MultiBranchProject mockMbp(String credentialId, User user) {
    MultiBranchProject mbp = mock(MultiBranchProject.class);
    when(mbp.getName()).thenReturn("pipeline1");
    when(mbp.getParent()).thenReturn(j.jenkins);
    BitbucketSCMSource scmSource = mock(BitbucketSCMSource.class);
    when(scmSource.getServerUrl()).thenReturn(apiUrl);
    when(scmSource.getCredentialsId()).thenReturn(credentialId);
    when(scmSource.getRepoOwner()).thenReturn("TESTP");
    when(scmSource.getRepository()).thenReturn("pipeline-demo-test");
    when(mbp.getSCMSources()).thenReturn(Collections.singletonList(scmSource));
    // mock blueocean credential provider stuff
    BlueOceanCredentialsProvider.FolderPropertyImpl folderProperty = mock(BlueOceanCredentialsProvider.FolderPropertyImpl.class);
    DescribableList<AbstractFolderProperty<?>, AbstractFolderPropertyDescriptor> properties = new DescribableList<>(mbp);
    properties.add(new BlueOceanCredentialsProvider.FolderPropertyImpl(user.getId(), credentialId, BlueOceanCredentialsProvider.createDomain(apiUrl)));
    Domain domain = mock(Domain.class);
    when(domain.getName()).thenReturn(BitbucketServerScm.DOMAIN_NAME);
    when(folderProperty.getDomain()).thenReturn(domain);
    when(mbp.getProperties()).thenReturn(properties);
    return mbp;
}
Also used : BlueOceanCredentialsProvider(io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanCredentialsProvider) DescribableList(hudson.util.DescribableList) MultiBranchProject(jenkins.branch.MultiBranchProject) AbstractFolderPropertyDescriptor(com.cloudbees.hudson.plugins.folder.AbstractFolderPropertyDescriptor) Domain(com.cloudbees.plugins.credentials.domains.Domain) BitbucketSCMSource(com.cloudbees.jenkins.plugins.bitbucket.BitbucketSCMSource) AbstractFolderProperty(com.cloudbees.hudson.plugins.folder.AbstractFolderProperty)

Example 25 with Domain

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

the class BitbucketCloudScmContentProviderTest method mockMbp.

private MultiBranchProject mockMbp(String credentialId, User user) {
    MultiBranchProject mbp = mock(MultiBranchProject.class);
    when(mbp.getName()).thenReturn("pipeline1");
    when(mbp.getParent()).thenReturn(j.jenkins);
    BitbucketSCMSource scmSource = mock(BitbucketSCMSource.class);
    when(scmSource.getServerUrl()).thenReturn(apiUrl);
    when(scmSource.getCredentialsId()).thenReturn(credentialId);
    when(scmSource.getRepoOwner()).thenReturn(USER_UUID);
    when(scmSource.getRepository()).thenReturn("demo1");
    when(mbp.getSCMSources()).thenReturn(Collections.singletonList(scmSource));
    // mock blueocean credential provider stuff
    BlueOceanCredentialsProvider.FolderPropertyImpl folderProperty = mock(BlueOceanCredentialsProvider.FolderPropertyImpl.class);
    DescribableList<AbstractFolderProperty<?>, AbstractFolderPropertyDescriptor> properties = new DescribableList<>(mbp);
    properties.add(new BlueOceanCredentialsProvider.FolderPropertyImpl(user.getId(), credentialId, BlueOceanCredentialsProvider.createDomain(apiUrl)));
    Domain domain = mock(Domain.class);
    when(domain.getName()).thenReturn(BitbucketCloudScm.DOMAIN_NAME);
    when(folderProperty.getDomain()).thenReturn(domain);
    when(mbp.getProperties()).thenReturn(properties);
    return mbp;
}
Also used : BlueOceanCredentialsProvider(io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanCredentialsProvider) DescribableList(hudson.util.DescribableList) MultiBranchProject(jenkins.branch.MultiBranchProject) AbstractFolderPropertyDescriptor(com.cloudbees.hudson.plugins.folder.AbstractFolderPropertyDescriptor) Domain(com.cloudbees.plugins.credentials.domains.Domain) BitbucketSCMSource(com.cloudbees.jenkins.plugins.bitbucket.BitbucketSCMSource) AbstractFolderProperty(com.cloudbees.hudson.plugins.folder.AbstractFolderProperty)

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