"Email has already been taken" creating Project Access Tokens

Summary

This is a weird edge case, but you might want to address it. If there is a user on an instance with a secondary email created to match the pattern projectID_botX@example.com, the project access tokens will fail to create when the X starts matching the one in the email.

Steps to reproduce

Create a user on an instance, create a project (assume it has and id of 111). Add a secondary email to the created user with a pattern: project111_bot@example.com. Try creating a project access token for project 111 -> "Email has already been taken"

Tested on 13.6.0-ee.0

When creating via rails console we get "Email has already been taken"

irb(main):003:0> project = Project.find(7)
=> #<Project id:7 dddddd/ubggggggg/ppppppp>>
irb(main):004:0> current_user = User.find_by_username('root')
=> #<User id:1 @root>
irb(main):005:0> key_identity = "#{current_user.id}:#{project.id}"
=> "1:7"
irb(main):006:0> token_response = ResourceAccessTokens::CreateService.new(current_user, project, {}).execute
=> #<ServiceResponse:0x00007fa562a9a0b0 @status=:error, @message="Email has already been taken", @payload={}, @http_status=nil>

Example Project

none

What is the current bug behavior?

error "Failed to create new project access token: Email has already been taken"

What is the expected correct behavior?

Project Access Token is successfully created.

Relevant logs and/or screenshots

image

Output of checks

Results of GitLab environment info

Expand for output related to GitLab environment info
System information
System:		Ubuntu 18.04
Proxy:		no
Current User:	git
Using RVM:	no
Ruby Version:	2.7.2p137
Gem Version:	3.1.4
Bundler Version:2.1.4
Rake Version:	13.0.1
Redis Version:	5.0.9
Git Version:	2.29.0
Sidekiq Version:5.2.9
Go Version:	unknown

GitLab information
Version:	13.6.0-ee
Revision:	c050e8fb184
Directory:	/opt/gitlab/embedded/service/gitlab-rails
DB Adapter:	PostgreSQL
DB Version:	11.7
URL:		https://kategrechishkina-gitlab-1.sr.gitlab.support
HTTP Clone URL:	https://kategrechishkina-gitlab-1.sr.gitlab.support/some-group/some-project.git
SSH Clone URL:	git@kategrechishkina-gitlab-1.sr.gitlab.support:some-group/some-project.git
Elasticsearch:	no
Geo:		no
Using LDAP:	no
Using Omniauth:	yes
Omniauth Providers: 

GitLab Shell
Version:	13.13.0
Repository storage paths:
- default: 	/var/opt/gitlab/git-data/repositories
GitLab Shell path:		/opt/gitlab/embedded/service/gitlab-shell
Git:		/opt/gitlab/embedded/bin/git

Results of GitLab application Check

Expand for output related to the GitLab application check
Checking GitLab subtasks ...

Checking GitLab Shell ...

GitLab Shell: ... GitLab Shell version >= 13.13.0 ? ... OK (13.13.0) Running /opt/gitlab/embedded/service/gitlab-shell/bin/check Internal API available: OK Redis available via internal API: OK gitlab-shell self-check successful

Checking GitLab Shell ... Finished

Checking Gitaly ...

Gitaly: ... default ... OK

Checking Gitaly ... Finished

Checking Sidekiq ...

Sidekiq: ... Running? ... yes Number of Sidekiq processes ... 1

Checking Sidekiq ... Finished

Checking Incoming Email ...

Incoming Email: ... Reply by email is disabled in config/gitlab.yml

Checking Incoming Email ... Finished

Checking LDAP ...

LDAP: ... LDAP is disabled in config/gitlab.yml

Checking LDAP ... Finished

Checking GitLab App ...

Git configured correctly? ... yes Database config exists? ... yes All migrations up? ... yes Database contains orphaned GroupMembers? ... no GitLab config exists? ... yes GitLab config up to date? ... yes Log directory writable? ... yes Tmp directory writable? ... yes Uploads directory exists? ... yes Uploads directory has correct permissions? ... yes Uploads directory tmp has correct permissions? ... skipped (no tmp uploads folder yet) Init script exists? ... skipped (omnibus-gitlab has no init script) Init script up-to-date? ... skipped (omnibus-gitlab has no init script) Projects have namespace: ... 4/1 ... yes 5/2 ... yes 6/3 ... yes 9/4 ... yes 14/5 ... yes 18/6 ... yes 19/7 ... yes 9/8 ... yes Redis version >= 4.0.0? ... yes Ruby version >= 2.5.3 ? ... yes (2.7.2) Git version >= 2.29.0 ? ... yes (2.29.0) Git user has default SSH configuration? ... yes Active users: ... 23 Is authorized keys file accessible? ... yes GitLab configured to store new projects in hashed storage? ... yes All projects are in hashed storage? ... yes Elasticsearch version 7.x (6.4 - 6.x deprecated to be removed in 13.8)? ... skipped (elasticsearch is disabled)

Checking GitLab App ... Finished

Checking GitLab subtasks ... Finished

Possible fixes

Edited by Kate Grechishkina