Gitlab: 使用API方式修改邮箱不成功的对应方法

末蓝、 2023-03-03 11:13 27阅读 0赞

在这里插入图片描述

这篇文章介绍一下在Gitlab中通过API修改邮箱不成功的对应方法。

现象说明

使用API修改邮箱(字段为email),返回信息正常,但是结果中仍为之前邮箱信息。

原因与对应方法

版本说明

本文memo的内容为Gitlab的Api为v4版本的情况

环境准备

环境的准备以及token等信息的设定可参看下文:

  • https://liumiaocn.blog.csdn.net/article/details/107443400

问题再现

测试用户准备

准备如下用户信息(liumiao/12341234), 执行日志如下所示:

  1. liumiaocn:api liumiao$ curl -X POST -H "PRIVATE-TOKEN: ${access_token}" http://${gitlab_url}/api/v4/users \
  2. > -H 'cache-control: no-cache' \
  3. > -H 'content-type: application/json' \
  4. > -d '{ "email": "liumiaocn@outlook.com",
  5. > "username": "liumiaocn",
  6. > "password": "12341234",
  7. > "name": "liumiaocn",
  8. > "skip_confirmation": "true"
  9. > }' |jq .
  10. % Total % Received % Xferd Average Speed Time Time Time Current
  11. Dload Upload Total Spent Left Speed
  12. 100 833 100 690 100 143 1751 362 --:--:-- --:--:-- --:--:-- 2114
  13. {
  14. "id": 2,
  15. "name": "liumiaocn",
  16. "username": "liumiaocn",
  17. "state": "active",
  18. "avatar_url": "https://www.gravatar.com/avatar/95c1f7ff72d71b448592a335ba80fb64?s=80&d=identicon",
  19. "web_url": "http://fb1429a3b28c/liumiaocn",
  20. "created_at": "2020-07-30T23:20:49.710Z",
  21. "bio": null,
  22. "location": null,
  23. "skype": "",
  24. "linkedin": "",
  25. "twitter": "",
  26. "website_url": "",
  27. "organization": null,
  28. "last_sign_in_at": null,
  29. "confirmed_at": "2020-07-30T23:20:49.650Z",
  30. "last_activity_on": null,
  31. "email": "liumiaocn@outlook.com",
  32. "theme_id": 1,
  33. "color_scheme_id": 1,
  34. "projects_limit": 100000,
  35. "current_sign_in_at": null,
  36. "identities": [],
  37. "can_create_group": true,
  38. "can_create_project": true,
  39. "two_factor_enabled": false,
  40. "external": false,
  41. "private_profile": null
  42. }
  43. liumiaocn:api liumiao$

登录确认此用户的邮箱地址
在这里插入图片描述

修改用户邮箱

将用户邮箱从liumiaocn@outlook.com修改为liumiao123@outlook.com

  1. liumiaocn:api liumiao$ userid=2
  2. liumiaocn:api liumiao$ curl -X PUT -H "PRIVATE-TOKEN: ${access_token}" http://${gitlab_url}/api/v4/users/${userid} \
  3. > -H 'cache-control: no-cache' \
  4. > -H 'content-type: application/json' \
  5. > -d '{ "email": "liumiaocn123@outlook.com"}'
  6. {"id":2,"name":"liumiaocn","username":"liumiaocn","state":"active","avatar_url":"https://www.gravatar.com/avatar/95c1f7ff72d71b448592a335ba80fb64?s=80\u0026d=identicon","web_url":"http://fb1429a3b28c/liumiaocn","created_at":"2020-07-30T23:20:49.710Z","bio":null,"location":null,"skype":"","linkedin":"","twitter":"","website_url":"","organization":null,"last_sign_in_at":"2020-07-30T23:22:06.395Z","confirmed_at":"2020-07-30T23:20:49.650Z","last_activity_on":"2020-07-30","email":"liumiaocn@outlook.com","theme_id":1,"color_scheme_id":1,"projects_limit":100000,"current_sign_in_at":"2020-07-30T23:22:06.395Z","identities":[],"can_create_group":true,"can_create_project":true,"two_factor_enabled":false,"external":false,"private_profile":null}liumiaocn:api liumiao$
  7. liumiaocn:api liumiao$

