Search in sources :

Example 1 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.BadRequestExpception("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 2 with Domain

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

the class NPMRegistryValidatorTest method test_credentials_ok.

@Test
public void test_credentials_ok() throws Exception {
    String credentialsId = "secret";
    Credentials credentials = new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, credentialsId, "", "user", "password");
    Map<Domain, List<Credentials>> credentialsMap = new HashMap<>();
    credentialsMap.put(Domain.global(), Arrays.asList(credentials));
    SystemCredentialsProvider.getInstance().setDomainCredentialsMap(credentialsMap);
    FreeStyleProject prj = mock(FreeStyleProject.class);
    when(prj.hasPermission(isA(Permission.class))).thenReturn(true);
    DescriptorImpl descriptor = mock(DescriptorImpl.class);
    when(descriptor.doCheckCredentialsId(any(Item.class), (String) any(), anyString())).thenCallRealMethod();
    String serverURL = "http://acme.com";
    FormValidation result = descriptor.doCheckCredentialsId(prj, credentialsId, serverURL);
    assertThat(result.kind, is(Kind.OK));
}
Also used : Item(hudson.model.Item) FormValidation(hudson.util.FormValidation) HashMap(java.util.HashMap) DescriptorImpl(jenkins.plugins.nodejs.configfiles.NPMRegistry.DescriptorImpl) Permission(hudson.security.Permission) List(java.util.List) UsernamePasswordCredentialsImpl(com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl) Domain(com.cloudbees.plugins.credentials.domains.Domain) FreeStyleProject(hudson.model.FreeStyleProject) Credentials(com.cloudbees.plugins.credentials.Credentials) Test(org.junit.Test)

Example 3 with Domain

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

the class GithubMockBase method mockMbp.

protected MultiBranchProject mockMbp(String credentialId, User user, String credentialDomainName) {
    MultiBranchProject mbp = mock(MultiBranchProject.class);
    when(mbp.getName()).thenReturn("PR-demo");
    when(mbp.getParent()).thenReturn(j.jenkins);
    GitHubSCMSource scmSource = mock(GitHubSCMSource.class);
    when(scmSource.getApiUri()).thenReturn(githubApiUrl);
    when(scmSource.getCredentialsId()).thenReturn(credentialId);
    when(scmSource.getRepoOwner()).thenReturn("cloudbeers");
    when(scmSource.getRepository()).thenReturn("PR-demo");
    when(mbp.getSCMSources()).thenReturn(Collections.singletonList(scmSource));
    BlueOceanCredentialsProvider.FolderPropertyImpl folderProperty = mock(BlueOceanCredentialsProvider.FolderPropertyImpl.class);
    DescribableList<AbstractFolderProperty<?>, AbstractFolderPropertyDescriptor> mbpProperties = new DescribableList<>(mbp);
    mbpProperties.add(new BlueOceanCredentialsProvider.FolderPropertyImpl(user.getId(), credentialId, BlueOceanCredentialsProvider.createDomain(githubApiUrl)));
    Domain domain = mock(Domain.class);
    when(domain.getName()).thenReturn(credentialDomainName);
    when(folderProperty.getDomain()).thenReturn(domain);
    when(mbp.getProperties()).thenReturn(mbpProperties);
    return mbp;
}
Also used : BlueOceanCredentialsProvider(io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanCredentialsProvider) DescribableList(hudson.util.DescribableList) MultiBranchProject(jenkins.branch.MultiBranchProject) GitHubSCMSource(org.jenkinsci.plugins.github_branch_source.GitHubSCMSource) AbstractFolderPropertyDescriptor(com.cloudbees.hudson.plugins.folder.AbstractFolderPropertyDescriptor) Domain(com.cloudbees.plugins.credentials.domains.Domain) AbstractFolderProperty(com.cloudbees.hudson.plugins.folder.AbstractFolderProperty)

Example 4 with Domain

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

the class GithubEnterpriseApiTest method validateGithubToken.

@Test
public void validateGithubToken() throws IOException, UnirestException {
    String credentialId = createGithubEnterpriseCredential();
    assertEquals(GithubCredentialUtils.computeCredentialId(null, GithubEnterpriseScm.ID, githubApiUrl), credentialId);
    // check if this credentialId is created in correct user domain
    Domain domain = CredentialsUtils.findDomain(credentialId, user);
    assertEquals("blueocean-github-enterprise-domain", domain.getName());
}
Also used : Domain(com.cloudbees.plugins.credentials.domains.Domain) Test(org.junit.Test)

Example 5 with Domain

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

the class GithubApiTest method validateGithubToken.

@Test
public void validateGithubToken() throws IOException, UnirestException {
    // check credentialId of this SCM, should be null
    createGithubCredential(user);
    // check if this credentialId is created in correct user domain
    Domain domain = CredentialsUtils.findDomain("github", user);
    assertEquals("blueocean-github-domain", domain.getName());
    // now that there is github credentials setup, calling scm api to get credential should simply return that.
    Map r = new RequestBuilder(baseUrl).status(200).jwtToken(getJwtToken(j.jenkins, user.getId(), user.getId())).get("/organizations/jenkins/scm/github/?apiUrl=" + githubApiUrl).build(Map.class);
    assertEquals("github", r.get("credentialId"));
    assertEquals("github", r.get("id"));
    // now try validating again, it should return the same credentialId
    r = new RequestBuilder(baseUrl).data(MapsHelper.of("accessToken", accessToken)).status(200).jwtToken(getJwtToken(j.jenkins, user.getId(), user.getId())).put("/organizations/jenkins/scm/github/validate/?apiUrl=" + githubApiUrl).build(Map.class);
    assertEquals("github", r.get("credentialId"));
}
Also used : Domain(com.cloudbees.plugins.credentials.domains.Domain) Map(java.util.Map) 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