Search in sources :

Example 1 with StandardCredentials

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

the class GithubScmContentProvider method saveContent.

@SuppressWarnings("unchecked")
private Object saveContent(@Nonnull GithubScmSaveFileRequest githubRequest, @Nonnull Item item) {
    String apiUrl = GithubScm.DEFAULT_API_URI;
    String owner = null;
    String repo = null;
    String accessToken = null;
    String credentialId = null;
    if (item instanceof OrganizationFolder) {
        List<SCMNavigator> navigators = ((OrganizationFolder) item).getSCMNavigators();
        if (!navigators.isEmpty() && navigators.get(0) instanceof GitHubSCMNavigator) {
            GitHubSCMNavigator navigator = (GitHubSCMNavigator) navigators.get(0);
            if (navigator.getApiUri() != null) {
                apiUrl = navigator.getApiUri();
            }
            credentialId = navigator.getScanCredentialsId();
            owner = navigator.getRepoOwner();
        }
    } else if (item instanceof MultiBranchProject) {
        List<SCMSource> sources = ((MultiBranchProject) item).getSCMSources();
        if (!sources.isEmpty() && sources.get(0) instanceof GitHubSCMSource) {
            GitHubSCMSource source = (GitHubSCMSource) sources.get(0);
            if (source.getApiUri() != null) {
                apiUrl = source.getApiUri();
            }
            credentialId = source.getScanCredentialsId();
            owner = owner(source);
            repo = repo(source);
        }
    }
    if (credentialId != null) {
        StandardCredentials credentials = Connector.lookupScanCredentials((SCMSourceOwner) item, apiUrl, credentialId);
        if (credentials instanceof StandardUsernamePasswordCredentials) {
            accessToken = ((StandardUsernamePasswordCredentials) credentials).getPassword().getPlainText();
        } else {
            throw new ServiceException.BadRequestExpception("accessToken not found in pipeline: " + item.getFullName());
        }
    }
    return githubRequest.save(apiUrl, owner, repo, accessToken);
}
Also used : StandardUsernamePasswordCredentials(com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials) SCMNavigator(jenkins.scm.api.SCMNavigator) GitHubSCMNavigator(org.jenkinsci.plugins.github_branch_source.GitHubSCMNavigator) OrganizationFolder(jenkins.branch.OrganizationFolder) MultiBranchProject(jenkins.branch.MultiBranchProject) ArrayList(java.util.ArrayList) List(java.util.List) GitHubSCMSource(org.jenkinsci.plugins.github_branch_source.GitHubSCMSource) GitHubSCMNavigator(org.jenkinsci.plugins.github_branch_source.GitHubSCMNavigator) StandardCredentials(com.cloudbees.plugins.credentials.common.StandardCredentials)

Example 2 with StandardCredentials

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

the class GitPipelineCreateRequest method validate.

private void validate(String name, BlueScmConfig scmConfig) {
    if (scmConfig == null) {
        throw new ServiceException.BadRequestExpception(new ErrorMessage(400, "Failed to create Git pipeline").add(new ErrorMessage.Error("scmConfig", ErrorMessage.Error.ErrorCodes.MISSING.toString(), "scmConfig is required")));
    }
    List<ErrorMessage.Error> errors = new ArrayList<>();
    String sourceUri = scmConfig.getUri();
    if (sourceUri == null) {
        errors.add(new ErrorMessage.Error("scmConfig.uri", ErrorMessage.Error.ErrorCodes.MISSING.toString(), "uri is required"));
    } else {
        StandardCredentials credentials = null;
        if (scmConfig.getCredentialId() != null) {
            credentials = GitUtils.getCredentials(Jenkins.getInstance(), sourceUri, scmConfig.getCredentialId());
            if (credentials == null) {
                errors.add(new ErrorMessage.Error("scmConfig.credentialId", ErrorMessage.Error.ErrorCodes.NOT_FOUND.toString(), String.format("credentialId: %s not found", scmConfig.getCredentialId())));
            }
        }
        //validate credentials if no credential id (perhaps git repo doesn't need auth or credentials is present)
        if (scmConfig.getCredentialId() == null || credentials != null) {
            errors.addAll(GitUtils.validateCredentials(sourceUri, credentials));
        }
    }
    try {
        Jenkins.getInstance().getProjectNamingStrategy().checkName(getName());
    } catch (Failure f) {
        errors.add(new ErrorMessage.Error("scmConfig.name", ErrorMessage.Error.ErrorCodes.INVALID.toString(), name + "in not a valid name"));
    }
    if (Jenkins.getInstance().getItem(name) != null) {
        errors.add(new ErrorMessage.Error("name", ErrorMessage.Error.ErrorCodes.ALREADY_EXISTS.toString(), name + " already exists"));
    }
    if (!errors.isEmpty()) {
        throw new ServiceException.BadRequestExpception(new ErrorMessage(400, "Failed to create Git pipeline:" + name).addAll(errors));
    }
}
Also used : ArrayList(java.util.ArrayList) ErrorMessage(io.jenkins.blueocean.commons.ErrorMessage) StandardCredentials(com.cloudbees.plugins.credentials.common.StandardCredentials) Failure(hudson.model.Failure)

