Skip to content

feat(coderd): add endpoints for editing and deleting organizations #13287

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 28 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
dbauthz and dbmem impls
  • Loading branch information
aslilac committed May 15, 2024
commit 06378e4851828d217d15e85359644c03d244c8eb
9 changes: 8 additions & 1 deletion coderd/database/dbauthz/dbauthz.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/database/dbtime"
"github.com/coder/coder/v2/coderd/httpapi/httpapiconstraints"
"github.com/coder/coder/v2/coderd/rbac"

Check failure on line 24 in coderd/database/dbauthz/dbauthz.go

View workflow job for this annotation

GitHub Actions / lint

could not import github.com/coder/coder/v2/coderd/rbac (-: # github.com/coder/coder/v2/coderd/rbac
"github.com/coder/coder/v2/coderd/util/slice"
"github.com/coder/coder/v2/provisionersdk"
)
Expand Down Expand Up @@ -115,7 +115,7 @@
func New(db database.Store, authorizer rbac.Authorizer, logger slog.Logger, acs *atomic.Pointer[AccessControlStore]) database.Store {
// If the underlying db store is already a querier, return it.
// Do not double wrap.
if slices.Contains(db.Wrappers(), wrapname) {

Check failure on line 118 in coderd/database/dbauthz/dbauthz.go

View workflow job for this annotation

GitHub Actions / lint

cannot infer S (/home/runner/go/pkg/mod/golang.org/x/exp@v0.0.0-20240222234643-814bf88cf225/slices/slices.go:116:1) (typecheck)
return db
}
return &querier{
Expand Down Expand Up @@ -420,7 +420,7 @@
}

// Authorize the action
err = authorizer.Authorize(ctx, act, action, object.RBACObject())

Check failure on line 423 in coderd/database/dbauthz/dbauthz.go

View workflow job for this annotation

GitHub Actions / lint

object.RBACObject undefined (type ObjectType has no field or method RBACObject) (typecheck)
if err != nil {
return empty, logNotAuthorizedError(ctx, logger, err)
}
Expand Down Expand Up @@ -914,6 +914,10 @@
return q.db.DeleteOldWorkspaceAgentStats(ctx)
}

func (q *querier) DeleteOrganization(ctx context.Context, id uuid.UUID) error {
return deleteQ[database.Organization](q.log, q.auth, q.db.GetOrganizationByID, q.db.DeleteOrganization)(ctx, id)
}

