Skip to content

Commit

Permalink
Add support for prefix => true usage for role_perm.
Browse files Browse the repository at this point in the history
  • Loading branch information
hexfusion committed Jul 6, 2018
1 parent 9faf14e commit 2e4e5bc
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ matrix:
include:
- perl: 5.26
env: COVERAGE=1 # enables coverage+coveralls reporting
allow_failures:
- perl: "5.28"
before_install:
- curl https://coreos.com/dist/pubkeys/app-signing-pubkey.gpg | sudo apt-key add -
- wget https://github.com/coreos/etcd/releases/download/${ETCD_VER}/etcd-${ETCD_VER}-linux-amd64.tar.gz -O /tmp/etcd-${ETCD_VER}-linux-amd64.tar.gz
Expand Down
3 changes: 3 additions & 0 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ PUBLIC METHODS
Grants or revoke permission of a specified key or range to a specified
role.

$etcd->role_perm(
{ name => 'myrole', key => 'bar', permType => 'READ', prefix => 1 } )->grant;

user_role
See Net::Etcd::User::Role

Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ See [Net::Etcd::Auth::RolePermission](https://metacpan.org/pod/Net::Etcd::Auth::

Grants or revoke permission of a specified key or range to a specified role.

$etcd->role_perm(
{ name => 'myrole', key => 'bar', permType => 'READ', prefix => 1 } )->grant;

## user\_role

See [Net::Etcd::User::Role](https://metacpan.org/pod/Net::Etcd::User::Role)
Expand Down
4 changes: 3 additions & 1 deletion lib/Net/Etcd.pm
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ has cacert => (
default => $ENV{ETCD_CLIENT_CACERT_FILE}
);


=head2 ssl
To enable set to 1
Expand Down Expand Up @@ -313,6 +312,9 @@ See L<Net::Etcd::Auth::RolePermission>
Grants or revoke permission of a specified key or range to a specified role.
$etcd->role_perm(
{ name => 'myrole', key => 'bar', permType => 'READ', prefix => 1 } )->grant;
=cut

sub role_perm {
Expand Down
23 changes: 19 additions & 4 deletions lib/Net/Etcd/Auth/RolePermission.pm
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,19 @@ valid options are READ, WRITE, and READWRITE
=cut

has permType =>(
has permType =>(
is => 'ro',
isa => Str,
);

=head2 prefix
This is a helper accessor which is an alias for range_end => '\0' if passed a true value.
If range_end is also passed prefix will superceed it's value.
=cut

has prefix =>(
is => 'ro',
isa => Str,
);
Expand All @@ -109,11 +121,14 @@ has perm => (
sub _build_perm {
my ($self) = @_;
my $perm;
for my $key ( keys %{$self} ) {
unless ( $key =~ /(?:name|etcd|cb|endpoint)$/ ) {
if ($self->{prefix}) {
$self->{range_end} = encode_base64( "\0", '' );
}
for my $key ( keys %{$self} ) {
unless ( $key =~ /(?:prefix|name|etcd|cb|endpoint)$/ ) {
$perm->{$key} = $self->{$key};
}
}
}
return $perm;
}

Expand Down
18 changes: 16 additions & 2 deletions t/05-user.t
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ if ( $ENV{ETCD_TEST_HOST} and $ENV{ETCD_TEST_PORT} ) {
$config->{key_file} = $ENV{ETCD_CLIENT_KEY_FILE} || "$dir/t/tls/client-key.pem";
$config->{cert_file} = $ENV{ETCD_CLIENT_CERT_FILE} || "$dir/t/tls/client.pem";
$config->{ssl} = 1;
plan tests => 19;
plan tests => 21;
}
else {
plan skip_all =>
Expand Down Expand Up @@ -69,7 +69,7 @@ lives_ok(
sub {
$role =
$etcd->role_perm(
{ name => 'myrole', key => 'foo', permType => 'READ' } )->grant;
{ name => 'myrole', key => 'foo', permType => 'READ', range_end => "\0" } )->grant;
},
"role_perm grant"
);
Expand All @@ -78,6 +78,20 @@ lives_ok(

cmp_ok( $role->is_success, '==', 1, "role_perm grant success" );

lives_ok(
sub {
$role =
$etcd->role_perm(
{ name => 'myrole', key => 'bar', permType => 'READ', prefix => 1 } )->grant;
},
"role_perm grant with prefix"
);

#print STDERR Dumper($role);

cmp_ok( $role->is_success, '==', 1, "role_perm grant with prefix success" );


# grant role
lives_ok(
sub {
Expand Down

0 comments on commit 2e4e5bc

Please sign in to comment.