Skip to content

Commit

Permalink
Revert "Merge pull request #39 from docker/cm/remove-credcheck"
Browse files Browse the repository at this point in the history
This reverts commit f9247c6, reversing
changes made to 306d049.
  • Loading branch information
ColinMcNeil committed Oct 9, 2024
1 parent fa32df8 commit d7d309c
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/commands/runPrompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { showPromptPicker } from "../utils/promptPicker";
import { createOutputBuffer } from "../utils/promptFilename";
import { spawnPromptImage, writeKeyToVolume } from "../utils/promptRunner";
import { verifyHasOpenAIKey } from "./setOpenAIKey";
import { getCredential } from "../utils/credential";
import { setProjectDir } from "./setProjectDir";
import { postToBackendSocket } from "../utils/ddSocket";

Expand Down Expand Up @@ -160,13 +161,15 @@ export const runPrompt: (secrets: vscode.SecretStorage, mode: PromptOption) => v

progress.report({ increment: 5, message: "Detecting docker desktop token" });

const { Username, Password } = await getCredential("docker");

try {
progress.report({ increment: 5, message: "Mounting secrets..." });
await writeKeyToVolume(apiKey!);
progress.report({ increment: 5, message: "Running..." });
const ranges: Record<string, vscode.Range> = {};
const getBaseFunctionRange = () => new vscode.Range(doc.lineCount, 0, doc.lineCount, 0);
await spawnPromptImage(promptOption.id, runningLocal ? inputWorkspace! : workspaceFolder!.uri.fsPath, 'vscode-user', process.platform, async (json) => {
await spawnPromptImage(promptOption.id, runningLocal ? inputWorkspace! : workspaceFolder!.uri.fsPath, Username || 'vscode-user', Password, process.platform, async (json) => {
switch (json.method) {
case 'functions':
const functions = json.params;
Expand Down
31 changes: 31 additions & 0 deletions src/utils/credential.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { spawnSync } from "child_process";
import { env } from "vscode";

export const getCredential = async (service: string) => {
const auth = spawnSync(
`echo "https://index.docker.io/v1/" | docker-credential-desktop get`,
{
shell: process.platform === 'win32' ? "powershell" : true,
}
);
let Username = `vscode-${env.machineId}`;
let Password = "";
if (auth.stdout.toString().startsWith("{") && auth.status === 0 && !auth.error) {
try {
const authPayload = JSON.parse(auth.stdout.toString()) as {
"ServerURL": string,
"Username": string,
"Secret": string
};
Username = authPayload.Username;
Password = authPayload.Secret;
}
catch (e) {
throw new Error(`Expected JSON from docker-credential-desktop, got STDOUT: ${auth.stdout.toString()} STDERR: ${auth.stderr.toString()} ERR: ${(auth.error || "N/A").toString()}`);
}
}
return {
Username,
Password
};
};
33 changes: 33 additions & 0 deletions src/utils/ddSocket.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,43 @@
import os from 'os';
import path from 'path';
import http from 'http';
import { ctx } from '../extension';
import { ExtensionContext } from 'vscode';
const getDevhomePrefix = () => {
return process.env['DEVHOME'] ? path.basename(process.env['DEVHOME']) : '';
}

const getDevhome = (): string => {
return process.env['DEVHOME'] ?? os.homedir();
}
const getUserDataDirectory = (
/** This distinction is only currently meaningful on Windows */
type: 'local' | 'roaming' = 'local',
): string => {
const devhome = getDevhome();
if (os.platform() === 'win32') {
return path.join(
devhome,
'AppData',
type === 'local' ? 'Local' : 'Roaming',
'Docker',
);
}
if (os.platform() === 'linux') {
return path.join(devhome, '.docker', 'desktop');
}
if (os.platform() === 'darwin') {
return path.join(
devhome,
'Library',
'Containers',
'com.docker.docker',
'Data',
);
}

throw new Error('Unrecognized platform');
}

export function getBackendSocketByPlatform(): string {
switch (os.platform()) {
Expand Down
7 changes: 4 additions & 3 deletions src/utils/promptRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { CancellationToken, commands, window, workspace } from "vscode";
import { setThreadId } from "../commands/setThreadId";
const output = window.createOutputChannel("Docker Labs: AI Tools");

export const getRunArgs = async (promptRef: string, projectDir: string, username: string, platform: string, render = false) => {
export const getRunArgs = async (promptRef: string, projectDir: string, username: string, platform: string, pat: string, render = false) => {
const isLocal = promptRef.startsWith('local://');
const isMarkdown = promptRef.toLowerCase().endsWith('.md');
const threadId = await commands.executeCommand<ReturnType<typeof setThreadId>>('docker.labs-ai-tools-vscode.thread-id', false)
Expand Down Expand Up @@ -37,6 +37,7 @@ export const getRunArgs = async (promptRef: string, projectDir: string, username
"--platform", platform,
...promptArgs,
'--jsonrpc',
...(pat ? ['--pat', pat] : []),
...(threadId ? ['--thread-id', threadId] : []),
];

Expand Down Expand Up @@ -120,8 +121,8 @@ const runAndStream = async (command: string, args: string[], callback: (json: an
});
};

export const spawnPromptImage = async (promptArg: string, projectDir: string, username: string, platform: string, callback: (json: any) => Promise<void>, token: CancellationToken) => {
const args = await getRunArgs(promptArg!, projectDir!, username, platform);
export const spawnPromptImage = async (promptArg: string, projectDir: string, username: string, platform: string, pat: string, callback: (json: any) => Promise<void>, token: CancellationToken) => {
const args = await getRunArgs(promptArg!, projectDir!, username, platform, pat);
callback({ method: 'message', params: { debug: `Running ${args.join(' ')}` } });
return runAndStream("docker", args, callback, token);
};
Expand Down

0 comments on commit d7d309c

Please sign in to comment.