Skip to content
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

Fix project creation conflict during user creation #3815

Merged
merged 3 commits into from
Jul 10, 2024

Conversation

rdimitrov
Copy link
Member

Summary

The following PR fixes the conflict that may happen if the project we try to create during CreateUser already exists.

The proposed solution checks if there's such project with that name, if there's appends a random 4 char string and tries again until it finds a unique combination.

Change Type

Mark the type of change your PR introduces:

  • Bug fix (resolves an issue without affecting existing features)
  • Feature (adds new functionality without breaking changes)
  • Breaking change (may impact existing functionalities or require documentation updates)
  • Documentation (updates or additions to documentation)
  • Refactoring or test improvements (no bug fixes or new functionality)

Testing

Outline how the changes were tested, including steps to reproduce and any relevant configurations.
Attach screenshots if helpful.

Review Checklist:

  • Reviewed my own code for quality and clarity.
  • Added comments to complex or tricky code sections.
  • Updated any affected documentation.
  • Included tests that validate the fix or feature.
  • Checked that related changes are merged.

@rdimitrov rdimitrov requested a review from a team as a code owner July 9, 2024 13:01
@coveralls
Copy link

coveralls commented Jul 9, 2024

Coverage Status

coverage: 52.132% (-0.07%) from 52.201%
when pulling 404d81a on fix-project-conflict
into bef2527 on main.

jhrozek
jhrozek previously approved these changes Jul 10, 2024
Copy link
Contributor

@jhrozek jhrozek left a comment

Choose a reason for hiding this comment

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

Since this is a hack to get around an immediate bug, feel free to merge, but please also open an issue for the proper fix

@@ -78,7 +80,13 @@ func (s *Server) CreateUser(ctx context.Context,
// Set up the default project for the user
baseName := subject
if token.PreferredUsername() != "" {
baseName = token.PreferredUsername()
// Check if `project_name_lower_idx` unique constraint was violated. This happens when
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this an outdated comment? We check the other way around, to see if there's sql.ErrNoRows returned right?

Copy link
Member Author

Choose a reason for hiding this comment

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

You're right, this was a leftover copy-pasta from the other place where we had this check 👍

// getUniqueProjectBaseName is used to generate a unique project name
func getUniqueProjectBaseName(ctx context.Context, store db.Store, baseName string) (string, error) {
uniqueBaseName := baseName
for {
Copy link
Contributor

Choose a reason for hiding this comment

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

I know it's unlikely we hit a really infinite loop here, but for {} always makes me nervous. Do you think we could add some upper limit?

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'll add a 10 times retry mechanism. It should be more than enough 👍

@rdimitrov
Copy link
Member Author

@jhrozek - Opened an issue to track the proper way to fix this through refactoring - #3826

Signed-off-by: Radoslav Dimitrov <radoslav@stacklok.com>
Signed-off-by: Radoslav Dimitrov <radoslav@stacklok.com>
@rdimitrov rdimitrov merged commit 33d7744 into main Jul 10, 2024
20 checks passed
@rdimitrov rdimitrov deleted the fix-project-conflict branch July 10, 2024 11:15
dmjb pushed a commit that referenced this pull request Jul 12, 2024
* Fix project creation conflict during user creation

Signed-off-by: Radoslav Dimitrov <radoslav@stacklok.com>

* Add for loop retry counter and update comment

Signed-off-by: Radoslav Dimitrov <radoslav@stacklok.com>

---------

Signed-off-by: Radoslav Dimitrov <radoslav@stacklok.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants