Skip to content

Commit 472c8ca

Browse files
committed
Merge remote-tracking branch 'origin/main' into stevenmasley/dbauthz_less_db_call
2 parents 3ca3b12 + 7f226d4 commit 472c8ca

File tree

455 files changed

+14495
-6901
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

455 files changed

+14495
-6901
lines changed

.github/workflows/ci.yaml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ jobs:
506506
507507
- uses: actions/setup-node@v3
508508
with:
509-
node-version: "14"
509+
node-version: "16.16.0"
510510

511511
- name: Install node_modules
512512
run: ./scripts/yarn_install.sh
@@ -555,7 +555,7 @@ jobs:
555555

556556
- uses: actions/setup-node@v3
557557
with:
558-
node-version: "14"
558+
node-version: "16.16.0"
559559

560560
- name: Echo Go Cache Paths
561561
id: go-cache-paths
@@ -609,6 +609,10 @@ jobs:
609609
# only get 1 commit on shallow checkout.
610610
fetch-depth: 0
611611

612+
- uses: actions/setup-node@v3
613+
with:
614+
node-version: "16.16.0"
615+
612616
- name: Install dependencies
613617
run: cd site && yarn
614618

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113
"stretchr",
114114
"STTY",
115115
"stuntest",
116+
"tanstack",
116117
"tailbroker",
117118
"tailcfg",
118119
"tailexchange",

Makefile

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -368,9 +368,15 @@ install: build/coder_$(VERSION)_$(GOOS)_$(GOARCH)$(GOOS_BIN_EXT)
368368
cp "$<" "$$output_file"
369369
.PHONY: install
370370

371-
fmt: fmt/prettier fmt/terraform fmt/shfmt
371+
fmt: fmt/prettier fmt/terraform fmt/shfmt fmt/go
372372
.PHONY: fmt
373373

374+
fmt/go:
375+
# VS Code users should check out
376+
# https://github.com/mvdan/gofumpt#visual-studio-code
377+
go run mvdan.cc/gofumpt@v0.4.0 -w -l .
378+
.PHONY: fmt/go
379+
374380
fmt/prettier:
375381
echo "--- prettier"
376382
cd site
@@ -418,7 +424,7 @@ gen: \
418424
provisionerd/proto/provisionerd.pb.go \
419425
site/src/api/typesGenerated.ts \
420426
docs/admin/prometheus.md \
421-
docs/cli/coder.md \
427+
docs/cli.md \
422428
docs/admin/audit-logs.md \
423429
coderd/apidoc/swagger.json \
424430
.prettierignore.include \
@@ -438,7 +444,7 @@ gen/mark-fresh:
438444
provisionerd/proto/provisionerd.pb.go \
439445
site/src/api/typesGenerated.ts \
440446
docs/admin/prometheus.md \
441-
docs/cli/coder.md \
447+
docs/cli.md \
442448
docs/admin/audit-logs.md \
443449
coderd/apidoc/swagger.json \
444450
.prettierignore.include \
@@ -494,10 +500,11 @@ docs/admin/prometheus.md: scripts/metricsdocgen/main.go scripts/metricsdocgen/me
494500
cd site
495501
yarn run format:write:only ../docs/admin/prometheus.md
496502