结果发现返回的信息中email字段并没有发生变化。

验证方法1: 结合用户操作完成

登录之后发现如下信息,实际还是邮箱验证的机制起了作用,如果gitlab的smtp设定之后,此时修改后的邮箱应该已经收到邮件,用户自行点击里面的链接方可完成邮箱的修改。
在这里插入图片描述
此种方式也是较为安全的方式,前提是SMTP的设定和用户的操作以及使用上都是可以接受的。

验证方法2: 使用页面添加

方式1实际上还是需要用户点击发出来的URL链接,考虑使用从页面添加email并修改,确认是否能够保存成功。首先添加如下email地址
在这里插入图片描述
然后再修改email,虽然显示生效,但是实际上并未生效,刷新之后就会发现实际并没有保存成功,这种方式尚未验证成功。
在这里插入图片描述

验证方式3:修改gitlab设定并重启服务

思路:将用户相关的gitlab的ruby代码删去验证部分,重启gitlab服务。简单来说,删除用户模块相关文件中的验证部分,然后重启即可。

  • 引入部分:/opt/gitlab/embedded/service/gitlab-rails/app/models/user.rb
  • 使用部分:/opt/gitlab/embedded/service/gitlab-rails/app/views/profiles/show.html.haml

步骤1: 进入到gitlab容器中

  1. liumiaocn:api liumiao$ docker exec -it gitlab_gitlab_1 sh
  2. #

步骤2: 删除user.rb中的关联代码

  1. # cp -p /opt/gitlab/embedded/service/gitlab-rails/app/models/user.rb /opt/gitlab/embedded/service/gitlab-rails/app/models/user.rb.org
  2. # vi /opt/gitlab/embedded/service/gitlab-rails/app/models/user.rb
  3. # diff /opt/gitlab/embedded/service/gitlab-rails/app/models/user.rb /opt/gitlab/embedded/service/gitlab-rails/app/models/user.rb.org
  4. 56c56
  5. < :validatable, :omniauthable, :registerable
  6. ---
  7. > :validatable, :omniauthable, :confirmable, :registerable
  8. #

步骤3: 重启gitlab服务

  1. # gitlab-ctl restart
  2. ok: run: alertmanager: (pid 30175) 1s
  3. ok: run: gitaly: (pid 30206) 0s
  4. ok: run: gitlab-monitor: (pid 30219) 0s
  5. ok: run: gitlab-workhorse: (pid 30238) 1s
  6. ok: run: logrotate: (pid 30251) 0s
  7. ok: run: nginx: (pid 30258) 1s
  8. ok: run: node-exporter: (pid 30266) 0s
  9. ok: run: postgres-exporter: (pid 30275) 1s
  10. ok: run: postgresql: (pid 30360) 0s
  11. ok: run: prometheus: (pid 30369) 1s
  12. ok: run: redis: (pid 30385) 0s
  13. ok: run: redis-exporter: (pid 30390) 1s
  14. ok: run: sidekiq: (pid 30399) 1s
  15. ok: run: sshd: (pid 30406) 0s
  16. ok: run: unicorn: (pid 30413) 0s
  17. #

结果验证:此时确认即可发现,已经可以设定

  1. liumiaocn:gitlab liumiao$ curl -X PUT -H "PRIVATE-TOKEN: ${access_token}" http://${gitlab_url}/api/v4/users/${userid} \
  2. > -H 'cache-control: no-cache' \
  3. > -H 'content-type: application/json' \
  4. > -d '{ "email": "liumiao@outlook.com"}'
  5. {"id":2,"name":"liumiaocn","username":"liumiaocn","state":"active","avatar_url":"https://www.gravatar.com/avatar/cf0d8c6aee3403a64e55ce27898eb710?s=80\u0026d=identicon","web_url":"http://a47b8f2ccff7/liumiaocn","created_at":"2020-07-31T03:58:34.373Z","bio":"","location":"","skype":"","linkedin":"","twitter":"","website_url":"","organization":"","last_sign_in_at":"2020-07-31T04:02:25.818Z","confirmed_at":"2020-07-31T03:58:34.178Z","last_activity_on":"2020-07-31","email":"liumiao@outlook.com","theme_id":1,"color_scheme_id":1,"projects_limit":100000,"current_sign_in_at":"2020-07-31T04:02:25.818Z","identities":[],"can_create_group":true,"can_create_project":true,"two_factor_enabled":false,"external":false,"private_profile":false}liumiaocn:gitlab liumiao$

