Skip to content
Merged
Prev Previous commit
Next Next commit
protect reserved keys
  • Loading branch information
f0ssel committed Sep 16, 2024
commit 40826789d1b4562e9bfe10b986c5b48e6fa84ff4
6 changes: 6 additions & 0 deletions coderd/database/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion coderd/database/queries/provisionerkeys.sql
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,13 @@ SELECT
FROM
provisioner_keys
WHERE
organization_id = $1;
organization_id = $1
AND
id != '11111111-1111-1111-1111-111111111111'::uuid
AND
id != '22222222-2222-2222-2222-222222222222'::uuid
AND
id != '33333333-3333-3333-3333-333333333333'::uuid;

-- name: DeleteProvisionerKey :exec
DELETE FROM
Expand Down
33 changes: 20 additions & 13 deletions enterprise/coderd/provisionerkeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package coderd
import (
"fmt"
"net/http"
"slices"

"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/httpapi"
Expand Down Expand Up @@ -54,6 +55,20 @@ func (api *API) postProvisionerKey(rw http.ResponseWriter, r *http.Request) {
return
}

reserved := []string{"built-in", "psk", "user-auth"}
if slices.Contains(reserved, req.Name) {
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
Message: fmt.Sprintf("Name cannot be reserved name '%s'", req.Name),
Validations: []codersdk.ValidationError{
{
Field: "name",
Detail: fmt.Sprintf("Name cannot be reserved name '%s'", req.Name),
},
},
})
return
}

params, token, err := provisionerkey.New(organization.ID, req.Name, req.Tags)
if err != nil {
httpapi.InternalServerError(rw, err)
Expand Down Expand Up @@ -108,24 +123,16 @@ func (api *API) provisionerKeys(rw http.ResponseWriter, r *http.Request) {
// @Router /organizations/{organization}/provisionerkeys/{provisionerkey} [delete]
func (api *API) deleteProvisionerKey(rw http.ResponseWriter, r *http.Request) {
ctx := r.Context()
organization := httpmw.OrganizationParam(r)
provisionerKey := httpmw.ProvisionerKeyParam(r)

pk, err := api.Database.GetProvisionerKeyByName(ctx, database.GetProvisionerKeyByNameParams{
OrganizationID: organization.ID,
Name: provisionerKey.Name,
})
if err != nil {
if httpapi.Is404Error(err) {
httpapi.ResourceNotFound(rw)
return
}

httpapi.InternalServerError(rw, err)
if provisionerKey.ID.String() == codersdk.ProvisionerKeyIDBuiltIn || provisionerKey.ID.String() == codersdk.ProvisionerKeyIDPSK || provisionerKey.ID.String() == codersdk.ProvisionerKeyIDUserAuth {
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
Message: fmt.Sprintf("Cannot delete '%s' provisioner key", provisionerKey.Name),
})
return
}

err = api.Database.DeleteProvisionerKey(ctx, pk.ID)
err := api.Database.DeleteProvisionerKey(ctx, provisionerKey.ID)
if err != nil {
httpapi.InternalServerError(rw, err)
return
Expand Down