Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 80a2989

Browse files
committed
Merge branch 'main' of github.com:coder/coder into bq/fe-examples
2 parents a2e07fb + 1bc4eb5 commit 80a2989

Some content is hidden

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

67 files changed

+10064
-146
lines changed

Makefile

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,17 @@ else
4444
ZSTDFLAGS := -6
4545
endif
4646

47+
# Common paths to exclude from find commands, this rule is written so
48+
# that it can be it can be used in a chain of AND statements (meaning
49+
# you can simply write `find . $(FIND_EXCLUSIONS) -name thing-i-want`).
50+
# Note, all find statements should be written with `.` or `./path` as
51+
# the search path so that these exclusions match.
52+
FIND_EXCLUSIONS= \
53+
-not \( \( -path '*/.git/*' -o -path './build/*' -o -path './vendor/*' -o -path './.coderv2/*' -o -path '*/node_modules/*' -o -path './site/out/*' \) -prune \)
4754
# Source files used for make targets, evaluated on use.
48-
GO_SRC_FILES = $(shell find . -not \( -path './.git/*' -o -path './build/*' -o -path './vendor/*' -o -path './.coderv2/*' -o -path './site/node_modules/*' -o -path './site/out/*' \) -type f -name '*.go')
55+
GO_SRC_FILES = $(shell find . $(FIND_EXCLUSIONS) -type f -name '*.go')
4956
# All the shell files in the repo, excluding ignored files.
50-
SHELL_SRC_FILES = $(shell find . -not \( -path './.git/*' -o -path './build/*' -o -path './vendor/*' -o -path './.coderv2/*' -o -path './site/node_modules/*' -o -path './site/out/*' \) -type f -name '*.sh')
57+
SHELL_SRC_FILES = $(shell find . $(FIND_EXCLUSIONS) -type f -name '*.sh')
5158

5259
# All ${OS}_${ARCH} combos we build for. Windows binaries have the .exe suffix.
5360
OS_ARCHES := \
@@ -341,7 +348,7 @@ build/coder_helm_$(VERSION).tgz:
341348
--version "$(VERSION)" \
342349
--output "$@"
343350

344-
site/out/index.html: site/package.json $(shell find ./site -not -path './site/node_modules/*' -type f \( -name '*.ts' -o -name '*.tsx' \))
351+
site/out/index.html: site/package.json $(shell find ./site $(FIND_EXCLUSIONS) -type f \( -name '*.ts' -o -name '*.tsx' \))
345352
./scripts/yarn_install.sh
346353
cd site
347354
yarn build
@@ -403,13 +410,14 @@ gen: \
403410
provisionersdk/proto/provisioner.pb.go \
404411
provisionerd/proto/provisionerd.pb.go \
405412
site/src/api/typesGenerated.ts \
406-
docs/admin/prometheus.md
413+
docs/admin/prometheus.md \
414+
coderd/apidoc/swagger.json
407415
.PHONY: gen
408416

409417
# Mark all generated files as fresh so make thinks they're up-to-date. This is
410418
# used during releases so we don't run generation scripts.
411419
gen/mark-fresh:
412-
files="coderd/database/dump.sql coderd/database/querier.go provisionersdk/proto/provisioner.pb.go provisionerd/proto/provisionerd.pb.go site/src/api/typesGenerated.ts docs/admin/prometheus.md"
420+
files="coderd/database/dump.sql coderd/database/querier.go provisionersdk/proto/provisioner.pb.go provisionerd/proto/provisionerd.pb.go site/src/api/typesGenerated.ts docs/admin/prometheus.md coderd/apidoc/swagger.json"
413421
for file in $$files; do
414422
echo "$$file"
415423
if [ ! -f "$$file" ]; then
@@ -447,7 +455,7 @@ provisionerd/proto/provisionerd.pb.go: provisionerd/proto/provisionerd.proto
447455
--go-drpc_opt=paths=source_relative \
448456
./provisionerd/proto/provisionerd.proto
449457