步骤4: 确认错误日志并修改

上述粗暴地删除之后,并没有定位其他的影响,可根据日志信息结合确认,日志路径如下所示:/var/log/gitlab/gitlab-rails/production.log

在GitLab页面登录之后,点击右上角的settings菜单项,页面不能正常显示,出现500错误,上述日志中出现如下错误信息:

  1. Completed 500 Internal Server Error in 1245ms (ActiveRecord: 23.9ms)
  2. ActionView::Template::Error (undefined method `user_confirmation_path' for #<#<Class:0x00007f5462351c50>:0x00007f546233b108>
  3. Did you mean? new_confirmation_path):
  4. 83: - if @user.read_only_attribute?(:email)
  5. 84: = f.text_field :email, required: true, readonly: true, help: "Your email address was automatically set based on your #{ attribute_provider_label(:email) } account."
  6. 85: - else
  7. 86: = f.text_field :email, required: true, value: (@user.email unless @user.temp_oauth_email?),
  8. 87: help: user_email_help_text(@user)
  9. 88: = f.select :public_email, options_for_select(@user.all_emails, selected: @user.public_email),
  10. 89: { help: 'This email will be displayed on your public profile.', include_blank: 'Do not show on profile' },
  11. app/helpers/users_helper.rb:11:in `user_email_help_text'
  12. app/views/profiles/show.html.haml:86:in `block in _app_views_profiles_show_html_haml__1825549301882057885_70000200764700'
  13. app/views/profiles/show.html.haml:4:in `_app_views_profiles_show_html_haml__1825549301882057885_70000200764700'
  14. lib/gitlab/i18n.rb:51:in `with_locale'
  15. lib/gitlab/i18n.rb:57:in `with_user_locale'
  16. app/controllers/application_controller.rb:401:in `set_locale'
  17. lib/gitlab/middleware/multipart.rb:97:in `call'
  18. lib/gitlab/request_profiler/middleware.rb:14:in `call'
  19. lib/gitlab/middleware/go.rb:17:in `call'
  20. lib/gitlab/etag_caching/middleware.rb:11:in `call'
  21. lib/gitlab/middleware/rails_queue_duration.rb:22:in `call'
  22. lib/gitlab/metrics/rack_middleware.rb:15:in `block in call'
  23. lib/gitlab/metrics/transaction.rb:53:in `run'
  24. lib/gitlab/metrics/rack_middleware.rb:15:in `call'
  25. lib/gitlab/middleware/read_only/controller.rb:38:in `call'
  26. lib/gitlab/middleware/read_only.rb:16:in `call'
  27. lib/gitlab/middleware/basic_health_check.rb:25:in `call'
  28. lib/gitlab/request_context.rb:18:in `call'
  29. lib/gitlab/metrics/requests_rack_middleware.rb:27:in `call'
  30. lib/gitlab/middleware/release_env.rb:10:in `call'

根据上述信息,做如下修改

  1. # pwd
  2. /opt/gitlab/embedded/service/gitlab-rails/app/views/profiles
  3. # cp -p show.html.haml show.html.haml.org
  4. # vi show.html.haml
  5. # diff show.html.haml show.html.haml.org
  6. 84a85,87
  7. > - else
  8. > = f.text_field :email, required: true, value: (@user.email unless @user.temp_oauth_email?),
  9. > help: user_email_help_text(@user)
  10. # gitlab-ctl restart
  11. ok: run: alertmanager: (pid 39114) 0s
  12. ok: run: gitaly: (pid 39127) 0s
  13. ok: run: gitlab-monitor: (pid 39142) 1s
  14. ok: run: gitlab-workhorse: (pid 39159) 0s
  15. ok: run: logrotate: (pid 39175) 1s
  16. ok: run: nginx: (pid 39181) 0s
  17. ok: run: node-exporter: (pid 39190) 1s
  18. ok: run: postgres-exporter: (pid 39196) 0s
  19. ok: run: postgresql: (pid 39282) 0s
  20. ok: run: prometheus: (pid 39290) 0s
  21. ok: run: redis: (pid 39299) 0s
  22. ok: run: redis-exporter: (pid 39311) 1s
  23. ok: run: sidekiq: (pid 39320) 1s
  24. ok: run: sshd: (pid 39327) 0s
  25. ok: run: unicorn: (pid 39334) 0s
  26. #

步骤5: 重启并确认

重启gitlab服务

  1. # gitlab-ctl restart
  2. ok: run: alertmanager: (pid 39114) 0s
  3. ok: run: gitaly: (pid 39127) 0s
  4. ok: run: gitlab-monitor: (pid 39142) 1s
  5. ok: run: gitlab-workhorse: (pid 39159) 0s
  6. ok: run: logrotate: (pid 39175) 1s
  7. ok: run: nginx: (pid 39181) 0s
  8. ok: run: node-exporter: (pid 39190) 1s
  9. ok: run: postgres-exporter: (pid 39196) 0s
  10. ok: run: postgresql: (pid 39282) 0s
  11. ok: run: prometheus: (pid 39290) 0s
  12. ok: run: redis: (pid 39299) 0s
  13. ok: run: redis-exporter: (pid 39311) 1s
  14. ok: run: sidekiq: (pid 39320) 1s
  15. ok: run: sshd: (pid 39327) 0s
  16. ok: run: unicorn: (pid 39334) 0s
  17. #

再次登录确认,发现删除两行之后页面已经能够正常打开了,凑合用吧。
在这里插入图片描述

步骤6: 再次确认

重新确认API方式是否可以修改和设定,此处将邮件设定为liumiaocn456@outlook.com, 可以看到执行之后立即进行了修改,无需进行进一步邮箱的确认了

  1. liumiaocn:gitlab liumiao$ userid=2
  2. liumiaocn:gitlab liumiao$ curl -X PUT -H "PRIVATE-TOKEN: ${access_token}" http://${gitlab_url}/api/v4/users/${userid} \
  3. > -H 'cache-control: no-cache' \
  4. > -H 'content-type: application/json' \
  5. > -d '{ "email": "liumiao456@outlook.com"}'
  6. {"id":2,"name":"liumiaocn","username":"liumiaocn","state":"active","avatar_url":"https://www.gravatar.com/avatar/f8d4a298670af74bf1e24eb284dc38ee?s=80\u0026d=identicon","web_url":"http://a47b8f2ccff7/liumiaocn","created_at":"2020-07-31T03:58:34.373Z","bio":"","location":"","skype":"","linkedin":"","twitter":"","website_url":"","organization":"","last_sign_in_at":"2020-07-31T04:02:25.818Z","confirmed_at":"2020-07-31T03:58:34.178Z","last_activity_on":"2020-07-31","email":"liumiao456@outlook.com","theme_id":1,"color_scheme_id":1,"projects_limit":100000,"current_sign_in_at":"2020-07-31T04:02:25.818Z","identities":[],"can_create_group":true,"can_create_project":true,"two_factor_enabled":false,"external":false,"private_profile":false}liumiaocn:gitlab liumiao$
  7. liumiaocn:gitlab liumiao$

在这里插入图片描述

注意事项

此种做法严重不推荐,因为这种方式之下后续的升级和其他做法都会存在后续问题,而且此处只修改了一个页面,比如可以预见的就是/profile/emails之类的页面也会Whoops,当在企业内网之中不得已而为之,或者在流程上无法接受中断之后用户登录点击链接的返回方式等情况下,可使用本文整理的方式进行暂定对应,坑先埋下,后续再填。

发表评论

表情:
评论列表 (有 0 条评论,27人围观)

还没有评论,来说两句吧...

相关阅读