Etcd/Auth.pm

filter: Statementstmt 56.2% 36 / 64 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 66.6% 10 / 15 Podpod 100% 3 / 3 totaltotal 45.7% 49 / 107 SCAR 31.7
CC
9
Cov
43%
authenticate
30.0
disable
12.0
_build_name
6.0
CRAP 23.7
« 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 Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 ▶use Types::Standard qw(Str Int Bool HashRef ArrayRef);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 9 ▶use Net::Etcd::Auth::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 9 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 9 with 'Net::Etcd::Role::Actions';
19  
20 9 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22  
23 =head1 NAME
24  
25 Net::Etcd::Auth
26  
27 =cut
28  
29 9 our $VERSION = '0.020';
30  
31 =head1 DESCRIPTION
32  
33 Authentication
34  
35 =cut
36  
37 =head1 SYNOPSIS
38  
39 # enable auth
40 $etcd->user_add
41  
42 # add user
43 $etcd->user_add( { name => 'samba', password =>'P@$$' });
44  
45 # add role
46 $etcd->role( { name => 'myrole' })->add;
47  
48 # grant role
49 $etcd->user_role( { user => 'samba', role => 'myrole' })->grant;
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.
101  
102 my $token = $etcd->auth({ name => $user, password => $pass })->authenticate;
103  
104 =cut
105  
106 sub authenticate {
107 0 ▶ my ( $self, $options ) = @_;
Subroutine authenticate: not called CC=5 SCAR=34.0
Pod documented
108 0 $self->{endpoint} = '/auth/authenticate';
109 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
110 0 $self->request;
111 0 my $auth = from_json($self->{response}{content});
112 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
113 0 $self->etcd->{auth_token} = $auth->{token};
114 }
115 0 return;
116 }
117  
118 =head2 enable
119  
120 Enable authentication.
121  
122 $etcd->auth()->enable;
123  
124 =cut
125  
126 sub enable {
127 0 ▶ my ( $self, $options ) = @_;
Subroutine enable: not called CC=1 SCAR=6.9
Pod documented
128 0 $self->{endpoint} = '/auth/enable';
129 0 $self->{json_args} = '{}';
130 0 $self->request;
131  
132 # init token
133 0 $self->etcd->auth()->authenticate;
134 0 return $self;
135 }
136  
137 =head2 disable
138  
139 Disable authentication, this requires a valid root password.
140  
141 $etcd->auth({ name => 'root', $password => $pass })->disable;
142  
143 =cut
144  
145 sub disable {
146 0 ▶ my ( $self, $options ) = @_;
Subroutine disable: not called CC=3 SCAR=24.8
Pod documented
147 0 $self->{endpoint} = '/auth/disable';
148 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
149 unless ($self->password && $self->name);
150 0 $self->request;
151 0 $self->etcd->clear_auth_token;
152 0 return $self;
153 }
154  
155 9 1;
« Etcd/KV.pm ↑ Summary Etcd/Maintenance.pm »