Gerrit团队知道这个问题,但目前还没有解决方案:https://code.google.com/p/gerrit/issues/detail?id=2677 不确定Jenkins是否有类似的问题。
对于Jenkins,请执行以下操作:
对于Gerrit:
要么在机器本身上(将GERRIT_HOME更改为您的机器上的位置):
使用以下两种方法之一打开sql数据库:
[推荐] 通过ssh可用的gerrit命令:
ssh gerrit.revault.ch gerrit gsql
在机器本身上执行OR操作(将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将您的帐户映射到不同的用户名、电子邮件地址等。
对于给定的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
假设您已经安装了Gerrit并且它正在侦听地址10.10.10.10:8080。 您需要配置Gerrit使用基本身份验证,在您的 ${gerrit_installation}/etc/gerrit.config 中,[auth] 部分应如下所示:
[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将用户名转发给Gerrit的方式。
在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控制台。如果您还没有项目,请先创建一个项目。例如:example-it-authentication
在项目上转到API和身份验证:
service apache2 restart
你完成了!
Jenkins
假设您已经安装了Jenkins并且正在侦听10.10.10.11:8080。
对于Jenkins,配置几乎相同。您需要安装并激活反向代理身份验证插件http://wiki.jenkins-ci.org/display/JENKINS/Reverse+Proxy+Auth+Plugin。 在配置全局安全性下,选中“通过反向代理的HTTP标头”单选按钮。
默认值对应于以下配置。 您需要在API控制台https://console.developers.google.com/project中创建与jenkins主机名匹配的凭据。像以前一样报告给您的配置文件(例如: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支持。
Cannot assign user name xxx@abc.com to account 73; name does not conform
。解决方案是在Apache配置中设置OIDCRemoteUserClaim email ^(.*)@
。值的第二部分是一个正则表达式,只返回电子邮件的名称部分。之后,我必须为没有域的现有用户在account_external_ids
中创建新条目。 - kreshoOIDCAuthRequestParams hd=example.com
<Location>
部分中执行以下操作:Require claim hd:example.com
为了限制访问权限只允许来自example.com
谷歌域的用户,如果您的应用程序对任何谷歌账户开放,则不应使用电子邮件前缀作为主要标识符,因为有可能出现不同域中的用户具有相同的用户前缀而产生冲突。
这就是为什么最好依赖于完整的电子邮件地址,例如:
OIDCRemoteUserClaim email
或者是 Google 在 sub
声明中使用的(不透明)主标识符,例如:
OIDCRemoteUserClaim sub
OIDCAuthNHeader X-Forwarded-User
OIDCAuthRequestParams openid.realm=<urlencoded-realm-value>
OIDCRemoteUserClaim openid_id
想要全面了解配置基础知识,请参考以下链接:https://github.com/pingidentity/mod_auth_openidc/blob/master/auth_openidc.conf
java -jar gerrit-2.10.1-4-a83387b.war init -d gerrit_site_path
gerrit_site_path/bin/gerrit.sh restart