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.BadRequestException("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;
}
use of com.cloudbees.plugins.credentials.domains.Domain in project blueocean-plugin by jenkinsci.
the class BlueOceanCredentialsProvider method getCredentials.
@Nonnull
public <C extends Credentials> List<C> getCredentials(@Nonnull final Class<C> type, @Nullable ItemGroup itemGroup, @Nullable Authentication authentication, @Nonnull List<DomainRequirement> domainRequirements) {
final List<C> result = new ArrayList<>();
final FolderPropertyImpl prop = propertyOf(itemGroup);
if (prop != null && prop.domain.test(domainRequirements)) {
final User proxyUser = User.get(prop.getUser(), false, Collections.emptyMap());
if (proxyUser != null) {
try (ACLContext ignored = ACL.as(proxyUser.impersonate())) {
for (CredentialsStore s : CredentialsProvider.lookupStores(proxyUser)) {
for (Domain d : s.getDomains()) {
if (d.test(PROXY_REQUIREMENT)) {
for (Credentials c : filter(s.getCredentials(d), withId(prop.getId()))) {
if (type.isInstance(c)) {
result.add((C) c);
}
}
}
}
}
} catch (UsernameNotFoundException ex) {
logger.warn("BlueOceanCredentialsProvider#getCredentials(): Username attached to credentials can not be found");
}
}
}
return result;
}
use of com.cloudbees.plugins.credentials.domains.Domain in project blueocean-plugin by jenkinsci.
the class CredentialsUtils method updateCredentialsInUserStore.
public static void updateCredentialsInUserStore(@Nonnull Credentials current, @Nonnull Credentials replacement, @Nonnull User user, @Nonnull String domainName, @Nonnull List<DomainSpecification> domainSpecifications) throws IOException {
CredentialsStore store = findUserStoreFirstOrNull(user);
if (store == null) {
throw new ServiceException.ForbiddenException(String.format("Logged in user: %s doesn't have writable credentials store", user.getId()));
}
Domain domain = findOrCreateDomain(store, domainName, domainSpecifications);
if (!store.updateCredentials(domain, current, replacement)) {
throw new ServiceException.UnexpectedErrorException("Failed to update credential to domain");
}
}
use of com.cloudbees.plugins.credentials.domains.Domain in project blueocean-plugin by jenkinsci.
the class CredentialsUtils method createCredentialsInUserStore.
public static void createCredentialsInUserStore(@Nonnull Credentials credential, @Nonnull User user, @Nonnull String domainName, @Nonnull List<DomainSpecification> domainSpecifications) throws IOException {
CredentialsStore store = findUserStoreFirstOrNull(user);
if (store == null) {
throw new ServiceException.ForbiddenException(String.format("Logged in user: %s doesn't have writable credentials store", user.getId()));
}
Domain domain = findOrCreateDomain(store, domainName, domainSpecifications);
if (!store.addCredentials(domain, credential)) {
throw new ServiceException.UnexpectedErrorException("Failed to add credential to domain");
}
}
use of com.cloudbees.plugins.credentials.domains.Domain in project blueocean-plugin by jenkinsci.
the class CredentialApiTest method createSshCredentialUsingSshFileOnMaster.
@Test
public void createSshCredentialUsingSshFileOnMaster() 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("privateKeyFile", "~/.ssh/blah", "stapler-class", "com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey$FileOnMasterPrivateKeySource")).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"));
}
Aggregations