Skip to content

Commit 1a98647

Browse files
committed
Merge branch 'main' into disablessh
2 parents c89179b + 656dcc0 commit 1a98647

Some content is hidden

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

46 files changed

+920
-775
lines changed

coderd/audit.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ import (
2121
)
2222

2323
func (api *API) auditLogs(rw http.ResponseWriter, r *http.Request) {
24+
ctx := r.Context()
2425
if !api.Authorize(r, rbac.ActionRead, rbac.ResourceAuditLog) {
2526
httpapi.Forbidden(rw)
2627
return
2728
}
2829

29-
ctx := r.Context()
3030
page, ok := parsePagination(rw, r)
3131
if !ok {
3232
return
@@ -35,7 +35,7 @@ func (api *API) auditLogs(rw http.ResponseWriter, r *http.Request) {
3535
queryStr := r.URL.Query().Get("q")
3636
filter, errs := auditSearchQuery(queryStr)
3737
if len(errs) > 0 {
38-
httpapi.Write(rw, http.StatusBadRequest, codersdk.Response{
38+
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
3939
Message: "Invalid audit search query.",
4040
Validations: errs,
4141
})
@@ -56,7 +56,7 @@ func (api *API) auditLogs(rw http.ResponseWriter, r *http.Request) {
5656
return
5757
}
5858

59-
httpapi.Write(rw, http.StatusOK, codersdk.AuditLogResponse{
59+
httpapi.Write(ctx, rw, http.StatusOK, codersdk.AuditLogResponse{
6060
AuditLogs: convertAuditLogs(dblogs),
6161
})
6262
}
@@ -71,7 +71,7 @@ func (api *API) auditLogCount(rw http.ResponseWriter, r *http.Request) {
7171
queryStr := r.URL.Query().Get("q")
7272
filter, errs := auditSearchQuery(queryStr)
7373
if len(errs) > 0 {
74-
httpapi.Write(rw, http.StatusBadRequest, codersdk.Response{
74+
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
7575
Message: "Invalid audit search query.",
7676
Validations: errs,
7777
})
@@ -90,7 +90,7 @@ func (api *API) auditLogCount(rw http.ResponseWriter, r *http.Request) {
9090
return
9191
}
9292

93-
httpapi.Write(rw, http.StatusOK, codersdk.AuditLogCountResponse{
93+
httpapi.Write(ctx, rw, http.StatusOK, codersdk.AuditLogCountResponse{
9494
Count: count,
9595
})
9696
}
@@ -131,7 +131,7 @@ func (api *API) generateFakeAuditLog(rw http.ResponseWriter, r *http.Request) {
131131
}
132132

133133
var params codersdk.CreateTestAuditLogRequest
134-
if !httpapi.Read(rw, r, &params) {
134+
if !httpapi.Read(ctx, rw, r, &params) {
135135
return
136136
}
137137
if params.Action == "" {

coderd/coderd.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ func New(options *Options) *API {
188188
// Build-Version is helpful for debugging.
189189
func(next http.Handler) http.Handler {
190190
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
191-
w.Header().Add("Build-Version", buildinfo.Version())
191+
w.Header().Add("X-Coder-Build-Version", buildinfo.Version())
192192
next.ServeHTTP(w, r)
193193
})
194194
},
@@ -229,7 +229,7 @@ func New(options *Options) *API {
229229
httpmw.RateLimitPerMinute(options.APIRateLimit),
230230
)
231231
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
232-
httpapi.Write(w, http.StatusOK, codersdk.Response{
232+
httpapi.Write(r.Context(), w, http.StatusOK, codersdk.Response{
233233
//nolint:gocritic
234234
Message: "👋",
235235
})
@@ -239,7 +239,7 @@ func New(options *Options) *API {
239239

240240
r.Route("/buildinfo", func(r chi.Router) {
241241
r.Get("/", func(rw http.ResponseWriter, r *http.Request) {
242-
httpapi.Write(rw, http.StatusOK, codersdk.BuildInfoResponse{
242+
httpapi.Write(r.Context(), rw, http.StatusOK, codersdk.BuildInfoResponse{
243243
ExternalURL: buildinfo.ExternalURL(),
244244
Version: buildinfo.Version(),
245245
})
@@ -430,7 +430,7 @@ func New(options *Options) *API {
430430
// error message when transitioning from WebRTC to Tailscale. See:
431431
// https://github.com/coder/coder/issues/4126
432432
r.Get("/dial", func(w http.ResponseWriter, r *http.Request) {
433-
httpapi.Write(w, http.StatusGone, codersdk.Response{
433+
httpapi.Write(r.Context(), w, http.StatusGone, codersdk.Response{
434434
Message: "Your Coder CLI is out of date, and requires v0.8.15+ to connect!",
435435
})
436436
})

coderd/csp.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func (api *API) logReportCSPViolations(rw http.ResponseWriter, r *http.Request)
2323
err := dec.Decode(&v)
2424
if err != nil {
2525
api.Logger.Warn(ctx, "csp violation", slog.Error(err))
26-
httpapi.Write(rw, http.StatusBadRequest, codersdk.Response{
26+
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
2727
Message: "Failed to read body, invalid json.",
2828
Detail: err.Error(),
2929
})
@@ -36,5 +36,5 @@ func (api *API) logReportCSPViolations(rw http.ResponseWriter, r *http.Request)
3636
}
3737
api.Logger.Warn(ctx, "csp violation", fields...)
3838

39-
httpapi.Write(rw, http.StatusOK, "ok")
39+
httpapi.Write(ctx, rw, http.StatusOK, "ok")
4040
}

coderd/files.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
)
2020

2121
func (api *API) postFile(rw http.ResponseWriter, r *http.Request) {
22+
ctx := r.Context()
2223
apiKey := httpmw.APIKey(r)
2324
// This requires the site wide action to create files.
2425
// Once created, a user can read their own files uploaded
@@ -32,7 +33,7 @@ func (api *API) postFile(rw http.ResponseWriter, r *http.Request) {
3233
switch contentType {
3334
case "application/x-tar":
3435
default:
35-
httpapi.Write(rw, http.StatusBadRequest, codersdk.Response{
36+
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
3637
Message: fmt.Sprintf("Unsupported content type header %q.", contentType),
3738
})
3839
return
@@ -41,57 +42,58 @@ func (api *API) postFile(rw http.ResponseWriter, r *http.Request) {
4142
r.Body = http.MaxBytesReader(rw, r.Body, 10*(10<<20))
4243
data, err := io.ReadAll(r.Body)
4344
if err != nil {
44-
httpapi.Write(rw, http.StatusBadRequest, codersdk.Response{
45+
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
4546
Message: "Failed to read file from request.",
4647
Detail: err.Error(),
4748
})
4849
return
4950
}
5051
hashBytes := sha256.Sum256(data)
5152
hash := hex.EncodeToString(hashBytes[:])
52-
file, err := api.Database.GetFileByHash(r.Context(), hash)
53+
file, err := api.Database.GetFileByHash(ctx, hash)
5354
if err == nil {
5455
// The file already exists!
55-
httpapi.Write(rw, http.StatusOK, codersdk.UploadResponse{
56+
httpapi.Write(ctx, rw, http.StatusOK, codersdk.UploadResponse{
5657
Hash: file.Hash,
5758
})
5859
return
5960
}
60-
file, err = api.Database.InsertFile(r.Context(), database.InsertFileParams{
61+
file, err = api.Database.InsertFile(ctx, database.InsertFileParams{
6162
Hash: hash,
6263
CreatedBy: apiKey.UserID,
6364
CreatedAt: database.Now(),
6465
Mimetype: contentType,
6566
Data: data,
6667
})
6768
if err != nil {
68-
httpapi.Write(rw, http.StatusInternalServerError, codersdk.Response{
69+
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
6970
Message: "Internal error saving file.",
7071
Detail: err.Error(),
7172
})
7273
return
7374
}
7475

75-
httpapi.Write(rw, http.StatusCreated, codersdk.UploadResponse{
76+
httpapi.Write(ctx, rw, http.StatusCreated, codersdk.UploadResponse{
7677
Hash: file.Hash,
7778
})
7879
}
7980

8081
func (api *API) fileByHash(rw http.ResponseWriter, r *http.Request) {
82+
ctx := r.Context()
8183
hash := chi.URLParam(r, "hash")
8284
if hash == "" {
83-
httpapi.Write(rw, http.StatusBadRequest, codersdk.Response{
85+
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
8486
Message: "File hash must be provided in url.",
8587
})
8688
return
8789
}
88-
file, err := api.Database.GetFileByHash(r.Context(), hash)
90+
file, err := api.Database.GetFileByHash(ctx, hash)
8991
if errors.Is(err, sql.ErrNoRows) {
9092
httpapi.ResourceNotFound(rw)
9193
return
9294
}
9395
if err != nil {
94-
httpapi.Write(rw, http.StatusInternalServerError, codersdk.Response{
96+
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
9597
Message: "Internal error fetching file.",
9698
Detail: err.Error(),
9799
})

coderd/gitsshkey.go

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
)
1313

1414
func (api *API) regenerateGitSSHKey(rw http.ResponseWriter, r *http.Request) {
15+
ctx := r.Context()
1516
user := httpmw.UserParam(r)
1617

1718
if !api.Authorize(r, rbac.ActionUpdate, rbac.ResourceUserData.WithOwner(user.ID.String())) {
@@ -21,37 +22,37 @@ func (api *API) regenerateGitSSHKey(rw http.ResponseWriter, r *http.Request) {
2122

2223
privateKey, publicKey, err := gitsshkey.Generate(api.SSHKeygenAlgorithm)
2324
if err != nil {
24-
httpapi.Write(rw, http.StatusInternalServerError, codersdk.Response{
25+
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
2526
Message: "Internal error generating a new SSH keypair.",
2627
Detail: err.Error(),
2728
})
2829
return
2930
}
3031

31-
err = api.Database.UpdateGitSSHKey(r.Context(), database.UpdateGitSSHKeyParams{
32+
err = api.Database.UpdateGitSSHKey(ctx, database.UpdateGitSSHKeyParams{
3233
UserID: user.ID,
3334
UpdatedAt: database.Now(),
3435
PrivateKey: privateKey,
3536
PublicKey: publicKey,
3637
})
3738
if err != nil {
38-
httpapi.Write(rw, http.StatusInternalServerError, codersdk.Response{
39+
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
3940
Message: "Internal error updating user's git SSH key.",
4041
Detail: err.Error(),
4142
})
4243
return
4344
}
4445

45-
newKey, err := api.Database.GetGitSSHKey(r.Context(), user.ID)
46+
newKey, err := api.Database.GetGitSSHKey(ctx, user.ID)
4647
if err != nil {
47-
httpapi.Write(rw, http.StatusInternalServerError, codersdk.Response{
48+
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
4849
Message: "Internal error fetching user's git SSH key.",
4950
Detail: err.Error(),
5051
})
5152
return
5253
}
5354

54-
httpapi.Write(rw, http.StatusOK, codersdk.GitSSHKey{
55+
httpapi.Write(ctx, rw, http.StatusOK, codersdk.GitSSHKey{
5556
UserID: newKey.UserID,
5657
CreatedAt: newKey.CreatedAt,
5758
UpdatedAt: newKey.UpdatedAt,
@@ -61,23 +62,24 @@ func (api *API) regenerateGitSSHKey(rw http.ResponseWriter, r *http.Request) {
6162
}
6263

6364
func (api *API) gitSSHKey(rw http.ResponseWriter, r *http.Request) {
65+
ctx := r.Context()
6466
user := httpmw.UserParam(r)
6567

6668
if !api.Authorize(r, rbac.ActionRead, rbac.ResourceUserData.WithOwner(user.ID.String())) {
6769
httpapi.ResourceNotFound(rw)
6870
return
6971
}
7072

71-
gitSSHKey, err := api.Database.GetGitSSHKey(r.Context(), user.ID)
73+
gitSSHKey, err := api.Database.GetGitSSHKey(ctx, user.ID)
7274
if err != nil {
73-
httpapi.Write(rw, http.StatusInternalServerError, codersdk.Response{
75+
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
7476
Message: "Internal error fetching user's SSH key.",
7577
Detail: err.Error(),
7678
})
7779
return
7880
}
7981

80-
httpapi.Write(rw, http.StatusOK, codersdk.GitSSHKey{
82+
httpapi.Write(ctx, rw, http.StatusOK, codersdk.GitSSHKey{
8183
UserID: gitSSHKey.UserID,
8284
CreatedAt: gitSSHKey.CreatedAt,
8385
UpdatedAt: gitSSHKey.UpdatedAt,
@@ -87,44 +89,45 @@ func (api *API) gitSSHKey(rw http.ResponseWriter, r *http.Request) {
8789
}
8890

8991
func (api *API) agentGitSSHKey(rw http.ResponseWriter, r *http.Request) {
92+
ctx := r.Context()
9093
agent := httpmw.WorkspaceAgent(r)
91-
resource, err := api.Database.GetWorkspaceResourceByID(r.Context(), agent.ResourceID)
94+
resource, err := api.Database.GetWorkspaceResourceByID(ctx, agent.ResourceID)
9295
if err != nil {
93-
httpapi.Write(rw, http.StatusInternalServerError, codersdk.Response{
96+
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
9497
Message: "Internal error fetching workspace resource.",
9598
Detail: err.Error(),
9699
})
97100
return
98101
}
99102

100-
job, err := api.Database.GetWorkspaceBuildByJobID(r.Context(), resource.JobID)
103+
job, err := api.Database.GetWorkspaceBuildByJobID(ctx, resource.JobID)
101104
if err != nil {
102-
httpapi.Write(rw, http.StatusInternalServerError, codersdk.Response{
105+
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
103106
Message: "Internal error fetching workspace build.",
104107
Detail: err.Error(),
105108
})
106109
return
107110
}
108111

109-
workspace, err := api.Database.GetWorkspaceByID(r.Context(), job.WorkspaceID)
112+
workspace, err := api.Database.GetWorkspaceByID(ctx, job.WorkspaceID)
110113
if err != nil {
111-
httpapi.Write(rw, http.StatusInternalServerError, codersdk.Response{
114+
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
112115
Message: "Internal error fetching workspace.",
113116
Detail: err.Error(),
114117
})
115118
return
116119
}
117120

118-
gitSSHKey, err := api.Database.GetGitSSHKey(r.Context(), workspace.OwnerID)
121+
gitSSHKey, err := api.Database.GetGitSSHKey(ctx, workspace.OwnerID)
119122
if err != nil {
120-
httpapi.Write(rw, http.StatusInternalServerError, codersdk.Response{
123+
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
121124
Message: "Internal error fetching git SSH key.",
122125
Detail: err.Error(),
123126
})
124127
return
125128
}
126129

127-
httpapi.Write(rw, http.StatusOK, codersdk.AgentGitSSHKey{
130+
httpapi.Write(ctx, rw, http.StatusOK, codersdk.AgentGitSSHKey{
128131
PublicKey: gitSSHKey.PublicKey,
129132
PrivateKey: gitSSHKey.PrivateKey,
130133
})

0 commit comments

Comments
 (0)