Merge branch 'main' into fix_getWorkflowJob

This commit is contained in:
Liam Newman
2021-05-27 23:32:25 -07:00
committed by GitHub
3 changed files with 156 additions and 7 deletions

View File

@@ -206,7 +206,7 @@
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId> <artifactId>maven-javadoc-plugin</artifactId>
<version>3.2.0</version> <version>3.3.0</version>
<configuration> <configuration>
<source>8</source> <source>8</source>
<failOnWarnings>true</failOnWarnings> <failOnWarnings>true</failOnWarnings>
@@ -436,7 +436,7 @@
<dependency> <dependency>
<groupId>com.infradna.tool</groupId> <groupId>com.infradna.tool</groupId>
<artifactId>bridge-method-annotation</artifactId> <artifactId>bridge-method-annotation</artifactId>
<version>1.18</version> <version>1.21</version>
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<!-- for stapler-jetty --> <!-- for stapler-jetty -->
@@ -525,7 +525,7 @@
<dependency> <dependency>
<groupId>org.mockito</groupId> <groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId> <artifactId>mockito-core</artifactId>
<version>3.9.0</version> <version>3.10.0</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
@@ -537,7 +537,7 @@
<dependency> <dependency>
<groupId>com.github.tomakehurst</groupId> <groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-jre8-standalone</artifactId> <artifactId>wiremock-jre8-standalone</artifactId>
<version>2.27.2</version> <version>2.28.0</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>

View File

