Skip to content

Commit

Permalink
Add token based authentication via grpc-gateway with tests. Fixes GH #18
Browse files Browse the repository at this point in the history
  • Loading branch information
hexfusion committed Jun 17, 2017
1 parent 82685c0 commit 555849e
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 27 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
sudo: required
env:
global:
- ETCD_VER=v3.2.0
- ETCD_VER=v3.2.0_plus_git
- ETCDCTL_API=3
- ETCD_TEST_HOST=127.0.0.1
- ETCD_TEST_PORT=2379
Expand All @@ -23,7 +23,7 @@ matrix:
env: COVERAGE=1 # enables coverage+coveralls reporting
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
- wget https://github.com/hexfusion/etcd/releases/download/${ETCD_VER}/etcd-${ETCD_VER}-linux-amd64.tar.gz -O /tmp/etcd-${ETCD_VER}-linux-amd64.tar.gz
- mkdir -p /tmp/test-etcd
- tar xzvf /tmp/etcd-${ETCD_VER}-linux-amd64.tar.gz -C /tmp/test-etcd --strip-components=1
- /tmp/test-etcd/etcd&
Expand Down
29 changes: 15 additions & 14 deletions lib/Net/Etcd/Auth.pm
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use warnings;
=cut

use Moo;
use JSON;
use Carp;
use Types::Standard qw(Str Int Bool HashRef ArrayRef);
use Net::Etcd::Auth::Role;
Expand Down Expand Up @@ -70,9 +71,9 @@ has name => (

sub _build_name {
my ($self) = @_;
my $user = $self->etcd->name;
return $user if $user;
return;
my $user = $self->etcd->name;
return $user if $user;
return;
}

=head2 password
Expand All @@ -87,18 +88,18 @@ has password => (

sub _build_password {
my ($self) = @_;
my $pwd = $self->etcd->password;
return $pwd if $pwd;
return;
my $pwd = $self->etcd->password;
return $pwd if $pwd;
return;
}

=head1 PUBLIC METHODS
=head2 authenticate
Enable authentication, this requires name and password.
Returns token with valid authentication.
$etcd->auth({ name => $user, password => $pass })->authenticate;
my $token = $etcd->auth({ name => $user, password => $pass })->authenticate;
=cut

Expand All @@ -107,12 +108,12 @@ sub authenticate {
$self->{endpoint} = '/auth/authenticate';
$self->{headers}{'Content-Type'} = 'application/json';
return unless ($self->password && $self->name);
# $self->password;
# $self->name;
#print STDERR Dumper($self);
$self->request;

return $self;
$self->request;
my $auth = from_json($self->{response}{content});
if ($auth && defined $auth->{token}) {
return $auth->{token};
}
return;
}

=head2 enable
Expand Down
2 changes: 1 addition & 1 deletion lib/Net/Etcd/Auth/Role.pm
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ Delete role

sub delete {
my ($self) = @_;
confess 'name required for ' . __PACKAGE__ . '->delete'
confess 'role required for ' . __PACKAGE__ . '->delete'
unless $self->{role};
$self->{endpoint} = '/auth/role/delete';
$self->request;
Expand Down
11 changes: 2 additions & 9 deletions lib/Net/Etcd/Role/Actions.pm
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,9 @@ has headers => ( is => 'lazy' );
sub _build_headers {
my ($self) = @_;
my $headers;
my $auth = $self->etcd->auth->authenticate;
# print STDERR Dumper($auth->authenticate);
my $auth_token;
$auth_token = $auth->token if $auth;

print STDERR "Auth token " . $auth_token if $auth_token;

my $token = $self->etcd->auth->authenticate;
$headers->{'Content-Type'} = 'application/json';
$headers->{'Authorization'} = $auth_token if $auth_token;
$headers->{'Authorization'} = $token if $token;
return $headers;
}
=head2 hold
Expand Down Expand Up @@ -128,7 +122,6 @@ sub _build_request {
my $cv = $self->cv ? $self->cv : AE::cv;
$cv->begin;

print STDERR Dumper($self->headers);
http_request(
'POST',
$self->etcd->api_path . $self->{endpoint},
Expand Down
28 changes: 27 additions & 1 deletion t/auth.t
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ if ( $ENV{ETCD_TEST_HOST} and $ENV{ETCD_TEST_PORT}) {
$host = $ENV{ETCD_TEST_HOST};
$port = $ENV{ETCD_TEST_PORT};

plan tests => 6;
plan tests => 13;
}
else {
plan skip_all => "Please set environment variable ETCD_TEST_HOST and ETCD_TEST_PORT.";
Expand Down Expand Up @@ -77,4 +77,30 @@ lives_ok(
"disable auth"
);


cmp_ok( $role->{response}{success}, '==', 1, "revoke role success" );

# cleanup role
lives_ok( sub { $role = $etcd->role( { role => 'root' } )->delete;
},
"delete role" );

#print STDERR Dumper($role);

cmp_ok( $role->{response}{success}, '==', 1, "delete role success" );


# remove user
lives_ok(
sub {
$user =
$etcd->user( { name => 'root' })->delete;
},
"delete user"
);

#print STDERR Dumper($user);

cmp_ok( $user->{response}{success}, '==', 1, "delete user success" );

1;

0 comments on commit 555849e

Please sign in to comment.