Project

General

Profile

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

Stefan Stefansson, 2010-02-03 03:36

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 3 Jean-Philippe Lang
In this documentation, we will configure apache to delegate authentication to mod_perl. It's tested on apache2 with mysql and postgresql but should work with allmost every databases for which there is a perl DBD module.
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 15 Jean-Philippe Lang
  sudo aptitude install libapache2-svn libapache-dbi-perl libapache2-mod-perl2 libdbd-mysql-perl libdigest-sha1-perl
14 1 Nicolas Chuche
15 15 Jean-Philippe Lang
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.
16 1 Nicolas Chuche
17
h2. Enabling apache modules
18
19
On debian/ubuntu :
20
21
<pre>
22 15 Jean-Philippe Lang
sudo a2enmod dav
23
sudo a2enmod dav_svn
24
sudo a2enmod perl
25 4 Jean-Philippe Lang
</pre>
26 1 Nicolas Chuche
27 15 Jean-Philippe Lang
h2. Apache configuration for Subversion repositories
28 1 Nicolas Chuche
29 15 Jean-Philippe Lang
You first need to copy or link @Redmine.pm@ to @/usr/lib/perl5/Apache/Redmine.pm@
30
Then add the following Location directives to your apache configuration (for example in @/etc/APACHE_DIR/conf.d/@):
31 1 Nicolas Chuche
32 17 Joachim Fritschi
* the old how-to which suggested two separate locations for with @/svn@  and @/svn-private@ can be avoided
33
* with the @Satisfy any@ keyword from Apache you can define different authentication policies
34
* read access from the redmine-server or any validated user
35
* write access only validated users
36 15 Jean-Philippe Lang
37 17 Joachim Fritschi
38 15 Jean-Philippe Lang
<pre>
39 1 Nicolas Chuche
   # /svn location for users
40
   PerlLoadModule Apache::Redmine
41
   <Location /svn>
42
     DAV svn
43 19 Joachim Fritschi
     SVNParentPath "/var/svn"
44 17 Joachim Fritschi
     Order deny,allow
45
     Deny from all
46
     Satisfy any
47 1 Nicolas Chuche
48
     PerlAccessHandler Apache::Authn::Redmine::access_handler
49
     PerlAuthenHandler Apache::Authn::Redmine::authen_handler
50 17 Joachim Fritschi
     AuthType Basic
51 18 Joachim Fritschi
     AuthName "Redmine SVN Repository"
52 17 Joachim Fritschi
53
     #read-only access	
54
     <Limit GET PROPFIND OPTIONS REPORT>
55 19 Joachim Fritschi
        Require valid-user
56 17 Joachim Fritschi
        Allow from redmine.server.ip
57
        # Allow from another-ip
58
     	Satisfy any
59
     </Limit>
60
     # write access
61
     <LimitExcept GET PROPFIND OPTIONS REPORT>
62
   	Require valid-user
63
     </LimitExcept>
64
65
66 1 Nicolas Chuche
     ## for mysql
67
     RedmineDSN "DBI:mysql:database=databasename;host=my.db.server"
68 4 Jean-Philippe Lang
     ## for postgres
69 1 Nicolas Chuche
     # RedmineDSN "DBI:Pg:dbname=databasename;host=my.db.server"
70 4 Jean-Philippe Lang
     ## for SQLite3
71 1 Nicolas Chuche
     # RedmineDSN "DBI:SQLite:dbname=database.db"
72
73
     RedmineDbUser "redmine"
74
     RedmineDbPass "password"
75
  </Location>
76
77
</pre>
78
79 17 Joachim Fritschi
h3. Testing the configuration:
80 1 Nicolas Chuche
81 17 Joachim Fritschi
After reloading apache conf, you can try to browse some repository with:
82
83 1 Nicolas Chuche
<pre>
84
svn ls http://my.svn.server/svn/myproject
85 4 Jean-Philippe Lang
</pre>
86 1 Nicolas Chuche
87 17 Joachim Fritschi
Any non-public repository should ask for a username and password.
88 4 Jean-Philippe Lang
89 17 Joachim Fritschi
To test the authentication that allows you redmine server to read all repositories:
90 1 Nicolas Chuche
91 17 Joachim Fritschi
Reading a private repository:
92 3 Jean-Philippe Lang
<pre>
93 17 Joachim Fritschi
svn ls http://my.svn.server/svn/myproject
94
</pre>
95
Try writing to the repository:
96
<pre>
97
svn mkdir http://my.svn.server/svn/myproject/testdir
98
</pre>
99
This should fail and ask for a password.
100
101
102
h3. optional LDAP Authentication
103
104
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.
105
106 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.*
107
> 
108
> 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.
109
> 
110
> 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.
111
112 17 Joachim Fritschi
<pre>
113 8 Nicolas Chuche
   PerlLoadModule Apache::Redmine
114 17 Joachim Fritschi
   PerlLoadModule  Authen::Simple::LDAP
115
   # PerlLoadModule  IO::Socket::SSL
116 12 Todd Nine
   <Location /svn>
117
     DAV svn
118
     SVNParentPath "/var/svn"
119
120
     AuthType Basic
121
     AuthName redmine
122
     Require valid-user
123
124
     PerlAccessHandler Apache::Authn::Redmine::access_handler
125
     PerlAuthenHandler Apache::Authn::Redmine::authen_handler
126
  
127
     ## for mysql
128
     RedmineDSN "DBI:mysql:database=databasename;host=my.db.server"
129
     ## for postgres
130
     # RedmineDSN "DBI:Pg:dbname=databasename;host=my.db.server"
131
132
     RedmineDbUser "redmine"
133
     RedmineDbPass "password"
134
     #Cache the last 50 auth entries
135
     RedmineCacheCredsMax 50
136
  </Location>
137
</pre>
138 15 Jean-Philippe Lang
139 12 Todd Nine
140 15 Jean-Philippe Lang
h2. Apache configuration for Git repositories
141
142
Now that reposman.rb can create git repositories, you can use Redmine.pm to access them the same way than subversion. 
143 8 Nicolas Chuche
144
You first need to copy or link Redmine.pm to /usr/lib/perl5/Apache/Redmine.pm, then you add this configuration to apache : 
145
146
<pre>
147 9 Nicolas Chuche
Alias /git /var/git
148 8 Nicolas Chuche
149
PerlLoadModule Apache::Redmine
150
<Location /git>
151
  DAV on
152
153
  AuthType Basic
154
  Require valid-user
155
  AuthName "Git"
156
157
  PerlAccessHandler Apache::Authn::Redmine::access_handler
158
  PerlAuthenHandler Apache::Authn::Redmine::authen_handler
159
160
  RedmineDSN "DBI:mysql:database=redmine;host=localhost"
161
  RedmineDbUser "redmine"
162
  RedmineDbPass "password"
163
</Location>
164
165
Alias /git-private /var/git
166
167
<Location /git-private>
168
   Order deny,allow
169
   Deny from all
170
   <Limit GET PROPFIND OPTIONS REPORT>
171
      Options Indexes FollowSymLinks MultiViews
172
   Allow from 127.0.0.1
173
   </Limit>
174
</Location>
175
</pre>
176
177
To verify that you can access repository through Redmine.pm, you can use curl :
178
<pre>
179
% curl --netrc --location http://localhost/git/ecookbook/HEAD   
180 13 Thomas Pihl
ref: refs/heads/master
181
</pre>
182
183
h2. Gotchas
184
185
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.
186
Example: 
187 1 Nicolas Chuche
> ActionController::RoutingError (No route matches "/svn/rm-code" with {:method=>:get}):
188
(if your repo are named rm-code)