feat: add GitHub token injection for remote executors and Docker session resume (#654)
* feat: add GitHub token injection for remote executors and Docker session resume - Add global GitHub access token configuration (backend + frontend) - Inject GitHub token into Docker and Sprites executors via env vars and Git credential helper - Transition Docker executor from host mounting to repository cloning - Implement Docker session resume (reconnect to existing containers after backend restart) - Add shell validation using exec.LookPath in agentctl - Add scriptengine providers for agent install and agentctl placeholders * fix: format github-status.tsx with prettier * fix: resolve lint issues (goconst, errcheck, file-length) * fix: address code review findings - Fix resolveGHCLIToken to check both GITHUB_TOKEN and GH_TOKEN guards - Fix credential helper to use \\ fallback - Change /bin/bash to /bin/sh in credential helper for Alpine compat - Strip embedded token from .git/config after clone in prepare scripts - Extract buildLaunchAgentRequest helpers to stay under 80-line limit - Add tests for GitHubAuthProvider, injectTokenIntoURL, reconnect validation - Add GH_TOKEN-only test case for resolveGHCLIToken * refactor(executor): extract GitHub credential resolution to separate file Move injectGitHubToken, injectGitHubTokenFromCLI, resolveRemoteCredentials, resolveAuthSecrets, methodIDToEnvVar, resolveGHCLIToken, and detectGHToken out of executor_execute.go (was 803 logical lines, over the 800-line limit after merging main) into a new executor_credentials.go file. Also adds the nil-guard on secretStore in resolveAuthSecrets that was flagged by review. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> --------- Co-authored-by: Kandev Agent <agent@kandev.dev> Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -1,10 +1,22 @@
|
||||
"use client";
|
||||
|
||||
import { IconCheck, IconX, IconRefresh } from "@tabler/icons-react";
|
||||
import { useState, useCallback } from "react";
|
||||
import {
|
||||
IconCheck,
|
||||
IconX,
|
||||
IconRefresh,
|
||||
IconKey,
|
||||
IconTrash,
|
||||
IconEye,
|
||||
IconEyeOff,
|
||||
} from "@tabler/icons-react";
|
||||
import { Badge } from "@kandev/ui/badge";
|
||||
import { Button } from "@kandev/ui/button";
|
||||
import { Input } from "@kandev/ui/input";
|
||||
import { Spinner } from "@kandev/ui/spinner";
|
||||
import { useGitHubStatus } from "@/hooks/domains/github/use-github-status";
|
||||
import { useToast } from "@/components/toast-provider";
|
||||
import { configureGitHubToken, clearGitHubToken } from "@/lib/api/domains/github-api";
|
||||
import type { AuthDiagnostics } from "@/lib/types/github";
|
||||
|
||||
function DiagnosticsOutput({ diagnostics }: { diagnostics: AuthDiagnostics }) {
|
||||
@@ -31,8 +43,102 @@ function DiagnosticsOutput({ diagnostics }: { diagnostics: AuthDiagnostics }) {
|
||||
);
|
||||
}
|
||||
|
||||
function TokenConfigForm({ onSuccess }: { onSuccess: () => void }) {
|
||||
const [token, setToken] = useState("");
|
||||
const [showToken, setShowToken] = useState(false);
|
||||
const [saving, setSaving] = useState(false);
|
||||
const { toast } = useToast();
|
||||
|
||||
const handleSubmit = useCallback(
|
||||
async (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
if (!token.trim()) return;
|
||||
|
||||
setSaving(true);
|
||||
try {
|
||||
await configureGitHubToken(token.trim());
|
||||
toast({ description: "GitHub token configured successfully", variant: "success" });
|
||||
setToken("");
|
||||
onSuccess();
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : "Failed to configure token";
|
||||
toast({ description: message, variant: "error" });
|
||||
} finally {
|
||||
setSaving(false);
|
||||
}
|
||||
},
|
||||
[token, toast, onSuccess],
|
||||
);
|
||||
|
||||
return (
|
||||
<form onSubmit={handleSubmit} className="space-y-3">
|
||||
<div className="flex gap-2">
|
||||
<div className="relative flex-1">
|
||||
<Input
|
||||
type={showToken ? "text" : "password"}
|
||||
placeholder="ghp_xxxxxxxxxxxx"
|
||||
value={token}
|
||||
onChange={(e) => setToken(e.target.value)}
|
||||
className="pr-8 font-mono text-sm"
|
||||
/>
|
||||
<Button
|
||||
type="button"
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
className="absolute right-1 top-1/2 -translate-y-1/2 h-6 w-6 p-0 cursor-pointer"
|
||||
onClick={() => setShowToken(!showToken)}
|
||||
>
|
||||
{showToken ? (
|
||||
<IconEyeOff className="h-3.5 w-3.5" />
|
||||
) : (
|
||||
<IconEye className="h-3.5 w-3.5" />
|
||||
)}
|
||||
</Button>
|
||||
</div>
|
||||
<Button
|
||||
type="submit"
|
||||
size="sm"
|
||||
disabled={!token.trim() || saving}
|
||||
className="cursor-pointer"
|
||||
>
|
||||
{saving ? <Spinner className="h-4 w-4" /> : <IconKey className="h-4 w-4 mr-1" />}
|
||||
Configure
|
||||
</Button>
|
||||
</div>
|
||||
<p className="text-xs text-muted-foreground">
|
||||
Create a{" "}
|
||||
<a
|
||||
href="https://github.com/settings/tokens/new?scopes=repo,read:org&description=KanDev"
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
className="underline cursor-pointer"
|
||||
>
|
||||
Personal Access Token
|
||||
</a>{" "}
|
||||
with <code className="bg-muted px-1 rounded">repo</code> and{" "}
|
||||
<code className="bg-muted px-1 rounded">read:org</code> scopes.
|
||||
</p>
|
||||
</form>
|
||||
);
|
||||
}
|
||||
|
||||
export function GitHubStatusCard() {
|
||||
const { status, loaded, loading, refresh } = useGitHubStatus();
|
||||
const { toast } = useToast();
|
||||
const [clearing, setClearing] = useState(false);
|
||||
|
||||
const handleClearToken = useCallback(async () => {
|
||||
setClearing(true);
|
||||
try {
|
||||
await clearGitHubToken();
|
||||
toast({ description: "GitHub token removed", variant: "success" });
|
||||
refresh();
|
||||
} catch {
|
||||
toast({ description: "Failed to clear token", variant: "error" });
|
||||
} finally {
|
||||
setClearing(false);
|
||||
}
|
||||
}, [toast, refresh]);
|
||||
|
||||
if (loading || !loaded) {
|
||||
return (
|
||||
@@ -45,7 +151,7 @@ export function GitHubStatusCard() {
|
||||
|
||||
if (!status || !status.authenticated) {
|
||||
return (
|
||||
<div className="space-y-3">
|
||||
<div className="space-y-4">
|
||||
<div className="flex items-center gap-2">
|
||||
<IconX className="h-4 w-4 text-red-500" />
|
||||
<span className="text-sm font-medium">Not connected to GitHub</span>
|
||||
@@ -54,28 +160,26 @@ export function GitHubStatusCard() {
|
||||
Refresh
|
||||
</Button>
|
||||
</div>
|
||||
<div className="text-xs text-muted-foreground space-y-1.5">
|
||||
<p>Connect using one of these methods:</p>
|
||||
<ol className="list-decimal list-inside space-y-1 pl-1">
|
||||
|
||||
{/* Token configuration form */}
|
||||
<div className="space-y-2">
|
||||
<p className="text-sm font-medium">Configure GitHub Token</p>
|
||||
<TokenConfigForm onSuccess={refresh} />
|
||||
</div>
|
||||
|
||||
{/* Alternative methods */}
|
||||
<div className="text-xs text-muted-foreground space-y-1.5 border-t pt-3">
|
||||
<p>Other authentication methods:</p>
|
||||
<ul className="list-disc list-inside space-y-1 pl-1">
|
||||
<li>
|
||||
<strong>Environment variable</strong> — set{" "}
|
||||
<code className="bg-muted px-1 rounded">GITHUB_TOKEN</code> or{" "}
|
||||
<code className="bg-muted px-1 rounded">GH_TOKEN</code> when starting Kandev
|
||||
<code className="bg-muted px-1 rounded">GITHUB_TOKEN</code> when starting Kandev
|
||||
</li>
|
||||
<li>
|
||||
<strong>Kandev secret</strong> — add a secret named{" "}
|
||||
<code className="bg-muted px-1 rounded">GITHUB_TOKEN</code> in{" "}
|
||||
<a href="/settings/general/secrets" className="underline cursor-pointer">
|
||||
Settings > Secrets
|
||||
</a>
|
||||
, then click Refresh above
|
||||
</li>
|
||||
<li>
|
||||
<strong>GitHub CLI</strong> — install{" "}
|
||||
<code className="bg-muted px-1 rounded">gh</code> and run{" "}
|
||||
<strong>GitHub CLI</strong> — run{" "}
|
||||
<code className="bg-muted px-1 rounded">gh auth login</code>
|
||||
</li>
|
||||
</ol>
|
||||
</ul>
|
||||
</div>
|
||||
{status?.diagnostics && <DiagnosticsOutput diagnostics={status.diagnostics} />}
|
||||
</div>
|
||||
@@ -83,14 +187,33 @@ export function GitHubStatusCard() {
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="flex items-center gap-2">
|
||||
<IconCheck className="h-4 w-4 text-green-500" />
|
||||
<span className="text-sm">
|
||||
Connected as <strong>{status.username}</strong>
|
||||
</span>
|
||||
<Badge variant="secondary" className="text-xs">
|
||||
{status.auth_method === "gh_cli" ? "gh CLI" : "PAT"}
|
||||
</Badge>
|
||||
<div className="space-y-3">
|
||||
<div className="flex items-center gap-2">
|
||||
<IconCheck className="h-4 w-4 text-green-500" />
|
||||
<span className="text-sm">
|
||||
Connected as <strong>{status.username}</strong>
|
||||
</span>
|
||||
<Badge variant="secondary" className="text-xs">
|
||||
{status.auth_method === "gh_cli" ? "gh CLI" : "PAT"}
|
||||
</Badge>
|
||||
{status.token_configured && (
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
onClick={handleClearToken}
|
||||
disabled={clearing}
|
||||
className="cursor-pointer h-6 px-2 text-muted-foreground hover:text-destructive"
|
||||
title="Remove configured token"
|
||||
>
|
||||
{clearing ? <Spinner className="h-3.5 w-3.5" /> : <IconTrash className="h-3.5 w-3.5" />}
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
{status.token_configured && (
|
||||
<p className="text-xs text-muted-foreground">
|
||||
Token stored in secrets. This token is used by remote agents for GitHub operations.
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user