Client/Auth.pm

filter: Statementstmt 81.5% 62 / 76 Branchbran 42.8% 12 / 28 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 8 / 8 Podpod 100% 3 / 3 totaltotal 71.6% 86 / 120 CC 18 SCAR 21.6
CC
18
Cov
70%
get_openrc
21.2
login
7.5
services_from_catalog
5.0
CRAP 8.7
« Client/API/Convert.pm ↑ Summary Client/Request.pm »
1 package Net::OpenStack::Client::Auth;
2 7 $Net::OpenStack::Client::Auth::VERSION = '0.1.4';
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 7 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 7 Readonly my $OPENRC_REQUIRED => [qw(username password auth_url)];
9  
10 7 Readonly my %OPENRC_DEFAULT => {
11 identity_api_version => 3,
12 project_domain_name => 'Default',
13 user_domain_name => 'Default',
14 };
15  
16 7 Readonly my $DEFAULT_ENDPOINT_INTERFACE_PREFERENCE => [qw(admin internal public)];
17  
18 # read openrc file and extract the variables in hashref
19 sub _parse_openrc
20 4 ▶{
Subroutine _parse_openrc: called CC=4 SCAR=14.1
21 4 my ($self, $fn) = @_;
22  
23 4 my $res;
24 4 ▶ if (open(my $fh, $fn)) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, $fn) { } 4 0 4
25 4 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 32 4 36
26 32 chomp;
27 32 ▶ if (m/^\s*(?:export\s+)(\w+)\s*=\s*(['"]?)(.+)\2\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*(?:export\s+)(\w+)\s*=\s*(['"]?)(.+)\2\s*$/) 32 0 32
28 32 $res->{$1} = $3;
29 }
30 }
31 4 close($fh);
32 4 $self->debug("Parsed openrc file $fn: found variables ".join(',', sort keys %$res));
33 } else {
34 0 $self->error("Failed to openrc file $fn: $!");
35 }
36 4 return $res;
37 }
38  
39  
40 =head1 methods
41  
42 =over
43  
44 =item get_openrc
45  
46 Given variable name, get OS_<uppercase variable name> from hashref C<data>.
47  
48 Use default from OPENRC_DEFAULT, if none exists.
49 If none exists, and no default and in OPENRC_REQUIRED, report error.
50  
51 =cut
52  
53 sub get_openrc
54 21 ▶{
Subroutine get_openrc: called CC=6 SCAR=30.5
Pod documented
55 21 my ($self, $var, $data) = @_;
56  
57 21 my $full_var = "OS_".uc($var);
58 21 ▶ if (exists($data->{$full_var})) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $data->{$full_var}) { } 21 0 21
elsif (exists $OPENRC_DEFAULT{$full_var}) { } 0 0 0
59 21 return $data->{$full_var};
60 } elsif (exists($OPENRC_DEFAULT{$full_var})) {
61 0 return $OPENRC_DEFAULT{$full_var};
62 } else {
63 0 ▶ my $req = (grep {$_ eq $var} @$OPENRC_REQUIRED) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
grep({$_ eq $var;} @$OPENRC_REQUIRED) ? : 0 0 0
64 0 ▶ my $method = $req ? 'error' : 'debug';
Branch: 1 branch
BranchTrueFalseTotal
$req ? : 0 0 0
65 0 ▶ $self->$method("openrc ".($req ? 'required ' : '')."variable $var ($full_var) not found");
Branch: 1 branch
BranchTrueFalseTotal
$req ? : 0 0 0
66 }
67  
68 0 return;
69 }
70  
71  
72 =item login
73  
74 Login and obtain token for further authentication.
75  
76 Options:
77  
78 =over
79  
80 =item openrc: openrc file to parse to extract the login details.
81  
82  
83 =back
84  
85 =cut
86  
87 sub login
88 3 ▶{
Subroutine login: called CC=6 SCAR=20.1
Pod documented
89 3 my ($self, %opts) = @_;
90  
91 3 ▶ if ($opts{openrc}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'openrc'}) { } 3 0 3
92 my $openrc = $self->_parse_openrc($opts{openrc})
93 3 ▶ or return;
Branch: 1 branch
BranchTrueFalseTotal
my $openrc = $self->_parse_openrc($opts{"openrc"}) or return 3 0 3
94  
95 3 ▶ my $os = sub {return $self->get_openrc(shift, $openrc)};
Subroutine __ANON__: called CC=1 SCAR=0.0
96  
97 3 my $version = version->new('v'.&$os('identity_api_version'));
98 3 $self->{versions}->{identity} = $version;
99 3 ▶ if ($self->{versions}->{identity} == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'versions'}{'identity'} == 3) { } 3 0 3
100 3 $self->{services}->{identity} = &$os('auth_url');
101 3 ▶ my $opts = {
Condition33%1/3
&$os('user_domain_name') || &$os('project_domain_name')
l!l&&r!l&&!r
300
MC/DC0%0/2
&$os('user_domain_name') || &$os('project_domain_name')
&$os('user_domain_name') &$os('project_domain_name')
Truth table33%1/3
A || B
A= &$os('user_domain_name')B= &$os('project_domain_name')
ABresult
1X1
011
000
102 methods => ['password'],
103 user_name => &$os('username'),
104 password => &$os('password'),
105 user_domain_name => &$os('user_domain_name') || &$os('project_domain_name'),
106 project_domain_name => &$os('project_domain_name'),
107 project_name => &$os('project_name'),
108 };
109  
110 3 my $resp = $self->api_identity_tokens(map {$_ => $opts->{$_}} grep {defined($opts->{$_})} keys %$opts);
111 3 ▶ if ($resp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resp) { } 3 0 3
112 # token in result attr
113 3 $self->{token} = $resp->result;
114 3 $self->verbose("login succesful, obtained a token");
115 # parse the catalog
116 3 $self->services_from_catalog($resp->{data}->{token}->{catalog});
117 } else {
118 0 $self->error("login: failed to get token $resp->{error}");
119 0 return;
120 }
121 } else {
122 0 $self->error("login: only identity v3 supported for now");
123 0 return;
124 }
125 } else {
126 0 $self->error("login: only openrc supported for now");
127 0 return;
128 }
129  
130 3 return 1;
131 }
132  
133 =item services_from_catalog
134  
135 Parse the catalog arrayref, and build up the services attribute
136  
137 =cut
138  
139 sub services_from_catalog
140 3 ▶{
Subroutine services_from_catalog: called CC=5 SCAR=16.1
Pod documented
141 3 my ($self, $catalog) = @_;
142  
143 # TODO: allow to change this
144 3 my @pref_intfs = (@$DEFAULT_ENDPOINT_INTERFACE_PREFERENCE);
145  
146 3 foreach my $service (@$catalog) {
147 2 my $type = $service->{type};
148 2 my $endpoint;
149 2 foreach my $intf (@pref_intfs) {
150 3 my @epts = grep {$_->{interface} eq $intf} @{$service->{endpoints}};
151 3 ▶ if (@epts) {
Branch: 1 branch
BranchTrueFalseTotal
if (@epts) 2 1 3
152 2 $endpoint = $epts[0]->{url};
153 2 last;
154 }
155 }
156 2 my $msg = "for service $type from catalog";
157 2 ▶ if ($endpoint) {
Branch: 1 branch
BranchTrueFalseTotal
if ($endpoint) { } 2 0 2
158 2 $self->{services}->{$type} = $endpoint;
159 2 $self->verbose("Added endpoint $endpoint $msg");
160 } else {
161 0 $self->error("No endpoint $msg using preferred interfaces ".join(",", @pref_intfs));
162 }
163 }
164 }
165  
166 =pod
167  
168 =back
169  
170 =cut
171  
172 7 1;
« Client/API/Convert.pm ↑ Summary Client/Request.pm »