gitleb 登陆方式_为gitlab10.x增加使用remote_user HTTP头的方式登录

项目的结构是这样的:

客户端通过Apache来访问后端的gitlab(gitlab的版本是10.4,手动从源码安装的简体中文版) , Apache作为gitlab的反向代理服务器

Apache内置了CAS的客户端,未登录的用户会重定向到CAS去登录,登录之后,跳转到gitlab,带上一个名为 remote_user的http header 来标识用户的身份

gitlab需要接收这个header,并让用户登录,若系统中不存在该用户,gitlab要从ldap中拿到用户的信息,创建用户,然后让用户登录

实现的方法:

gitlab没有现成的配置来实现这个需求, omniauth-http-header等方案,原理上是可行的,但是在实际操作过程中,很难集成到gitlab中去(因为要修改Gemfile)

所以,我在做的过程中,采用了如下的方法: 参考 omniauth-http-header的代码,自己实现一个 omniauth的provider,添加到gitlab中去(不使用gem包的方式)

首先,在/home/git/gitlab/lib文件夹下,新建http_header.rb,并修改文件的归属为git用户,内容如下:

require 'omniauth'

module OmniAuth

module Strategies

class HttpHeader

include OmniAuth::Strategy

option :name, 'http_header'

option :authorization_uri, nil

option :uid_header, 'remote_user'

option :info_headers, {}

option :remote_ip, nil

def request_phase

redirect callback_url

end

def callback_phase

if options.remote_ip && !Array(options.remote_ip).include?(request.ip)

raise ::OmniAuth::Error, "Callback from unauthorized IP #{request.ip}"

end

super

end

uid do

fetch_header options.uid_header

end

info do

options.info_headers.each_with_object({}) do |(attribute, header), info|

info[attribute] = fetch_header header

end

end

private

def fetch_header(header)

print request.env

request.env.fetch "HTTP_#{header.upcase.gsub('-', '_')}"

end

end

end

end

在omniauth的初始化文件(/home/git/gitlab/config/initializers/omniauth.rb)里require和加载这个文件:

增加了首行的require

require '/home/git/gitlab/lib/http_header.rb'

和最后的

Rails.application.config.middleware.use OmniAuth::Builder do

provider :http_header,

uid_header: 'remote_user'

end

整个文件如下:

require '/home/git/gitlab/lib/http_header.rb'

if Gitlab::LDAP::Config.enabled?

module OmniAuth::Strategies

Gitlab::LDAP::Config.available_servers.each do |server|

# do not redeclare LDAP

next if server['provider_name'] == 'ldap'

const_set(server['provider_class'], Class.new(LDAP))

end

end

end

OmniAuth.config.full_host = Settings.gitlab['base_url']

OmniAuth.config.allowed_request_methods = [:post]

# In case of auto sign-in, the GET method is used (users don't get to click on a button)

OmniAuth.config.allowed_request_methods << :get if Gitlab.config.omniauth.auto_sign_in_with_provider.present?

OmniAuth.config.before_request_phase do |env|

Gitlab::RequestForgeryProtection.call(env)

end

if Gitlab.config.omniauth.enabled

provider_names = Gitlab.config.omniauth.providers.map(&:name)

require 'omniauth-kerberos' if provider_names.include?('kerberos')

end

module OmniAuth

module Strategies

autoload :Bitbucket, Rails.root.join('lib', 'omni_auth', 'strategies', 'bitbucket')

end

end

Rails.application.config.middleware.use OmniAuth::Builder do

provider :http_header,

uid_header: 'remote_user'

end

3.在gitlab.yml文件里配置ldap和omniauth的provider,没有的信息从ldap中拿到

相关配置如下(ldap配置和主题无关,被省略了):

omniauth:

enabled: true

auto_sign_in_with_provider: http_header

auto_link_ldap_user: true

providers:

- { name: 'http_header',

label: 'http_header',

args: {

uid_header: 'remote_user'

} }

4.nginx默认是会去掉带下划线的header,所以,remote_user在经过nginx的时候被干掉了,需要在nginx的server结点下配置这个指令:

underscores_in_headers on;

关于单点退出的问题:让apache拦截gitlab的退出地址,重定向到cas的退出地址上,在cas退出的时候,清除掉gitlab设置的sessioncookie _gitlab_session即可实现单点退出

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: 当出现 "fatal: Unencrypted HTTP is not supported for GitLab. Ensure the repository remote URL is using HTTPS." 错误时,这意味着你的GitLab仓库的远程URL使用的是HTTP而不是HTTPS。为了解决这个问题,你需要确保将仓库的远程URL更改为HTTPS。你可以使用以下命令来更改远程URL: git remote set-url origin https://github.com/your-username/your-repository.git 请将"your-username"替换为你的GitLab用户名,"your-repository"替换为你的仓库名称。这样就会将远程URL更改为HTTPS,从而解决这个问题。\[3\] #### 引用[.reference_title] - *1* *2* [vue初始化项目出现 npm ERR fatal unable to access ‘httpsgithub.comnhnraphael.git‘ OpenSSL SSL_read](https://blog.csdn.net/m0_67402235/article/details/123304508)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [tortoiseGit使用报错gitlab ssh Please make sure you have the correct access rights and the repos](https://blog.csdn.net/genghongsheng/article/details/126020831)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值