Project

General

Profile

Repositories access control with apache mod dav svn and mod perl » History » Version 42

Terence Mill, 2012-08-06 13:18

1 16 Jean-Philippe Lang
h1. Repositories access control with apache, mod_dav_svn and mod_perl
2 1 Nicolas Chuche
3 2 Nicolas Chuche
{{>TOC}}
4
5 4 Jean-Philippe Lang
h2. Overview
6 1 Nicolas Chuche
7 23 Eric Davis
In this documentation, we will configure apache to delegate authentication to mod_perl. It's tested on apache2 (@apache2-mpm-prefork@) with mysql and postgresql but should work with allmost every databases for which there is a perl DBD module.  Apache2 with the high speed thread model might not load Perl correctly (@apache2-mpm-worker@).
8 1 Nicolas Chuche
9
You need a working apache on your SVN server and you must install some modules at least mod_dav_svn, mod_perl2, DBI and DBD::mysql (or the DBD driver for you database as it should work on allmost all databases).
10 4 Jean-Philippe Lang
11 15 Jean-Philippe Lang
On Debian/ubuntu you can do :
12 11 Shaun Mangelsdorf
13 37 Mischa The Evil
  sudo aptitude install libapache2-svn libapache-dbi-perl libapache2-mod-perl2 libdbd-mysql-perl libdigest-sha1-perl libauthen-simple-ldap-perl
14 1 Nicolas Chuche
15 41 Zack s
On Turnkey Redmine Virtual Appliance you can do :
16 40 Zack s
17
  apt-get install libapache2-svn libapache-dbi-perl libapache2-mod-perl2 libauthen-simple-ldap-perl
18
19 37 Mischa The Evil
If the repositories are not created automatically by reposman.rb, it is important that the repository name is the same as the project identifier in Redmine, otherwise Redmine.pm will fail to authenticate users.  For example, if the path to the repository is @/path/to/repository/foo-bar@, then the project Identifier on the Settings page must be @foo-bar@.
20 1 Nicolas Chuche
21
h2. Enabling apache modules
22
23
On debian/ubuntu :
24
25
<pre>
26 15 Jean-Philippe Lang
sudo a2enmod dav
27 21 Marko Roeder
sudo a2enmod dav_svn # if you want to use svn
28
sudo a2enmod dav_fs  # if you want to use git
29 15 Jean-Philippe Lang
sudo a2enmod perl
30 4 Jean-Philippe Lang
</pre>
31 1 Nicolas Chuche
32 15 Jean-Philippe Lang
h2. Apache configuration for Subversion repositories
33 1 Nicolas Chuche
34
You first need to copy or link @Redmine.pm@ to @/usr/lib/perl5/Apache/Redmine.pm@
35 37 Mischa The Evil
* Redmine.pm can be found in $REDMINE_DIR/extra/svn/Redmine.pm
36
* In the Debian install, it is found /usr/share/redmine/extra/svn/Redmine.pm
37
38 15 Jean-Philippe Lang
Then add the following Location directives to your apache configuration (for example in @/etc/APACHE_DIR/conf.d/@):
39 1 Nicolas Chuche
40 17 Joachim Fritschi
* the old how-to which suggested two separate locations for with @/svn@  and @/svn-private@ can be avoided
41
* with the @Satisfy any@ keyword from Apache you can define different authentication policies
42
* read access from the redmine-server or any validated user
43
* write access only validated users
44 15 Jean-Philippe Lang
45 17 Joachim Fritschi
46 15 Jean-Philippe Lang
<pre>
47 1 Nicolas Chuche
   # /svn location for users
48
   PerlLoadModule Apache::Redmine
49
   <Location /svn>
50
     DAV svn
51 19 Joachim Fritschi
     SVNParentPath "/var/svn"
52 17 Joachim Fritschi
     Order deny,allow
53
     Deny from all
54
     Satisfy any
55 42 Terence Mill
     # If a client tries to svn update which involves updating many files, the update request might result in
56
     # an error Server sent unexpected return value (413 Request  Entity Too Large) in response to REPORT request,
57
     # because the size of the update request exceeds the limit allowed by the server. You can avoid this error by
58
     # disabling the request size limit by adding the line LimitXMLRequestBody 0 between the <Location...> and </Location> lines. 
59
     LimitXMLRequestBody 0
60
     
61
     # Only check Authentification for root path, nor again for recursive folder
62
     # Redmine core does only permit acces on repository level, so this doesn't hurt security. On the other hand it does boost performance a lot!
63
     SVNPathAuthz off
64 1 Nicolas Chuche
65
     PerlAccessHandler Apache::Authn::Redmine::access_handler
66
     PerlAuthenHandler Apache::Authn::Redmine::authen_handler
67 17 Joachim Fritschi
     AuthType Basic
68 18 Joachim Fritschi
     AuthName "Redmine SVN Repository"
69 17 Joachim Fritschi
70
     #read-only access	
71
     <Limit GET PROPFIND OPTIONS REPORT>
72 19 Joachim Fritschi
        Require valid-user
73 17 Joachim Fritschi
        Allow from redmine.server.ip
74
        # Allow from another-ip
75
     	Satisfy any
76
     </Limit>
77
     # write access
78
     <LimitExcept GET PROPFIND OPTIONS REPORT>
79
   	Require valid-user
80
     </LimitExcept>
81
82
83 1 Nicolas Chuche
     ## for mysql
84
     RedmineDSN "DBI:mysql:database=databasename;host=my.db.server"
85 4 Jean-Philippe Lang
     ## for postgres
86 1 Nicolas Chuche
     # RedmineDSN "DBI:Pg:dbname=databasename;host=my.db.server"
87 4 Jean-Philippe Lang
     ## for SQLite3
88 1 Nicolas Chuche
     # RedmineDSN "DBI:SQLite:dbname=database.db"
89
90
     RedmineDbUser "redmine"
91
     RedmineDbPass "password"
92
  </Location>
93
94
</pre>
95
96 17 Joachim Fritschi
h3. Testing the configuration:
97 1 Nicolas Chuche
98 17 Joachim Fritschi
After reloading apache conf, you can try to browse some repository with:
99
100 1 Nicolas Chuche
<pre>
101
svn ls http://my.svn.server/svn/myproject
102 4 Jean-Philippe Lang
</pre>
103 1 Nicolas Chuche
104 17 Joachim Fritschi
Any non-public repository should ask for a username and password.
105 4 Jean-Philippe Lang
106 17 Joachim Fritschi
To test the authentication that allows you redmine server to read all repositories:
107 1 Nicolas Chuche
108 17 Joachim Fritschi
Reading a private repository:
109 3 Jean-Philippe Lang
<pre>
110 17 Joachim Fritschi
svn ls http://my.svn.server/svn/myproject
111
</pre>
112
Try writing to the repository:
113
<pre>
114
svn mkdir http://my.svn.server/svn/myproject/testdir
115
</pre>
116
This should fail and ask for a password.
117
118
119
h3. optional LDAP Authentication
120
121
If you want to connect your LDAP authentication to Apache, you can install the Authen::Simple::LDAP perl module. I found that connecting to my LDAP server to authenticate with every request can be quite slow. I added the following to my configuration and had a significant performance increase. If you have configured an encrypted connection to the LDAP server you will need the IO::Socket::SSL module.
122
123 20 Stefan Stefansson
> *NOTE: the above wording is a little confusing. I attempt to clear up the issues I had with this in the following paragraph.*
124
> 
125 1 Nicolas Chuche
> First of all, make sure that you have the Net::LDAP module installed as well. I installed Authen::Simple::LDAP through CPAN and found that nothing worked. Eventually I figured out that this was because the Authen::Simple::LDAP did not require the Net::LDAP module as a dependency but it is needed for our purpose here. I did this on CentOS and it seems that the Net::LDAP module can be installed via yum (@yum install perl-LDAP@) but the Authen::Simple::LDAP had to be installed via CPAN since there's no RPM for it in the CentOS repositories.
126
> 
127 37 Mischa The Evil
128
To install the Authen::Simple::LDAP using CPAN use commands:
129
<pre>
130
cpan
131
cpan> install Authen::Simple::LDAP
132
</pre>
133
if the installation failed due to some dependencies, resolve the dependencies first.
134
135 20 Stefan Stefansson
> My second point is related to the below Apache config. The @PerlLoadModule Authen::Simple::LDAP@ is actually not required for having users authenticated via LDAP. It will happen automatically if both of the above modules are installed. So there really is no difference between the config snippet below and the one above except for the @RedmineCacheCredsMax 50@ line which is probably a good idea although it can result in users that have been deleted or removed in redmine still getting access to the repositories, at least for a little while.
136
137 17 Joachim Fritschi
<pre>
138 8 Nicolas Chuche
   PerlLoadModule Apache::Redmine
139 17 Joachim Fritschi
   PerlLoadModule  Authen::Simple::LDAP
140
   # PerlLoadModule  IO::Socket::SSL
141 12 Todd Nine
   <Location /svn>
142
     DAV svn
143 42 Terence Mill
     # If a client tries to svn update which involves updating many files, the update request might result in
144
     # an error Server sent unexpected return value (413 Request  Entity Too Large) in response to REPORT request,
145
     # because the size of the update request exceeds the limit allowed by the server. You can avoid this error by
146
     # disabling the request size limit by adding the line LimitXMLRequestBody 0 between the <Location...> and </Location> lines. 
147
     LimitXMLRequestBody 0
148
     
149
     # Only check Authentification for root path, nor again for recursive folder
150
     # Redmine core does only permit acces on repository level, so this doesn't hurt security. On the other hand it does boost performance a lot!
151
     SVNPathAuthz off
152
153 12 Todd Nine
     SVNParentPath "/var/svn"
154
155
     AuthType Basic
156
     AuthName redmine
157
     Require valid-user
158
159
     PerlAccessHandler Apache::Authn::Redmine::access_handler
160
     PerlAuthenHandler Apache::Authn::Redmine::authen_handler
161
  
162
     ## for mysql
163
     RedmineDSN "DBI:mysql:database=databasename;host=my.db.server"
164
     ## for postgres
165
     # RedmineDSN "DBI:Pg:dbname=databasename;host=my.db.server"
166
167 1 Nicolas Chuche
     RedmineDbUser "redmine"
168 12 Todd Nine
     RedmineDbPass "password"
169
     #Cache the last 50 auth entries
170
     RedmineCacheCredsMax 50
171 15 Jean-Philippe Lang
  </Location>
172 12 Todd Nine
</pre>
173 36 neil johnson
174 1 Nicolas Chuche
h2. Apache configuration for Git repositories
175 38 Lluís Vilanova
176
*TODO*: This should probably be moved into [[HowTo configure Redmine for advanced git integration]]
177 15 Jean-Philippe Lang
178 1 Nicolas Chuche
Now that reposman.rb can create git repositories, you can use Redmine.pm to access them the same way than subversion. 
179
180
You first need to copy or link Redmine.pm to /usr/lib/perl5/Apache/Redmine.pm, then you add this configuration to apache : 
181
182
<pre>
183
184 37 Mischa The Evil
PerlLoadModule Apache::Authn::Redmine
185 1 Nicolas Chuche
186 37 Mischa The Evil
SetEnv GIT_PROJECT_ROOT /path/to/git/repos
187
SetEnv GIT_HTTP_EXPORT_ALL
188
189
ScriptAlias /git/ /usr/bin/git-http-backend/
190
191
<Location /git>
192 8 Nicolas Chuche
  AuthType Basic
193
  Require valid-user
194 1 Nicolas Chuche
  AuthName "Git"
195 8 Nicolas Chuche
196
  PerlAccessHandler Apache::Authn::Redmine::access_handler
197
  PerlAuthenHandler Apache::Authn::Redmine::authen_handler
198 1 Nicolas Chuche
199 8 Nicolas Chuche
  RedmineDSN "DBI:mysql:database=redmine;host=localhost"
200
  RedmineDbUser "redmine"
201
  RedmineDbPass "password"
202 37 Mischa The Evil
  RedmineGitSmartHttp yes
203 8 Nicolas Chuche
</Location>
204
205
Alias /git-private /var/git
206
207
<Location /git-private>
208
   Order deny,allow
209
   Deny from all
210
   <Limit GET PROPFIND OPTIONS REPORT>
211
      Options Indexes FollowSymLinks MultiViews
212
   Allow from 127.0.0.1
213
   </Limit>
214
</Location>
215
</pre>
216
217
To verify that you can access repository through Redmine.pm, you can use curl :
218
<pre>
219
% curl --netrc --location http://localhost/git/ecookbook/HEAD   
220 13 Thomas Pihl
ref: refs/heads/master
221
</pre>
222
223 22 Diego Oliveira
h2. Apache configuration for Mercurial repositories
224
225 39 Lluís Vilanova
*TODO*: This should probably be moved into [[HowTo configure Redmine for advanced Mercurial integration]]
226
227 22 Diego Oliveira
Create a file caled "hgweb.config" in the same folder as "hgwebdir.cgi". This foder will be the root repository folder. Then edit the "hgweb.config" with something like this:
228
229
<pre>
230
[paths]
231
/=/path/to/root/repository/**
232
233
[web]
234
allow_push = *
235
allowbz2 = yes
236
allowgz = yes
237
allowzip = yes
238
239
</pre>
240
241
Follows the instructions to install Redmine.pm as described and configure your apache like this.
242
243
<pre>
244
    RewriteEngine on
245
    PerlLoadModule Apache2::Redmine
246
    PerlLoadModule Authen::Simple::LDAP
247
    ScriptAliasMatch ^/hg(.*)  /path/to/the/hgwebdir.cgi/$1
248
    <Location /hg>
249
        AuthType Basic
250
        AuthName "Mercurial"
251
        Require valid-user
252 1 Nicolas Chuche
253
        #Redmine auth
254
        PerlAccessHandler Apache::Authn::Redmine::access_handler
255
        PerlAuthenHandler Apache::Authn::Redmine::authen_handler
256
        RedmineDSN "DBI:mysql:database=redmine;host=localhost"
257
        RedmineDbUser "DB_USER"
258
        RedmineDbPass "DB_PASSWD"
259
    </Location>
260 37 Mischa The Evil
261
    # Note: Do not use hg-private as the rewrite rule above will cause strange things to occur.
262
    Alias /private-hg /path/to/hg/repos
263
    
264
    <Location /private-hg>
265
        Order deny, allow
266
        Deny from all
267
        <Limit GET PROPFIND OPTIONS REPORT>
268
            Options Indexes FollowSymLinks MultiViews
269
            Allow from 127.0.0.1
270
        </Limit>
271
    </Location>
272
273 22 Diego Oliveira
274 1 Nicolas Chuche
</pre>
275 22 Diego Oliveira
276
h2. Gotchas
277 13 Thomas Pihl
278
If you run this in Phusion Passenger, make sure you don't turn PassengerHighPerformance on. If you do, the rewrites to catch subversion dav will be bypassed with some interesting dump in the log as a result.
279
Example: 
280 1 Nicolas Chuche
> ActionController::RoutingError (No route matches "/svn/rm-code" with {:method=>:get}):
281
(if your repo are named rm-code)
282 27 Bill Dieter
283 37 Mischa The Evil
When using @Authen::Simple::LDAP@ for authentication, it is not sufficient to have the Administrator role to access a repository.  The user must have a role with that specifically allows the user to browse, read, or write the repository.