@@ -24,6 +24,9 @@ import javax.annotation.Nonnull;
*/ */
public class GitHubBuilder implements Cloneable { public class GitHubBuilder implements Cloneable {
// for testing
static File HOME_DIRECTORY = null;
// default scoped so unit tests can read them. // default scoped so unit tests can read them.
/* private */ String endpoint = GitHubClient.GITHUB_URL; /* private */ String endpoint = GitHubClient.GITHUB_URL;
@@ -60,13 +63,13 @@ public class GitHubBuilder implements Cloneable {
builder = fromEnvironment(); builder = fromEnvironment();
if (builder.authorizationProvider != null) if (builder.authorizationProvider != AuthorizationProvider.ANONYMOUS)
return builder; return builder;
try { try {
builder = fromPropertyFile(); builder = fromPropertyFile();
if (builder.authorizationProvider != null) if (builder.authorizationProvider != AuthorizationProvider.ANONYMOUS)
return builder; return builder;
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
// fall through // fall through
@@ -178,7 +181,7 @@ public class GitHubBuilder implements Cloneable {
* the io exception * the io exception
*/ */
public static GitHubBuilder fromPropertyFile() throws IOException { public static GitHubBuilder fromPropertyFile() throws IOException {
File homeDir = new File(System.getProperty("user.home")); File homeDir = HOME_DIRECTORY != null ? HOME_DIRECTORY : new File(System.getProperty("user.home"));
File propertyFile = new File(homeDir, ".github"); File propertyFile = new File(homeDir, ".github");
return fromPropertyFile(propertyFile.getPath()); return fromPropertyFile(propertyFile.getPath());
} }

View File

@@ -1,14 +1,19 @@
package org.kohsuke.github; package org.kohsuke.github;
import org.apache.commons.lang3.SystemUtils;
import org.junit.Assume; import org.junit.Assume;
import org.junit.Test; import org.junit.Test;
import org.kohsuke.github.authorization.AuthorizationProvider;
import org.kohsuke.github.authorization.UserAuthorizationProvider; import org.kohsuke.github.authorization.UserAuthorizationProvider;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Properties;
import static org.hamcrest.Matchers.*; import static org.hamcrest.Matchers.*;
@@ -137,6 +142,147 @@ public class GitHubConnectionTest extends AbstractGitHubWireMockTest {
assertThat(((UserAuthorizationProvider) builder.authorizationProvider).getLogin(), equalTo("bogus login")); assertThat(((UserAuthorizationProvider) builder.authorizationProvider).getLogin(), equalTo("bogus login"));
} }
@Test
public void testGitHubBuilderFromCredentialsWithEnvironment() throws IOException {
// we disable this test for JDK 16+ as the current hacks in setupEnvironment() don't work with JDK 16+
Assume.assumeThat(Double.valueOf(System.getProperty("java.specification.version")), lessThan(16.0));
Assume.assumeFalse(SystemUtils.IS_OS_WINDOWS);
Map<String, String> props = new HashMap<String, String>();
props.put("endpoint", "bogus endpoint url");
props.put("oauth", "bogus oauth token string");
setupEnvironment(props);
GitHubBuilder builder = GitHubBuilder.fromCredentials();
assertThat(builder.endpoint, equalTo("bogus endpoint url"));
assertThat(builder.authorizationProvider, instanceOf(UserAuthorizationProvider.class));
assertThat(builder.authorizationProvider.getEncodedAuthorization(), equalTo("token bogus oauth token string"));
assertThat(((UserAuthorizationProvider) builder.authorizationProvider).getLogin(), nullValue());
props.put("login", "bogus login");
setupEnvironment(props);
builder = GitHubBuilder.fromCredentials();
assertThat(builder.authorizationProvider, instanceOf(UserAuthorizationProvider.class));
assertThat(builder.authorizationProvider.getEncodedAuthorization(), equalTo("token bogus oauth token string"));
assertThat(((UserAuthorizationProvider) builder.authorizationProvider).getLogin(), equalTo("bogus login"));
props.put("jwt", "bogus jwt token string");
setupEnvironment(props);
builder = GitHubBuilder.fromCredentials();
assertThat(builder.authorizationProvider, not(instanceOf(UserAuthorizationProvider.class)));
assertThat(builder.authorizationProvider.getEncodedAuthorization(), equalTo("Bearer bogus jwt token string"));
props.put("password", "bogus weak password");
setupEnvironment(props);
builder = GitHubBuilder.fromCredentials();
assertThat(builder.authorizationProvider, instanceOf(UserAuthorizationProvider.class));
assertThat(builder.authorizationProvider.getEncodedAuthorization(),
equalTo("Basic Ym9ndXMgbG9naW46Ym9ndXMgd2VhayBwYXNzd29yZA=="));
assertThat(((UserAuthorizationProvider) builder.authorizationProvider).getLogin(), equalTo("bogus login"));
}
@Test
public void testGitHubBuilderFromCredentialsWithPropertyFile() throws IOException {
// we disable this test for JDK 16+ as the current hacks in setupEnvironment() don't work with JDK 16+
Assume.assumeThat(Double.valueOf(System.getProperty("java.specification.version")), lessThan(16.0));
Assume.assumeFalse(SystemUtils.IS_OS_WINDOWS);
Map<String, String> props = new HashMap<String, String>();
// Clear the environment
setupEnvironment(props);
try {
GitHubBuilder.HOME_DIRECTORY = new File(getTestDirectory());
try {
GitHubBuilder builder = GitHubBuilder.fromCredentials();
fail();
} catch (Exception e) {
assertThat(e, instanceOf(IOException.class));
assertThat(e.getMessage(), equalTo("Failed to resolve credentials from ~/.github or the environment."));
}
props = new HashMap<String, String>();
props.put("endpoint", "bogus endpoint url");
props.put("oauth", "bogus oauth token string");
setupPropertyFile(props);
GitHubBuilder builder = GitHubBuilder.fromCredentials();
assertThat(builder.endpoint, equalTo("bogus endpoint url"));
assertThat(builder.authorizationProvider, instanceOf(UserAuthorizationProvider.class));
assertThat(builder.authorizationProvider.getEncodedAuthorization(),
equalTo("token bogus oauth token string"));
assertThat(((UserAuthorizationProvider) builder.authorizationProvider).getLogin(), nullValue());
props.put("login", "bogus login");
setupPropertyFile(props);
builder = GitHubBuilder.fromCredentials();
assertThat(builder.authorizationProvider, instanceOf(UserAuthorizationProvider.class));
assertThat(builder.authorizationProvider.getEncodedAuthorization(),
equalTo("token bogus oauth token string"));
assertThat(((UserAuthorizationProvider) builder.authorizationProvider).getLogin(), equalTo("bogus login"));
props.put("jwt", "bogus jwt token string");
setupPropertyFile(props);
builder = GitHubBuilder.fromCredentials();
assertThat(builder.authorizationProvider, not(instanceOf(UserAuthorizationProvider.class)));
assertThat(builder.authorizationProvider.getEncodedAuthorization(),
equalTo("Bearer bogus jwt token string"));
props.put("password", "bogus weak password");
setupPropertyFile(props);
builder = GitHubBuilder.fromCredentials();
assertThat(builder.authorizationProvider, instanceOf(UserAuthorizationProvider.class));
assertThat(builder.authorizationProvider.getEncodedAuthorization(),
equalTo("Basic Ym9ndXMgbG9naW46Ym9ndXMgd2VhayBwYXNzd29yZA=="));
assertThat(((UserAuthorizationProvider) builder.authorizationProvider).getLogin(), equalTo("bogus login"));
} finally {
GitHubBuilder.HOME_DIRECTORY = null;
File propertyFile = new File(getTestDirectory(), ".github");
propertyFile.delete();
}
}
private void setupPropertyFile(Map<String, String> props) throws IOException {
File propertyFile = new File(getTestDirectory(), ".github");
Properties properties = new Properties();
properties.putAll(props);
properties.store(new FileOutputStream(propertyFile), "");
}
private String getTestDirectory() {
return new File("target").getAbsolutePath();
}
@Test
public void testAnonymous() throws IOException {
// we disable this test for JDK 16+ as the current hacks in setupEnvironment() don't work with JDK 16+
Assume.assumeThat(Double.valueOf(System.getProperty("java.specification.version")), lessThan(16.0));
Map<String, String> props = new HashMap<String, String>();
props.put("endpoint", mockGitHub.apiServer().baseUrl());
setupEnvironment(props);
// No values present except endpoint
GitHubBuilder builder = GitHubBuilder
.fromEnvironment("customLogin", "customPassword", "customOauth", "endpoint");
assertThat(builder.endpoint, equalTo(mockGitHub.apiServer().baseUrl()));
assertThat(builder.authorizationProvider, sameInstance(AuthorizationProvider.ANONYMOUS));
}
@Test @Test
public void testGithubBuilderWithAppInstallationToken() throws Exception { public void testGithubBuilderWithAppInstallationToken() throws Exception {