Example 3 with StandardCredentials

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

the class GitPipelineUpdateRequest method getGitScmSource.

@SuppressWarnings("unchecked")
private BranchSource getGitScmSource(MultiBranchProject mbp) {
    String sourceUri = null;
    String credentialId = null;
    if (scmConfig != null) {
        sourceUri = scmConfig.getUri();
        List<ErrorMessage.Error> errors = new ArrayList<>();
        StandardCredentials credentials = null;
        if (scmConfig.getCredentialId() != null) {
            credentials = GitUtils.getCredentials(Jenkins.getInstance(), sourceUri, scmConfig.getCredentialId());
            if (credentials == null) {
                errors.add(new ErrorMessage.Error("scmConfig.credentialId", ErrorMessage.Error.ErrorCodes.NOT_FOUND.toString(), String.format("credentialId: %s not found", scmConfig.getCredentialId())));
            }
        }
        if (sourceUri != null) {
            errors.addAll(GitUtils.validateCredentials(sourceUri, credentials));
        }
        credentialId = scmConfig.getCredentialId() == null ? "" : scmConfig.getCredentialId();
        if (!errors.isEmpty()) {
            throw new ServiceException.BadRequestExpception(new ErrorMessage(400, "Failed to create Git pipeline").addAll(errors));
        }
    }
    PersistedList<BranchSource> sources = mbp.getSourcesList();
    for (BranchSource source : sources) {
        if (source.getSource() instanceof GitSCMSource) {
            GitSCMSource gitSCMSource = (GitSCMSource) source.getSource();
            String remote = gitSCMSource.getRemote();
            if (sourceUri != null && !sourceUri.equals(gitSCMSource.getRemote())) {
                remote = sourceUri;
            }
            String cred = gitSCMSource.getCredentialsId();
            if (!gitSCMSource.getCredentialsId().equals(credentialId)) {
                cred = credentialId;
            }
            GitSCMSource s = new GitSCMSource(gitSCMSource.getId(), remote, cred, gitSCMSource.getIncludes(), gitSCMSource.getExcludes(), gitSCMSource.isIgnoreOnPushNotifications());
            s.setOwner(mbp);
            return new BranchSource(s);
        }
    }
    if (sourceUri != null) {
        //if no scm sources in this MBP project, add a new one using passed sourceUri
        return new BranchSource(new GitSCMSource(null, sourceUri, credentialId, "*", "", false));
    }
    return null;
}
Also used : ArrayList(java.util.ArrayList) GitSCMSource(jenkins.plugins.git.GitSCMSource) ErrorMessage(io.jenkins.blueocean.commons.ErrorMessage) BranchSource(jenkins.branch.BranchSource) StandardCredentials(com.cloudbees.plugins.credentials.common.StandardCredentials)

Example 4 with StandardCredentials

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

Aggregations

StandardCredentials (com.cloudbees.plugins.credentials.common.StandardCredentials)4 ArrayList (java.util.ArrayList)3 StandardUsernamePasswordCredentials (com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials)2 ErrorMessage (io.jenkins.blueocean.commons.ErrorMessage)2 OrganizationFolder (jenkins.branch.OrganizationFolder)2 AbstractFolderProperty (com.cloudbees.hudson.plugins.folder.AbstractFolderProperty)1 CredentialsStore (com.cloudbees.plugins.credentials.CredentialsStore)1 Domain (com.cloudbees.plugins.credentials.domains.Domain)1 DomainSpecification (com.cloudbees.plugins.credentials.domains.DomainSpecification)1 UsernamePasswordCredentialsImpl (com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl)1 Failure (hudson.model.Failure)1 User (hudson.model.User)1 PipelineBaseTest (io.jenkins.blueocean.rest.impl.pipeline.PipelineBaseTest)1 BlueOceanDomainRequirement (io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanDomainRequirement)1 BlueOceanDomainSpecification (io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanDomainSpecification)1 List (java.util.List)1 BranchSource (jenkins.branch.BranchSource)1 MultiBranchProject (jenkins.branch.MultiBranchProject)1 GitSCMSource (jenkins.plugins.git.GitSCMSource)1 SCMNavigator (jenkins.scm.api.SCMNavigator)1