Skip to main content
 首页 » 编程设计

jenkins之Gerrit 和 Jenkins 的 Google 身份验证

2025年05月04日78emanlee

Jenkins 和 Gerrit 都有适用于 OpenID 2.0 的插件,但 Google 已于 2014 年 5 月 19 日 ( https://developers.google.com/accounts/docs/OpenID ) 弃用此 API,因此无法使用新安装,并且现有安装必须迁移到 OAuth2.0(OpendID 连接)。尝试使用 OpenID 2.0 时,您将收到错误消息“错误 400:OpenID 身份验证请求包含未注册的域”。

Gerrit 团队已经意识到这个问题,但目前还没有解决方案:
https://code.google.com/p/gerrit/issues/detail?id=2677

不确定 Jenkins 。

请您参考如下方法:

更新 2014/11/05 :对于那些来到这里的人,请阅读下面的第一位。感谢 hans-zandbelt对于反馈。它包含在更新版本中。设置现在使用建议的改进,仅使用 mod_rewrite 将 gerrit 注销 url 重定向到正确的位置。另请注意,不是仅使用电子邮件的非域部分,而是未经修改地使用电子邮件。这意味着如果您碰巧有现有设置,则需要更改用户名映射。

对于 Jenkins ,请执行以下操作:

  • 将 ${jenkins_home}/users/youruser 移动到 ${jenkins_home}/users/youruser@yourdomain
  • 打开 ${jenkins_home}/config.xml 搜索“youruser”并替换为 youruser@yourdomain

  • 对于格里特:

    要么在机器上(将 GERRIT_HOME 更改为您机器上的位置):
  • 使用以下两种方法之一打开sql数据库:
  • 【推荐】通过 ssh 提供的 gerrit 命令:
    ssh  gerrit.revault.ch gerrit  gsql 
    
  • 或在机器本身上(将 GERRIT_HOME 更改为您机器上的位置):
    export GERRIT_HOME=/var/gerrit_home 
    pushd ${GERRIT_HOME} 
    java -cp $(find . -name "h2*.jar") org.h2.tools.Shell -url "jdbc:h2:file:${GERRIT_HOME}/db/ReviewDB;IFEXISTS=TRUE" 
    
  • 显示外部
    select * from ACCOUNT_EXTERNAL_IDS; 
    
  • 外部 ID 将您的帐户映射到不同的用户名、电子邮件等。
  • 以用户名为前缀的那些:例如username:test@example.com 用于 ssh/git 登录名
  • 以gerrit为前缀的那些:例如gerrit:test@example.com 用于 Web 界面
  • 对于给定的 account_id,您可以使用 sql 为现有用户添加新映射:例如
    insert into ACCOUNT_EXTERNAL_IDS values(1000032, NULL,NULL, 'username:test@example.com'); 
    insert into ACCOUNT_EXTERNAL_IDS values(1000032, NULL,NULL, 'gerrit:test@example.com'); 
    


  • 解决方案

    您可以使用 Apache 作为反向代理处理身份验证:

    格里特

    假设您已经安装了 Gerrit 并且它正在监听地址 10.10.10.10:8080。
    您必须配置 gerrit 以使用基本身份验证,即您的 [auth] 部分
    ${gerrit_installation}/etc/gerrit.config 应如下所示:
    [gerrit] 
            basePath = git 
            canonicalWebUrl = http://gerrit.example.com 
    [database] 
            type = h2 
            database = db/ReviewDB 
    [index] 
            type = LUCENE 
    [auth] 
            type = HTTP 
            emailFormat = {0}@example.com 
            httpHeader =  X-Forwarded-User 
    [sendemail] 
            smtpServer = localhost 
    [container] 
            user = gerrit 
            javaHome = /usr/lib/jvm/java-8-oracle/jre 
    [sshd] 
            listenAddress = 10.10.10.10:2222 
    [httpd] 
            listenUrl = http://10.10.10.10:8080/ 
    [cache] 
            directory = cache 
    

    用户名将在标题 X-Forwarded-User 中。这就是 Apache 将如何转发用户名
    给格里特。

    在 Apache 上,我们将使用支持 oauth2 的 mod_auth_openidc。欲了解更多信息和
    示例文档引用 https://github.com/pingidentity/mod_auth_openidc .在最近的 Ubuntu 上安装
    看起来像这样:
    sudo aptitude install libjansson-dev apache2 apache2-dev libcurl4-openssl-dev build-essential autoconf libhiredis-dev 
     
    git clone https://github.com/pingidentity/mod_auth_openidc.git 
    cd mod_auth_openidc 
    ./autogen.sh  
    ./configure 
    make 
    sudo make install 
     
    sudo a2enmod auth_openidc 
    sudo a2enmod proxy 
    sudo a2enmod proxy_http 
    sudo a2enmod headers 
    sudo a2enmod rewrite 
    

    您将需要添加站点配置,例如gerrit.conf 类似于下面的(您可能也需要 TLS)到/etc/apache2/sites-available 并使用以下命令激活它:
    sudo a2ensite gerrit.conf 
    

    文件/etc/apache2/sites-available/gerrit.conf 如下所示:
    <VirtualHost *:80> 
    ServerName gerrit.example.com 
    ServerAdmin webmaster@localhost 
    DocumentRoot /var/www/html 
    ErrorLog ${APACHE_LOG_DIR}/error.log 
    CustomLog ${APACHE_LOG_DIR}/access.log combined 
     
    OIDCProviderMetadataURL https://accounts.google.com/.well-known/openid-configuration 
    OIDCClientID <from api console> 
    OIDCClientSecret <from api console> 
     
    OIDCScope "openid email profile" 
    OIDCRedirectURI http://gerrit.example.com/oauth2callback 
    OIDCCryptoPassphrase <generate long random passphrase here, no sure if used> 
     
    OIDCSessionInactivityTimeout 600 
     
    OIDCCookiePath / 
     
    OIDCAuthRequestParams hd=example.com 
    OIDCRemoteUserClaim email 
    OIDCAuthNHeader X-Forwarded-User 
     
    RewriteEngine On 
    #LogLevel alert rewrite:trace2 
    RewriteRule ^/logout$ /oauth2callback?logout=http://gerrit.example.com/ [R] 
     
    ProxyPass /  http://gerrit.example.com:8080/ nocanon 
    ProxyPassReverse / http://gerrit.example.com:8080/ 
    ProxyRequests     Off 
    AllowEncodedSlashes NoDecode 
     
     
    <Proxy http://gerrit.example.com:8080/*> 
    # add rewrites here if necessary 
    </Proxy> 
     
    <Location /> 
       AuthType openid-connect 
       Require claim hd:example.com 
       Require valid-user 
    </Location> 
     
    </VirtualHost> 
    

    为了获取参数 OIDCClientID 和 OIDCClientSecret 到 https://console.developers.google.com/project 下的 api 控制台.如果您没有先创建项目,则凭据位于项目的上下文中。例如。示例验证



    在项目中转到 APIs & auth:
  • 在 APIs 下激活 Google+ API。
  • 在凭据下,OAuth 创建新的客户端 ID。
  • 在您的 apache 配置(例如 gerrit.conf)中填写 OIDCClientID 和 OIDCClientSecret
  • 在同意屏幕下填写电子邮件和产品名称(如果不这样做会出错)

  • service apache2 restart



    你应该完成!

    Jenkins

    假设您已经安装了 Jenkins 并且它正在监听 10.10.10.11:8080。

    对于 Jenkins,配置几乎相同。您将需要安装并激活反向代理身份验证插件 http://wiki.jenkins-ci.org/display/JENKINS/Reverse+Proxy+Auth+Plugin .在“配置全局安全性”下,选中“反向代理的 HTTP header ” radio 。


    默认值对应于下面的配置。您需要在 api 控制台中创建与 jenkins 主机名匹配的凭据 https://console.developers.google.com/project .像以前一样将它们报告给您的配置(例如 jenkins.conf)。这应该是全部。
    <VirtualHost *:80> 
    ServerName jenkins.example.com 
    ServerAdmin webmaster@localhost 
    DocumentRoot /var/www/html 
    ErrorLog ${APACHE_LOG_DIR}/error.log 
    CustomLog ${APACHE_LOG_DIR}/access.log combined 
     
    OIDCProviderMetadataURL https://accounts.google.com/.well-known/openid-configuration 
    OIDCClientID <from api console> 
    OIDCClientSecret <from api console> 
     
    OIDCScope "openid email profile" 
    OIDCRedirectURI http://jenkins.example.com/oauth2callback 
    OIDCCryptoPassphrase <generate long random passphrase here, no sure if used> 
     
    OIDCSessionInactivityTimeout 600 
     
    OIDCCookiePath / 
     
    OIDCAuthRequestParams hd=example.com 
    OIDCRemoteUserClaim email 
    OIDCAuthNHeader X-Forwarded-User 
     
    ProxyPass /  http://jenkins.example.com:8080/ nocanon 
    ProxyPassReverse / http://jenkins.example.com:8080/ 
    ProxyRequests     Off 
    AllowEncodedSlashes NoDecode 
     
    <Proxy http://jenkins.example.com:8080/*> 
    # add rewrites here if necessary 
    </Proxy> 
     
    <Location /> 
       AuthType openid-connect 
       Require claim hd:example.com 
       Require valid-user 
    </Location> 
     
    <Location ~ "^/(cli|jnlpJars|subversion|whoAmI|computer/[^/]+/slave-agent.jnlp|tcpSlaveAgentListener)"> 
     Satisfy Any 
     Allow from all  
    </Location> 
     
    </VirtualHost> 
    

    目前似乎不支持 mod_auth_openidc 中的组。如果您需要组,您可以安装一个 LDAP 来存储它们(但这可能不是您想要的,因为您使用的是 Google 身份验证)或等到 mod_auth_openidc 支持它。