Skip to content

Commit 4b4f8b4

Browse files
committed
JENKINS-30515 Adding messages for credentials use: empty, found and not found
1 parent 33a9343 commit 4b4f8b4

File tree

2 files changed

+151
-1
lines changed

2 files changed

+151
-1
lines changed

src/main/java/hudson/plugins/git/GitSCM.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113
import java.util.regex.Matcher;
114114
import java.util.regex.Pattern;
115115

116+
import static java.lang.String.format;
116117
import static org.apache.commons.collections.CollectionUtils.isEmpty;
117118
import static org.apache.commons.lang.StringUtils.isBlank;
118119

@@ -825,7 +826,9 @@ public GitClient createClient(TaskListener listener, EnvVars environment, Run<?,
825826

826827
for (UserRemoteConfig uc : getUserRemoteConfigs()) {
827828
String ucCredentialsId = uc.getCredentialsId();
828-
if (ucCredentialsId != null) {
829+
if (ucCredentialsId == null) {
830+
listener.getLogger().println("No credentials specified");
831+
} else {
829832
String url = getParameterString(uc.getUrl(), environment);
830833
List<StandardUsernameCredentials> urlCredentials = CredentialsProvider.lookupCredentials(
831834
StandardUsernameCredentials.class,
@@ -840,9 +843,12 @@ public GitClient createClient(TaskListener listener, EnvVars environment, Run<?,
840843
StandardUsernameCredentials credentials = CredentialsMatchers.firstOrNull(urlCredentials, idMatcher);
841844
if (credentials != null) {
842845
c.addCredentials(url, credentials);
846+
listener.getLogger().println(format("using credential %s", credentials.getId()));
843847
if (project != null && project.getLastBuild() != null) {
844848
CredentialsProvider.track(project.getLastBuild(), credentials);
845849
}
850+
} else {
851+
listener.getLogger().println(format("Warning: CredentialId \"%s\" could not be found.", ucCredentialsId));
846852
}
847853
}
848854
}
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
package hudson.plugins.git;
2+
3+
import com.cloudbees.plugins.credentials.*;
4+
import com.cloudbees.plugins.credentials.common.StandardCredentials;
5+
import com.cloudbees.plugins.credentials.domains.Domain;
6+
import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl;
7+
import jenkins.model.Jenkins;
8+
import jenkins.plugins.git.GitSampleRepoRule;
9+
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
10+
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
11+
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
12+
import org.junit.Before;
13+
import org.junit.Rule;
14+
import org.junit.Test;
15+
import org.jvnet.hudson.test.Issue;
16+
import org.jvnet.hudson.test.JenkinsRule;
17+
18+
import java.util.Collections;
19+
20+
import static org.hamcrest.Matchers.notNullValue;
21+
import static org.junit.Assert.assertEquals;
22+
import static org.junit.Assert.assertThat;
23+
24+
public class CredentialsUserRemoteConfigTest {
25+
26+
@Rule
27+
public JenkinsRule r = new JenkinsRule();
28+
@Rule
29+
public GitSampleRepoRule sampleRepo = new GitSampleRepoRule();
30+
31+
private CredentialsStore store = null;
32+
33+
@Before
34+
public void enableSystemCredentialsProvider() {
35+
SystemCredentialsProvider.getInstance().setDomainCredentialsMap(
36+
Collections.singletonMap(Domain.global(), Collections.<Credentials>emptyList()));
37+
for (CredentialsStore s : CredentialsProvider.lookupStores(Jenkins.getInstance())) {
38+
if (s.getProvider() instanceof SystemCredentialsProvider.ProviderImpl) {
39+
store = s;
40+
break;
41+
}
42+
}
43+
assertThat("The system credentials provider is enabled", store, notNullValue());
44+
}
45+
46+
@Issue("JENKINS-30515")
47+
@Test
48+
public void checkoutWithValidCredentials() throws Exception {
49+
sampleRepo.init();
50+
store.addCredentials(Domain.global(), createCredential(CredentialsScope.GLOBAL, "github"));
51+
store.save();
52+
53+
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
54+
p.setDefinition(new CpsFlowDefinition(
55+
"node {\n"
56+
+ " checkout(\n"
57+
+ " [$class: 'GitSCM', \n"
58+
+ " userRemoteConfigs: [[credentialsId: 'github', url: $/" + sampleRepo + "/$, branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false]]]\n"
59+
+ " )"
60+
+ "}"));
61+
WorkflowRun b = r.assertBuildStatusSuccess(p.scheduleBuild2(0));
62+
r.assertLogContains("using credential github", b);
63+
}
64+
65+
@Issue("JENKINS-30515")
66+
@Test
67+
public void checkoutWithDifferentCredentials() throws Exception {
68+
sampleRepo.init();
69+
store.addCredentials(Domain.global(), createCredential(CredentialsScope.GLOBAL, "other"));
70+
store.save();
71+
72+
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
73+
p.setDefinition(new CpsFlowDefinition(
74+
"node {\n"
75+
+ " checkout(\n"
76+
+ " [$class: 'GitSCM', \n"
77+
+ " userRemoteConfigs: [[credentialsId: 'github', url: $/" + sampleRepo + "/$, branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false]]]\n"
78+
+ " )"
79+
+ "}"));
80+
WorkflowRun b = r.assertBuildStatusSuccess(p.scheduleBuild2(0));
81+
System.out.println(JenkinsRule.getLog(b));
82+
r.assertLogContains("Warning: CredentialId \"github\" could not be found", b);
83+
}
84+
85+
@Issue("JENKINS-30515")
86+
@Test
87+
public void checkoutWithVInvalidCredentials() throws Exception {
88+
sampleRepo.init();
89+
store.addCredentials(Domain.global(), createCredential(CredentialsScope.SYSTEM, "github"));
90+
store.save();
91+
92+
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
93+
p.setDefinition(new CpsFlowDefinition(
94+
"node {\n"
95+
+ " checkout(\n"
96+
+ " [$class: 'GitSCM', \n"
97+
+ " userRemoteConfigs: [[credentialsId: 'github', url: $/" + sampleRepo + "/$, branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false]]]\n"
98+
+ " )"
99+
+ "}"));
100+
WorkflowRun b = r.assertBuildStatusSuccess(p.scheduleBuild2(0));
101+
r.assertLogContains("Warning: CredentialId \"github\" could not be found", b);
102+
}
103+
104+
@Issue("JENKINS-30515")
105+
@Test
106+
public void checkoutWithVNoCredentialsStoredButUsed() throws Exception {
107+
sampleRepo.init();
108+
109+
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
110+
p.setDefinition(new CpsFlowDefinition(
111+
"node {\n"
112+
+ " checkout(\n"
113+
+ " [$class: 'GitSCM', \n"
114+
+ " userRemoteConfigs: [[credentialsId: 'github', url: $/" + sampleRepo + "/$, branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false]]]\n"
115+
+ " )"
116+
+ "}"));
117+
WorkflowRun b = r.assertBuildStatusSuccess(p.scheduleBuild2(0));
118+
System.out.println(JenkinsRule.getLog(b));
119+
r.assertLogContains("Warning: CredentialId \"github\" could not be found", b);
120+
}
121+
122+
@Issue("JENKINS-30515")
123+
@Test
124+
public void checkoutWithVNoCredentialsSpecified() throws Exception {
125+
sampleRepo.init();
126+
127+
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
128+
p.setDefinition(new CpsFlowDefinition(
129+
"node {\n"
130+
+ " checkout(\n"
131+
+ " [$class: 'GitSCM', \n"
132+
+ " userRemoteConfigs: [[url: $/" + sampleRepo + "/$, branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false]]]\n"
133+
+ " )"
134+
+ "}"));
135+
WorkflowRun b = r.assertBuildStatusSuccess(p.scheduleBuild2(0));
136+
System.out.println(JenkinsRule.getLog(b));
137+
r.assertLogContains("No credentials specified", b);
138+
}
139+
140+
141+
private StandardCredentials createCredential(CredentialsScope scope, String id) {
142+
return new UsernamePasswordCredentialsImpl(scope, id, "desc: " + id, "username", "password");
143+
}
144+
}

0 commit comments

Comments
 (0)