- Notifications
You must be signed in to change notification settings - Fork928
feat: add workspace auditing#3966
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -22,6 +22,7 @@ import ( | ||
"cdr.dev/slog" | ||
"github.com/coder/coder/coderd/audit" | ||
"github.com/coder/coder/coderd/autobuild/schedule" | ||
"github.com/coder/coder/coderd/database" | ||
"github.com/coder/coder/coderd/httpapi" | ||
@@ -248,8 +249,18 @@ func (api *API) workspaceByOwnerAndName(rw http.ResponseWriter, r *http.Request) | ||
// Create a new workspace for the currently authenticated user. | ||
func (api *API) postWorkspacesByOrganization(rw http.ResponseWriter, r *http.Request) { | ||
var ( | ||
organization = httpmw.OrganizationParam(r) | ||
apiKey = httpmw.APIKey(r) | ||
aReq, commitAudit = audit.InitRequest[database.Workspace](rw, &audit.RequestParams{ | ||
Features: api.FeaturesService, | ||
Log: api.Logger, | ||
Request: r, | ||
Action: database.AuditActionCreate, | ||
}) | ||
) | ||
Comment on lines +252 to +261 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. It's shorter to not put these in a var block, so I'd rather have them out. What do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. I'm sorta partial to just having a var block at the top of each handler, I think it separates them cleanly from the actual code. Don't feel super strongly though, but I think it should be consistent. | ||
defer commitAudit() | ||
if !api.Authorize(r, rbac.ActionCreate, | ||
rbac.ResourceWorkspace.InOrg(organization.ID).WithOwner(apiKey.UserID.String())) { | ||
httpapi.ResourceNotFound(rw) | ||
@@ -325,7 +336,7 @@ func (api *API) postWorkspacesByOrganization(rw http.ResponseWriter, r *http.Req | ||
}) | ||
return | ||
} | ||
iferr != nil &&!errors.Is(err, sql.ErrNoRows) { | ||
httpapi.Write(rw, http.StatusInternalServerError, codersdk.Response{ | ||
Message: fmt.Sprintf("Internal error fetching workspace by name %q.", createWorkspace.Name), | ||
Detail: err.Error(), | ||
@@ -457,6 +468,8 @@ func (api *API) postWorkspacesByOrganization(rw http.ResponseWriter, r *http.Req | ||
}) | ||
return | ||
} | ||
aReq.New = workspace | ||
users, err := api.Database.GetUsersByIDs(r.Context(), []uuid.UUID{apiKey.UserID, workspaceBuild.InitiatorID}) | ||
if err != nil { | ||
httpapi.Write(rw, http.StatusInternalServerError, codersdk.Response{ | ||
@@ -476,7 +489,18 @@ func (api *API) postWorkspacesByOrganization(rw http.ResponseWriter, r *http.Req | ||
} | ||
func (api *API) patchWorkspace(rw http.ResponseWriter, r *http.Request) { | ||
var ( | ||
workspace = httpmw.WorkspaceParam(r) | ||
aReq, commitAudit = audit.InitRequest[database.Workspace](rw, &audit.RequestParams{ | ||
Features: api.FeaturesService, | ||
Log: api.Logger, | ||
Request: r, | ||
Action: database.AuditActionWrite, | ||
}) | ||
) | ||
defer commitAudit() | ||
aReq.Old = workspace | ||
if !api.Authorize(r, rbac.ActionUpdate, workspace) { | ||
httpapi.ResourceNotFound(rw) | ||
return | ||
@@ -488,18 +512,20 @@ func (api *API) patchWorkspace(rw http.ResponseWriter, r *http.Request) { | ||
} | ||
if req.Name == "" || req.Name == workspace.Name { | ||
aReq.New = workspace | ||
// Nothing changed, optionally this could be an error. | ||
rw.WriteHeader(http.StatusNoContent) | ||
return | ||
} | ||
// The reason we double check here is in case more fields can be | ||
// patched in the future, it's enough if one changes. | ||
name := workspace.Name | ||
if req.Name != "" || req.Name != workspace.Name { | ||
name = req.Name | ||
} | ||
newWorkspace, err := api.Database.UpdateWorkspace(r.Context(), database.UpdateWorkspaceParams{ | ||
ID: workspace.ID, | ||
Name: name, | ||
}) | ||
@@ -534,11 +560,23 @@ func (api *API) patchWorkspace(rw http.ResponseWriter, r *http.Request) { | ||
return | ||
} | ||
aReq.New = newWorkspace | ||
rw.WriteHeader(http.StatusNoContent) | ||
} | ||
func (api *API) putWorkspaceAutostart(rw http.ResponseWriter, r *http.Request) { | ||
var ( | ||
workspace = httpmw.WorkspaceParam(r) | ||
aReq, commitAudit = audit.InitRequest[database.Workspace](rw, &audit.RequestParams{ | ||
Features: api.FeaturesService, | ||
Log: api.Logger, | ||
Request: r, | ||
Action: database.AuditActionWrite, | ||
}) | ||
) | ||
defer commitAudit() | ||
aReq.Old = workspace | ||
if !api.Authorize(r, rbac.ActionUpdate, workspace) { | ||
httpapi.ResourceNotFound(rw) | ||
return | ||
@@ -578,10 +616,26 @@ func (api *API) putWorkspaceAutostart(rw http.ResponseWriter, r *http.Request) { | ||
}) | ||
return | ||
} | ||
newWorkspace := workspace | ||
newWorkspace.AutostartSchedule = dbSched | ||
aReq.New = newWorkspace | ||
rw.WriteHeader(http.StatusNoContent) | ||
} | ||
func (api *API) putWorkspaceTTL(rw http.ResponseWriter, r *http.Request) { | ||
var ( | ||
workspace = httpmw.WorkspaceParam(r) | ||
aReq, commitAudit = audit.InitRequest[database.Workspace](rw, &audit.RequestParams{ | ||
Features: api.FeaturesService, | ||
Log: api.Logger, | ||
Request: r, | ||
Action: database.AuditActionWrite, | ||
}) | ||
) | ||
defer commitAudit() | ||
if !api.Authorize(r, rbac.ActionUpdate, workspace) { | ||
httpapi.ResourceNotFound(rw) | ||
return | ||
@@ -592,6 +646,8 @@ func (api *API) putWorkspaceTTL(rw http.ResponseWriter, r *http.Request) { | ||
return | ||
} | ||
var dbTTL sql.NullInt64 | ||
err := api.Database.InTx(func(s database.Store) error { | ||
template, err := s.GetTemplateByID(r.Context(), workspace.TemplateID) | ||
if err != nil { | ||
@@ -601,7 +657,7 @@ func (api *API) putWorkspaceTTL(rw http.ResponseWriter, r *http.Request) { | ||
return xerrors.Errorf("fetch workspace template: %w", err) | ||
} | ||
dbTTL, err = validWorkspaceTTLMillis(req.TTLMillis, time.Duration(template.MaxTtl)) | ||
if err != nil { | ||
return codersdk.ValidationError{Field: "ttl_ms", Detail: err.Error()} | ||
} | ||
@@ -630,7 +686,11 @@ func (api *API) putWorkspaceTTL(rw http.ResponseWriter, r *http.Request) { | ||
return | ||
} | ||
newWorkspace := workspace | ||
newWorkspace.Ttl = dbTTL | ||
aReq.New = newWorkspace | ||
rw.WriteHeader(http.StatusNoContent) | ||
} | ||
func (api *API) putExtendWorkspace(rw http.ResponseWriter, r *http.Request) { | ||