-
Notifications
You must be signed in to change notification settings - Fork 1
/
PgBouncerAuthSyntax.pm
299 lines (198 loc) · 7.1 KB
/
PgBouncerAuthSyntax.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package App::GitHooks::Plugin::PgBouncerAuthSyntax;
use strict;
use warnings;
use base 'App::GitHooks::Plugin';
# External dependencies.
use Carp;
use File::Slurp qw();
# Internal dependencies.
use App::GitHooks::Constants qw( :PLUGIN_RETURN_CODES );
=head1 NAME
App::GitHooks::Plugin::PgBouncerAuthSyntax - Verify that the syntax of PgBouncer auth files is correct.
=head1 DESCRIPTION
This plugin verifies that staged PgBouncer authentication files have a proper
syntax before allowing the commit to be completed.
See http://pgbouncer.projects.pgfoundry.org/doc/config.html, under the
"Authentication File Format" section, for more information about the required
syntax.
=head1 VERSION
Version 1.1.0
=cut
our $VERSION = '1.1.0';
=head1 CONFIGURATION OPTIONS
This plugin supports the following options in the C<[PgBouncerAuthSyntax]>
section of your C<.githooksrc> file.
[PgBouncerAuthSyntax]
file_pattern = /^configs\/pgbouncer\/userlist.txt$/
comments_setting = disallow
=head2 file_pattern
A regular expression that will be checked against the path of files that are
committed and that indicates a PgBouncer auth file to analyze when it matches.
file_pattern = /^configs\/pgbouncer\/userlist.txt$/
=head2 comments_setting
As of version 1.5.4, PgBouncer does not allow comments. This will however
change in the next release, thanks to
L<this patch|https://github.com/markokr/pgbouncer-dev/commit/995acda16ce30cde67ab1839387138b7545ff786>.
Configure this setting accordingly based on your PgBouncer version:
=over 4
=item * I<allow_anywhere>
Allow comments anywhere. Use with PgBouncer versions above 1.5.4 (not
included).
comments_setting = allow_anywhere
=item * I<allow_end_only>
Allow comments at the end of the file only. PgBouncer will stop parsing the
auth file as soon as it encounters an incorrectly formatted line, so you can
technically add comments at the end of the file. This setting will prevent you
from accidentally adding anything but comments once the first comment is seen,
to catch errors that are otherwise tricky to debug.
comments_setting = allow_end_only
=item * I<disallow>
Don't allow comments at all. The safest setting for PgBouncer versions up to
1.5.4 (included).
comments_setting = disallow
=back
=head1 METHODS
=head2 get_file_pattern()
Return a pattern to filter the files this plugin should analyze.
my $file_pattern = App::GitHooks::Plugin::PgBouncerAuthSyntax->get_file_pattern(
app => $app,
);
=cut
sub get_file_pattern
{
my ( $class, %args ) = @_;
my $app = delete( $args{'app'} );
my $config = $app->get_config();
# Retrieve the config value.
my $regex = $config->get_regex( 'PgBouncerAuthSyntax', 'file_pattern' );
croak "'file_pattern' is not defined in the [PgBouncerAuthSyntax] section of your config file"
if !defined $regex;
return qr/$regex/;
}
=head2 get_file_check_description()
Return a description of the check performed on files by the plugin and that
will be displayed to the user, if applicable, along with an indication of the
success or failure of the plugin.
my $description = App::GitHooks::Plugin::PgBouncerAuthSyntax->get_file_check_description();
=cut
sub get_file_check_description
{
return 'The PgBouncer syntax is correct';
}
=head2 run_pre_commit_file()
Code to execute for each file as part of the pre-commit hook.
my $success = App::GitHooks::Plugin::PgBouncerAuthSyntax->run_pre_commit_file();
=cut
sub run_pre_commit_file
{
my ( $class, %args ) = @_;
my $file = delete( $args{'file'} );
my $git_action = delete( $args{'git_action'} );
my $app = delete( $args{'app'} );
my $repository = $app->get_repository();
my $config = $app->get_config();
# Ignore deleted files.
return $PLUGIN_RETURN_SKIPPED
if $git_action eq 'D';
# Determine which setting to use for comments.
my $comments_setting = $config->get( 'PgBouncerAuthSyntax', 'comments_setting' );
croak '"comments_setting" needs to be defined in the [PgBouncerAuthSyntax] section of your .githooksrc file'
if !defined( $comments_setting ) || ( $comments_setting eq '' );
croak 'The value of "comments_setting" in the [PgBouncerAuthSyntax] section of your .githooksrc file is not valid'
if $comments_setting !~ /^(?:allow_anywhere|allow_end_only|disallow)$/x;
# Retrieve lines.
my @lines = File::Slurp::read_file( $repository->work_tree() . '/' . $file );
# Find the incorrectly formatted lines.
my @issues = ();
my $comments_detected = 0;
for ( my $i = 0; $i < scalar( @lines ); $i++ )
{
my $line = $lines[ $i ];
# Skip blank lines.
next
if !defined( $line ) || ( $line eq '' );
# Handle comments.
if ( substr( $line, 0, 1 ) eq ';' )
{
$comments_detected = 1;
# If we don't allow comments, note the error before moving on to the next
# line.
if ( $comments_setting eq 'disallow' )
{
push(
@issues,
{
line_number => $i,
line => $line,
}
);
}
next;
}
if ( $comments_detected && ( $comments_setting eq 'allow_end_only' ) )
{
# This line is not a comment, but comment lines have already been seen
# and we only allow comments at the end of the file.
push(
@issues,
{
line_number => $i,
line => $line,
}
);
next;
}
# Skip lines with the correct username/password specification.
next
if $line =~ /
^
"[^"]*" # Username.
\ # Space.
"[^"]*" # Password.
(?:\ .*)? # Remainder of the line, no specific format required except
# for a space if anything follows.
$
/x;
push(
@issues,
{
line_number => $i,
line => $line,
}
);
}
die "Incorrectly formatted lines:\n" . join( '', map { "Line $_->{'line_number'}: $_->{'line'}" } @issues ) . "\n"
if scalar( @issues ) != 0;
return $PLUGIN_RETURN_PASSED;
}
=head1 BUGS
Please report any bugs or feature requests through the web interface at
L<https://github.com/guillaumeaubert/App-GitHooks-Plugin-PgBouncerAuthSyntax/issues/new>.
I will be notified, and then you'll automatically be notified of progress on
your bug as I make changes.
=head1 SUPPORT
You can find documentation for this module with the perldoc command.
perldoc App::GitHooks::Plugin::PgBouncerAuthSyntax
You can also look for information at:
=over
=item * GitHub's request tracker
L<https://github.com/guillaumeaubert/App-GitHooks-Plugin-PgBouncerAuthSyntax/issues>
=item * AnnoCPAN: Annotated CPAN documentation
L<http://annocpan.org/dist/app-githooks-plugin-pgbouncerauthsyntax>
=item * CPAN Ratings
L<http://cpanratings.perl.org/d/app-githooks-plugin-pgbouncerauthsyntax>
=item * MetaCPAN
L<https://metacpan.org/release/App-GitHooks-Plugin-PgBouncerAuthSyntax>
=back
=head1 AUTHOR
L<Guillaume Aubert|https://metacpan.org/author/AUBERTG>,
C<< <aubertg at cpan.org> >>.
=head1 COPYRIGHT & LICENSE
Copyright 2013-2017 Guillaume Aubert.
This code is free software; you can redistribute it and/or modify it under the
same terms as Perl 5 itself.
This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the LICENSE file for more details.
=cut
1;