Skip to content

Commit 196ee25

Browse files
authored
Merge pull request #1160 from bitwiseman/bug/withCreds
Fix withCredetials() to correctly detect missing creds
2 parents eac4990 + 0e02444 commit 196ee25

File tree

2 files changed

+152
-3
lines changed

2 files changed

+152
-3
lines changed

src/main/java/org/kohsuke/github/GitHubBuilder.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
*/
2525
public class GitHubBuilder implements Cloneable {
2626

27+
// for testing
28+
static File HOME_DIRECTORY = null;
29+
2730
// default scoped so unit tests can read them.
2831
/* private */ String endpoint = GitHubClient.GITHUB_URL;
2932

@@ -60,13 +63,13 @@ static GitHubBuilder fromCredentials() throws IOException {
6063

6164
builder = fromEnvironment();
6265

63-
if (builder.authorizationProvider != null)
66+
if (builder.authorizationProvider != AuthorizationProvider.ANONYMOUS)
6467
return builder;
6568

6669
try {
6770
builder = fromPropertyFile();
6871

69-
if (builder.authorizationProvider != null)
72+
if (builder.authorizationProvider != AuthorizationProvider.ANONYMOUS)
7073
return builder;
7174
} catch (FileNotFoundException e) {
7275
// fall through
@@ -178,7 +181,7 @@ public static GitHubBuilder fromEnvironment() throws IOException {
178181
* the io exception
179182
*/
180183
public static GitHubBuilder fromPropertyFile() throws IOException {
181-
File homeDir = new File(System.getProperty("user.home"));
184+
File homeDir = HOME_DIRECTORY != null ? HOME_DIRECTORY : new File(System.getProperty("user.home"));
182185
File propertyFile = new File(homeDir, ".github");
183186
return fromPropertyFile(propertyFile.getPath());
184187
}

src/test/java/org/kohsuke/github/GitHubConnectionTest.java

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
package org.kohsuke.github;
22

3+
import org.apache.commons.lang3.SystemUtils;
34
import org.junit.Assume;
45
import org.junit.Test;
6+
import org.kohsuke.github.authorization.AuthorizationProvider;
57
import org.kohsuke.github.authorization.UserAuthorizationProvider;
68

9+
import java.io.File;
10+
import java.io.FileOutputStream;
711
import java.io.IOException;
812
import java.lang.reflect.Field;
913
import java.util.Collections;
1014
import java.util.HashMap;
1115
import java.util.Map;
16+
import java.util.Properties;
1217

1318
import static org.hamcrest.Matchers.*;
1419

@@ -137,6 +142,147 @@ public void testGitHubBuilderFromCustomEnvironment() throws IOException {
137142
assertThat(((UserAuthorizationProvider) builder.authorizationProvider).getLogin(), equalTo("bogus login"));
138143
}
139144

145+
@Test
146+
public void testGitHubBuilderFromCredentialsWithEnvironment() throws IOException {
147+
// we disable this test for JDK 16+ as the current hacks in setupEnvironment() don't work with JDK 16+
148+
Assume.assumeThat(Double.valueOf(System.getProperty("java.specification.version")), lessThan(16.0));
149+
Assume.assumeFalse(SystemUtils.IS_OS_WINDOWS);
150+
151+
Map<String, String> props = new HashMap<String, String>();
152+
153+
props.put("endpoint", "bogus endpoint url");
154+
props.put("oauth", "bogus oauth token string");
155+
setupEnvironment(props);
156+
GitHubBuilder builder = GitHubBuilder.fromCredentials();
157+
158+
assertThat(builder.endpoint, equalTo("bogus endpoint url"));
159+
160+
assertThat(builder.authorizationProvider, instanceOf(UserAuthorizationProvider.class));
161+
assertThat(builder.authorizationProvider.getEncodedAuthorization(), equalTo("token bogus oauth token string"));
162+
assertThat(((UserAuthorizationProvider) builder.authorizationProvider).getLogin(), nullValue());
163+
164+
props.put("login", "bogus login");
165+
setupEnvironment(props);
166+
builder = GitHubBuilder.fromCredentials();
167+
168+
assertThat(builder.authorizationProvider, instanceOf(UserAuthorizationProvider.class));
169+
assertThat(builder.authorizationProvider.getEncodedAuthorization(), equalTo("token bogus oauth token string"));
170+
assertThat(((UserAuthorizationProvider) builder.authorizationProvider).getLogin(), equalTo("bogus login"));
171+
172+
props.put("jwt", "bogus jwt token string");
173+
setupEnvironment(props);
174+
builder = GitHubBuilder.fromCredentials();
175+
176+
assertThat(builder.authorizationProvider, not(instanceOf(UserAuthorizationProvider.class)));
177+
assertThat(builder.authorizationProvider.getEncodedAuthorization(), equalTo("Bearer bogus jwt token string"));
178+
179+
props.put("password", "bogus weak password");
180+
setupEnvironment(props);
181+
builder = GitHubBuilder.fromCredentials();
182+
183+
assertThat(builder.authorizationProvider, instanceOf(UserAuthorizationProvider.class));
184+
assertThat(builder.authorizationProvider.getEncodedAuthorization(),
185+
equalTo("Basic Ym9ndXMgbG9naW46Ym9ndXMgd2VhayBwYXNzd29yZA=="));
186+
assertThat(((UserAuthorizationProvider) builder.authorizationProvider).getLogin(), equalTo("bogus login"));
187+
}
188+
189+
@Test
190+
public void testGitHubBuilderFromCredentialsWithPropertyFile() throws IOException {
191+
// we disable this test for JDK 16+ as the current hacks in setupEnvironment() don't work with JDK 16+
192+
Assume.assumeThat(Double.valueOf(System.getProperty("java.specification.version")), lessThan(16.0));
193+
Assume.assumeFalse(SystemUtils.IS_OS_WINDOWS);
194+
195+
Map<String, String> props = new HashMap<String, String>();
196+
197+
// Clear the environment
198+
setupEnvironment(props);
199+
try {
200+
GitHubBuilder.HOME_DIRECTORY = new File(getTestDirectory());
201+
try {
202+
GitHubBuilder builder = GitHubBuilder.fromCredentials();
203+
fail();
204+
} catch (Exception e) {
205+
assertThat(e, instanceOf(IOException.class));
206+
assertThat(e.getMessage(), equalTo("Failed to resolve credentials from ~/.github or the environment."));
207+
}
208+
209+
props = new HashMap<String, String>();
210+
211+
props.put("endpoint", "bogus endpoint url");
212+
props.put("oauth", "bogus oauth token string");
213+
214+
setupPropertyFile(props);
215+
216+
GitHubBuilder builder = GitHubBuilder.fromCredentials();
217+
218+
assertThat(builder.endpoint, equalTo("bogus endpoint url"));
219+
220+
assertThat(builder.authorizationProvider, instanceOf(UserAuthorizationProvider.class));
221+
assertThat(builder.authorizationProvider.getEncodedAuthorization(),
222+
equalTo("token bogus oauth token string"));
223+
assertThat(((UserAuthorizationProvider) builder.authorizationProvider).getLogin(), nullValue());
224+
225+
props.put("login", "bogus login");
226+
setupPropertyFile(props);
227+
builder = GitHubBuilder.fromCredentials();
228+
229+
assertThat(builder.authorizationProvider, instanceOf(UserAuthorizationProvider.class));
230+
assertThat(builder.authorizationProvider.getEncodedAuthorization(),
231+
equalTo("token bogus oauth token string"));
232+
assertThat(((UserAuthorizationProvider) builder.authorizationProvider).getLogin(), equalTo("bogus login"));
233+
234+
props.put("jwt", "bogus jwt token string");
235+
setupPropertyFile(props);
236+
builder = GitHubBuilder.fromCredentials();
237+
238+
assertThat(builder.authorizationProvider, not(instanceOf(UserAuthorizationProvider.class)));
239+
assertThat(builder.authorizationProvider.getEncodedAuthorization(),
240+
equalTo("Bearer bogus jwt token string"));
241+
242+
props.put("password", "bogus weak password");
243+
setupPropertyFile(props);
244+
builder = GitHubBuilder.fromCredentials();
245+
246+
assertThat(builder.authorizationProvider, instanceOf(UserAuthorizationProvider.class));
247+
assertThat(builder.authorizationProvider.getEncodedAuthorization(),
248+
equalTo("Basic Ym9ndXMgbG9naW46Ym9ndXMgd2VhayBwYXNzd29yZA=="));
249+
assertThat(((UserAuthorizationProvider) builder.authorizationProvider).getLogin(), equalTo("bogus login"));
250+
} finally {
251+
GitHubBuilder.HOME_DIRECTORY = null;
252+
File propertyFile = new File(getTestDirectory(), ".github");
253+
propertyFile.delete();
254+
}
255+
}
256+
257+
private void setupPropertyFile(Map<String, String> props) throws IOException {
258+
File propertyFile = new File(getTestDirectory(), ".github");
259+
Properties properties = new Properties();
260+
properties.putAll(props);
261+
properties.store(new FileOutputStream(propertyFile), "");
262+
}
263+
264+
private String getTestDirectory() {
265+
return new File("target").getAbsolutePath();
266+
}
267+
268+
@Test
269+
public void testAnonymous() throws IOException {
270+
// we disable this test for JDK 16+ as the current hacks in setupEnvironment() don't work with JDK 16+
271+
Assume.assumeThat(Double.valueOf(System.getProperty("java.specification.version")), lessThan(16.0));
272+
273+
Map<String, String> props = new HashMap<String, String>();
274+
275+
props.put("endpoint", mockGitHub.apiServer().baseUrl());
276+
setupEnvironment(props);
277+
278+
// No values present except endpoint
279+
GitHubBuilder builder = GitHubBuilder
280+
.fromEnvironment("customLogin", "customPassword", "customOauth", "endpoint");
281+
282+
assertThat(builder.endpoint, equalTo(mockGitHub.apiServer().baseUrl()));
283+
assertThat(builder.authorizationProvider, sameInstance(AuthorizationProvider.ANONYMOUS));
284+
}
285+
140286
@Test
141287
public void testGithubBuilderWithAppInstallationToken() throws Exception {
142288

0 commit comments

Comments
 (0)