-
Notifications
You must be signed in to change notification settings - Fork 2
feat(cloud-agent-next): authenticate git via a credential helper #5336
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
kilo-code-bot
wants to merge
2
commits into
main
Choose a base branch
from
feat/cloud-agent-git-credential-helper
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
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
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,49 @@ | ||
| #!/bin/sh | ||
| set -eu | ||
|
|
||
| case "${1:-}" in | ||
| get) ;; | ||
| *) exit 0 ;; | ||
| esac | ||
|
|
||
| protocol= | ||
| host= | ||
| while IFS= read -r line || [ -n "$line" ]; do | ||
| [ -z "$line" ] && break | ||
| case "$line" in | ||
| protocol=*) protocol="${line#protocol=}" ;; | ||
| host=*) host="${line#host=}" ;; | ||
| esac | ||
| done | ||
|
|
||
| [ "$protocol" = https ] || exit 0 | ||
|
|
||
| host="${host%%:*}" | ||
| username= | ||
| password= | ||
|
|
||
| case "$host" in | ||
| github.com) | ||
| username=x-access-token | ||
| password="${GH_TOKEN:-}" | ||
| ;; | ||
| bitbucket.org) | ||
| username=x-token-auth | ||
| password="${BITBUCKET_TOKEN:-}" | ||
| ;; | ||
| *) | ||
| gitlab_host="${GITLAB_HOST:-gitlab.com}" | ||
| gitlab_host="${gitlab_host#https://}" | ||
| gitlab_host="${gitlab_host#http://}" | ||
| gitlab_host="${gitlab_host%%/*}" | ||
| gitlab_host="${gitlab_host%%:*}" | ||
| if [ "$host" = "$gitlab_host" ]; then | ||
| username=oauth2 | ||
| password="${GITLAB_TOKEN:-}" | ||
| fi | ||
| ;; | ||
| esac | ||
|
|
||
| [ -n "$password" ] || exit 0 | ||
|
|
||
| printf 'username=%s\npassword=%s\n' "$username" "$password" | ||
191 changes: 191 additions & 0 deletions
191
services/cloud-agent-next/src/kilo-git-credential.test.ts
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,191 @@ | ||
| import { spawnSync } from 'node:child_process'; | ||
| import fs from 'node:fs'; | ||
| import os from 'node:os'; | ||
| import path from 'node:path'; | ||
| import { fileURLToPath } from 'node:url'; | ||
| import { afterEach, describe, expect, it } from 'vitest'; | ||
|
|
||
| const scriptPath = path.resolve( | ||
| path.dirname(fileURLToPath(import.meta.url)), | ||
| '../scripts/kilo-git-credential' | ||
| ); | ||
| const tempDirs: string[] = []; | ||
|
|
||
| afterEach(() => { | ||
| for (const dir of tempDirs.splice(0)) { | ||
| fs.rmSync(dir, { recursive: true, force: true }); | ||
| } | ||
| }); | ||
|
|
||
| type HelperEnv = { | ||
| GH_TOKEN?: string; | ||
| GITLAB_TOKEN?: string; | ||
| GITLAB_HOST?: string; | ||
| BITBUCKET_TOKEN?: string; | ||
| }; | ||
|
|
||
| function credentialInput(protocol: string, host: string): string { | ||
| return `protocol=${protocol}\nhost=${host}\n\n`; | ||
| } | ||
|
|
||
| function runHelper( | ||
| action: string | undefined, | ||
| input: string, | ||
| env: HelperEnv = {} | ||
| ): { status: number | null; stdout: string; home: string } { | ||
| const home = fs.mkdtempSync(path.join(os.tmpdir(), 'kilo-git-credential-')); | ||
| tempDirs.push(home); | ||
| const result = spawnSync('sh', action === undefined ? [scriptPath] : [scriptPath, action], { | ||
| encoding: 'utf8', | ||
| input, | ||
| env: { | ||
| ...process.env, | ||
| HOME: home, | ||
| GH_TOKEN: undefined, | ||
| GITLAB_TOKEN: undefined, | ||
| GITLAB_HOST: undefined, | ||
| BITBUCKET_TOKEN: undefined, | ||
| ...env, | ||
| }, | ||
| }); | ||
| return { status: result.status, stdout: result.stdout, home }; | ||
| } | ||
|
|
||
| function parseCredential(stdout: string): { | ||
| username: string | undefined; | ||
| password: string | undefined; | ||
| } { | ||
| let username: string | undefined; | ||
| let password: string | undefined; | ||
| for (const line of stdout.split('\n')) { | ||
| if (line.startsWith('username=')) { | ||
| username = line.slice('username='.length); | ||
| } else if (line.startsWith('password=')) { | ||
| password = line.slice('password='.length); | ||
| } | ||
| } | ||
| return { username, password }; | ||
| } | ||
|
|
||
| function expectPassword(actual: string | undefined, expected: string): void { | ||
| if (actual !== expected) { | ||
| throw new Error('password did not match the provided token'); | ||
| } | ||
| } | ||
|
|
||
| describe('kilo-git-credential', () => { | ||
| it('returns GitHub credentials including a capability token', () => { | ||
| const token = 'kgh2.cap'; | ||
| const result = runHelper('get', credentialInput('https', 'github.com'), { GH_TOKEN: token }); | ||
| expect(result.status).toBe(0); | ||
| const parsed = parseCredential(result.stdout); | ||
| expect(parsed.username).toBe('x-access-token'); | ||
| expectPassword(parsed.password, token); | ||
| }); | ||
|
|
||
| it('returns GitLab credentials for gitlab.com', () => { | ||
| const token = 'kgl2.cap'; | ||
| const result = runHelper('get', credentialInput('https', 'gitlab.com'), { | ||
| GITLAB_TOKEN: token, | ||
| }); | ||
| expect(result.status).toBe(0); | ||
| const parsed = parseCredential(result.stdout); | ||
| expect(parsed.username).toBe('oauth2'); | ||
| expectPassword(parsed.password, token); | ||
| }); | ||
|
|
||
| it('returns GitLab credentials for a custom GITLAB_HOST and ignores gitlab.com', () => { | ||
| const token = 'kgl2.custom'; | ||
| const env = { GITLAB_TOKEN: token, GITLAB_HOST: 'gitlab.example.com' }; | ||
| const custom = runHelper('get', credentialInput('https', 'gitlab.example.com'), env); | ||
| expect(custom.status).toBe(0); | ||
| const parsed = parseCredential(custom.stdout); | ||
| expect(parsed.username).toBe('oauth2'); | ||
| expectPassword(parsed.password, token); | ||
|
|
||
| const defaultHost = runHelper('get', credentialInput('https', 'gitlab.com'), env); | ||
| expect(defaultHost.status).toBe(0); | ||
| expect(defaultHost.stdout).toBe(''); | ||
| }); | ||
|
|
||
| it('matches GITLAB_HOST and requested host when either includes a port', () => { | ||
| const token = 'kgl2.port'; | ||
| const env = { GITLAB_TOKEN: token, GITLAB_HOST: 'gitlab.example.com:8443' }; | ||
| const requestedWithPort = runHelper( | ||
| 'get', | ||
| credentialInput('https', 'gitlab.example.com:8443'), | ||
| env | ||
| ); | ||
| expect(requestedWithPort.status).toBe(0); | ||
| const parsedRequested = parseCredential(requestedWithPort.stdout); | ||
| expect(parsedRequested.username).toBe('oauth2'); | ||
| expectPassword(parsedRequested.password, token); | ||
|
|
||
| const requestedWithoutPort = runHelper( | ||
| 'get', | ||
| credentialInput('https', 'gitlab.example.com'), | ||
| env | ||
| ); | ||
| expect(requestedWithoutPort.status).toBe(0); | ||
| const parsedBare = parseCredential(requestedWithoutPort.stdout); | ||
| expect(parsedBare.username).toBe('oauth2'); | ||
| expectPassword(parsedBare.password, token); | ||
| }); | ||
|
|
||
| it('accepts GITLAB_HOST with a scheme and path', () => { | ||
| const token = 'kgl2.scheme'; | ||
| const result = runHelper('get', credentialInput('https', 'gitlab.example.com'), { | ||
| GITLAB_TOKEN: token, | ||
| GITLAB_HOST: 'https://gitlab.example.com/gitlab', | ||
| }); | ||
| expect(result.status).toBe(0); | ||
| const parsed = parseCredential(result.stdout); | ||
| expect(parsed.username).toBe('oauth2'); | ||
| expectPassword(parsed.password, token); | ||
| }); | ||
|
|
||
| it('returns Bitbucket credentials', () => { | ||
| const token = 'kbb1.cap'; | ||
| const result = runHelper('get', credentialInput('https', 'bitbucket.org'), { | ||
| BITBUCKET_TOKEN: token, | ||
| }); | ||
| expect(result.status).toBe(0); | ||
| const parsed = parseCredential(result.stdout); | ||
| expect(parsed.username).toBe('x-token-auth'); | ||
| expectPassword(parsed.password, token); | ||
| }); | ||
|
|
||
| it('prints nothing for an unmatched host', () => { | ||
| const result = runHelper('get', credentialInput('https', 'example.com'), { | ||
| GH_TOKEN: 'kgh2.unused', | ||
| GITLAB_TOKEN: 'kgl2.unused', | ||
| BITBUCKET_TOKEN: 'kbb1.unused', | ||
| }); | ||
| expect(result.status).toBe(0); | ||
| expect(result.stdout).toBe(''); | ||
| }); | ||
|
|
||
| it('prints nothing when the matching https token is missing', () => { | ||
| const result = runHelper('get', credentialInput('https', 'github.com')); | ||
| expect(result.status).toBe(0); | ||
| expect(result.stdout).toBe(''); | ||
| }); | ||
|
|
||
| it('prints nothing for http', () => { | ||
| const result = runHelper('get', credentialInput('http', 'github.com'), { | ||
| GH_TOKEN: 'kgh2.unused', | ||
| }); | ||
| expect(result.status).toBe(0); | ||
| expect(result.stdout).toBe(''); | ||
| }); | ||
|
|
||
| it.each(['store', 'erase', 'unknown'] as const)('%s exits 0 without writing files', action => { | ||
| const result = runHelper(action, credentialInput('https', 'github.com'), { | ||
| GH_TOKEN: 'kgh2.unused', | ||
| }); | ||
| expect(result.status).toBe(0); | ||
| expect(result.stdout).toBe(''); | ||
| expect(fs.existsSync(path.join(result.home, '.git-credentials'))).toBe(false); | ||
| expect(fs.readdirSync(result.home)).toEqual([]); | ||
| }); | ||
| }); |
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.
Uh oh!
There was an error while loading. Please reload this page.