Search in sources :

Example 1 with DomainSpecification

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

the class CredentialsUtils method generateDomainSpecifications.

public static List<DomainSpecification> generateDomainSpecifications(@Nullable String uriStr) {
    if (StringUtils.isBlank(uriStr)) {
        return Collections.emptyList();
    }
    List<DomainSpecification> domainSpecifications = new ArrayList<>();
    try {
        URI uri = new URI(uriStr);
        // XXX: UriRequirementBuilder.fromUri() maps "" path to "/", so need to take care of it here
        String path = uri.getRawPath() == null ? null : (uri.getRawPath().trim().isEmpty() ? "/" : uri.getRawPath());
        domainSpecifications.add(new PathSpecification(path, "", false));
        if (uri.getPort() != -1) {
            domainSpecifications.add(new HostnamePortSpecification(uri.getHost() + ":" + uri.getPort(), null));
        } else {
            domainSpecifications.add(new HostnameSpecification(uri.getHost(), null));
        }
        domainSpecifications.add(new SchemeSpecification(uri.getScheme()));
    } catch (URISyntaxException e) {
        //       for now, we are returning empty list to match with  URIRequirementBuilder.fromUri()
        return domainSpecifications;
    }
    return Collections.emptyList();
}
Also used : SchemeSpecification(com.cloudbees.plugins.credentials.domains.SchemeSpecification) HostnamePortSpecification(com.cloudbees.plugins.credentials.domains.HostnamePortSpecification) PathSpecification(com.cloudbees.plugins.credentials.domains.PathSpecification) ArrayList(java.util.ArrayList) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) HostnameSpecification(com.cloudbees.plugins.credentials.domains.HostnameSpecification) DomainSpecification(com.cloudbees.plugins.credentials.domains.DomainSpecification)

Example 2 with DomainSpecification

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

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

the class GithubPipelineCreateRequestTest 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.get())) {
        s.addCredentials(Domain.global(), new UsernamePasswordCredentialsImpl(CredentialsScope.USER, "github", "System GitHub Access Token", user.getId(), "12345"));
    }
    WorkflowMultiBranchProject mp = j.jenkins.createProject(WorkflowMultiBranchProject.class, "demo");
    AbstractFolderProperty prop = new BlueOceanCredentialsProvider.FolderPropertyImpl(user.getId(), credential.getId(), BlueOceanCredentialsProvider.createDomain("https://api.github.com"));
    mp.addProperty(prop);
    // lookup for created credential id in system store, it should resolve to previously created user store credential
    StandardCredentials c = Connector.lookupScanCredentials((Item) mp, "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
    mp.getProperties().remove(prop);
    // it must resolve to system credential
    c = Connector.lookupScanCredentials((Item) mp, null, credential.getId());
    assertEquals("System GitHub Access Token", c.getDescription());
}
Also used : User(hudson.model.User) BlueOceanDomainSpecification(io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanDomainSpecification) UsernamePasswordCredentialsImpl(com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl) DomainSpecification(com.cloudbees.plugins.credentials.domains.DomainSpecification) BlueOceanDomainSpecification(io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanDomainSpecification) AbstractFolderProperty(com.cloudbees.hudson.plugins.folder.AbstractFolderProperty) StandardUsernamePasswordCredentials(com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials) WorkflowMultiBranchProject(org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject) Item(hudson.model.Item) BlueOceanDomainRequirement(io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanDomainRequirement) CredentialsStore(com.cloudbees.plugins.credentials.CredentialsStore) Domain(com.cloudbees.plugins.credentials.domains.Domain) StandardCredentials(com.cloudbees.plugins.credentials.common.StandardCredentials) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PipelineBaseTest(io.jenkins.blueocean.rest.impl.pipeline.PipelineBaseTest) Test(org.junit.Test)

Example 4 with DomainSpecification

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

the class CredentialsUtils method generateDomainSpecifications.

public static List<DomainSpecification> generateDomainSpecifications(@Nullable String uriStr) {
    if (StringUtils.isBlank(uriStr)) {
        return Collections.emptyList();
    }
    List<DomainSpecification> domainSpecifications = new ArrayList<>();
    try {
        URI uri = new URI(uriStr);
        // XXX: UriRequirementBuilder.fromUri() maps "" path to "/", so need to take care of it here
        String path = uri.getRawPath() == null ? null : (uri.getRawPath().trim().isEmpty() ? "/" : uri.getRawPath());
        domainSpecifications.add(new PathSpecification(path, "", false));
        if (uri.getPort() != -1) {
            domainSpecifications.add(new HostnamePortSpecification(uri.getHost() + ":" + uri.getPort(), null));
        } else {
            domainSpecifications.add(new HostnameSpecification(uri.getHost(), null));
        }
        domainSpecifications.add(new SchemeSpecification(uri.getScheme()));
    } catch (URISyntaxException e) {
        // for now, we are returning empty list to match with  URIRequirementBuilder.fromUri()
        return domainSpecifications;
    }
    return Collections.emptyList();
}
Also used : SchemeSpecification(com.cloudbees.plugins.credentials.domains.SchemeSpecification) HostnamePortSpecification(com.cloudbees.plugins.credentials.domains.HostnamePortSpecification) PathSpecification(com.cloudbees.plugins.credentials.domains.PathSpecification) ArrayList(java.util.ArrayList) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) HostnameSpecification(com.cloudbees.plugins.credentials.domains.HostnameSpecification) DomainSpecification(com.cloudbees.plugins.credentials.domains.DomainSpecification)

Aggregations

DomainSpecification (com.cloudbees.plugins.credentials.domains.DomainSpecification)4 AbstractFolderProperty (com.cloudbees.hudson.plugins.folder.AbstractFolderProperty)2 CredentialsStore (com.cloudbees.plugins.credentials.CredentialsStore)2 StandardCredentials (com.cloudbees.plugins.credentials.common.StandardCredentials)2 StandardUsernamePasswordCredentials (com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials)2 Domain (com.cloudbees.plugins.credentials.domains.Domain)2 HostnamePortSpecification (com.cloudbees.plugins.credentials.domains.HostnamePortSpecification)2 HostnameSpecification (com.cloudbees.plugins.credentials.domains.HostnameSpecification)2 PathSpecification (com.cloudbees.plugins.credentials.domains.PathSpecification)2 SchemeSpecification (com.cloudbees.plugins.credentials.domains.SchemeSpecification)2 UsernamePasswordCredentialsImpl (com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl)2 User (hudson.model.User)2 PipelineBaseTest (io.jenkins.blueocean.rest.impl.pipeline.PipelineBaseTest)2 BlueOceanDomainRequirement (io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanDomainRequirement)2 BlueOceanDomainSpecification (io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanDomainSpecification)2 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 Item (hudson.model.Item)1