use of com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl in project workflow-cps-plugin by jenkinsci.
the class ArgumentsActionImplTest method testBasicCredentials.
@Test
public void testBasicCredentials() throws Exception {
String username = "bob";
String password = "s3cr3t";
UsernamePasswordCredentialsImpl c = new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, "test", "sample", username, password);
CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), c);
WorkflowJob job = r.jenkins.createProject(WorkflowJob.class, "credentialed");
job.setDefinition(new CpsFlowDefinition("node{ withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'test',\n" + " usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) {\n" + " //available as an env variable, but will be masked if you try to print it out any which way\n" + " echo \"$PASSWORD'\" \n" + " echo \"${env.USERNAME}\"\n" + " echo \"bob\"\n" + "} }\n" + "withCredentials([usernamePassword(credentialsId: 'test', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {\n" + " echo \"${env.USERNAME} ${env.PASSWORD}\"\n" + "}"));
WorkflowRun run = job.scheduleBuild2(0).getStartCondition().get();
r.waitForCompletion(run);
FlowExecution exec = run.getExecution();
String log = r.getLog(run);
ForkScanner scanner = new ForkScanner();
List<FlowNode> filtered = scanner.filteredNodes(exec, new DescriptorMatchPredicate(BindingStep.DescriptorImpl.class));
// Check the binding step is OK
Assert.assertEquals(8, filtered.size());
FlowNode node = Collections2.filter(filtered, FlowScanningUtils.hasActionPredicate(ArgumentsActionImpl.class)).iterator().next();
ArgumentsActionImpl act = node.getPersistentAction(ArgumentsActionImpl.class);
Assert.assertNotNull(act.getArgumentValue("bindings"));
Assert.assertNotNull(act.getArguments().get("bindings"));
// Test that masking really does mask bound credentials appropriately
filtered = scanner.filteredNodes(exec, new DescriptorMatchPredicate(EchoStep.DescriptorImpl.class));
for (FlowNode f : filtered) {
act = f.getPersistentAction(ArgumentsActionImpl.class);
Assert.assertEquals(ArgumentsAction.NotStoredReason.MASKED_VALUE, act.getArguments().get("message"));
Assert.assertNull(ArgumentsAction.getStepArgumentsAsString(f));
}
List<FlowNode> allStepped = scanner.filteredNodes(run.getExecution().getCurrentHeads(), FlowScanningUtils.hasActionPredicate(ArgumentsActionImpl.class));
// One ArgumentsActionImpl per block or atomic step
Assert.assertEquals(6, allStepped.size());
testDeserialize(exec);
}
use of com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl in project pipeline-aws-plugin by jenkinsci.
the class WithAWSStepTest method testStepWithGlobalCredentials.
@Test
public void testStepWithGlobalCredentials() throws Exception {
String globalCredentialsId = "global-aws-creds";
List<String> credentialIds = new ArrayList<>();
credentialIds.add(globalCredentialsId);
StandardUsernamePasswordCredentials key = new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, globalCredentialsId, "test-global-creds", "global-aws-access-key-id", "global-aws-secret-access-key");
SystemCredentialsProvider.getInstance().getCredentials().add(key);
SystemCredentialsProvider.getInstance().save();
WorkflowJob job = jenkinsRule.jenkins.createProject(WorkflowJob.class, "testStepWithGlobalCredentials");
job.setDefinition(new CpsFlowDefinition("" + "node {\n" + " withAWS (credentials: '" + globalCredentialsId + "') {\n" + " echo 'It works!'\n" + " }\n" + "}\n", true));
jenkinsRule.assertBuildStatusSuccess(job.scheduleBuild2(0));
}
use of com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl in project pipeline-aws-plugin by jenkinsci.
the class WithAWSStepTest method testListCredentials.
@Test
public void testListCredentials() throws Exception {
Folder folder = jenkinsRule.jenkins.createProject(Folder.class, "folder" + jenkinsRule.jenkins.getItems().size());
CredentialsStore folderStore = this.getFolderStore(folder);
StandardUsernamePasswordCredentials folderCredentials = new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, "folder-creds", "test-creds", "aws-access-key-id", "aws-secret-access-key");
StandardUsernamePasswordCredentials globalCredentials = new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, "global-creds", "test-creds", "aws-access-key-id", "aws-secret-access-key");
folderStore.addCredentials(Domain.global(), folderCredentials);
SystemCredentialsProvider.getInstance().getCredentials().add(globalCredentials);
SystemCredentialsProvider.getInstance().save();
WorkflowJob job = folder.createProject(WorkflowJob.class, "testStepWithFolderCredentials");
final WithAWSStep.DescriptorImpl descriptor = jenkinsRule.jenkins.getDescriptorByType(WithAWSStep.DescriptorImpl.class);
// 3 options: Root credentials, folder credentials and "none"
ListBoxModel list = descriptor.doFillCredentialsItems(job);
Assert.assertEquals(3, list.size());
StandardUsernamePasswordCredentials systemCredentials = new UsernamePasswordCredentialsImpl(CredentialsScope.SYSTEM, "system-creds", "test-creds", "aws-access-key-id", "aws-secret-access-key");
SystemCredentialsProvider.getInstance().getCredentials().add(systemCredentials);
// Still 3 options: Root credentials, folder credentials and "none"
list = descriptor.doFillCredentialsItems(job);
Assert.assertEquals(3, list.size());
}
use of com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl in project pipeline-aws-plugin by jenkinsci.
the class WithAWSStepTest method testStepWithFolderCredentials.
@Test
public void testStepWithFolderCredentials() throws Exception {
String folderCredentialsId = "folders-aws-creds";
// Create a folder with credentials in its store
Folder folder = jenkinsRule.jenkins.createProject(Folder.class, "folder" + jenkinsRule.jenkins.getItems().size());
CredentialsStore folderStore = this.getFolderStore(folder);
StandardUsernamePasswordCredentials inFolderCredentials = new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, folderCredentialsId, "test-folder-creds", "folder-aws-access-key-id", "folder-aws-secret-access-key");
folderStore.addCredentials(Domain.global(), inFolderCredentials);
SystemCredentialsProvider.getInstance().save();
List<String> credentialIds = new ArrayList<>();
credentialIds.add(folderCredentialsId);
WorkflowJob job = folder.createProject(WorkflowJob.class, "testStepWithFolderCredentials");
job.setDefinition(new CpsFlowDefinition("" + "node {\n" + " withAWS (credentials: '" + folderCredentialsId + "') {\n" + " echo 'It works!'\n" + " }\n" + "}\n", true));
jenkinsRule.assertBuildStatusSuccess(job.scheduleBuild2(0));
}
use of com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl in project nodejs-plugin by jenkinsci.
the class NPMRegistryValidatorTest method test_credentials_ok.
@Test
public void test_credentials_ok() throws Exception {
String credentialsId = "secret";
Credentials credentials = new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, credentialsId, "", "user", "password");
Map<Domain, List<Credentials>> credentialsMap = new HashMap<>();
credentialsMap.put(Domain.global(), Arrays.asList(credentials));
SystemCredentialsProvider.getInstance().setDomainCredentialsMap(credentialsMap);
FreeStyleProject prj = mock(FreeStyleProject.class);
when(prj.hasPermission(isA(Permission.class))).thenReturn(true);
DescriptorImpl descriptor = mock(DescriptorImpl.class);
when(descriptor.doCheckCredentialsId(any(Item.class), (String) any(), anyString())).thenCallRealMethod();
String serverURL = "http://acme.com";
FormValidation result = descriptor.doCheckCredentialsId(prj, credentialsId, serverURL);
assertThat(result.kind, is(Kind.OK));
}
Aggregations