Skip to content

feat: add header-process flag #9059

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 3 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Make header-command newline-delimited
Instead of JSON.
  • Loading branch information
code-asher committed Aug 14, 2023
commit 958c6c4301c662a0ef9f7e43b233692d73964a57
37 changes: 21 additions & 16 deletions cli/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cli

import (
"bufio"
"bytes"
"context"
"encoding/base64"
"encoding/json"
Expand Down Expand Up @@ -361,7 +363,7 @@ func (r *RootCmd) Command(subcommands []*clibase.Cmd) (*clibase.Cmd, error) {
{
Flag: varHeaderCommand,
Env: "CODER_HEADER_COMMAND",
Description: "An external process that outputs JSON-encoded key-value pairs to be used as additional HTTP headers added to all requests.",
Description: "An external command that outputs additional HTTP headers added to all requests. The command must output each header as `key=value` on its own line.",
Value: clibase.StringOf(&r.headerCommand),
Group: globalGroup,
},
Expand Down Expand Up @@ -605,36 +607,39 @@ func (r *RootCmd) setClient(ctx context.Context, client *codersdk.Client, server
transport: http.DefaultTransport,
header: http.Header{},
}
for _, header := range r.header {
parts := strings.SplitN(header, "=", 2)
if len(parts) < 2 {
return xerrors.Errorf("split header %q had less than two parts", header)
}
transport.header.Add(parts[0], parts[1])
}
headers := r.header
if r.headerCommand != "" {
shell := "sh"
caller := "-c"
if runtime.GOOS == "windows" {
shell = "cmd.exe"
caller = "/c"
}
var outBuf bytes.Buffer
// #nosec
cmd := exec.CommandContext(ctx, shell, caller, r.headerCommand)
cmd.Env = append(os.Environ(), "CODER_URL="+serverURL.String())
out, err := cmd.Output()
cmd.Stdout = &outBuf
cmd.Stderr = io.Discard
err := cmd.Run()
if err != nil {
return xerrors.Errorf("failed to run %v (out: %q): %w", cmd.Args, out, err)
return xerrors.Errorf("failed to run %v: %w", cmd.Args, err)
}
var headers map[string]string
err = json.Unmarshal(out, &headers)
if err != nil {
return xerrors.Errorf("failed to parse json from %v (out: %q): %w", cmd.Args, out, err)
scanner := bufio.NewScanner(&outBuf)
for scanner.Scan() {
headers = append(headers, scanner.Text())
}
for key, value := range headers {
transport.header.Add(key, value)
if err := scanner.Err(); err != nil {
return xerrors.Errorf("scan %v: %w", cmd.Args, err)
}
}
for _, header := range headers {
parts := strings.SplitN(header, "=", 2)
if len(parts) < 2 {
return xerrors.Errorf("split header %q had less than two parts", header)
}
transport.header.Add(parts[0], parts[1])
}
client.URL = serverURL
client.HTTPClient = &http.Client{
Transport: transport,
Expand Down
5 changes: 3 additions & 2 deletions cli/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func TestRoot(t *testing.T) {
assert.Equal(t, "wow", r.Header.Get("X-Testing"))
assert.Equal(t, "Dean was Here!", r.Header.Get("Cool-Header"))
assert.Equal(t, "very-wow-"+url, r.Header.Get("X-Process-Testing"))
assert.Equal(t, "more-wow", r.Header.Get("X-Process-Testing2"))
w.WriteHeader(http.StatusGone)
}))
defer srv.Close()
Expand All @@ -94,7 +95,7 @@ func TestRoot(t *testing.T) {
"--no-version-warning",
"--header", "X-Testing=wow",
"--header", "Cool-Header=Dean was Here!",
"--header-command", "printf '{\"X-Process-Testing\": \"very-wow-'"+coderURLEnv+"'\"}'",
"--header-command", "printf X-Process-Testing=very-wow-"+coderURLEnv+"'\\r\\n'X-Process-Testing2=more-wow",
"login", srv.URL,
)
inv.Stdout = buf
Expand Down Expand Up @@ -169,7 +170,7 @@ func TestDERPHeaders(t *testing.T) {
"--no-version-warning",
"ping", workspace.Name,
"-n", "1",
"--header-command", "printf '{\"X-Process-Testing\": \"very-wow\"}'",
"--header-command", "printf X-Process-Testing=very-wow",
}
for k, v := range expectedHeaders {
if k != "X-Process-Testing" {
Expand Down
5 changes: 3 additions & 2 deletions cli/testdata/coder_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ variables or flags.
Can be specified multiple times.

--header-command string, $CODER_HEADER_COMMAND
An external command that outputs JSON-encoded key-value pairs to be
used as additional HTTP headers added to all requests.
An external command that outputs additional HTTP headers added to all
requests. The command must output each header as `key=value` on its
own line.

--no-feature-warning bool, $CODER_NO_FEATURE_WARNING
Suppress warnings about unlicensed features.
Expand Down
2 changes: 1 addition & 1 deletion docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ Additional HTTP headers added to all requests. Provide as key=value. Can be spec
| Type | <code>string</code> |
| Environment | <code>$CODER_HEADER_COMMAND</code> |

An external command that outputs JSON-encoded key-value pairs to be used as additional HTTP headers added to all requests.
An external command that outputs additional HTTP headers added to all requests. The command must output each header as `key=value` on its own line.

### --no-feature-warning

Expand Down
5 changes: 3 additions & 2 deletions enterprise/cli/testdata/coder_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ variables or flags.
Can be specified multiple times.

--header-command string, $CODER_HEADER_COMMAND
An external command that outputs JSON-encoded key-value pairs to be
used as additional HTTP headers added to all requests.
An external command that outputs additional HTTP headers added to all
requests. The command must output each header as `key=value` on its
own line.

--no-feature-warning bool, $CODER_NO_FEATURE_WARNING
Suppress warnings about unlicensed features.
Expand Down