Skip to content

chore: ensure default org always exists #12412

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 12 commits into from
Mar 5, 2024
Prev Previous commit
Next Next commit
ensure everyone group on first user
  • Loading branch information
Emyrk committed Mar 4, 2024
commit 9997f59e0129beddea723822ccfe3a4ac6a511d8
3 changes: 2 additions & 1 deletion coderd/database/migrations/000198_ensure_default_org.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ SELECT
true
WHERE
-- Only insert if no organizations exist.
NOT EXISTS (SELECT * FROM organizations)
NOT EXISTS (SELECT * FROM organizations);

12 changes: 12 additions & 0 deletions coderd/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,18 @@ func (api *API) postFirstUser(rw http.ResponseWriter, r *http.Request) {
return
}

//nolint:gocritic // ensure everyone group
_, err = api.Database.InsertAllUsersGroup(dbauthz.AsSystemRestricted(ctx), defaultOrg.ID)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this also be part of the migration?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about that too. What I didn't like about the migration was I can't tell if the INSERT INTO organizations ran with an insert, or without. So the followup of INSERT INTO groups cannot be dependent on if an org did not exist at the start of the migration or not.

Putting it in postFirstUser ensures that it is a new instance setup, so I know this is safe.

Although we do not allow dropping the everyone group, so it should be a safe INSERT INTO groups ... DO NOTHING.


I can move it to a migration if you feel that is better.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm just wary of all this side-effect stuff of creating a user.

I also kind of like the symmetry of creating an org and the corresponding everyone group together, since that's presumably what we'll do when we allow creating new organizations, since there is no similar "first user creation" event to tie it to.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw this comment after the approval merge, my mistake.

I can make another migration for the everyone group and do nothing on conflict.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Up to you

// A unique constraint violation just means the group already exists.
// This should not happen, but is ok if it does.
if err != nil && !database.IsUniqueViolation(err) {
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Message: "Internal error creating all users group.",
Detail: err.Error(),
})
return
}

//nolint:gocritic // needed to create first user
user, organizationID, err := api.CreateUser(dbauthz.AsSystemRestricted(ctx), api.Database, CreateUserRequest{
CreateUserRequest: codersdk.CreateUserRequest{
Expand Down