blib/lib/Google/Auth/ComputeEngine.pm

filter: Statementstmt 28.8% 30 / 104 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 61.5% 8 / 13 Podpod 0.0% 0 / 2 totaltotal 23.1% 38 / 164 CC 19 SCAR 36.5
CC
19
Cov
21%
__ANON__
90.0
on_gce
42.0
_build_project_id
20.0
CRAP 38.3
« blib/lib/Google/Auth/IAMCredentials.pm ↑ Summary blib/lib/Google/Auth/Credentials.pm »
1 # Copyright 2022 Google LLC and contributors
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14  
15 package Google::Auth::ComputeEngine;
16  
17 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 13 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 13 extends 'Google::Auth::Credentials';
23  
24 13 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 13 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 13 ▶use Google::Auth::Exceptions;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 13 ▶use Google::Auth::RetryHelper;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 13 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 has ua => (
31 is => 'ro',
32 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
33 0 my $ua = LWP::UserAgent->new(timeout => 2);
34 0 $ua->env_proxy;
35 0 return $ua;
36 },
37 13 );
38  
39 13 has '+project_id' => (
40 is => 'lazy',
41 builder => '_build_project_id',
42 );
43  
44 13 has scope => (
45 is => 'ro',
46 required => 0,
47 );
48  
49 13 our $_on_gce;
50  
51 0 ▶sub _build_project_id {
Subroutine _build_project_id: not called CC=4 SCAR=30.0
52 0 my ($self) = @_;
53  
54 0 ▶ return $ENV{GOOGLE_CLOUD_PROJECT} if $ENV{GOOGLE_CLOUD_PROJECT};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"GOOGLE_CLOUD_PROJECT"} 0 0 0
55  
56 0 ▶ my $host = $ENV{GCE_METADATA_HOST} // 'metadata.google.internal';
Condition0%0/2
$ENV{"GCE_METADATA_HOST"} // "metadata.google.internal"
l!l
00
MC/DC0%0/1
$ENV{"GCE_METADATA_HOST"} // "metadata.google.internal"
$ENV{"GCE_METADATA_HOST"}
Truth table0%0/2
A
A= $ENV{"GCE_METADATA_HOST"}
Aresult
11
00
57  
58 0 $self->ua->no_proxy($host, '169.254.169.254');
59  
60 0 my $url = "http://$host/computeMetadata/v1/project/project-id";
61  
62 0 $log->infof('Fetching project ID from GCE metadata server at %s', $url);
63  
64 0 my $future = $self->transport->request(
65 method => 'GET',
66 url => $url,
67 headers => {'Metadata-Flavor' => 'Google'},
68 );
69  
70 0 my ($body, $headers);
71 0 my $future_res = eval { ($body, $headers) = $future->get(); 1 };
72  
73 0 ▶ if ($future_res) {
Branch: 1 branch
BranchTrueFalseTotal
if ($future_res) { } 0 0 0
74 0 return $body;
75 } else {
76 0 my ($err_msg, $cat, $details) = $future->failure;
77 0 $log->warnf('Failed to fetch project ID from GCE metadata server: %s',
78 $err_msg);
79 0 return;
80 }
81 }
82  
83 0 ▶sub on_gce {
Subroutine on_gce: not called CC=6 SCAR=37.4
Pod undocumented
84 0 my ($class, %options) = @_;
85 0 ▶ return $_on_gce if defined $_on_gce;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Google::Auth::ComputeEngine::_on_gce 0 0 0
86  
87 0 ▶ if ($ENV{GCE_METADATA_HOST}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"GCE_METADATA_HOST"}) 0 0 0
88 0 $_on_gce = 1;
89 0 return $_on_gce;
90 }
91  
92 0 my $transport = $options{transport};
93 0 ▶ if (!$transport) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($transport) 0 0 0
94 0 require Google::Cloud::Transport::Adapter::LWP;
95 0 my $ua = LWP::UserAgent->new(timeout => 1);
96 0 $ua->no_proxy('metadata.google.internal', '169.254.169.254');
97 0 $transport = Google::Cloud::Transport::Adapter::LWP->new(user_agent => $ua);
98 }
99  
100 0 my $host = 'metadata.google.internal';
101 0 my $future = $transport->request(
102 method => 'GET',
103 url => 'http://' . $host . '/computeMetadata/v1/instance/',
104 headers => {'Metadata-Flavor' => 'Google'},
105 );
106  
107 0 my $future_res = eval { $future->get(); 1 };
108 0 ▶ if ($future_res) {
Branch: 1 branch
BranchTrueFalseTotal
if ($future_res) 0 0 0
109 0 $_on_gce = 1;
110 0 return $_on_gce;
111 }
112  
113 0 $host = '169.254.169.254';
114 0 $future = $transport->request(
115 method => 'GET',
116 url => 'http://' . $host . '/computeMetadata/v1/instance/',
117 headers => {'Metadata-Flavor' => 'Google'},
118 );
119  
120 0 $future_res = eval { $future->get(); 1 };
121 0 ▶ $_on_gce = $future_res ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$future_res ? : 0 0 0
122 0 return $_on_gce;
123 }
124  
125 0 ▶sub fetch_access_token {
Subroutine fetch_access_token: not called CC=3 SCAR=24.8
Pod undocumented
126 0 my ($self, %options) = @_;
127  
128 0 ▶ my $host = $ENV{GCE_METADATA_HOST} // 'metadata.google.internal';
Condition0%0/2
$ENV{"GCE_METADATA_HOST"} // "metadata.google.internal"
l!l
00
MC/DC0%0/1
$ENV{"GCE_METADATA_HOST"} // "metadata.google.internal"
$ENV{"GCE_METADATA_HOST"}
Truth table0%0/2
A
A= $ENV{"GCE_METADATA_HOST"}
Aresult
11
00
129  
130 # Ensure no proxy for metadata server
131 0 $self->ua->no_proxy($host, '169.254.169.254');
132  
133 0 my $url =
134 "http://$host/computeMetadata/v1/instance/service-accounts/default/token";
135  
136 0 $log->infof("Fetching access token from GCE metadata server at $url");
137  
138 my $response_body = Google::Auth::RetryHelper->execute_with_retry(
139 0 ▶ sub {
Subroutine __ANON__: not called CC=9 SCAR=45.0
140 0 my $future = $self->transport->request(
141 method => 'GET',
142 url => $url,
143 headers => {'Metadata-Flavor' => 'Google'},
144 );
145  
146 0 my ($body, $headers);
147 0 my $future_res = eval { ($body, $headers) = $future->get(); 1 };
148  
149 0 ▶ if (!$future_res) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($future_res) 0 0 0
150 0 my ($err_msg, $cat, $details) = $future->failure;
151 0 my $code = 0;
152 0 my $error_body = '';
153 0 ▶ if (ref($details) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $details eq 'HASH') { } 0 0 0
elsif (eval { do { $details->can('code') } }) { } 0 0 0
154 0 ▶ $code = $details->{code} // 0;
Condition0%0/2
$details->{"code"} // 0
l!l
00
MC/DC0%0/1
$details->{"code"} // 0
$details->{"code"}
Truth table0%0/2
A
A= $details->{"code"}
Aresult
11
00
155 0 ▶ $error_body = $details->{body} // '';
Condition0%0/2
$details->{"body"} // ""
l!l
00
MC/DC0%0/1
$details->{"body"} // ""
$details->{"body"}
Truth table0%0/2
A
A= $details->{"body"}
Aresult
11
00
156 0 } elsif (eval { $details->can('code') }) {
157 0 ▶ $code = $details->code // 0;
Condition0%0/2
$details->code // 0
l!l
00
MC/DC0%0/1
$details->code // 0
$details->code
Truth table0%0/2
A
A= $details->code
Aresult
11
00
158 0 ▶ $error_body = $details->decoded_content // $details->content // '';
Condition0%0/5
($details->decoded_content // $details->content) // ""
l!l
00
$details->decoded_content // $details->content
l!l&&r!l&&!r
000
MC/DC0%0/2
($details->decoded_content // $details->content) // ""
$details->decoded_content $details->content
Truth table0%0/3
A // B
A= $details->decoded_contentB= $details->content
ABresult
1X1
011
000
159 }
160 Google::Auth::Error->throw(
161 0 'HTTP request failed with status ' . $code . ': ' . $error_body);
162 }
163 0 return $body;
164 },
165 0 %options
166 );
167  
168 0 my $res_data = decode_json($response_body);
169 0 my $token = $res_data->{access_token};
170 0 ▶ my $expires = $res_data->{expires_in} // 3600;
Condition0%0/2
$res_data->{"expires_in"} // 3600
l!l
00
MC/DC0%0/1
$res_data->{"expires_in"} // 3600
$res_data->{"expires_in"}
Truth table0%0/2
A
A= $res_data->{"expires_in"}
Aresult
11
00
171  
172 0 $self->access_token($token);
173 0 $self->expires_at(time() + $expires);
174  
175 0 return $token;
176 }
177  
178 13 1;
179  
« blib/lib/Google/Auth/IAMCredentials.pm ↑ Summary blib/lib/Google/Auth/Credentials.pm »