Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc763cee

Browse files
committed
Add header-process flag
This allows specifying a command to run that can output headers forcases where users require dynamic headers (like to authenticate to theirVPN).The primary use case is to add this flag in SSH configs created by theVS Code plugin, although maybe config-ssh should do the same.
1 parent6af6e85 commitc763cee

File tree

7 files changed

+69
-13
lines changed

7 files changed

+69
-13
lines changed

‎cli/login.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func (r *RootCmd) login() *clibase.Cmd {
7676
serverURL.Scheme="https"
7777
}
7878

79-
client,err:=r.createUnauthenticatedClient(serverURL)
79+
client,err:=r.createUnauthenticatedClient(ctx,serverURL)
8080
iferr!=nil {
8181
returnerr
8282
}

‎cli/root.go

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"net/http"
1414
"net/url"
1515
"os"
16+
"os/exec"
1617
"os/signal"
1718
"path/filepath"
1819
"runtime"
@@ -55,6 +56,7 @@ const (
5556
varAgentToken="agent-token"
5657
varAgentURL="agent-url"
5758
varHeader="header"
59+
varHeaderProcess="header-process"
5860
varNoOpen="no-open"
5961
varNoVersionCheck="no-version-warning"
6062
varNoFeatureWarning="no-feature-warning"
@@ -356,6 +358,13 @@ func (r *RootCmd) Command(subcommands []*clibase.Cmd) (*clibase.Cmd, error) {
356358
Value:clibase.StringArrayOf(&r.header),
357359
Group:globalGroup,
358360
},
361+
{
362+
Flag:varHeaderProcess,
363+
Env:"CODER_HEADER_PROCESS",
364+
Description:"An external process that outputs JSON-encoded key-value pairs to be used as additional HTTP headers added to all requests.",
365+
Value:clibase.StringOf(&r.headerProcess),
366+
Group:globalGroup,
367+
},
359368
{
360369
Flag:varNoOpen,
361370
Env:"CODER_NO_OPEN",
@@ -437,6 +446,7 @@ type RootCmd struct {
437446
tokenstring
438447
globalConfigstring
439448
header []string
449+
headerProcessstring
440450
agentTokenstring
441451
agentURL*url.URL
442452
forceTTYbool
@@ -540,9 +550,7 @@ func (r *RootCmd) initClientInternal(client *codersdk.Client, allowTokenMissing
540550
returnerr
541551
}
542552
}
543-
err=r.setClient(
544-
client,r.clientURL,
545-
)
553+
err=r.setClient(inv.Context(),client,r.clientURL)
546554
iferr!=nil {
547555
returnerr
548556
}
@@ -592,7 +600,7 @@ func (r *RootCmd) initClientInternal(client *codersdk.Client, allowTokenMissing
592600
}
593601
}
594602

595-
func (r*RootCmd)setClient(client*codersdk.Client,serverURL*url.URL)error {
603+
func (r*RootCmd)setClient(ctx context.Context,client*codersdk.Client,serverURL*url.URL)error {
596604
transport:=&headerTransport{
597605
transport:http.DefaultTransport,
598606
header: http.Header{},
@@ -604,16 +612,39 @@ func (r *RootCmd) setClient(client *codersdk.Client, serverURL *url.URL) error {
604612
}
605613
transport.header.Add(parts[0],parts[1])
606614
}
615+
ifr.headerProcess!="" {
616+
shell:="sh"
617+
caller:="-c"
618+
ifruntime.GOOS=="windows" {
619+
shell="cmd.exe"
620+
caller="/c"
621+
}
622+
// #nosec
623+
cmd:=exec.CommandContext(ctx,shell,caller,r.headerProcess)
624+
cmd.Env=append(os.Environ(),"CODER_URL="+serverURL.String())
625+
out,err:=cmd.Output()
626+
iferr!=nil {
627+
returnxerrors.Errorf("failed to run %v (out: %q): %w",cmd.Args,out,err)
628+
}
629+
varheadersmap[string]string
630+
err=json.Unmarshal(out,&headers)
631+
iferr!=nil {
632+
returnxerrors.Errorf("failed to parse json from %v (out: %q): %w",cmd.Args,out,err)
633+
}
634+
forkey,value:=rangeheaders {
635+
transport.header.Add(key,value)
636+
}
637+
}
607638
client.URL=serverURL
608639
client.HTTPClient=&http.Client{
609640
Transport:transport,
610641
}
611642
returnnil
612643
}
613644

614-
func (r*RootCmd)createUnauthenticatedClient(serverURL*url.URL) (*codersdk.Client,error) {
645+
func (r*RootCmd)createUnauthenticatedClient(ctx context.Context,serverURL*url.URL) (*codersdk.Client,error) {
615646
varclient codersdk.Client
616-
err:=r.setClient(&client,serverURL)
647+
err:=r.setClient(ctx,&client,serverURL)
617648
return&client,err
618649
}
619650

‎cli/root_test.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,20 +72,24 @@ func TestRoot(t *testing.T) {
7272
t.Run("Header",func(t*testing.T) {
7373
t.Parallel()
7474

75+
varurlstring
7576
varcalledint64
7677
srv:=httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter,r*http.Request) {
7778
atomic.AddInt64(&called,1)
7879
assert.Equal(t,"wow",r.Header.Get("X-Testing"))
7980
assert.Equal(t,"Dean was Here!",r.Header.Get("Cool-Header"))
81+
assert.Equal(t,"very-wow-"+url,r.Header.Get("X-Process-Testing"))
8082
w.WriteHeader(http.StatusGone)
8183
}))
8284
defersrv.Close()
85+
url=srv.URL
8386
buf:=new(bytes.Buffer)
8487
inv,_:=clitest.New(t,
8588
"--no-feature-warning",
8689
"--no-version-warning",
8790
"--header","X-Testing=wow",
8891
"--header","Cool-Header=Dean was Here!",
92+
"--header-process","printf '{\"X-Process-Testing\":\"very-wow-'$CODER_URL'\"}'",
8993
"login",srv.URL,
9094
)
9195
inv.Stdout=buf
@@ -97,8 +101,8 @@ func TestRoot(t *testing.T) {
97101
})
98102
}
99103

100-
// TestDERPHeaders ensures that the client sends the global `--header`sto the
101-
// DERP server when connecting.
104+
// TestDERPHeaders ensures that the client sends the global `--header`sand
105+
//`--header-process` to theDERP server when connecting.
102106
funcTestDERPHeaders(t*testing.T) {
103107
t.Parallel()
104108

@@ -129,8 +133,9 @@ func TestDERPHeaders(t *testing.T) {
129133
// Inject custom /derp handler so we can inspect the headers.
130134
var (
131135
expectedHeaders=map[string]string{
132-
"X-Test-Header":"test-value",
133-
"Cool-Header":"Dean was Here!",
136+
"X-Test-Header":"test-value",
137+
"Cool-Header":"Dean was Here!",
138+
"X-Process-Testing":"very-wow",
134139
}
135140
derpCalledint64
136141
)
@@ -159,9 +164,12 @@ func TestDERPHeaders(t *testing.T) {
159164
"--no-version-warning",
160165
"ping",workspace.Name,
161166
"-n","1",
167+
"--header-process","printf '{\"X-Process-Testing\":\"very-wow\"}'",
162168
}
163169
fork,v:=rangeexpectedHeaders {
164-
args=append(args,"--header",fmt.Sprintf("%s=%s",k,v))
170+
ifk!="X-Process-Testing" {
171+
args=append(args,"--header",fmt.Sprintf("%s=%s",k,v))
172+
}
165173
}
166174
inv,root:=clitest.New(t,args...)
167175
clitest.SetupConfig(t,client,root)

‎cli/testdata/coder_--help.golden

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ variables or flags.
6262
Additional HTTP headers added to all requests. Provide as key=value.
6363
Can be specified multiple times.
6464

65+
--header-process string, $CODER_HEADER_PROCESS
66+
An external process that outputs JSON-encoded key-value pairs to be
67+
used as additional HTTP headers added to all requests.
68+
6569
--no-feature-warning bool, $CODER_NO_FEATURE_WARNING
6670
Suppress warnings about unlicensed features.
6771

‎cli/vscodessh.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func (r *RootCmd) vscodeSSH() *clibase.Cmd {
8686
client.SetSessionToken(string(sessionToken))
8787

8888
// This adds custom headers to the request!
89-
err=r.setClient(client,serverURL)
89+
err=r.setClient(ctx,client,serverURL)
9090
iferr!=nil {
9191
returnxerrors.Errorf("set client: %w",err)
9292
}

‎docs/cli.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,15 @@ Path to the global `coder` config directory.
9696

9797
Additional HTTP headers added to all requests. Provide as key=value. Can be specified multiple times.
9898

99+
###--header-process
100+
101+
|||
102+
| -----------| ----------------------------------|
103+
| Type| <code>string</code>|
104+
| Environment| <code>$CODER_HEADER_PROCESS</code>|
105+
106+
An external process that outputs JSON-encoded key-value pairs to be used as additional HTTP headers added to all requests.
107+
99108
###--no-feature-warning
100109

101110
|||

‎enterprise/cli/testdata/coder_--help.golden

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ variables or flags.
3333
Additional HTTP headers added to all requests. Provide as key=value.
3434
Can be specified multiple times.
3535

36+
--header-process string, $CODER_HEADER_PROCESS
37+
An external process that outputs JSON-encoded key-value pairs to be
38+
used as additional HTTP headers added to all requests.
39+
3640
--no-feature-warning bool, $CODER_NO_FEATURE_WARNING
3741
Suppress warnings about unlicensed features.
3842

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp