| 1 | package Beam::Runnable::AllowUsers; | ||||||||||
| 2 | 2 | our $VERSION = '0.016'; | |||||||||
| 3 | # ABSTRACT: Only allow certain users to run a command | ||||||||||
| 4 | |||||||||||
| 5 | #pod =head1 SYNOPSIS | ||||||||||
| 6 | #pod | ||||||||||
| 7 | #pod ### In a Runnable module | ||||||||||
| 8 | #pod package My::Runnable::Script; | ||||||||||
| 9 | #pod use Moo; | ||||||||||
| 10 | #pod with 'Beam::Runnable', 'Beam::Runnable::AllowUsers'; | ||||||||||
| 11 | #pod has '+allow_users' => ( default => [ 'root' ] ); | ||||||||||
| 12 | #pod sub run { } | ||||||||||
| 13 | #pod | ||||||||||
| 14 | #pod ### In a container config file | ||||||||||
| 15 | #pod runnable: | ||||||||||
| 16 | #pod $class: My::Runnable::Script | ||||||||||
| 17 | #pod $with: | ||||||||||
| 18 | #pod - 'Beam::Runnable::AllowUsers' | ||||||||||
| 19 | #pod allow_users: | ||||||||||
| 20 | #pod - root | ||||||||||
| 21 | #pod - doug | ||||||||||
| 22 | #pod | ||||||||||
| 23 | #pod =head1 DESCRIPTION | ||||||||||
| 24 | #pod | ||||||||||
| 25 | #pod This role checks to ensure that only certain users can run a command. If | ||||||||||
| 26 | #pod an unauthorized user runs the command, it dies with an error instead. | ||||||||||
| 27 | #pod | ||||||||||
| 28 | #pod B<NOTE:> This is mostly a demonstration of a L<Beam::Runnable> role. | ||||||||||
| 29 | #pod Users that can write to the configuration file can edit who is allowed | ||||||||||
| 30 | #pod to run the command, and there are other ways to prevent access to | ||||||||||
| 31 | #pod a file/command. | ||||||||||
| 32 | #pod | ||||||||||
| 33 | #pod =head1 SEE ALSO | ||||||||||
| 34 | #pod | ||||||||||
| 35 | #pod L<Beam::Runnable>, L<perlfunc/getpwuid>, L<< perlvar/$> >> | ||||||||||
| 36 | #pod | ||||||||||
| 37 | #pod =cut | ||||||||||
| 38 | |||||||||||
| 39 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 40 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 41 | 2 | ▶ | use Moo::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 42 | 2 | ▶ | use List::Util qw( any ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 43 | 2 | ▶ | use Types::Standard qw( ArrayRef Str ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 44 | |||||||||||
| 45 | #pod =attr allow_users | ||||||||||
| 46 | #pod | ||||||||||
| 47 | #pod An array reference of user names that are allowed to run this task. | ||||||||||
| 48 | #pod | ||||||||||
| 49 | #pod =cut | ||||||||||
| 50 | |||||||||||
| 51 | 2 | has allow_users => ( | |||||||||
| 52 | is => 'ro', | ||||||||||
| 53 | isa => ArrayRef[ Str ], | ||||||||||
| 54 | required => 1, | ||||||||||
| 55 | ); | ||||||||||
| 56 | |||||||||||
| 57 | #pod =method run | ||||||||||
| 58 | #pod | ||||||||||
| 59 | #pod This role wraps the C<run> method of your runnable class to check that | ||||||||||
| 60 | #pod the user is authorized. | ||||||||||
| 61 | #pod | ||||||||||
| 62 | #pod =cut | ||||||||||
| 63 | |||||||||||
| 64 | 2 | ▶ | before run => sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 65 | 2 | my ( $self, @args ) = @_; | |||||||||
| 66 | 2 | my $user = getpwuid( $> ); | |||||||||
| 67 | die "Unauthorized user: $user\n" | ||||||||||
| 68 | 2 | ▶ | unless any { $_ eq $user } @{ $self->allow_users }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||
| 69 | 2 | }; | |||||||||
| 70 | |||||||||||
| 71 | 2 | 1; | |||||||||
| 72 | |||||||||||
| 73 | __END__ | ||||||||||