use of com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl in project nodejs-plugin by jenkinsci.
the class RegistryHelperTest method setUp.
@Before
public void setUp() throws Exception {
user = new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, "privateId", "dummy desc", "myuser", "mypassword");
CredentialsStore store = CredentialsProvider.lookupStores(j.getInstance()).iterator().next();
store.addCredentials(Domain.global(), user);
}
use of com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl in project pipeline-aws-plugin by jenkinsci.
the class S3UploadStepIntegrationTest method smokes.
@Issue("JENKINS-49025")
@Test
public void smokes() throws Exception {
String globalCredentialsId = "x";
StandardUsernamePasswordCredentials key = new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, globalCredentialsId, "x", "x", "x");
SystemCredentialsProvider.getInstance().getCredentials().add(key);
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition("node('" + r.createSlave().getNodeName() + "') {\n" + " withAWS (credentials: '" + globalCredentialsId + "') {\n" + " writeFile file: 'x', text: ''\n" + " try {\n" + " s3Upload bucket: 'x', file: 'x', path: 'x'\n" + " fail 'should not have worked'\n" + " } catch (com.amazonaws.services.s3.model.AmazonS3Exception x) {\n" + " echo(/got $x as expected/)\n" + " }\n" + " }\n" + "}\n", true));
r.assertBuildStatusSuccess(p.scheduleBuild2(0));
}
use of com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl in project blueocean-plugin by jenkinsci.
the class GitScm method createPWCredentials.
private void createPWCredentials(String credentialId, User user, @JsonBody JSONObject request, String repositoryUrl) {
StandardUsernamePasswordCredentials existingCredential = CredentialsUtils.findCredential(credentialId, StandardUsernamePasswordCredentials.class, new BlueOceanDomainRequirement());
String requestUsername = request.getString("userName");
String requestPassword = request.getString("password");
// Un-normalized repositoryUrl so the description matches user input.
String description = String.format("%s for %s", CREDENTIAL_DESCRIPTION_PW, repositoryUrl);
final StandardUsernamePasswordCredentials newCredential = new UsernamePasswordCredentialsImpl(CredentialsScope.USER, credentialId, description, requestUsername, requestPassword);
try {
if (existingCredential == null) {
CredentialsUtils.createCredentialsInUserStore(newCredential, user, CREDENTIAL_DOMAIN_NAME, Collections.singletonList(new BlueOceanDomainSpecification()));
} else {
CredentialsUtils.updateCredentialsInUserStore(existingCredential, newCredential, user, CREDENTIAL_DOMAIN_NAME, Collections.singletonList(new BlueOceanDomainSpecification()));
}
} catch (IOException e) {
throw new ServiceException.UnexpectedErrorException("Could not persist credential", e);
}
}
use of com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl 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());
}
use of com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl in project blueocean-plugin by jenkinsci.
the class GithubScmTest method mockCredentials.
void mockCredentials(String userId, String accessToken, String credentialId, String domainName) throws Exception {
// Mock Credentials
UsernamePasswordCredentialsImpl credentials = mock(UsernamePasswordCredentialsImpl.class);
whenNew(UsernamePasswordCredentialsImpl.class).withAnyArguments().thenReturn(credentials);
when(credentials.getId()).thenReturn(credentialId);
when(credentials.getUsername()).thenReturn(userId);
Secret secret = mock(Secret.class);
when(secret.getPlainText()).thenReturn(accessToken);
when(credentials.getPassword()).thenReturn(secret);
CredentialsMatcher credentialsMatcher = mock(CredentialsMatcher.class);
mockStatic(CredentialsMatchers.class);
mockStatic(CredentialsProvider.class);
when(CredentialsMatchers.withId(credentialId)).thenReturn(credentialsMatcher);
BlueOceanDomainRequirement blueOceanDomainRequirement = mock(BlueOceanDomainRequirement.class);
whenNew(BlueOceanDomainRequirement.class).withNoArguments().thenReturn(blueOceanDomainRequirement);
when(CredentialsProvider.class, "lookupCredentials", StandardUsernamePasswordCredentials.class, jenkins, authentication, blueOceanDomainRequirement).thenReturn(Collections.singletonList(credentials));
when(CredentialsMatchers.class, "firstOrNull", Collections.singletonList(credentials), credentialsMatcher).thenReturn(credentials);
when(CredentialsMatchers.allOf(credentialsMatcher)).thenReturn(credentialsMatcher);
// Mock credentials Domain
Domain domain = mock(Domain.class);
when(domain.getName()).thenReturn(domainName);
// Mock credentials Store
CredentialsStore credentialsStore = mock(CredentialsStore.class);
when(credentialsStore.hasPermission(CredentialsProvider.CREATE)).thenReturn(true);
when(credentialsStore.hasPermission(CredentialsProvider.UPDATE)).thenReturn(true);
when(credentialsStore.getDomainByName(domainName)).thenReturn(domain);
when(CredentialsProvider.class, "lookupStores", user).thenReturn(Collections.singletonList(credentialsStore));
when(credentialsStore.addCredentials(domain, credentials)).thenReturn(true);
when(credentialsStore.updateCredentials(domain, credentials, credentials)).thenReturn(true);
}
Aggregations