Deleting project with deploy key deletes the corresponding key only in database and not in authorized_keys
### Summary When deleting a project with deploy keys, the corresponding keys are only deleted in the database and not in the authorized_keys file of the git user. Because of the orphaned entry in authorized_keys the key does not work if you reuse this key on another project. ### Steps to reproduce 1. Create a private project. 2. Add a deploy key. 3. Verify that GitLab greats you with your username when logging in using "ssh -T git@your-gitbab-server". 4. Record the database id of your deploy key. Get it with this command on your GitLab server: `sudo -u gitlab-psql /opt/gitlab/embedded/bin/psql -h /var/opt/gitlab/postgresql/ gitlabhq_production -c "select id from keys where fingerprint = 'YOUR_FINGERPRINT';"`. 5. Delete project. 6. Create another private project and add the same public key as in step 2 as deploy key. 7. Try to clone the project created in step 5 over ssh. You should get the following message: ``` GitLab: The project you were looking for could not be found. fatal: Could not read from remote repository. Please make sure you have the correct access rights ``` 8. Verify that GitLab greats you as anonymous when logging in using "ssh -T git@your-gitbab-server" 9. Get the id of your deploy key again as in step 4. 10. Have a look into `/var/opt/gitlab/.ssh/authorized_keys`, search for your public key and verify that it still uses the old database id (recorded in step 4) in "command=... key-OLD_KEY" and not the new one you recorded in step 9. 12. Delete the wrong line from authorized_keys. 13. Delete deploy key from the project using web gui. 14. Readd the deploy key to your project. 15. Everything should be fine now! ### What is the current *bug* behavior? The key isn't refreshed in authorized_keys and still has the same (database) id as the old key and because of that GitLab can't find a corresponding key to your reused public key. ### What is the expected *correct* behavior? GitLab should remove the key from authorized_keys file when deleting the key in the database. ### Output of checks #### Results of GitLab environment info <details> <summary>Expand for output related to GitLab environment info</summary> <pre> System information System: Ubuntu 18.04 Current User: git Using RVM: no Ruby Version: 2.5.3p105 Gem Version: 2.7.6 Bundler Version:1.16.6 Rake Version: 12.3.2 Redis Version: 3.2.12 Git Version: 2.18.1 Sidekiq Version:5.2.5 Go Version: unknown GitLab information Version: 11.9.8 Revision: 48528bcd618 Directory: /opt/gitlab/embedded/service/gitlab-rails DB Adapter: postgresql URL: https://xxxxxx HTTP Clone URL: https://xxxxxx/some-group/some-project.git SSH Clone URL: git@xxxxxx:some-group/some-project.git Using LDAP: yes Using Omniauth: yes Omniauth Providers: GitLab Shell Version: 8.7.1 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 </pre> </details> #### Results of GitLab application Check <details> <summary>Expand for output related to the GitLab application check</summary> <pre> 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: ... Server: ldapmain LDAP authentication... Success LDAP users with access to your GitLab server (only showing the first 100 results) **Removed** 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? ... yes 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: ... 3/1 ... yes 3/2 ... yes 3/3 ... yes 3/4 ... yes 3/5 ... yes 3/6 ... yes 3/7 ... yes 3/8 ... yes 3/9 ... yes 3/10 ... yes 3/11 ... yes 3/15 ... yes 5/16 ... yes 5/17 ... yes 5/18 ... yes 6/19 ... yes 5/20 ... yes 5/21 ... yes 5/22 ... yes 7/25 ... yes 2/27 ... yes 7/29 ... yes 2/30 ... yes 5/31 ... yes 7/32 ... yes 2/37 ... yes 2/38 ... yes 2/39 ... yes 2/41 ... yes 4/42 ... yes Redis version >= 2.8.0? ... yes Ruby version >= 2.3.5 ? ... yes (2.5.3) Git version >= 2.18.0 ? ... yes (2.18.1) Git user has default SSH configuration? ... yes Active users: ... 9 Checking GitLab App ... Finished Checking GitLab subtasks ... Finished </pre> </details>
issue