-
Notifications
You must be signed in to change notification settings - Fork 5
Support GitHub app auth #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
scottillogical
wants to merge
10
commits into
cloudfoundry-community:main
Choose a base branch
from
scottillogical:support_github_app_auth
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
cb5a0e7
Add support for authentication as a GitHub App
fatmcgav-depop e510076
github app support in git clone
a76e2d3
fix github org
8dc473a
disable access token when using github app
4834f59
add github app docs to readme
ace00a2
revert changes to dockerfile - update github app credential helper
d53c757
invert condition
79f07a7
invert condition
1249e3e
formatted selection
f05bdfd
fix github organization
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,17 +36,25 @@ func NewGitClient(source *Source, dir string, output io.Writer) (*GitClient, err | |
os.Setenv("GIT_LFS_SKIP_SMUDGE", "true") | ||
} | ||
return &GitClient{ | ||
AccessToken: source.AccessToken, | ||
Directory: dir, | ||
Output: output, | ||
AccessToken: source.AccessToken, | ||
PrivateKey: source.PrivateKey, | ||
UseGithubApp: source.UseGitHubApp, | ||
ApplicationID: source.ApplicationID, | ||
GithubOrganization: source.GithubOrganization, | ||
Directory: dir, | ||
Output: output, | ||
}, nil | ||
} | ||
|
||
// GitClient ... | ||
type GitClient struct { | ||
AccessToken string | ||
Directory string | ||
Output io.Writer | ||
AccessToken string | ||
UseGithubApp bool | ||
Directory string | ||
ApplicationID int64 | ||
GithubOrganization string | ||
PrivateKey string | ||
Output io.Writer | ||
} | ||
|
||
func (g *GitClient) command(name string, arg ...string) *exec.Cmd { | ||
|
@@ -55,9 +63,15 @@ func (g *GitClient) command(name string, arg ...string) *exec.Cmd { | |
cmd.Stdout = g.Output | ||
cmd.Stderr = g.Output | ||
cmd.Env = os.Environ() | ||
if !g.UseGithubApp { | ||
cmd.Env = append(cmd.Env, | ||
"X_OAUTH_BASIC_TOKEN="+g.AccessToken) | ||
} | ||
|
||
cmd.Env = append(cmd.Env, | ||
"X_OAUTH_BASIC_TOKEN="+g.AccessToken, | ||
"GIT_ASKPASS=/usr/local/bin/askpass.sh") | ||
fmt.Fprint(os.Stderr, fmt.Sprintf("\n%s %v", name, arg)) | ||
|
||
return cmd | ||
} | ||
|
||
|
@@ -75,12 +89,29 @@ func (g *GitClient) Init(branch string) error { | |
if err := g.command("git", "config", "--global", "user.email", "concourse@local").Run(); err != nil { | ||
return fmt.Errorf("failed to configure git email: %s", err) | ||
} | ||
if err := g.command("git", "config", "--global", "url.https://[email protected]/.insteadOf", "[email protected]:").Run(); err != nil { | ||
return fmt.Errorf("failed to configure github url: %s", err) | ||
} | ||
if err := g.command("git", "config", "--global", "url.https://.insteadOf", "git://").Run(); err != nil { | ||
return fmt.Errorf("failed to configure github url: %s", err) | ||
} | ||
if !g.UseGithubApp { | ||
if err := g.command("git", "config", "url.https://[email protected]/.insteadOf", "[email protected]:").Run(); err != nil { | ||
return fmt.Errorf("failed to configure github url: %s", err) | ||
} | ||
} else { | ||
err := ioutil.WriteFile(filePath, []byte(g.PrivateKey), 0600) | ||
if err != nil { | ||
fmt.Println("Error writing private key:", err) | ||
os.Exit(1) | ||
} | ||
|
||
helperStr := fmt.Sprintf("!git-credential-github-app --appId %d -organization %s -username x-access-token -privateKeyFile /tmp/git-resource-private-key", g.ApplicationID, g.GithubOrganization) | ||
if err := g.command("git", "config", "credential.https://github.com.helper", helperStr).Run(); err != nil { | ||
return fmt.Errorf("failed to configure github url: %s", err) | ||
} | ||
} else { | ||
if err := g.command("git", "config", "url.https://[email protected]/.insteadOf", "[email protected]:").Run(); err != nil { | ||
return fmt.Errorf("failed to configure github url: %s", err) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Smaller branch of if-then-else should come first: please invert the condition. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
return nil | ||
} | ||
|
||
|
@@ -232,6 +263,9 @@ func (g *GitClient) Endpoint(uri string) (string, error) { | |
if err != nil { | ||
return "", fmt.Errorf("failed to parse commit url: %s", err) | ||
} | ||
endpoint.User = url.UserPassword("x-oauth-basic", g.AccessToken) | ||
if !g.UseGithubApp { | ||
endpoint.User = url.UserPassword("x-oauth-basic", g.AccessToken) | ||
} | ||
|
||
return endpoint.String(), nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package resource_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
resource "github.com/telia-oss/github-pr-resource" | ||
) | ||
|
||
func TestSource(t *testing.T) { | ||
tests := []struct { | ||
description string | ||
source resource.Source | ||
wantErr string | ||
}{ | ||
{ | ||
description: "validate passes", | ||
source: resource.Source{ | ||
AccessToken: "123456", | ||
Repository: "test/test", | ||
}, | ||
}, | ||
{ | ||
description: "should have an access_token", | ||
source: resource.Source{ | ||
Repository: "test/test", | ||
}, | ||
wantErr: "access_token must be set if not using GitHub App authentication", | ||
}, | ||
{ | ||
description: "should have a repository", | ||
source: resource.Source{ | ||
AccessToken: "123456", | ||
}, | ||
wantErr: "repository must be set", | ||
}, | ||
{ | ||
description: "should support GitHub App authentication", | ||
source: resource.Source{ | ||
Repository: "test/test", | ||
GithubOrganization: "test", | ||
UseGitHubApp: true, | ||
PrivateKey: "key.pem", | ||
ApplicationID: 123456, | ||
InstallationID: 1, | ||
}, | ||
}, | ||
{ | ||
description: "private_key App configuration values", | ||
source: resource.Source{ | ||
Repository: "test/test", | ||
UseGitHubApp: true, | ||
ApplicationID: 123456, | ||
InstallationID: 1, | ||
}, | ||
wantErr: "private_key is required for GitHub App authentication", | ||
}, | ||
{ | ||
description: "requires an application_id and installation_id GitHub App configuration values", | ||
source: resource.Source{ | ||
Repository: "test/test", | ||
UseGitHubApp: true, | ||
PrivateKey: "key.pem", | ||
ApplicationID: 123456, | ||
}, | ||
wantErr: "application_id and installation_id must be set if using GitHub App authentication", | ||
}, | ||
{ | ||
description: "should not have an access_token when using GitHub App authentication", | ||
source: resource.Source{ | ||
Repository: "test/test", | ||
UseGitHubApp: true, | ||
GithubOrganization: "test", | ||
PrivateKey: "key.pem", | ||
ApplicationID: 123456, | ||
InstallationID: 1, | ||
AccessToken: "123456", | ||
}, | ||
wantErr: "access_token is not required when using GitHub App authentication", | ||
}, | ||
{ | ||
description: "requires v3_endpoint when v4_endpoint is set", | ||
source: resource.Source{ | ||
AccessToken: "123456", | ||
Repository: "test/test", | ||
V3Endpoint: "https://github.com/v3", | ||
}, | ||
wantErr: "v4_endpoint must be set together with v3_endpoint", | ||
}, | ||
{ | ||
description: "requires v4_endpoint when v3_endpoint is set", | ||
source: resource.Source{ | ||
AccessToken: "123456", | ||
Repository: "test/test", | ||
V4Endpoint: "https://github.com/v4", | ||
}, | ||
wantErr: "v3_endpoint must be set together with v4_endpoint", | ||
}, | ||
} | ||
|
||
for _, tc := range tests { | ||
t.Run(tc.description, func(t *testing.T) { | ||
err := tc.source.Validate() | ||
|
||
if tc.wantErr != "" { | ||
if err == nil { | ||
t.Logf("Expected error '%s', got nothing", tc.wantErr) | ||
t.Fail() | ||
} | ||
assert.EqualError(t, err, tc.wantErr, fmt.Sprintf("Expected '%s', got '%s'", tc.wantErr, err)) | ||
} | ||
|
||
if tc.wantErr == "" && err != nil { | ||
t.Logf("Got an error when none expected: %s", err) | ||
t.Fail() | ||
} | ||
}) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding such external component should be justified in the PR description.
Plus, why not use the latest v0.3.3 version?
Which raises the question of how we shall bump this new dependency?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I updated it to the latest version 9f4ae0f
I am open to suggestions for how to keep it up to date. with say, the git resource, we have to build our own version of the git resource to add support for github app auth, since the regular git resource is agnostic to providers. but supporting github apps in this resource seems highly desirable