use of com.cloudbees.plugins.credentials.domains.Domain in project blueocean-plugin by jenkinsci.
the class CredentialApiTest method createSshCredentialUsingDefaultSshOnMaster.
@Test
public void createSshCredentialUsingDefaultSshOnMaster() throws IOException {
SystemCredentialsProvider.ProviderImpl system = ExtensionList.lookup(CredentialsProvider.class).get(SystemCredentialsProvider.ProviderImpl.class);
CredentialsStore systemStore = system.getStore(j.getInstance());
systemStore.addDomain(new Domain("domain1", null, null));
Map<String, Object> resp = post("/organizations/jenkins/credentials/system/domains/domain1/credentials/", ImmutableMap.of("credentials", new ImmutableMap.Builder<String, Object>().put("privateKeySource", ImmutableMap.of("stapler-class", "com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey$UsersPrivateKeySource")).put("passphrase", "ssh2").put("scope", "GLOBAL").put("description", "ssh2 desc").put("$class", "com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey").put("username", "ssh2").build()), 201);
Assert.assertEquals("SSH Username with private key", resp.get("typeName"));
Assert.assertEquals("domain1", resp.get("domain"));
}
use of com.cloudbees.plugins.credentials.domains.Domain in project blueocean-plugin by jenkinsci.
the class GitScmTest method shouldGetForbiddenForBadCredentialIdOnUpdate2.
@Test
public void shouldGetForbiddenForBadCredentialIdOnUpdate2() throws IOException, UnirestException {
User user = login();
String mbp = createMbp(user);
SystemCredentialsProvider.ProviderImpl system = ExtensionList.lookup(CredentialsProvider.class).get(SystemCredentialsProvider.ProviderImpl.class);
CredentialsStore systemStore = system.getStore(j.getInstance());
systemStore.addDomain(new Domain("domain1", null, null));
Map<String, Object> resp = createCredentials(user, ImmutableMap.of("credentials", new ImmutableMap.Builder<String, Object>().put("password", "abcd").put("stapler-class", "com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl").put("scope", "USER").put("domain", "blueocean-git-domain").put("description", "joe desc").put("$class", "com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl").put("username", "joe").build()));
String credentialId = (String) resp.get("id");
Assert.assertNotNull(credentialId);
put("/organizations/jenkins/pipelines/" + mbp + "/", ImmutableMap.of("name", mbp, "$class", "io.jenkins.blueocean.blueocean_git_pipeline.GitPipelineUpdateRequest", "scmConfig", ImmutableMap.of("uri", "git@github.com:vivek/capability-annotation.git", "credentialId", credentialId)), 400);
}
use of com.cloudbees.plugins.credentials.domains.Domain in project blueocean-plugin by jenkinsci.
the class GitScmTest method shouldGetForbiddenForBadCredentialIdOnUpdate1.
@Test
public void shouldGetForbiddenForBadCredentialIdOnUpdate1() throws IOException, UnirestException {
User user = login();
String mbp = createMbp(user);
SystemCredentialsProvider.ProviderImpl system = ExtensionList.lookup(CredentialsProvider.class).get(SystemCredentialsProvider.ProviderImpl.class);
CredentialsStore systemStore = system.getStore(j.getInstance());
systemStore.addDomain(new Domain("domain1", null, null));
Map resp = createCredentials(user, ImmutableMap.of("credentials", new ImmutableMap.Builder<String, Object>().put("privateKeySource", ImmutableMap.of("privateKey", "abcabc1212", "stapler-class", "com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey$DirectEntryPrivateKeySource")).put("passphrase", "ssh2").put("scope", "USER").put("domain", "blueocean-git-domain").put("description", "ssh2 desc").put("$class", "com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey").put("username", "ssh2").build()));
String credentialId = (String) resp.get("id");
Assert.assertNotNull(credentialId);
put("/organizations/jenkins/pipelines/" + mbp + "/", ImmutableMap.of("name", mbp, "$class", "io.jenkins.blueocean.blueocean_git_pipeline.GitPipelineUpdateRequest", "scmConfig", ImmutableMap.of("uri", "git@github.com:vivek/capability-annotation.git", "credentialId", credentialId)), 400);
}
use of com.cloudbees.plugins.credentials.domains.Domain in project blueocean-plugin by jenkinsci.
the class GithubPipelineCreateRequest method create.
@SuppressWarnings("unchecked")
@Override
public BluePipeline create(Reachable parent) throws IOException {
String apiUrl = null;
//default
String orgName = getName();
String credentialId = null;
StringBuilder sb = new StringBuilder();
List<String> repos = new ArrayList<>();
if (scmConfig != null) {
apiUrl = StringUtils.defaultIfBlank(scmConfig.getUri(), GithubScm.DEFAULT_API_URI);
if (scmConfig.getConfig().get("orgName") instanceof String) {
orgName = (String) scmConfig.getConfig().get("orgName");
}
credentialId = scmConfig.getCredentialId();
if (scmConfig != null && scmConfig.getConfig().get("repos") instanceof List) {
for (String r : (List<String>) scmConfig.getConfig().get("repos")) {
sb.append(String.format("(%s\\b)?", r));
repos.add(r);
}
}
}
User authenticatedUser = User.current();
if (authenticatedUser == null) {
throw new ServiceException.UnauthorizedException("Must login to create a pipeline");
}
TopLevelItem item = null;
try {
if (credentialId != null) {
validateCredentialId(credentialId, apiUrl);
}
item = create(Jenkins.getInstance(), getName(), DESCRIPTOR, CustomOrganizationFolderDescriptor.class);
if (item instanceof OrganizationFolder) {
if (credentialId != null) {
//Find domain attached to this credentialId, if present check if it's BlueOcean specific domain then
//add the properties otherwise simply use it
Domain domain = CredentialsUtils.findDomain(credentialId, authenticatedUser);
if (domain == null) {
//this should not happen since validateCredentialId found the credential
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())) {
((OrganizationFolder) item).addProperty(new BlueOceanCredentialsProvider.FolderPropertyImpl(authenticatedUser.getId(), credentialId, BlueOceanCredentialsProvider.createDomain(apiUrl)));
}
}
GitHubSCMNavigator gitHubSCMNavigator = new GitHubSCMNavigator(apiUrl, orgName, credentialId, credentialId);
if (sb.length() > 0) {
gitHubSCMNavigator.setPattern(sb.toString());
}
// cick of github scan build
OrganizationFolder organizationFolder = (OrganizationFolder) item;
organizationFolder.getNavigators().replace(gitHubSCMNavigator);
if (repos.size() == 1) {
SCMSourceEvent.fireNow(new SCMSourceEventImpl(repos.get(0), item, apiUrl, gitHubSCMNavigator));
} else {
organizationFolder.scheduleBuild(new Cause.UserIdCause());
}
return new GithubOrganizationFolder(organizationFolder, parent.getLink());
}
} catch (Exception e) {
String msg = String.format("Error creating pipeline %s: %s", getName(), e.getMessage());
logger.error(msg, e);
if (item != null) {
try {
item.delete();
} catch (InterruptedException e1) {
logger.error(String.format("Error creating pipeline %s: %s", getName(), e1.getMessage()), e1);
throw new ServiceException.UnexpectedErrorException("Error cleaning up pipeline " + getName() + " due to error: " + e.getMessage(), e);
}
}
if (e instanceof ServiceException) {
throw e;
}
throw new ServiceException.UnexpectedErrorException(msg, e);
}
return null;
}
use of com.cloudbees.plugins.credentials.domains.Domain 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