| 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.022'; | |||||||||||||||||||||||||||
| 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 | 0 | ▶ | sub _build_name { | ||||||||||||||||||||||||||
|
Subroutine
_build_name: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 73 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 74 | 0 | my $user = $self->etcd->name; | |||||||||||||||||||||||||||
| 75 | 0 | ▶ | return $user if $user; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | 0 | ▶ | sub _build_password { | ||||||||||||||||||||||||||
|
Subroutine
_build_password: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 90 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 91 | 0 | my $pwd = $self->etcd->password; | |||||||||||||||||||||||||||
| 92 | 0 | ▶ | return $pwd if $pwd; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | 0 | ▶ | sub authenticate { | ||||||||||||||||||||||||||
|
Subroutine
authenticate: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 113 | 0 | my ( $self, $options ) = @_; | |||||||||||||||||||||||||||
| 114 | 0 | local $@; | |||||||||||||||||||||||||||
| 115 | 0 | $self->{endpoint} = '/auth/authenticate'; | |||||||||||||||||||||||||||
| 116 | 0 | ▶ | return unless ($self->password && $self->name); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->password and $self->name
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
| |||||||||||||||||||||||||||||
| 117 | 0 | $self->request; | |||||||||||||||||||||||||||
| 118 | 0 | my $auth; | |||||||||||||||||||||||||||
| 119 | 0 | eval { $auth = from_json($self->{response}{content}) }; | |||||||||||||||||||||||||||
| 120 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 0 | $self->{response}{content} = 'error: ' . $@; | |||||||||||||||||||||||||||
| 122 | 0 | return; | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | 0 | ▶ | if ($auth && defined $auth->{token}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$auth and defined $auth->{"token"}
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"}
| |||||||||||||||||||||||||||||
| 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 | 0 | ▶ | sub enable { | ||||||||||||||||||||||||||
|
Subroutine
enable: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 139 | 0 | my ( $self, $options ) = @_; | |||||||||||||||||||||||||||
| 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 | 0 | ▶ | sub disable { | ||||||||||||||||||||||||||
|
Subroutine
disable: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 158 | 0 | my ( $self, $options ) = @_; | |||||||||||||||||||||||||||
| 159 | 0 | $self->{endpoint} = '/auth/disable'; | |||||||||||||||||||||||||||
| 160 | 0 | ▶ | confess 'root name and password required for ' . __PACKAGE__ . '->disable' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->password and $self->name
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
| |||||||||||||||||||||||||||||
| 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; | |||||||||||||||||||||||||||