Skip to content

Problematic regression with ~/.github file authentication #1155

Closed

Description

@gsmet

Authenticating with ~/.github when using GitHub.connect() doesn't work anymore in recent versions of the GitHub API.

The client "authenticates" as anonymous instead of using the credentials.

The issue is that the GitHubBuilder.authorizationProvider field is never null as it's now initialized to an ANONYMOUS instance here: https://github.com/hub4j/github-api/blob/main/src/main/java/org/kohsuke/github/GitHubBuilder.java#L35 .

Thus when arriving here https://github.com/hub4j/github-api/blob/main/src/main/java/org/kohsuke/github/GitHubBuilder.java#L63 , the field is never null and we never get the chance to use the ~/.github property file to authenticate a few lines below.

I'm not exactly sure how we want to fix this. A comment in GitHubBuilder.fromCredentials() says @throws IOException If there are no credentials defined in the ~/.github properties file or the process environment. which is obviously not true anymore.

So:

Note that it can be a bit hard to debug as in my case the GH API was just throwing 404 Not Found errors when trying to do some write operations instead of complaining about the missing credentials.

Also, no idea on how to test this :).

Thoughts?

Activity

  1. gsmet commented on May 25, 2021

    @gsmet
    ContributorAuthor

    Workaround is to directly use GitHubBuilder.fromPropertyFile().build().

  2. bitwiseman commented on May 27, 2021

    @bitwiseman
    Member

    @gsmet

    #945 is definitely the cause of the problem.

    We need to have a mode for testing where the property file location is settable.

    public static GitHubBuilder fromPropertyFile() throws IOException {
    File homeDir = new File(System.getProperty("user.home"));
    File propertyFile = new File(homeDir, ".github");
    return fromPropertyFile(propertyFile.getPath());

    The code at

    builder = fromEnvironment();
    if (builder.authorizationProvider != null)
    return builder;
    try {
    builder = fromPropertyFile();
    if (builder.authorizationProvider != null)
    return builder;
    } catch (FileNotFoundException e) {
    // fall through
    cause = e;
    }
    should read:

            if (builder.authorizationProvider != AuthorizationProvider.ANONYMOUS)
                return builder;
    
            try {
                builder = fromPropertyFile();
    
                if (builder.authorizationProvider != AuthorizationProvider.ANONYMOUS)
                    return builder;
    
  3. gsmet commented on May 27, 2021

    @gsmet
    ContributorAuthor

    Yeah, that's the fix I had in mind. The funny part will be to create a test :).

  4. added a commit that references this issue on May 27, 2021
    80e1710
  5. bitwiseman commented on May 27, 2021

    @bitwiseman
    Member

    @gsmet Take a look now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions