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

Jim McAleer, 2019-06-12 06:20

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 2 Nicolas Chuche
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 1 Nicolas Chuche
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 43 James Ang
<pre>sudo aptitude install libapache2-svn libapache-dbi-perl \
14 43 James Ang
libapache2-mod-perl2 libdbd-mysql-perl libdigest-sha1-perl \
15 43 James Ang
libauthen-simple-ldap-perl</pre>
16 41 Zack s
17 1 Nicolas Chuche
On Turnkey Redmine Virtual Appliance you can do :
18 1 Nicolas Chuche
19 43 James Ang
<pre>apt-get install libapache2-svn libapache-dbi-perl \
20 43 James Ang
libapache2-mod-perl2 libauthen-simple-ldap-perl</pre>
21 40 Zack s
22 47 Jim McAleer
On Turnkey Redmine Virtual Appliance v 15.0 and greater you can do :
23 47 Jim McAleer
24 47 Jim McAleer
<pre>
25 47 Jim McAleer
apt-get install libapache2-mod-svn libapache-dbi-perl libapache2-mod-perl2 libauthen-simple-ldap-perl
26 47 Jim McAleer
</pre>
27 47 Jim McAleer
28 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@.
29 1 Nicolas Chuche
30 1 Nicolas Chuche
h2. Enabling apache modules
31 1 Nicolas Chuche
32 1 Nicolas Chuche
On debian/ubuntu :
33 1 Nicolas Chuche
34 1 Nicolas Chuche
<pre>
35 15 Jean-Philippe Lang
sudo a2enmod dav
36 21 Marko Roeder
sudo a2enmod dav_svn # if you want to use svn
37 21 Marko Roeder
sudo a2enmod dav_fs  # if you want to use git
38 15 Jean-Philippe Lang
sudo a2enmod perl
39 4 Jean-Philippe Lang
</pre>
40 1 Nicolas Chuche
41 15 Jean-Philippe Lang
h2. Apache configuration for Subversion repositories
42 1 Nicolas Chuche
43 1 Nicolas Chuche
You first need to copy or link @Redmine.pm@ to @/usr/lib/perl5/Apache/Redmine.pm@
44 37 Mischa The Evil
* Redmine.pm can be found in $REDMINE_DIR/extra/svn/Redmine.pm
45 37 Mischa The Evil
* In the Debian install, it is found /usr/share/redmine/extra/svn/Redmine.pm
46 37 Mischa The Evil
47 15 Jean-Philippe Lang
Then add the following Location directives to your apache configuration (for example in @/etc/APACHE_DIR/conf.d/@):
48 1 Nicolas Chuche
49 17 Joachim Fritschi
* the old how-to which suggested two separate locations for with @/svn@  and @/svn-private@ can be avoided
50 17 Joachim Fritschi
* with the @Satisfy any@ keyword from Apache you can define different authentication policies
51 17 Joachim Fritschi
* read access from the redmine-server or any validated user
52 17 Joachim Fritschi
* write access only validated users
53 15 Jean-Philippe Lang
54 17 Joachim Fritschi
55 15 Jean-Philippe Lang
<pre>
56 46 Antonio García-Domínguez
   # enables connection pooling (very useful for checkouts with many files)
57 46 Antonio García-Domínguez
   PerlModule Apache::DBI
58 46 Antonio García-Domínguez
   PerlOptions +GlobalRequest
59 46 Antonio García-Domínguez
60 1 Nicolas Chuche
   # /svn location for users
61 1 Nicolas Chuche
   PerlLoadModule Apache::Redmine
62 1 Nicolas Chuche
   <Location /svn>
63 1 Nicolas Chuche
     DAV svn
64 45 Friedrich Schiller
65 45 Friedrich Schiller
     ### uncomment the following line when using subversion 1.8 or newer (see http://subversion.apache.org/docs/release-notes/1.8.html#serf-skelta-default)
66 45 Friedrich Schiller
     # SVNAllowBulkUpdates Prefer
67 45 Friedrich Schiller
68 1 Nicolas Chuche
     SVNParentPath "/var/svn"
69 1 Nicolas Chuche
     Order deny,allow
70 1 Nicolas Chuche
     Deny from all
71 1 Nicolas Chuche
     Satisfy any
72 43 James Ang
     # If a client tries to svn update which involves updating many files,
73 43 James Ang
     # the update request might result in an error Server sent unexpected
74 43 James Ang
     # return value (413 Request  Entity Too Large) in response to REPORT
75 43 James Ang
     # request,because the size of the update request exceeds the limit
76 43 James Ang
     # allowed by the server. You can avoid this error by disabling the
77 43 James Ang
     # request size limit by adding the line LimitXMLRequestBody 0
78 43 James Ang
     # between the <Location...> and </Location> lines. 
79 42 Terence Mill
     LimitXMLRequestBody 0
80 42 Terence Mill
     
81 43 James Ang
     # Only check Authentication for root path, nor again for recursive
82 43 James Ang
     # folder.
83 43 James Ang
     # Redmine core does only permit access on repository level, so this
84 43 James Ang
     # doesn't hurt security. On the other hand it does boost performance
85 43 James Ang
     # a lot!
86 42 Terence Mill
     SVNPathAuthz off
87 1 Nicolas Chuche
88 1 Nicolas Chuche
     PerlAccessHandler Apache::Authn::Redmine::access_handler
89 1 Nicolas Chuche
     PerlAuthenHandler Apache::Authn::Redmine::authen_handler
90 17 Joachim Fritschi
     AuthType Basic
91 18 Joachim Fritschi
     AuthName "Redmine SVN Repository"
92 44 F Abu-Nimeh
     AuthUserFile /dev/null
93 17 Joachim Fritschi
94 17 Joachim Fritschi
     #read-only access	
95 17 Joachim Fritschi
     <Limit GET PROPFIND OPTIONS REPORT>
96 19 Joachim Fritschi
        Require valid-user
97 17 Joachim Fritschi
        Allow from redmine.server.ip
98 17 Joachim Fritschi
        # Allow from another-ip
99 17 Joachim Fritschi
     	Satisfy any
100 17 Joachim Fritschi
     </Limit>
101 17 Joachim Fritschi
     # write access
102 17 Joachim Fritschi
     <LimitExcept GET PROPFIND OPTIONS REPORT>
103 17 Joachim Fritschi
   	Require valid-user
104 17 Joachim Fritschi
     </LimitExcept>
105 17 Joachim Fritschi
106 17 Joachim Fritschi
107 1 Nicolas Chuche
     ## for mysql
108 1 Nicolas Chuche
     RedmineDSN "DBI:mysql:database=databasename;host=my.db.server"
109 4 Jean-Philippe Lang
     ## for postgres
110 1 Nicolas Chuche
     # RedmineDSN "DBI:Pg:dbname=databasename;host=my.db.server"
111 4 Jean-Philippe Lang
     ## for SQLite3
112 1 Nicolas Chuche
     # RedmineDSN "DBI:SQLite:dbname=database.db"
113 1 Nicolas Chuche
114 1 Nicolas Chuche
     RedmineDbUser "redmine"
115 1 Nicolas Chuche
     RedmineDbPass "password"
116 1 Nicolas Chuche
  </Location>
117 1 Nicolas Chuche
118 1 Nicolas Chuche
</pre>
119 1 Nicolas Chuche
120 17 Joachim Fritschi
h3. Testing the configuration:
121 1 Nicolas Chuche
122 17 Joachim Fritschi
After reloading apache conf, you can try to browse some repository with:
123 17 Joachim Fritschi
124 1 Nicolas Chuche
<pre>
125 1 Nicolas Chuche
svn ls http://my.svn.server/svn/myproject
126 4 Jean-Philippe Lang
</pre>
127 1 Nicolas Chuche
128 17 Joachim Fritschi
Any non-public repository should ask for a username and password.
129 4 Jean-Philippe Lang
130 17 Joachim Fritschi
To test the authentication that allows you redmine server to read all repositories:
131 1 Nicolas Chuche
132 17 Joachim Fritschi
Reading a private repository:
133 3 Jean-Philippe Lang
<pre>
134 17 Joachim Fritschi
svn ls http://my.svn.server/svn/myproject
135 17 Joachim Fritschi
</pre>
136 17 Joachim Fritschi
Try writing to the repository:
137 17 Joachim Fritschi
<pre>
138 17 Joachim Fritschi
svn mkdir http://my.svn.server/svn/myproject/testdir
139 17 Joachim Fritschi
</pre>
140 17 Joachim Fritschi
This should fail and ask for a password.
141 17 Joachim Fritschi
142 17 Joachim Fritschi
143 17 Joachim Fritschi
h3. optional LDAP Authentication
144 17 Joachim Fritschi
145 17 Joachim Fritschi
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.
146 17 Joachim Fritschi
147 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.*
148 20 Stefan Stefansson
> 
149 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.
150 37 Mischa The Evil
> 
151 37 Mischa The Evil
152 37 Mischa The Evil
To install the Authen::Simple::LDAP using CPAN use commands:
153 37 Mischa The Evil
<pre>
154 20 Stefan Stefansson
cpan
155 1 Nicolas Chuche
cpan> install Authen::Simple::LDAP
156 1 Nicolas Chuche
</pre>
157 1 Nicolas Chuche
if the installation failed due to some dependencies, resolve the dependencies first.
158 1 Nicolas Chuche
159 1 Nicolas Chuche
> 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.
160 1 Nicolas Chuche
161 1 Nicolas Chuche
<pre>
162 20 Stefan Stefansson
   PerlLoadModule Apache::Redmine
163 17 Joachim Fritschi
   PerlLoadModule  Authen::Simple::LDAP
164 1 Nicolas Chuche
   # PerlLoadModule  IO::Socket::SSL
165 1 Nicolas Chuche
   <Location /svn>
166 8 Nicolas Chuche
     DAV svn
167 43 James Ang
     # If a client tries to svn update which involves updating many files, the
168 43 James Ang
     # update request might result in an error Server sent unexpected return
169 43 James Ang
     # value (413 Request  Entity Too Large) in response to REPORT request,
170 43 James Ang
     # because the size of the update request exceeds the limit allowed by the
171 43 James Ang
     # server. You can avoid this error by disabling the request size limit by
172 43 James Ang
     # adding the line LimitXMLRequestBody 0 between the <Location...> and
173 43 James Ang
     # </Location> lines. 
174 42 Terence Mill
     LimitXMLRequestBody 0
175 42 Terence Mill
     
176 42 Terence Mill
     # Only check Authentification for root path, nor again for recursive folder
177 43 James Ang
     # Redmine core does only permit acces on repository level, so this doesn't
178 43 James Ang
     # hurt security. On the other hand it does boost performance a lot!
179 42 Terence Mill
     SVNPathAuthz off
180 42 Terence Mill
181 12 Todd Nine
     SVNParentPath "/var/svn"
182 12 Todd Nine
183 12 Todd Nine
     AuthType Basic
184 12 Todd Nine
     AuthName redmine
185 12 Todd Nine
     Require valid-user
186 12 Todd Nine
187 12 Todd Nine
     PerlAccessHandler Apache::Authn::Redmine::access_handler
188 12 Todd Nine
     PerlAuthenHandler Apache::Authn::Redmine::authen_handler
189 12 Todd Nine
  
190 12 Todd Nine
     ## for mysql
191 12 Todd Nine
     RedmineDSN "DBI:mysql:database=databasename;host=my.db.server"
192 12 Todd Nine
     ## for postgres
193 12 Todd Nine
     # RedmineDSN "DBI:Pg:dbname=databasename;host=my.db.server"
194 12 Todd Nine
195 1 Nicolas Chuche
     RedmineDbUser "redmine"
196 12 Todd Nine
     RedmineDbPass "password"
197 12 Todd Nine
     #Cache the last 50 auth entries
198 12 Todd Nine
     RedmineCacheCredsMax 50
199 15 Jean-Philippe Lang
  </Location>
200 12 Todd Nine
</pre>
201 36 neil johnson
202 1 Nicolas Chuche
h2. Apache configuration for Git repositories
203 38 Lluís Vilanova
204 38 Lluís Vilanova
*TODO*: This should probably be moved into [[HowTo configure Redmine for advanced git integration]]
205 15 Jean-Philippe Lang
206 1 Nicolas Chuche
Now that reposman.rb can create git repositories, you can use Redmine.pm to access them the same way than subversion. 
207 1 Nicolas Chuche
208 1 Nicolas Chuche
You first need to copy or link Redmine.pm to /usr/lib/perl5/Apache/Redmine.pm, then you add this configuration to apache : 
209 1 Nicolas Chuche
210 1 Nicolas Chuche
<pre>
211 1 Nicolas Chuche
212 37 Mischa The Evil
PerlLoadModule Apache::Authn::Redmine
213 1 Nicolas Chuche
214 37 Mischa The Evil
SetEnv GIT_PROJECT_ROOT /path/to/git/repos
215 37 Mischa The Evil
SetEnv GIT_HTTP_EXPORT_ALL
216 37 Mischa The Evil
217 37 Mischa The Evil
ScriptAlias /git/ /usr/bin/git-http-backend/
218 37 Mischa The Evil
219 37 Mischa The Evil
<Location /git>
220 8 Nicolas Chuche
  AuthType Basic
221 8 Nicolas Chuche
  Require valid-user
222 1 Nicolas Chuche
  AuthName "Git"
223 8 Nicolas Chuche
224 8 Nicolas Chuche
  PerlAccessHandler Apache::Authn::Redmine::access_handler
225 8 Nicolas Chuche
  PerlAuthenHandler Apache::Authn::Redmine::authen_handler
226 1 Nicolas Chuche
227 8 Nicolas Chuche
  RedmineDSN "DBI:mysql:database=redmine;host=localhost"
228 8 Nicolas Chuche
  RedmineDbUser "redmine"
229 8 Nicolas Chuche
  RedmineDbPass "password"
230 37 Mischa The Evil
  RedmineGitSmartHttp yes
