Etcd/Auth.pm

filter: Statementstmt 49.2% 33 / 67 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 64.2% 9 / 14 Podpod 100% 3 / 3 totaltotal 40.5% 45 / 111 SCAR 35.4
CC
10
Cov
38%
authenticate
42.0
disable
12.0
_build_name
6.0
CRAP 34.4
« Etcd/KV.pm ↑ Summary Etcd/Maintenance.pm »
1 9 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 package Net::Etcd::Auth;
3  
4 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =encoding utf8
8  
9 =cut
10  
11 9 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use Types::Standard qw(Str);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 ▶use Net::Etcd::Auth::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 9 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 9 with 'Net::Etcd::Role::Actions';
18 9 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20  
21 =head1 NAME
22  
23 Net::Etcd::Auth
24  
25 =cut
26  
27 9 our $VERSION = '0.021';
28  
29 =head1 DESCRIPTION
30  
31 Authentication
32  
33 =cut
34  
35 =head1 SYNOPSIS
36  
37 Steps to enable authentication.
38  
39 # add user
40 $etcd->user({ name => 'root', password => 'toor' })->add;
41  
42 # add role
43 $etcd->role({ name => 'root' })->add;
44  
45 # grant role
46 $etcd->user_role({ user => 'root', role => 'root' })->grant;
47  
48 # enable auth
49 $etcd->auth()->enable;
50  
51 =cut
52  
53 =head2 endpoint
54  
55 =cut
56  
57 9 has endpoint => (
58 is => 'ro',
59 isa => Str,
60 );
61  
62 =head2 name
63  
64 Defaults to $etcd->name
65  
66 =cut
67  
68 9 has name => (
69 is => 'lazy',
70 );
71  
72 sub _build_name {
73 0 ▶ my ($self) = @_;
Subroutine _build_name: not called CC=2 SCAR=17.9
74 0 my $user = $self->etcd->name;
75 0 ▶ return $user if $user;
Branch: 1 branch
BranchTrueFalseTotal
if $user 0 0 0
76 0 return;
77 }
78  
79 =head2 password
80  
81 Defaults to $etcd->password
82  
83 =cut
84  
85 9 has password => (
86 is => 'lazy',
87 );
88  
89 sub _build_password {
90 0 ▶ my ($self) = @_;
Subroutine _build_password: not called CC=2 SCAR=17.9
91 0 my $pwd = $self->etcd->password;
92 0 ▶ return $pwd if $pwd;
Branch: 1 branch
BranchTrueFalseTotal
if $pwd 0 0 0
93 0 return;
94 }
95  
96 =head1 PUBLIC METHODS
97  
98 =head2 authenticate
99  
100 Returns token with valid authentication. The token must then be passed in the Authorization header.
101 The module handles passing, storing and refreshing of the token for you as long as you define a name
102 and password.
103  
104 # authenticate etcd as root
105 $etcd = Net::Etcd->new({ name => 'root', password => 'toor' });
106  
107 # switch to non root user
108 $etcd->auth({ name => $user, password => $pass })->authenticate;
109  
110 =cut
111  
112 sub authenticate {
113 0 ▶ my ( $self, $options ) = @_;
Subroutine authenticate: not called CC=6 SCAR=37.4
Pod documented
114 0 local $@;
115 0 $self->{endpoint} = '/auth/authenticate';
116 0 ▶ return unless ($self->password && $self->name);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->password and $self->name 0 0 0
Condition0%0/3
$self->password and $self->name
!ll&&!rl&&r
000
MC/DC0%0/2
$self->password and $self->name
$self->password $self->name
Truth table0%0/3
A and B
A= $self->passwordB= $self->name
ABresult
0X0
100
111
117 0 $self->request;
118 0 my $auth;
119 0 eval { $auth = from_json($self->{response}{content}) };
120 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
121 0 $self->{response}{content} = 'error: ' . $@;
122 0 return;
123 }
124 0 ▶ if ($auth && defined $auth->{token}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($auth and defined $auth->{"token"}) 0 0 0
Condition0%0/3
$auth and defined $auth->{"token"}
!ll&&!rl&&r
000
MC/DC0%0/2
$auth and defined $auth->{"token"}
$auth defined $auth->{"token"}
Truth table0%0/3
A and B
A= $authB= defined $auth->{"token"}
ABresult
0X0
100
111
125 0 $self->etcd->{auth_token} = $auth->{token};
126 }
127 0 return;
128 }
129  
130 =head2 enable
131  
132 Enable authentication.
133  
134 $etcd->auth()->enable;
135  
136 =cut
137  
138 sub enable {
139 0 ▶ my ( $self, $options ) = @_;
Subroutine enable: not called CC=1 SCAR=6.9
Pod documented
140 0 $self->{endpoint} = '/auth/enable';
141 0 $self->{json_args} = '{}';
142 0 $self->request;
143  
144 # init token
145 0 $self->etcd->auth()->authenticate;
146 0 return $self;
147 }
148  
149 =head2 disable
150  
151 Disable authentication, this requires a valid root password.
152  
153 $etcd->auth({ name => 'root', password => $pass })->disable;
154  
155 =cut
156  
157 sub disable {
158 0 ▶ my ( $self, $options ) = @_;
Subroutine disable: not called CC=3 SCAR=24.8
Pod documented
159 0 $self->{endpoint} = '/auth/disable';
160 0 ▶ confess 'root name and password required for ' . __PACKAGE__ . '->disable'
Branch: 1 branch
BranchTrueFalseTotal
unless $self->password and $self->name 0 0 0
Condition0%0/3
$self->password and $self->name
!ll&&!rl&&r
000
MC/DC0%0/2
$self->password and $self->name
$self->password $self->name
Truth table0%0/3
A and B
A= $self->passwordB= $self->name
ABresult
0X0
100
111
161 unless ($self->password && $self->name);
162 0 $self->request;
163 0 $self->etcd->clear_auth_token;
164 0 return $self;
165 }
166  
167 9 1;
« Etcd/KV.pm ↑ Summary Etcd/Maintenance.pm »