func (q *querier) DeleteReplicasUpdatedBefore(ctx context.Context, updatedAt time.Time) error {
if err := q.authorizeContext(ctx, policy.ActionDelete, rbac.ResourceSystem); err != nil {
return err
Expand Down Expand Up @@ -2754,7 +2758,10 @@
}

func (q *querier) UpdateOrganization(ctx context.Context, arg database.UpdateOrganizationParams) (database.Organization, error) {
panic("not implemented")
fetch := func(ctx context.Context, arg database.UpdateOrganizationParams) (database.Organization, error) {
return q.db.GetOrganizationByID(ctx, arg.ID)
}
return updateWithReturn(q.log, q.auth, fetch, q.db.UpdateOrganization)(ctx, arg)
}

func (q *querier) UpdateProvisionerDaemonLastSeenAt(ctx context.Context, arg database.UpdateProvisionerDaemonLastSeenAtParams) error {
Expand Down
19 changes: 18 additions & 1 deletion coderd/database/dbmem/dbmem.go
Original file line number Diff line number Diff line change
Expand Up @@ -1573,6 +1573,16 @@ func (q *FakeQuerier) DeleteOldWorkspaceAgentStats(_ context.Context) error {
return nil
}

func (q *FakeQuerier) DeleteOrganization(ctx context.Context, id uuid.UUID) error {
for i, org := range q.organizations {
if org.ID == id {
q.organizations = append(q.organizations[:i], q.organizations[i+1:]...)
return nil
}
}
return sql.ErrNoRows
}

func (q *FakeQuerier) DeleteReplicasUpdatedBefore(_ context.Context, before time.Time) error {
q.mutex.Lock()
defer q.mutex.Unlock()
Expand Down Expand Up @@ -7149,7 +7159,14 @@ func (q *FakeQuerier) UpdateOrganization(ctx context.Context, arg database.Updat
return database.Organization{}, err
}

panic("not implemented")
for i, org := range q.organizations {
if org.ID == arg.ID {
org.Name = arg.Name
q.organizations[i] = org
return org, nil
}
}
return database.Organization{}, sql.ErrNoRows
}

func (q *FakeQuerier) UpdateProvisionerDaemonLastSeenAt(_ context.Context, arg database.UpdateProvisionerDaemonLastSeenAtParams) error {
Expand Down
7 changes: 7 additions & 0 deletions coderd/database/dbmetrics/dbmetrics.go

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

14 changes: 14 additions & 0 deletions coderd/database/dbmock/dbmock.go

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

1 change: 1 addition & 0 deletions coderd/database/querier.go

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

13 changes: 13 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/organizations.sql
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ VALUES
-- If no organizations exist, and this is the first, make it the default.
($1, $2, $3, $4, $5, (SELECT TRUE FROM organizations LIMIT 1) IS NULL) RETURNING *;


-- name: UpdateOrganization :one
UPDATE
organizations
Expand All @@ -64,3 +63,10 @@ SET
WHERE
id = $1
RETURNING *;

-- name: DeleteOrganization :exec
DELETE FROM
organizations
WHERE
id = $1 AND
is_default = false;
1 change: 0 additions & 1 deletion coderd/organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ func (api *API) postOrganizations(rw http.ResponseWriter, r *http.Request) {
// @Router /organizations/{organization} [patch]
func (api *API) patchOrganization(rw http.ResponseWriter, r *http.Request) {
ctx := r.Context()
apiKey := httpmw.APIKey(r)

var req codersdk.PatchOrganizationRequest
if !httpapi.Read(ctx, rw, r, &req) {
Expand Down
4 changes: 2 additions & 2 deletions coderd/rbac/object.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package rbac

import (
"fmt"

Check failure on line 4 in coderd/rbac/object.go

View workflow job for this annotation

GitHub Actions / test-go (macos-latest)

"fmt" imported and not used

Check failure on line 4 in coderd/rbac/object.go

View workflow job for this annotation

GitHub Actions / test-go (macos-latest)

"fmt" imported and not used

Check failure on line 4 in coderd/rbac/object.go

View workflow job for this annotation

GitHub Actions / gen

"fmt" imported and not used

Check failure on line 4 in coderd/rbac/object.go

View workflow job for this annotation

GitHub Actions / test-go-pg

"fmt" imported and not used

Check failure on line 4 in coderd/rbac/object.go

View workflow job for this annotation

GitHub Actions / test-go-pg

"fmt" imported and not used

Check failure on line 4 in coderd/rbac/object.go

View workflow job for this annotation

GitHub Actions / test-go (ubuntu-latest)

"fmt" imported and not used

Check failure on line 4 in coderd/rbac/object.go

View workflow job for this annotation

GitHub Actions / test-go (ubuntu-latest)

"fmt" imported and not used

Check failure on line 4 in coderd/rbac/object.go

View workflow job for this annotation

GitHub Actions / test-e2e

"fmt" imported and not used

Check failure on line 4 in coderd/rbac/object.go

View workflow job for this annotation

GitHub Actions / lint

"fmt" imported and not used

Check failure on line 4 in coderd/rbac/object.go

View workflow job for this annotation

GitHub Actions / lint

"fmt" imported and not used

Check failure on line 4 in coderd/rbac/object.go

View workflow job for this annotation

GitHub Actions / test-e2e-enterprise

"fmt" imported and not used

Check failure on line 4 in coderd/rbac/object.go

View workflow job for this annotation

GitHub Actions / test-go-race

"fmt" imported and not used

Check failure on line 4 in coderd/rbac/object.go

View workflow job for this annotation

GitHub Actions / test-go-race

"fmt" imported and not used

Check failure on line 4 in coderd/rbac/object.go

View workflow job for this annotation

GitHub Actions / test-go (windows-2022)

"fmt" imported and not used

Check failure on line 4 in coderd/rbac/object.go

View workflow job for this annotation

GitHub Actions / test-go (windows-2022)

"fmt" imported and not used

"github.com/google/uuid"

Expand Down Expand Up @@ -36,10 +36,10 @@
func (z Object) ValidAction(action policy.Action) error {
perms, ok := policy.RBACPermissions[z.Type]
if !ok {
return fmt.Errorf("invalid type %q", z.Type)
return xerrors.Errorf("invalid type %q", z.Type)

Check failure on line 39 in coderd/rbac/object.go

View workflow job for this annotation

GitHub Actions / test-go (macos-latest)

undefined: xerrors

Check failure on line 39 in coderd/rbac/object.go

View workflow job for this annotation

GitHub Actions / test-go (macos-latest)

undefined: xerrors

Check failure on line 39 in coderd/rbac/object.go

View workflow job for this annotation

GitHub Actions / gen

undefined: xerrors

Check failure on line 39 in coderd/rbac/object.go

View workflow job for this annotation

GitHub Actions / test-go-pg

undefined: xerrors

Check failure on line 39 in coderd/rbac/object.go

View workflow job for this annotation

GitHub Actions / test-go-pg

undefined: xerrors

Check failure on line 39 in coderd/rbac/object.go

View workflow job for this annotation

GitHub Actions / test-go (ubuntu-latest)

undefined: xerrors

Check failure on line 39 in coderd/rbac/object.go

View workflow job for this annotation

GitHub Actions / test-go (ubuntu-latest)

undefined: xerrors

Check failure on line 39 in coderd/rbac/object.go

View workflow job for this annotation

GitHub Actions / test-e2e

undefined: xerrors

Check failure on line 39 in coderd/rbac/object.go

View workflow job for this annotation

GitHub Actions / lint

undefined: xerrors

Check failure on line 39 in coderd/rbac/object.go

View workflow job for this annotation

GitHub Actions / lint

undefined: xerrors

Check failure on line 39 in coderd/rbac/object.go

View workflow job for this annotation

GitHub Actions / test-e2e-enterprise

undefined: xerrors

Check failure on line 39 in coderd/rbac/object.go

View workflow job for this annotation

GitHub Actions / test-go-race

undefined: xerrors

Check failure on line 39 in coderd/rbac/object.go

View workflow job for this annotation

GitHub Actions / test-go-race

undefined: xerrors

Check failure on line 39 in coderd/rbac/object.go

View workflow job for this annotation

GitHub Actions / test-go (windows-2022)

undefined: xerrors

Check failure on line 39 in coderd/rbac/object.go

View workflow job for this annotation

GitHub Actions / test-go (windows-2022)

undefined: xerrors
}
if _, ok := perms.Actions[action]; !ok {
return fmt.Errorf("invalid action %q for type %q", action, z.Type)
return xerrors.Errorf("invalid action %q for type %q", action, z.Type)

Check failure on line 42 in coderd/rbac/object.go

View workflow job for this annotation

GitHub Actions / test-go (macos-latest)

undefined: xerrors

Check failure on line 42 in coderd/rbac/object.go

View workflow job for this annotation

GitHub Actions / test-go (macos-latest)

undefined: xerrors

Check failure on line 42 in coderd/rbac/object.go

View workflow job for this annotation

GitHub Actions / gen

undefined: xerrors

Check failure on line 42 in coderd/rbac/object.go

View workflow job for this annotation

GitHub Actions / test-go-pg

undefined: xerrors

Check failure on line 42 in coderd/rbac/object.go

View workflow job for this annotation

GitHub Actions / test-go-pg

undefined: xerrors

Check failure on line 42 in coderd/rbac/object.go

View workflow job for this annotation

GitHub Actions / test-go (ubuntu-latest)

undefined: xerrors

Check failure on line 42 in coderd/rbac/object.go

View workflow job for this annotation

GitHub Actions / test-go (ubuntu-latest)

undefined: xerrors

Check failure on line 42 in coderd/rbac/object.go

View workflow job for this annotation

GitHub Actions / test-e2e

undefined: xerrors

Check failure on line 42 in coderd/rbac/object.go

View workflow job for this annotation

GitHub Actions / lint

undefined: xerrors) (typecheck)

Check failure on line 42 in coderd/rbac/object.go

View workflow job for this annotation

GitHub Actions / lint

undefined: xerrors) (typecheck)

Check failure on line 42 in coderd/rbac/object.go

View workflow job for this annotation

GitHub Actions / test-e2e-enterprise

undefined: xerrors

Check failure on line 42 in coderd/rbac/object.go

View workflow job for this annotation

GitHub Actions / test-go-race

undefined: xerrors

Check failure on line 42 in coderd/rbac/object.go

View workflow job for this annotation

GitHub Actions / test-go-race

undefined: xerrors

Check failure on line 42 in coderd/rbac/object.go

View workflow job for this annotation

GitHub Actions / test-go (windows-2022)

undefined: xerrors

Check failure on line 42 in coderd/rbac/object.go

View workflow job for this annotation

GitHub Actions / test-go (windows-2022)

undefined: xerrors
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion coderd/rbac/roles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ func TestRolePermissions(t *testing.T) {
},
// Some admin style resources
{
Name: "Licences",
Name: "Licenses",
Actions: []policy.Action{policy.ActionCreate, policy.ActionRead, policy.ActionDelete},
Resource: rbac.ResourceLicense,
AuthorizeMap: map[bool][]authSubject{
Expand Down
Loading