497-
docs/cli/coder.md: scripts/clidocgen/main.go $(GO_SRC_FILES) docs/manifest.json
498-
BASE_PATH="." go run scripts/clidocgen/main.go
503+
docs/cli.md: scripts/clidocgen/main.go $(GO_SRC_FILES) docs/manifest.json
504+
rm -rf ./docs/cli/*.md
505+
BASE_PATH="." go run ./scripts/clidocgen
499506
cd site
500-
yarn run format:write:only ../docs/cli/*.md ../docs/manifest.json
507+
yarn run format:write:only ../docs/cli.md ../docs/cli/*.md ../docs/manifest.json
501508

502509
docs/admin/audit-logs.md: scripts/auditdocgen/main.go enterprise/audit/table.go
503510
go run scripts/auditdocgen/main.go

agent/agent.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ func (a *agent) runLoop(ctx context.Context) {
156156
go a.reportLifecycleLoop(ctx)
157157

158158
for retrier := retry.New(100*time.Millisecond, 10*time.Second); retrier.Wait(ctx); {
159-
a.logger.Info(ctx, "running loop")
159+
a.logger.Info(ctx, "connecting to coderd")
160160
err := a.run(ctx)
161161
// Cancel after the run is complete to clean up any leaked resources!
162162
if err == nil {
@@ -169,7 +169,7 @@ func (a *agent) runLoop(ctx context.Context) {
169169
return
170170
}
171171
if errors.Is(err, io.EOF) {
172-
a.logger.Info(ctx, "likely disconnected from coder", slog.Error(err))
172+
a.logger.Info(ctx, "disconnected from coderd")
173173
continue
174174
}
175175
a.logger.Warn(ctx, "run exited with error", slog.Error(err))
@@ -197,7 +197,7 @@ func (a *agent) reportLifecycleLoop(ctx context.Context) {
197197
break
198198
}
199199

200-
a.logger.Debug(ctx, "post lifecycle state", slog.F("state", state))
200+
a.logger.Debug(ctx, "reporting lifecycle state", slog.F("state", state))
201201

202202
err := a.client.PostLifecycle(ctx, agentsdk.PostLifecycleRequest{
203203
State: state,
@@ -242,7 +242,7 @@ func (a *agent) run(ctx context.Context) error {
242242
if err != nil {
243243
return xerrors.Errorf("fetch metadata: %w", err)
244244
}
245-
a.logger.Info(ctx, "fetched metadata")
245+
a.logger.Info(ctx, "fetched metadata", slog.F("metadata", metadata))
246246

247247
// Expand the directory and send it back to coderd so external
248248
// applications that rely on the directory can use it.
@@ -330,13 +330,10 @@ func (a *agent) run(ctx context.Context) error {
330330
go NewWorkspaceAppHealthReporter(
331331
a.logger, metadata.Apps, a.client.PostAppHealth)(appReporterCtx)
332332

333-
a.logger.Debug(ctx, "running tailnet with derpmap", slog.F("derpmap", metadata.DERPMap))
334-
335333
a.closeMutex.Lock()
336334
network := a.network
337335
a.closeMutex.Unlock()
338336
if network == nil {
339-
a.logger.Debug(ctx, "creating tailnet")
340337
network, err = a.createTailnet(ctx, metadata.DERPMap)
341338
if err != nil {
342339
return xerrors.Errorf("create tailnet: %w", err)
@@ -385,10 +382,9 @@ func (a *agent) run(ctx context.Context) error {
385382
network.SetDERPMap(metadata.DERPMap)
386383
}
387384

388-
a.logger.Debug(ctx, "running coordinator")
385+
a.logger.Debug(ctx, "running tailnet connection coordinator")
389386
err = a.runCoordinator(ctx, network)
390387
if err != nil {
391-
a.logger.Debug(ctx, "coordinator exited", slog.Error(err))
392388
return xerrors.Errorf("run coordinator: %w", err)
393389
}
394390
return nil
@@ -474,7 +470,9 @@ func (a *agent) createTailnet(ctx context.Context, derpMap *tailcfg.DERPMap) (_
474470
for {
475471
conn, err := reconnectingPTYListener.Accept()
476472
if err != nil {
477-
logger.Debug(ctx, "accept pty failed", slog.Error(err))
473+
if !a.isClosed() {
474+
logger.Debug(ctx, "accept pty failed", slog.Error(err))
475+
}
478476
break
479477
}
480478
wg.Add(1)
@@ -529,7 +527,9 @@ func (a *agent) createTailnet(ctx context.Context, derpMap *tailcfg.DERPMap) (_
529527
for {
530528
conn, err := speedtestListener.Accept()
531529
if err != nil {
532-
a.logger.Debug(ctx, "speedtest listener failed", slog.Error(err))
530+
if !a.isClosed() {
531+
a.logger.Debug(ctx, "speedtest listener failed", slog.Error(err))
532+
}
533533
break
534534
}
535535
wg.Add(1)
@@ -600,8 +600,10 @@ func (a *agent) runCoordinator(ctx context.Context, network *tailnet.Conn) error
600600
return err
601601
}
602602
defer coordinator.Close()
603-
a.logger.Info(ctx, "connected to coordination server")
604-
sendNodes, errChan := tailnet.ServeCoordinator(coordinator, network.UpdateNodes)
603+
a.logger.Info(ctx, "connected to coordination endpoint")
604+
sendNodes, errChan := tailnet.ServeCoordinator(coordinator, func(nodes []*tailnet.Node) error {
605+
return network.UpdateNodes(nodes, false)
606+
})
605607
network.SetNodeCallback(sendNodes)
606608
select {
607609
case <-ctx.Done():
@@ -644,7 +646,6 @@ func (a *agent) runStartupScript(ctx context.Context, script string) error {
644646
}
645647

646648
func (a *agent) init(ctx context.Context) {
647-
a.logger.Info(ctx, "generating host key")
648649
// Clients' should ignore the host key when connecting.
649650
// The agent needs to authenticate with coderd to SSH,
650651
// so SSH authentication doesn't improve security.

agent/agent_test.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,12 +1179,21 @@ func setupAgent(t *testing.T, metadata agentsdk.Metadata, ptyTimeout time.Durati
11791179
coordinator.ServeClient(serverConn, uuid.New(), agentID)
11801180
}()
11811181
sendNode, _ := tailnet.ServeCoordinator(clientConn, func(node []*tailnet.Node) error {
1182-
return conn.UpdateNodes(node)
1182+
return conn.UpdateNodes(node, false)
11831183
})
11841184
conn.SetNodeCallback(sendNode)
1185-
return &codersdk.WorkspaceAgentConn{
1185+
agentConn := &codersdk.WorkspaceAgentConn{
11861186
Conn: conn,
1187-
}, c, statsCh, fs
1187+
}
1188+
t.Cleanup(func() {
1189+
_ = agentConn.Close()
1190+
})
1191+
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitMedium)
1192+
defer cancel()
1193+
if !agentConn.AwaitReachable(ctx) {
1194+
t.Fatal("agent not reachable")
1195+
}
1196+
return agentConn, c, statsCh, fs
11881197
}
11891198

11901199
var dialTestPayload = []byte("dean-was-here123")

agent/ssh.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func (h *forwardedUnixHandler) HandleSSHRequest(ctx ssh.Context, _ *ssh.Server,
7070

7171
// Create socket parent dir if not exists.
7272
parentDir := filepath.Dir(addr)
73-
err = os.MkdirAll(parentDir, 0700)
73+
err = os.MkdirAll(parentDir, 0o700)
7474
if err != nil {
7575
h.log.Warn(ctx, "create parent dir for SSH unix forward request",
7676
slog.F("parent_dir", parentDir),

cli/clitest/clitest.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func extractTar(t *testing.T, data []byte, directory string) {
7878
path := filepath.Join(directory, header.Name)
7979
mode := header.FileInfo().Mode()
8080
if mode == 0 {
81-
mode = 0600
81+
mode = 0o600
8282
}
8383
switch header.Typeflag {
8484
case tar.TypeDir:

cli/cliui/gitauth.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package cliui
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"io"
7+
"time"
8+
9+
"github.com/briandowns/spinner"
10+
11+
"github.com/coder/coder/codersdk"
12+
)
13+
14+
type GitAuthOptions struct {
15+
Fetch func(context.Context) ([]codersdk.TemplateVersionGitAuth, error)
16+
FetchInterval time.Duration
17+
}
18+
19+
func GitAuth(ctx context.Context, writer io.Writer, opts GitAuthOptions) error {
20+
if opts.FetchInterval == 0 {
21+
opts.FetchInterval = 500 * time.Millisecond
22+
}
23+
gitAuth, err := opts.Fetch(ctx)
24+
if err != nil {
25+
return err
26+
}
27+
28+
spin := spinner.New(spinner.CharSets[78], 100*time.Millisecond, spinner.WithColor("fgHiGreen"))
29+
spin.Writer = writer
30+
spin.ForceOutput = true
31+
spin.Suffix = " Waiting for Git authentication..."
32+
defer spin.Stop()
33+
34+
ticker := time.NewTicker(opts.FetchInterval)
35+
defer ticker.Stop()
36+
for _, auth := range gitAuth {
37+
if auth.Authenticated {
38+
return nil
39+
}
40+
41+
_, _ = fmt.Fprintf(writer, "You must authenticate with %s to create a workspace with this template. Visit:\n\n\t%s\n\n", auth.Type.Pretty(), auth.AuthenticateURL)
42+
43+
ticker.Reset(opts.FetchInterval)
44+
spin.Start()
45+
for {
46+
select {
47+
case <-ctx.Done():
48+
return ctx.Err()
49+
case <-ticker.C:
50+
}
51+
gitAuth, err := opts.Fetch(ctx)
52+
if err != nil {
53+
return err
54+
}
55+
var authed bool
56+
for _, a := range gitAuth {
57+
if !a.Authenticated || a.ID != auth.ID {
58+
continue
59+
}
60+
authed = true
61+
break
62+
}
63+
// The user authenticated with the provider!
64+
if authed {
65+
break
66+
}
67+
}
68+
spin.Stop()
69+
_, _ = fmt.Fprintf(writer, "Successfully authenticated with %s!\n\n", auth.Type.Pretty())
70+
}
71+
return nil
72+
}

cli/cliui/gitauth_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package cliui_test
2+
3+
import (
4+
"context"
5+
"net/url"
6+
"sync/atomic"
7+
"testing"
8+
"time"
9+
10+
"github.com/spf13/cobra"
11+
"github.com/stretchr/testify/assert"
12+
13+
"github.com/coder/coder/cli/cliui"
14+
"github.com/coder/coder/codersdk"
15+
"github.com/coder/coder/pty/ptytest"
16+
"github.com/coder/coder/testutil"
17+
)
18+
19+
func TestGitAuth(t *testing.T) {
20+
t.Parallel()
21+
22+
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
23+
defer cancel()
24+
25+
ptty := ptytest.New(t)
26+
cmd := &cobra.Command{
27+
RunE: func(cmd *cobra.Command, args []string) error {
28+
var fetched atomic.Bool
29+
return cliui.GitAuth(cmd.Context(), cmd.OutOrStdout(), cliui.GitAuthOptions{
30+
Fetch: func(ctx context.Context) ([]codersdk.TemplateVersionGitAuth, error) {
31+
defer fetched.Store(true)
32+
return []codersdk.TemplateVersionGitAuth{{
33+
ID: "github",
34+
Type: codersdk.GitProviderGitHub,
35+
Authenticated: fetched.Load(),
36+
AuthenticateURL: "https://example.com/gitauth/github?redirect=" + url.QueryEscape("/gitauth?notify"),
37+
}}, nil
38+
},
39+
FetchInterval: time.Millisecond,
40+
})
41+
},
42+
}
43+
cmd.SetOutput(ptty.Output())
44+
cmd.SetIn(ptty.Input())
45+
done := make(chan struct{})
46+
go func() {
47+
defer close(done)
48+
err := cmd.Execute()
49+
assert.NoError(t, err)
50+
}()
51+
ptty.ExpectMatchContext(ctx, "You must authenticate with")
52+
ptty.ExpectMatchContext(ctx, "https://example.com/gitauth/github")
53+
ptty.ExpectMatchContext(ctx, "Successfully authenticated with GitHub")
54+
<-done
55+
}

cli/config/file.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (f File) Delete() error {
6060

6161
// Write writes the string to the file.
6262
func (f File) Write(s string) error {
63-
return write(string(f), 0600, []byte(s))
63+
return write(string(f), 0o600, []byte(s))
6464
}
6565

6666
// Read reads the file to a string.
@@ -72,7 +72,7 @@ func (f File) Read() (string, error) {
7272
// open opens a file in the configuration directory,
7373
// creating all intermediate directories.
7474
func open(path string, flag int, mode os.FileMode) (*os.File, error) {
75-
err := os.MkdirAll(filepath.Dir(path), 0750)
75+
err := os.MkdirAll(filepath.Dir(path), 0o750)
7676
if err != nil {
7777
return nil, err
7878
}

0 commit comments

Comments
 (0)