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);
}
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));
}
}
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;
}
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());
}
Aggregations