450-
site/src/api/typesGenerated.ts: scripts/apitypings/main.go $(shell find codersdk -type f -name '*.go')
458+
site/src/api/typesGenerated.ts: scripts/apitypings/main.go $(shell find ./codersdk $(FIND_EXCLUSIONS) -type f -name '*.go')
451459
go run scripts/apitypings/main.go > site/src/api/typesGenerated.ts
452460
cd site
453461
yarn run format:types
@@ -457,6 +465,11 @@ docs/admin/prometheus.md: scripts/metricsdocgen/main.go scripts/metricsdocgen/me
457465
cd site
458466
yarn run format:write ../docs/admin/prometheus.md
459467

468+
coderd/apidoc/swagger.json: $(shell find ./scripts/apidocgen -not \( -path './scripts/apidocgen/node_modules' -prune \) -type f) $(wildcard coderd/*.go) $(wildcard codersdk/*.go)
469+
./scripts/apidocgen/generate.sh
470+
cd site
471+
yarn run format:write ../docs/api ../docs/manifest.json ../coderd/apidoc/swagger.json
472+
460473
update-golden-files: cli/testdata/.gen-golden
461474
.PHONY: update-golden-files
462475

agent/ports_supported.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func (lp *listeningPortsHandler) getListeningPorts() ([]codersdk.ListeningPort,
3232
seen := make(map[uint16]struct{}, len(tabs))
3333
ports := []codersdk.ListeningPort{}
3434
for _, tab := range tabs {
35-
if tab.LocalAddr == nil || tab.LocalAddr.Port < uint16(codersdk.MinimumListeningPort) {
35+
if tab.LocalAddr == nil || tab.LocalAddr.Port < codersdk.MinimumListeningPort {
3636
continue
3737
}
3838

cli/clitest/clitest.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"io/ioutil"
99
"os"
1010
"path/filepath"
11+
"strings"
1112
"testing"
1213

1314
"github.com/spf13/cobra"
@@ -55,7 +56,7 @@ func CreateTemplateVersionSource(t *testing.T, responses *echo.Responses) string
5556
directory := t.TempDir()
5657
f, err := ioutil.TempFile(directory, "*.tf")
5758
require.NoError(t, err)
58-
f.Close()
59+
_ = f.Close()
5960
data, err := echo.Tar(responses)
6061
require.NoError(t, err)
6162
extractTar(t, data, directory)
@@ -70,6 +71,9 @@ func extractTar(t *testing.T, data []byte, directory string) {
7071
break
7172
}
7273
require.NoError(t, err)
74+
if header.Name == "." || strings.Contains(header.Name, "..") {
75+
continue
76+
}
7377
// #nosec
7478
path := filepath.Join(directory, header.Name)
7579
mode := header.FileInfo().Mode()

cli/deployment/config.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ func newConfig() *codersdk.DeploymentConfig {
303303
Name: "TLS Client Auth",
304304
Usage: "Policy the server will follow for TLS Client Authentication. Accepted values are \"none\", \"request\", \"require-any\", \"verify-if-given\", or \"require-and-verify\".",
305305
Flag: "tls-client-auth",
306-
Default: "request",
306+
Default: "none",
307307
},
308308
KeyFiles: &codersdk.DeploymentConfigField[[]string]{
309309
Name: "TLS Key Files",
@@ -452,6 +452,14 @@ func newConfig() *codersdk.DeploymentConfig {
452452
Flag: "max-token-lifetime",
453453
Default: 24 * 30 * time.Hour,
454454
},
455+
Swagger: &codersdk.SwaggerConfig{
456+
Enable: &codersdk.DeploymentConfigField[bool]{
457+
Name: "Enable swagger endpoint",
458+
Usage: "Expose the swagger endpoint via /swagger.",
459+
Flag: "swagger-enable",
460+
Default: false,
461+
},
462+
},
455463
}
456464
}
457465

cli/rename_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ func TestRename(t *testing.T) {
2727
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
2828
defer cancel()
2929

30-
want := workspace.Name + "-test"
30+
// Only append one letter because it's easy to exceed maximum length:
31+
// E.g. "compassionate-chandrasekhar82" + "t".
32+
want := workspace.Name + "t"
3133
cmd, root := clitest.New(t, "rename", workspace.Name, want, "--yes")
3234
clitest.SetupConfig(t, client, root)
3335
pty := ptytest.New(t)

cli/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ func Core() []*cobra.Command {
9898
users(),
9999
versionCmd(),
100100
workspaceAgent(),
101+
vscodeipcCmd(),
101102
}
102103
}
103104

cli/server.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,10 @@ func Server(vip *viper.Viper, newAPI func(context.Context, *coderd.Options) (*co
678678
), cfg.Prometheus.Address.Value, "prometheus")()
679679
}
680680

681+
if cfg.Swagger.Enable.Value {
682+
options.SwaggerEndpoint = cfg.Swagger.Enable.Value
683+
}
684+
681685
// We use a separate coderAPICloser so the Enterprise API
682686
// can have it's own close functions. This is cleaner
683687
// than abstracting the Coder API itself.
@@ -687,16 +691,17 @@ func Server(vip *viper.Viper, newAPI func(context.Context, *coderd.Options) (*co
687691
}
688692

689693
client := codersdk.New(localURL)
690-
if cfg.TLS.Enable.Value {
691-
// Secure transport isn't needed for locally communicating!
694+
if localURL.Scheme == "https" && isLocalhost(localURL.Hostname()) {
695+
// The certificate will likely be self-signed or for a different
696+
// hostname, so we need to skip verification.
692697
client.HTTPClient.Transport = &http.Transport{
693698
TLSClientConfig: &tls.Config{
694699
//nolint:gosec
695700
InsecureSkipVerify: true,
696701
},
697702
}
698-
defer client.HTTPClient.CloseIdleConnections()
699703
}
704+
defer client.HTTPClient.CloseIdleConnections()
700705

701706
// This is helpful for tests, but can be silently ignored.
702707
// Coder may be ran as users that don't have permission to write in the homedir,
@@ -1400,7 +1405,7 @@ func startBuiltinPostgres(ctx context.Context, cfg config.Root, logger slog.Logg
14001405
if err != nil {
14011406
return "", nil, xerrors.Errorf("read postgres port: %w", err)
14021407
}
1403-
pgPort, err := strconv.Atoi(pgPortRaw)
1408+
pgPort, err := strconv.ParseUint(pgPortRaw, 10, 16)
14041409
if err != nil {
14051410
return "", nil, xerrors.Errorf("parse postgres port: %w", err)
14061411
}
@@ -1461,3 +1466,9 @@ func redirectHTTPToAccessURL(handler http.Handler, accessURL *url.URL) http.Hand
14611466
handler.ServeHTTP(w, r)
14621467
})
14631468
}
1469+
1470+
// isLocalhost returns true if the host points to the local machine. Intended to
1471+
// be called with `u.Hostname()`.
1472+
func isLocalhost(host string) bool {
1473+
return host == "localhost" || host == "127.0.0.1" || host == "::1"
1474+
}

cli/speedtest.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func speedtest() *cobra.Command {
7171
return ctx.Err()
7272
case <-ticker.C:
7373
}
74-
dur, err := conn.Ping(ctx)
74+
dur, p2p, err := conn.Ping(ctx)
7575
if err != nil {
7676
continue
7777
}
@@ -80,7 +80,7 @@ func speedtest() *cobra.Command {
8080
continue
8181
}
8282
peer := status.Peer[status.Peers()[0]]
83-
if peer.CurAddr == "" && direct {
83+
if !p2p && direct {
8484
cmd.Printf("Waiting for a direct connection... (%dms via %s)\n", dur.Milliseconds(), peer.Relay)
8585
continue
8686
}

cli/ssh_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ func setupWorkspaceForAgent(t *testing.T, mutate func([]*proto.Agent) []*proto.A
6565
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
6666
workspace := coderdtest.CreateWorkspace(t, client, user.OrganizationID, template.ID)
6767
coderdtest.AwaitWorkspaceBuildJob(t, client, workspace.LatestBuild.ID)
68+
workspace, err := client.Workspace(context.Background(), workspace.ID)
69+
require.NoError(t, err)
6870

6971
return client, workspace, agentToken
7072
}

cli/testdata/coder_server_--help.golden

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ Flags:
169169
"ecdsa", or "rsa4096".
170170
Consumes $CODER_SSH_KEYGEN_ALGORITHM
171171
(default "ed25519")
172+
--swagger-enable Expose the swagger endpoint via /swagger.
173+
Consumes $CODER_SWAGGER_ENABLE
172174
--telemetry Whether telemetry is enabled or not.
173175
Coder collects anonymized usage data to
174176
help improve our product.
@@ -196,7 +198,7 @@ Flags:
196198
"verify-if-given", or
197199
"require-and-verify".
198200
Consumes $CODER_TLS_CLIENT_AUTH (default
199-
"request")
201+
"none")
200202
--tls-client-ca-file string PEM-encoded Certificate Authority file
201203
used for checking the authenticity of
202204
client

cli/vscodeipc.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package cli
2+
3+
import (
4+
"fmt"
5+
"net"
6+
"net/http"
7+
"net/url"
8+
9+
"github.com/google/uuid"
10+
"github.com/spf13/cobra"
11+
"golang.org/x/xerrors"
12+
13+
"github.com/coder/coder/cli/cliflag"
14+
"github.com/coder/coder/cli/vscodeipc"
15+
"github.com/coder/coder/codersdk"
16+
)
17+
18+
// vscodeipcCmd spawns a local HTTP server on the provided port that listens to messages.
19+
// It's made for use by the Coder VS Code extension. See: https://github.com/coder/vscode-coder
20+
func vscodeipcCmd() *cobra.Command {
21+
var (
22+
rawURL string
23+
token string
24+
port uint16
25+
)
26+
cmd := &cobra.Command{
27+
Use: "vscodeipc <workspace-agent>",
28+
Args: cobra.ExactArgs(1),
29+
SilenceUsage: true,
30+
Hidden: true,
31+
RunE: func(cmd *cobra.Command, args []string) error {
32+
if rawURL == "" {
33+
return xerrors.New("CODER_URL must be set!")
34+
}
35+
// token is validated in a header on each request to prevent
36+
// unauthenticated clients from connecting.
37+
if token == "" {
38+
return xerrors.New("CODER_TOKEN must be set!")
39+
}
40+
listener, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", port))
41+
if err != nil {
42+
return xerrors.Errorf("listen: %w", err)
43+
}
44+
defer listener.Close()
45+
addr, ok := listener.Addr().(*net.TCPAddr)
46+
if !ok {
47+
return xerrors.Errorf("listener.Addr() is not a *net.TCPAddr: %T", listener.Addr())
48+
}
49+
url, err := url.Parse(rawURL)
50+
if err != nil {
51+
return err
52+
}
53+
agentID, err := uuid.Parse(args[0])
54+
if err != nil {
55+
return err
56+
}
57+
client := codersdk.New(url)
58+
client.SetSessionToken(token)
59+
60+
handler, closer, err := vscodeipc.New(cmd.Context(), client, agentID, nil)
61+
if err != nil {
62+
return err
63+
}
64+
defer closer.Close()
65+
// nolint:gosec
66+
server := http.Server{
67+
Handler: handler,
68+
}
69+
defer server.Close()
70+
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "%s\n", addr.String())
71+
errChan := make(chan error, 1)
72+
go func() {
73+
err := server.Serve(listener)
74+
errChan <- err
75+
}()
76+
select {
77+
case <-cmd.Context().Done():
78+
return cmd.Context().Err()
79+
case err := <-errChan:
80+
return err
81+
}
82+
},
83+
}
84+
cliflag.StringVarP(cmd.Flags(), &rawURL, "url", "u", "CODER_URL", "", "The URL of the Coder instance!")
85+
cliflag.StringVarP(cmd.Flags(), &token, "token", "t", "CODER_TOKEN", "", "The session token of the user!")
86+
cmd.Flags().Uint16VarP(&port, "port", "p", 0, "The port to listen on!")
87+
return cmd
88+
}

0 commit comments

Comments
 (0)