HowTo configure Redmine for advanced git integration » History » Version 21

Mr. DTTH, 2013-08-08 04:58

1 1 Felix Schäfer
h1. HowTo configure Redmine for advanced git integration
2 1 Felix Schäfer
3 3 Felix Schäfer
{{>TOC}}
4 3 Felix Schäfer
5 1 Felix Schäfer
h2. Scope
6 1 Felix Schäfer
7 21 Mr. DTTH
_Install on Centos 6.x_
8 21 Mr. DTTH
9 17 Mr. DTTH
This HowTo explains how to serve git repositories on apache through the http-based "git-smart-http protocol":http://progit.org/2010/03/04/smart-http.html introduced in git 1.6.6. 
10 1 Felix Schäfer
11 17 Mr. DTTH
The git-smart-http offers various advantages over ssh or git-based access: you can use redmine access control as-is, no need for extra ssh keys or whatnot, you can secure it through SSL as needed, and there's generally less problems with firewalls and https/https ports than exist with ssh and git ports. git-smart-http also doesn't have some of the drawbacks of its "dumb" predecessor, as it doesn't require any complex DAV setup.
12 1 Felix Schäfer
13 17 Mr. DTTH
This HowTo is mainly written from memory and was conducted on a setup which was already serving [[Repositories_access_control_with_apache_mod_dav_svn_and_mod_perl|svn repositories integrated with redmine]], so it might be possible that I forgot some things or take them for granted. 
14 17 Mr. DTTH
15 17 Mr. DTTH
This is a wiki page, feel free to correct or amend anything you find lacking :-) You can also "drop me a line":/users/3866.
16 17 Mr. DTTH
17 7 Felix Schäfer
Another option to integrate grack with redmine is the "modified grack+redmine plugin":http://github.com/friflaj/redmine_grack or "any other grack modified for redmine":http://github.com/search?q=grack&type=Everything&repo=&langOverride=&start_value=1, though those ones lack documentation and I haven't tried them, so I can't say much about those.
18 1 Felix Schäfer
19 1 Felix Schäfer
h2. Prerequisites
20 1 Felix Schäfer
21 1 Felix Schäfer
* Apache with mod_perl (access control)
22 1 Felix Schäfer
* git (version at least 1.6.6)
23 1 Felix Schäfer
* A way to serve git-smart-http
24 10 Hallison Vasconcelos Batista
** mod_cgi (or mod_cgid) if you want to use the stock "git-http-backend":http://www.kernel.org/pub/software/scm/git/docs/git-http-backend.html
25 1 Felix Schäfer
** a rack server if you want to use "grack":http://github.com/schacon/grack (basically a rack wrapper around the right git commands) or
26 10 Hallison Vasconcelos Batista
"git-webby":http://git.io/BU7twg (another implementation based on grack but written in Sinatra).
27 1 Felix Schäfer
28 17 Mr. DTTH
You should already have a rack server to run redmine, and that's why I chose grack as the backend and which I will describe in this tutorial. 
29 1 Felix Schäfer
30 17 Mr. DTTH
Using the stock git-http-backend should be quite straightforward though (skip the [[HowTo_configure_Redmine_for_advanced_git_integration#Install-grack|grack installation]] part and get your install with the git-http-backend going (the "git-http-backend manpage":http://www.kernel.org/pub/software/scm/git/docs/git-http-backend.html has some examples), when that's done go on with the [[HowTo_configure_Redmine_for_advanced_git_integration#Access-control|access control]] part).
31 17 Mr. DTTH
32 20 Mr. DTTH
h2. Install Git
33 20 Mr. DTTH
34 20 Mr. DTTH
<pre><code class="bash">
35 20 Mr. DTTH
yum install git
36 20 Mr. DTTH
</code></pre>
37 20 Mr. DTTH
38 2 Felix Schäfer
h2. Install grack
39 1 Felix Schäfer
40 3 Felix Schäfer
h3. Get the sources
41 2 Felix Schäfer
42 19 Mr. DTTH
Fetch grack from its "github repository":http://github.com/schacon/grack, I checked out mine to @/var/www/grack@
43 1 Felix Schäfer
44 19 Mr. DTTH
<pre><code class="bash">
45 19 Mr. DTTH
cd /var/www
46 19 Mr. DTTH
git clone http://github.com/schacon/grack.git
47 19 Mr. DTTH
</code></pre>
48 1 Felix Schäfer
49 18 Mr. DTTH
And create a directory for repositories :
50 18 Mr. DTTH
51 18 Mr. DTTH
<pre><code class="bash">
52 1 Felix Schäfer
mkdir /opt/repositories
53 21 Mr. DTTH
mkdir /opt/repositories/git
54 18 Mr. DTTH
chown -R apache:apache /opt/repositories/git
55 18 Mr. DTTH
</code></pre>
56 18 Mr. DTTH
57 2 Felix Schäfer
h3. Configuration
58 2 Felix Schäfer
59 2 Felix Schäfer
Edit the @config.ru@ file and adapt it to your local configuration. @project_root@ must contain the path to the directory containing your git repositories, @git_path@ must obviously contain the path to the git, mine looks like this (on gentoo):
60 1 Felix Schäfer
61 21 Mr. DTTH
<pre><code class="bash">
62 21 Mr. DTTH
vi /var/www/grack/config.ru
63 21 Mr. DTTH
</code></pre>
64 21 Mr. DTTH
65 21 Mr. DTTH
And edit file :
66 21 Mr. DTTH
67 2 Felix Schäfer
<pre><code class="ruby">$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/lib')
68 1 Felix Schäfer
69 2 Felix Schäfer
use Rack::ShowExceptions
70 1 Felix Schäfer
71 18 Mr. DTTH
require 'grack'
72 1 Felix Schäfer
73 18 Mr. DTTH
require 'git_adapter'
74 2 Felix Schäfer
75 18 Mr. DTTH
config = {
76 18 Mr. DTTH
  :project_root => "/opt/repositories/git",
77 2 Felix Schäfer
  :git_path => '/usr/bin/git',
78 2 Felix Schäfer
  :upload_pack => true,
79 1 Felix Schäfer
  :receive_pack => true,
80 1 Felix Schäfer
}
81 1 Felix Schäfer
82 21 Mr. DTTH
run GitHttp::App.new(config)
83 21 Mr. DTTH
</code></pre>
84 3 Felix Schäfer
85 1 Felix Schäfer
h3. Integrate with Apache
86 1 Felix Schäfer
87 17 Mr. DTTH
You could obviously use any rack server you like at this point, but the access control mechanism @Redmine.pm@ is written for apache with mod_perl, so you will at least need to reverse proxy your rack server through apache. 
88 1 Felix Schäfer
89 17 Mr. DTTH
My rack server of choice is "passenger":http://modrails.com/ (solid performance, apache module, mostly simple configuration) and it is already configured on my system. 
90 17 Mr. DTTH
91 17 Mr. DTTH
As passenger installation and configuration is not within the scope of this HowTo, please refer to the "passenger documentation":http://modrails.com/documentation.html or to the passenger installation guide from your distribution.
92 17 Mr. DTTH
93 17 Mr. DTTH
There's a little more work to do here to get passenger to work with this, you will need to create the directories @public@ and @tmp@ in the grack directory. 
94 1 Felix Schäfer
95 1 Felix Schäfer
Please also be aware that in the standard configuration, passenger will run the grack application with the same user and group owning the @config.ru@ file. This user must have read- and write-access as needed to the git repositories!
96 1 Felix Schäfer
97 21 Mr. DTTH
Create directories 'public' and 'tmp' in /var/www/grack for apache :
98 21 Mr. DTTH
99 21 Mr. DTTH
<pre><code class="bash">
100 21 Mr. DTTH
cd /var/www/grack
101 21 Mr. DTTH
mkdir public
102 21 Mr. DTTH
mkdir tmp
103 21 Mr. DTTH
chown -R apache:apache /var/www/grack
104 21 Mr. DTTH
</code></pre>
105 21 Mr. DTTH
106 21 Mr. DTTH
Edit config file "/etc/httpd/conf/httpd.conf" for support multi virtualhost by remove comment :
107 21 Mr. DTTH
108 21 Mr. DTTH
<pre><code class="bash">
109 21 Mr. DTTH
NameVirtualHost *:80
110 21 Mr. DTTH
</code></pre>
111 21 Mr. DTTH
112 21 Mr. DTTH
Create a file virtualhost :
113 21 Mr. DTTH
114 21 Mr. DTTH
<pre><code class="bash">
115 21 Mr. DTTH
vi /etc/httpd/conf.d/git.conf
116 21 Mr. DTTH
</code></pre>
117 21 Mr. DTTH
118 21 Mr. DTTH
119 2 Felix Schäfer
120 2 Felix Schäfer
<pre><code class="apache"><VirtualHost yo.ur.i.p:80>
121 2 Felix Schäfer
    ServerName git.myhost.com
122 2 Felix Schäfer
123 2 Felix Schäfer
    ServerAdmin root@myhost.com
124 2 Felix Schäfer
    DocumentRoot "/var/www/git.myhost.com/public"
125 2 Felix Schäfer
126 2 Felix Schäfer
    <Directory "/var/www/git.myhost.com/public">
127 2 Felix Schäfer
        Options None
128 2 Felix Schäfer
        AllowOverride None
129 2 Felix Schäfer
        Order allow,deny
130 2 Felix Schäfer
        Allow from all
131 2 Felix Schäfer
    </Directory>
132 2 Felix Schäfer
</VirtualHost></code></pre>
133 1 Felix Schäfer
134 10 Hallison Vasconcelos Batista
At this point, if you have a repository in @/var/git/git.myhost.com/myrepo@, you should be able to access it through @http://git.myhost.com/myrepo@, for example @git ls-remote http://git.myhost.com/myrepo@ should show you some information about the repository.
135 10 Hallison Vasconcelos Batista
136 10 Hallison Vasconcelos Batista
h2. Install git-webby
137 3 Felix Schäfer
138 3 Felix Schäfer
Follow the instructions available in "repository page":http://git.io/BU7twg that use basically the same instructions described above.
139 3 Felix Schäfer
140 3 Felix Schäfer
h2. Access control
141 3 Felix Schäfer
142 3 Felix Schäfer
You now have a working git server, albeit with no access control. Currently, the shipped perl module for access control @Redmine.pm@ (in @extra/svn/@ in your redmine directory) does not support access control for the git-smart-http protocol, the patch in #4905 aims to implement that.
143 3 Felix Schäfer
144 11 Gregory Bartholomew
h3. Applying the patch
145 1 Felix Schäfer
146 3 Felix Schäfer
Download the latest (or better: correct) version of the patch from #4905 to your redmine directory. In the redmine directory, apply the patch: @patch -p1 < the-patch-file.patch@ should work (if it tells you stuff about being unable to apply a hunk, the patch is incompatible with your @Redmine.pm@ version, if it says other stuff, try @patch -p0 < the-patch-file.patch@ or @patch Redmine.pm < the-patch-file.patch@, if it still borks, ask for advice on #4905).
147 1 Felix Schäfer
148 1 Felix Schäfer
-You will possibly still need to edit the file from here, because the current version of the patch only works for repositories served from @http://git.myhost.com/git/myrepo@ though the above example uses @http://git.myhost.com/myrepo@.- This step isn't needed anymore, the patch has been updated to take the information from the @Location@ block from apache into account.
149 8 Felix Schäfer
150 3 Felix Schäfer
h3. Configuring Apache
151 17 Mr. DTTH
152 3 Felix Schäfer
You now have to make Apache aware of your new authentication module (if you already had done this step for subversion integration, you can go to the @Location@ directives directly). 
153 17 Mr. DTTH
154 17 Mr. DTTH
Copy or link @Redmine.pm@ (from your @extra/svn/@ directory) to @/usr/lib/perl5/Apache/Redmine.pm@ (ubuntu) or wherever your distribution puts its apache perl modules (e.g. gentoo puts them in @/usr/lib64/perl5/vendor_perl/5.8.8/Apache/@, fedora puts them in @/usr/lib64/perl5/vendor_perl/Apache/@).
155 3 Felix Schäfer
156 3 Felix Schäfer
Having done that, reload apache to make sure everything in the patching phase went well (if not, remove the link or the file create in the step just before and restart apache to get apache back up, try to find the error in your Redmine.pm file). Now edit your vhost configuration to look somewhat like (same as above but with more stuff):
157 3 Felix Schäfer
158 3 Felix Schäfer
<pre><code class="apache"><VirtualHost yo.ur.i.p:80>
159 3 Felix Schäfer
    ServerName git.myhost.com
160 3 Felix Schäfer
161 3 Felix Schäfer
    ServerAdmin root@myhost.com
162 3 Felix Schäfer
    DocumentRoot "/var/www/git.myhost.com/public"
163 3 Felix Schäfer
164 3 Felix Schäfer
    PerlLoadModule Apache::Redmine
165 3 Felix Schäfer
166 3 Felix Schäfer
    <Directory "/var/www/git.myhost.com/public">
167 3 Felix Schäfer
        Options None
168 3 Felix Schäfer
        AllowOverride None
169 3 Felix Schäfer
        Order allow,deny
170 3 Felix Schäfer
        Allow from all
171 3 Felix Schäfer
    </Directory>
172 3 Felix Schäfer
173 3 Felix Schäfer
    <Location "/">
174 3 Felix Schäfer
        AuthType Basic
175 3 Felix Schäfer
        AuthName "Redmine git repositories"
176 3 Felix Schäfer
        Require valid-user
177 3 Felix Schäfer
178 3 Felix Schäfer
        PerlAccessHandler Apache::Authn::Redmine::access_handler
179 3 Felix Schäfer
        PerlAuthenHandler Apache::Authn::Redmine::authen_handler
180 3 Felix Schäfer
181 3 Felix Schäfer
        ## for mysql
182 3 Felix Schäfer
        RedmineDSN "DBI:mysql:database=databasename;host=my.db.server"
183 3 Felix Schäfer
        ## for postgres
184 3 Felix Schäfer
        # RedmineDSN "DBI:Pg:dbname=databasename;host=my.db.server"
185 3 Felix Schäfer
        ## for SQLite3
186 3 Felix Schäfer
        # RedmineDSN "DBI:SQLite:dbname=database.db"
187 3 Felix Schäfer
188 3 Felix Schäfer
        RedmineDbUser "redmine"
189 3 Felix Schäfer
        RedmineDbPass "password"
190 1 Felix Schäfer
        RedmineGitSmartHttp yes
191 3 Felix Schäfer
    </Location>
192 1 Felix Schäfer
</VirtualHost></code></pre>
193 1 Felix Schäfer
194 3 Felix Schäfer
Reload your apache, and everything should be good and well :-)
195 3 Felix Schäfer
196 3 Felix Schäfer
h2. Known issues
197 17 Mr. DTTH
198 1 Felix Schäfer
If you are using the stock git-http-backend directly under apache and you are finding errors like "Request not supported: '/git/your-git-repo'" in your apache error log, you may need to add "SetEnv REMOTE_USER=$REDIRECT_REMOTE_USER" to the to the list of environment variables that you are setting in your apache configuration.  
199 17 Mr. DTTH
200 17 Mr. DTTH
Unfortionately, this setting may cause redmine to borke.  If so, you will need to set the variable for only the requests that are passed through git-http-backend.  One way to accomplish this is with mod_rewrite.  Below is a sample apache configuration from a Fedora 17 system that uses git-http-backend and mod_rewrite.
201 13 Gregory Bartholomew
202 13 Gregory Bartholomew
In httpd.conf:
203 13 Gregory Bartholomew
204 13 Gregory Bartholomew
<pre><code class="apache">Listen xxx.xxx.xxx.xxx:80
205 13 Gregory Bartholomew
<VirtualHost xxx.xxx.xxx.xxx:80>
206 13 Gregory Bartholomew
   DocumentRoot /var/www/redmine/public
207 13 Gregory Bartholomew
   ServerName servername.domain:80
208 13 Gregory Bartholomew
   Include conf/servername.conf
209 13 Gregory Bartholomew
</VirtualHost>
210 13 Gregory Bartholomew
211 13 Gregory Bartholomew
Listen xxx.xxx.xxx.xxx:443
212 13 Gregory Bartholomew
<VirtualHost xxx.xxx.xxx.xxx:443>
213 13 Gregory Bartholomew
   DocumentRoot /var/www/redmine/public
214 13 Gregory Bartholomew
   ServerName servername.domain:443
215 13 Gregory Bartholomew
   Include conf/servername.conf
216 13 Gregory Bartholomew
   Include conf/ssl.conf
217 13 Gregory Bartholomew
</VirtualHost></code></pre>
218 13 Gregory Bartholomew
219 13 Gregory Bartholomew
In servername.conf:
220 13 Gregory Bartholomew
221 13 Gregory Bartholomew
<pre><code class="apache">PerlLoadModule Apache::Authn::Redmine
222 13 Gregory Bartholomew
223 13 Gregory Bartholomew
SetEnv GIT_PROJECT_ROOT /git-1/repositories
224 13 Gregory Bartholomew
SetEnv GIT_HTTP_EXPORT_ALL
225 13 Gregory Bartholomew
226 13 Gregory Bartholomew
<IfModule mod_rewrite.c>
227 13 Gregory Bartholomew
   RewriteEngine On
228 13 Gregory Bartholomew
229 13 Gregory Bartholomew
   RewriteCond %{HTTPS} ^off$
230 13 Gregory Bartholomew
   RewriteCond %{REQUEST_URI} !^/git-private/
231 13 Gregory Bartholomew
   RewriteRule ^.*$ https://servername.domain$0 [R=301,L]
232 13 Gregory Bartholomew
   RewriteRule ^/git/(.*/objects/[0-9a-f]{2}/[0-9a-f]{38})$ /git-1/repositories/$1 [L]
233 13 Gregory Bartholomew
   RewriteRule ^/git/(.*/objects/pack/pack-[0-9a-f]{40}.(pack|idx))$ /git-1/repositories/$1 [L]
234 13 Gregory Bartholomew
   RewriteRule ^/git/(.*)$ /usr/libexec/git-core/git-http-backend/$1 [E=REMOTE_USER:$REDIRECT_REMOTE_USER,H=cgi-script,L]
235 13 Gregory Bartholomew
</IfModule>
236 13 Gregory Bartholomew
237 15 Gregory Bartholomew
<Directory /usr/libexec/git-core>
238 15 Gregory Bartholomew
   <Files "git-http-backend">
239 15 Gregory Bartholomew
      Options +ExecCGI
240 13 Gregory Bartholomew
   </Files>
241 13 Gregory Bartholomew
</Directory>
242 13 Gregory Bartholomew
243 13 Gregory Bartholomew
<Location /git>
244 13 Gregory Bartholomew
   AuthType Basic
245 13 Gregory Bartholomew
   AuthName "CAMPUS"
246 13 Gregory Bartholomew
   AuthBasicProvider external
247 13 Gregory Bartholomew
   AuthExternal pwauth
248 13 Gregory Bartholomew
   Require valid-user
249 13 Gregory Bartholomew
250 13 Gregory Bartholomew
   PerlAccessHandler Apache::Authn::Redmine::access_handler
251 13 Gregory Bartholomew
   PerlAuthenHandler Apache::Authn::Redmine::authen_handler
252 13 Gregory Bartholomew
 
253 13 Gregory Bartholomew
   RedmineDSN "DBI:mysql:database=redmine;host=localhost" 
254 14 Gregory Bartholomew
   RedmineDbUser "redmine" 
255 13 Gregory Bartholomew
   # RedmineDbPass "password"
256 13 Gregory Bartholomew
   RedmineGitSmartHttp yes
257 13 Gregory Bartholomew
</Location>
258 13 Gregory Bartholomew
259 13 Gregory Bartholomew
Alias /git-private /git-1/repositories
260 13 Gregory Bartholomew
261 13 Gregory Bartholomew
<Location /git-private>
262 13 Gregory Bartholomew
   Order deny,allow
263 13 Gregory Bartholomew
   Deny from all
264 13 Gregory Bartholomew
   <Limit GET PROPFIND OPTIONS REPORT>
265 13 Gregory Bartholomew
      Options Indexes FollowSymLinks MultiViews
266 13 Gregory Bartholomew
      Allow from 127.0.0.1
267 13 Gregory Bartholomew
      Allow from localhost
268 13 Gregory Bartholomew
   </Limit>
269 13 Gregory Bartholomew
</Location>
270 13 Gregory Bartholomew
271 13 Gregory Bartholomew
<Directory "/var/www/redmine/public">
272 13 Gregory Bartholomew
   RailsEnv production
273 13 Gregory Bartholomew
   RailsBaseURI /
274 13 Gregory Bartholomew
275 13 Gregory Bartholomew
   Options -MultiViews
276 13 Gregory Bartholomew
   AllowOverride All
277 13 Gregory Bartholomew
</Directory></code></pre>
278 13 Gregory Bartholomew
279 13 Gregory Bartholomew
In conf/ssl.conf:
280 13 Gregory Bartholomew
281 13 Gregory Bartholomew
<pre><code class="apache">LogLevel warn
282 13 Gregory Bartholomew
SSLEngine on
283 13 Gregory Bartholomew
SSLProtocol all -SSLv2
284 13 Gregory Bartholomew
SSLCipherSuite RC4-SHA:AES128-SHA:ALL:!ADH:!EXP:!LOW:!MD5:!SSLV2:!NULL
285 13 Gregory Bartholomew
SSLCertificateFile /etc/pki/tls/certs/your-server.crt
286 13 Gregory Bartholomew
SSLCertificateKeyFile /etc/pki/tls/private/your-server.key
287 13 Gregory Bartholomew
SSLCertificateChainFile /etc/pki/tls/certs/server-chain.crt
288 13 Gregory Bartholomew
SSLCACertificateFile /etc/pki/tls/certs/ca-bundle.crt
289 13 Gregory Bartholomew
290 13 Gregory Bartholomew
<Files ~ "\.(cgi|shtml|phtml|php3?)$">
291 13 Gregory Bartholomew
    SSLOptions +StdEnvVars
292 13 Gregory Bartholomew
</Files>
293 13 Gregory Bartholomew
<Directory "/var/www/cgi-bin">
294 13 Gregory Bartholomew
    SSLOptions +StdEnvVars
295 13 Gregory Bartholomew
</Directory>
296 13 Gregory Bartholomew
297 13 Gregory Bartholomew
SetEnvIf User-Agent ".*MSIE.*" \
298 13 Gregory Bartholomew
         nokeepalive ssl-unclean-shutdown \
299 13 Gregory Bartholomew
         downgrade-1.0 force-response-1.0
300 13 Gregory Bartholomew
</code></pre>
301 13 Gregory Bartholomew
302 13 Gregory Bartholomew
In conf.d/ssl.conf:
303 13 Gregory Bartholomew
304 13 Gregory Bartholomew
<pre><code class="apache">LoadModule ssl_module modules/mod_ssl.so
305 1 Felix Schäfer
SSLPassPhraseDialog exec:/usr/libexec/httpd-ssl-pass-dialog
306 1 Felix Schäfer
SSLSessionCache         shmcb:/var/cache/mod_ssl/scache(512000)
307 1 Felix Schäfer
SSLSessionCacheTimeout  300
308 13 Gregory Bartholomew
SSLMutex default
309 13 Gregory Bartholomew
SSLRandomSeed startup file:/dev/urandom  256
310 13 Gregory Bartholomew
SSLRandomSeed connect builtin
311 13 Gregory Bartholomew
SSLCryptoDevice builtin
312 13 Gregory Bartholomew
</code></pre>
313 17 Mr. DTTH
314 17 Mr. DTTH
You will also need to have the perl modules Net::LDAP, Authen::Simple, and Authen::Simple::LDAP installed.  The first two are available in Fedora's default package repositories.  
315 17 Mr. DTTH
316 13 Gregory Bartholomew
The third must be installed after the other two and it must be obtained directly from cpan.  Below are the commands that I used to install these packages on Fedora 17.
317 15 Gregory Bartholomew
318 13 Gregory Bartholomew
yum -y install gcc make perl-LDAP perl-Authen-Simple
319 13 Gregory Bartholomew
cpan
320 1 Felix Schäfer
cpan> install Authen::Simple::LDAP