-
Notifications
You must be signed in to change notification settings - Fork 4.3k
feat: add GitHub App authentication support #2562
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
pyama86
wants to merge
3
commits into
github:main
Choose a base branch
from
pyama86:feat/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
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ import ( | |
| "errors" | ||
| "fmt" | ||
| "os" | ||
| "strconv" | ||
| "strings" | ||
| "time" | ||
|
|
||
|
|
@@ -34,8 +35,16 @@ var ( | |
| Long: `Start a server that communicates via standard input/output streams using JSON-RPC messages.`, | ||
| RunE: func(_ *cobra.Command, _ []string) error { | ||
| token := viper.GetString("personal_access_token") | ||
| if token == "" { | ||
| return errors.New("GITHUB_PERSONAL_ACCESS_TOKEN not set") | ||
|
|
||
| // Parse GitHub App authentication config | ||
| appID, privateKey, installationID, err := parseAppAuthConfig() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| useAppAuth := appID != 0 && len(privateKey) > 0 && installationID != 0 | ||
|
|
||
| if token == "" && !useAppAuth { | ||
| return errors.New("GITHUB_PERSONAL_ACCESS_TOKEN not set (or configure GitHub App auth with GITHUB_APP_ID, GITHUB_APP_PRIVATE_KEY/GITHUB_APP_PRIVATE_KEY_PATH, and GITHUB_APP_INSTALLATION_ID)") | ||
| } | ||
|
|
||
| // If you're wondering why we're not using viper.GetStringSlice("toolsets"), | ||
|
|
@@ -94,6 +103,9 @@ var ( | |
| InsidersMode: viper.GetBool("insiders"), | ||
| ExcludeTools: excludeTools, | ||
| RepoAccessCacheTTL: &ttl, | ||
| AppID: appID, | ||
| PrivateKey: privateKey, | ||
| InstallationID: installationID, | ||
| } | ||
| return ghmcp.RunStdioServer(stdioServerConfig) | ||
| }, | ||
|
|
@@ -235,3 +247,53 @@ func wordSepNormalizeFunc(_ *pflag.FlagSet, name string) pflag.NormalizedName { | |
| } | ||
| return pflag.NormalizedName(name) | ||
| } | ||
|
|
||
| // parseAppAuthConfig reads GitHub App authentication config from environment variables. | ||
| // Returns (0, nil, 0, nil) when no App auth is configured. | ||
| func parseAppAuthConfig() (appID int64, privateKey []byte, installationID int64, err error) { | ||
| appIDStr := viper.GetString("app_id") | ||
| installationIDStr := viper.GetString("app_installation_id") | ||
| privateKeyStr := viper.GetString("app_private_key") | ||
| privateKeyPath := viper.GetString("app_private_key_path") | ||
|
|
||
| // If none are set, App auth is not configured | ||
| if appIDStr == "" && installationIDStr == "" && privateKeyStr == "" && privateKeyPath == "" { | ||
| return 0, nil, 0, nil | ||
| } | ||
|
|
||
| // If some but not all are set, that's a configuration error | ||
| if appIDStr == "" || installationIDStr == "" || (privateKeyStr == "" && privateKeyPath == "") { | ||
| return 0, nil, 0, errors.New("incomplete GitHub App auth config: GITHUB_APP_ID, GITHUB_APP_INSTALLATION_ID, and GITHUB_APP_PRIVATE_KEY or GITHUB_APP_PRIVATE_KEY_PATH are all required") | ||
| } | ||
|
Comment on lines
+265
to
+267
Author
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. Fixed. Now returns an error when both |
||
|
|
||
| if privateKeyStr != "" && privateKeyPath != "" { | ||
| return 0, nil, 0, errors.New("GITHUB_APP_PRIVATE_KEY and GITHUB_APP_PRIVATE_KEY_PATH are mutually exclusive") | ||
| } | ||
|
|
||
| appID, err = strconv.ParseInt(appIDStr, 10, 64) | ||
| if err != nil { | ||
| return 0, nil, 0, fmt.Errorf("invalid GITHUB_APP_ID: %w", err) | ||
| } | ||
|
|
||
| installationID, err = strconv.ParseInt(installationIDStr, 10, 64) | ||
| if err != nil { | ||
| return 0, nil, 0, fmt.Errorf("invalid GITHUB_APP_INSTALLATION_ID: %w", err) | ||
| } | ||
|
|
||
| if privateKeyStr != "" { | ||
| // Environment variables often use literal "\n" instead of actual newlines. | ||
| // Only replace when the value has no real newlines to avoid corrupting | ||
| // keys that were correctly passed with actual newlines. | ||
| if !strings.Contains(privateKeyStr, "\n") { | ||
| privateKeyStr = strings.ReplaceAll(privateKeyStr, `\n`, "\n") | ||
| } | ||
| privateKey = []byte(privateKeyStr) | ||
| } else { | ||
| privateKey, err = os.ReadFile(privateKeyPath) | ||
| if err != nil { | ||
| return 0, nil, 0, fmt.Errorf("failed to read private key from %s: %w", privateKeyPath, err) | ||
| } | ||
| } | ||
|
|
||
| return appID, privateKey, installationID, 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
Oops, something went wrong.
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.
These env vars are correctly picked up via
viper.AutomaticEnv()with theGITHUB_prefix. For example,viper.GetString("app_id")readsGITHUB_APP_IDbecause the env prefix is set togithuband the key replacer converts-to_. No explicitBindEnvis needed for this case.