231 8 Nicolas Chuche
</Location>
232 8 Nicolas Chuche
233 8 Nicolas Chuche
Alias /git-private /var/git
234 8 Nicolas Chuche
235 8 Nicolas Chuche
<Location /git-private>
236 8 Nicolas Chuche
   Order deny,allow
237 8 Nicolas Chuche
   Deny from all
238 8 Nicolas Chuche
   <Limit GET PROPFIND OPTIONS REPORT>
239 8 Nicolas Chuche
      Options Indexes FollowSymLinks MultiViews
240 8 Nicolas Chuche
   Allow from 127.0.0.1
241 8 Nicolas Chuche
   </Limit>
242 8 Nicolas Chuche
</Location>
243 8 Nicolas Chuche
</pre>
244 8 Nicolas Chuche
245 8 Nicolas Chuche
To verify that you can access repository through Redmine.pm, you can use curl :
246 8 Nicolas Chuche
<pre>
247 8 Nicolas Chuche
% curl --netrc --location http://localhost/git/ecookbook/HEAD   
248 13 Thomas Pihl
ref: refs/heads/master
249 13 Thomas Pihl
</pre>
250 13 Thomas Pihl
251 22 Diego Oliveira
h2. Apache configuration for Mercurial repositories
252 22 Diego Oliveira
253 39 Lluís Vilanova
*TODO*: This should probably be moved into [[HowTo configure Redmine for advanced Mercurial integration]]
254 39 Lluís Vilanova
255 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:
256 22 Diego Oliveira
257 22 Diego Oliveira
<pre>
258 22 Diego Oliveira
[paths]
259 22 Diego Oliveira
/=/path/to/root/repository/**
260 22 Diego Oliveira
261 22 Diego Oliveira
[web]
262 22 Diego Oliveira
allow_push = *
263 22 Diego Oliveira
allowbz2 = yes
264 22 Diego Oliveira
allowgz = yes
265 22 Diego Oliveira
allowzip = yes
266 22 Diego Oliveira
267 22 Diego Oliveira
</pre>
268 22 Diego Oliveira
269 22 Diego Oliveira
Follows the instructions to install Redmine.pm as described and configure your apache like this.
270 22 Diego Oliveira
271 22 Diego Oliveira
<pre>
272 22 Diego Oliveira
    RewriteEngine on
273 22 Diego Oliveira
    PerlLoadModule Apache2::Redmine
274 22 Diego Oliveira
    PerlLoadModule Authen::Simple::LDAP
275 22 Diego Oliveira
    ScriptAliasMatch ^/hg(.*)  /path/to/the/hgwebdir.cgi/$1
276 22 Diego Oliveira
    <Location /hg>
277 22 Diego Oliveira
        AuthType Basic
278 22 Diego Oliveira
        AuthName "Mercurial"
279 22 Diego Oliveira
        Require valid-user
280 1 Nicolas Chuche
281 1 Nicolas Chuche
        #Redmine auth
282 1 Nicolas Chuche
        PerlAccessHandler Apache::Authn::Redmine::access_handler
283 1 Nicolas Chuche
        PerlAuthenHandler Apache::Authn::Redmine::authen_handler
284 1 Nicolas Chuche
        RedmineDSN "DBI:mysql:database=redmine;host=localhost"
285 1 Nicolas Chuche
        RedmineDbUser "DB_USER"
286 1 Nicolas Chuche
        RedmineDbPass "DB_PASSWD"
287 1 Nicolas Chuche
    </Location>
288 37 Mischa The Evil
289 37 Mischa The Evil
    # Note: Do not use hg-private as the rewrite rule above will cause strange things to occur.
290 37 Mischa The Evil
    Alias /private-hg /path/to/hg/repos
291 37 Mischa The Evil
    
292 37 Mischa The Evil
    <Location /private-hg>
293 37 Mischa The Evil
        Order deny, allow
294 37 Mischa The Evil
        Deny from all
295 37 Mischa The Evil
        <Limit GET PROPFIND OPTIONS REPORT>
296 37 Mischa The Evil
            Options Indexes FollowSymLinks MultiViews
297 37 Mischa The Evil
            Allow from 127.0.0.1
298 37 Mischa The Evil
        </Limit>
299 37 Mischa The Evil
    </Location>
300 37 Mischa The Evil
301 22 Diego Oliveira
302 1 Nicolas Chuche
</pre>
303 22 Diego Oliveira
304 22 Diego Oliveira
h2. Gotchas
305 13 Thomas Pihl
306 13 Thomas Pihl
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.
307 13 Thomas Pihl
Example: 
308 1 Nicolas Chuche
> ActionController::RoutingError (No route matches "/svn/rm-code" with {:method=>:get}):
309 1 Nicolas Chuche
(if your repo are named rm-code)
310 27 Bill Dieter
311 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.