-
Notifications
You must be signed in to change notification settings - Fork 24
Add customizable header command #119
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
697b30e
Fix equals being lost in SSH config value
code-asher 811673f
Add header command setting
code-asher f9b416a
Check for missing base URL
code-asher 8a56af9
A bit too many quotes
code-asher c67bca2
Fix merging headers into config object
code-asher 37118f9
Better fix for getting base URL
code-asher 5923769
Avoid logging in when no url has been set
code-asher a7f21be
Fix getting headers on login
code-asher 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
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,57 @@ | ||
import * as os from "os" | ||
import { it, expect } from "vitest" | ||
import { getHeaders } from "./headers" | ||
|
||
const logger = { | ||
writeToCoderOutputChannel() { | ||
// no-op | ||
}, | ||
} | ||
|
||
it("should return no headers", async () => { | ||
await expect(getHeaders(undefined, undefined, logger)).resolves.toStrictEqual({}) | ||
await expect(getHeaders("localhost", undefined, logger)).resolves.toStrictEqual({}) | ||
await expect(getHeaders(undefined, "command", logger)).resolves.toStrictEqual({}) | ||
await expect(getHeaders("localhost", "", logger)).resolves.toStrictEqual({}) | ||
await expect(getHeaders("", "command", logger)).resolves.toStrictEqual({}) | ||
await expect(getHeaders("localhost", " ", logger)).resolves.toStrictEqual({}) | ||
await expect(getHeaders(" ", "command", logger)).resolves.toStrictEqual({}) | ||
}) | ||
|
||
it("should return headers", async () => { | ||
await expect(getHeaders("localhost", "printf foo=bar'\n'baz=qux", logger)).resolves.toStrictEqual({ | ||
foo: "bar", | ||
baz: "qux", | ||
}) | ||
await expect(getHeaders("localhost", "printf foo=bar'\r\n'baz=qux", logger)).resolves.toStrictEqual({ | ||
foo: "bar", | ||
baz: "qux", | ||
}) | ||
await expect(getHeaders("localhost", "printf foo=bar'\r\n'", logger)).resolves.toStrictEqual({ foo: "bar" }) | ||
await expect(getHeaders("localhost", "printf foo=bar", logger)).resolves.toStrictEqual({ foo: "bar" }) | ||
await expect(getHeaders("localhost", "printf foo=bar=", logger)).resolves.toStrictEqual({ foo: "bar=" }) | ||
await expect(getHeaders("localhost", "printf foo=bar=baz", logger)).resolves.toStrictEqual({ foo: "bar=baz" }) | ||
await expect(getHeaders("localhost", "printf foo=", logger)).resolves.toStrictEqual({ foo: "" }) | ||
}) | ||
|
||
it("should error on malformed or empty lines", async () => { | ||
await expect(getHeaders("localhost", "printf foo=bar'\r\n\r\n'", logger)).rejects.toMatch(/Malformed/) | ||
await expect(getHeaders("localhost", "printf '\r\n'foo=bar", logger)).rejects.toMatch(/Malformed/) | ||
await expect(getHeaders("localhost", "printf =foo", logger)).rejects.toMatch(/Malformed/) | ||
await expect(getHeaders("localhost", "printf foo", logger)).rejects.toMatch(/Malformed/) | ||
await expect(getHeaders("localhost", "printf ' =foo'", logger)).rejects.toMatch(/Malformed/) | ||
await expect(getHeaders("localhost", "printf 'foo =bar'", logger)).rejects.toMatch(/Malformed/) | ||
await expect(getHeaders("localhost", "printf 'foo foo=bar'", logger)).rejects.toMatch(/Malformed/) | ||
await expect(getHeaders("localhost", "printf ''", logger)).rejects.toMatch(/Malformed/) | ||
}) | ||
|
||
it("should have access to environment variables", async () => { | ||
const coderUrl = "dev.coder.com" | ||
await expect( | ||
getHeaders(coderUrl, os.platform() === "win32" ? "printf url=%CODER_URL" : "printf url=$CODER_URL", logger), | ||
).resolves.toStrictEqual({ url: coderUrl }) | ||
}) | ||
|
||
it("should error on non-zero exit", async () => { | ||
await expect(getHeaders("localhost", "exit 10", logger)).rejects.toMatch(/exited unexpectedly with code 10/) | ||
}) |
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,64 @@ | ||
import * as cp from "child_process" | ||
import * as util from "util" | ||
|
||
export interface Logger { | ||
writeToCoderOutputChannel(message: string): void | ||
} | ||
|
||
interface ExecException { | ||
code?: number | ||
stderr?: string | ||
stdout?: string | ||
} | ||
|
||
function isExecException(err: unknown): err is ExecException { | ||
return typeof (err as ExecException).code !== "undefined" | ||
} | ||
|
||
// TODO: getHeaders might make more sense to directly implement on Storage | ||
// but it is difficult to test Storage right now since we use vitest instead of | ||
// the standard extension testing framework which would give us access to vscode | ||
// APIs. We should revert the testing framework then consider moving this. | ||
|
||
// getHeaders executes the header command and parses the headers from stdout. | ||
// Both stdout and stderr are logged on error but stderr is otherwise ignored. | ||
// Throws an error if the process exits with non-zero or the JSON is invalid. | ||
// Returns undefined if there is no header command set. No effort is made to | ||
// validate the JSON other than making sure it can be parsed. | ||
export async function getHeaders( | ||
url: string | undefined, | ||
command: string | undefined, | ||
logger: Logger, | ||
): Promise<Record<string, string>> { | ||
const headers: Record<string, string> = {} | ||
if (typeof url === "string" && url.trim().length > 0 && typeof command === "string" && command.trim().length > 0) { | ||
let result: { stdout: string; stderr: string } | ||
try { | ||
result = await util.promisify(cp.exec)(command, { | ||
env: { | ||
...process.env, | ||
CODER_URL: url, | ||
}, | ||
}) | ||
} catch (error) { | ||
if (isExecException(error)) { | ||
logger.writeToCoderOutputChannel(`Header command exited unexpectedly with code ${error.code}`) | ||
logger.writeToCoderOutputChannel(`stdout: ${error.stdout}`) | ||
logger.writeToCoderOutputChannel(`stderr: ${error.stderr}`) | ||
throw new Error(`Header command exited unexpectedly with code ${error.code}`) | ||
} | ||
throw new Error(`Header command exited unexpectedly: ${error}`) | ||
} | ||
const lines = result.stdout.replace(/\r?\n$/, "").split(/\r?\n/) | ||
for (let i = 0; i < lines.length; ++i) { | ||
const [key, value] = lines[i].split(/=(.*)/) | ||
// Header names cannot be blank or contain whitespace and the Coder CLI | ||
// requires that there be an equals sign (the value can be blank though). | ||
if (key.length === 0 || key.indexOf(" ") !== -1 || typeof value === "undefined") { | ||
throw new Error(`Malformed line from header command: [${lines[i]}] (out: ${result.stdout})`) | ||
} | ||
headers[key] = value | ||
} | ||
} | ||
return headers | ||
} |
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
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.
hey @code-asher this does not compile..
I've tried to run it locally and it failed
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.
Yeah just realized I messed this up, fixing now.
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.
Pushed a fix!
Uh oh!
There was an error while loading. Please reload this page.
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.
Turns out I had another issue where the URL in storage was not set yet (for the login request) resulting in no headers. I think everything is working now, will test a bit more then merge this in. Let me know if you find anything else wrong!