blib/lib/Google/Auth/ComputeEngine.pm

filter: Statementstmt 38.4% 30 / 78 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 61.5% 8 / 13 Podpod 0.0% 0 / 2 totaltotal 32.7% 38 / 116 CC 11 SCAR 25.3
CC
11
Cov
31%
on_gce
30.0
_build_project_id
20.0
fetch_access_token
12.0
CRAP 12.5
« blib/lib/Google/Auth/WebUserAuthorizer.pm ↑ Summary blib/lib/Google/Auth/UserAuthorizer.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 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 12 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 12 extends 'Google::Auth::Credentials';
23  
24 12 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 12 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 12 ▶use Google::Auth::Exceptions;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 12 ▶use Google::Auth::RetryHelper;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 12 ▶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 12 );
38  
39 12 has '+project_id' => (
40 is => 'lazy',
41 builder => '_build_project_id',
42 );
43  
44 12 has scope => (
45 is => 'ro',
46 required => 0,
47 );
48  
49 12 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 $ua = $self->ua;
57 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
58  
59 0 $ua->no_proxy($host, '169.254.169.254');
60  
61 0 my $url = "http://$host/computeMetadata/v1/project/project-id";
62  
63 0 $log->infof('Fetching project ID from GCE metadata server at %s', $url);
64  
65 0 my $response = $ua->get($url, 'Metadata-Flavor' => 'Google');
66 0 ▶ if ($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) { } 0 0 0
67 0 return $response->decoded_content;
68 } else {
69 0 $log->warnf('Failed to fetch project ID from GCE metadata server: %s',
70 $response->status_line);
71 0 return;
72 }
73 }
74  
75 0 ▶sub on_gce {
Subroutine on_gce: not called CC=5 SCAR=34.0
Pod undocumented
76 0 my ($class, %options) = @_;
77 0 ▶ return $_on_gce if defined $_on_gce;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Google::Auth::ComputeEngine::_on_gce 0 0 0
78  
79 0 ▶ if ($ENV{GCE_METADATA_HOST}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"GCE_METADATA_HOST"}) 0 0 0
80 0 $_on_gce = 1;
81 0 return $_on_gce;
82 }
83  
84 0 my $ua = LWP::UserAgent->new(timeout => 1);
85 0 $ua->no_proxy('metadata.google.internal', '169.254.169.254');
86  
87 0 my $host = 'metadata.google.internal';
88 0 my $response = $ua->get("http://$host/computeMetadata/v1/instance/",
89 'Metadata-Flavor' => 'Google');
90  
91 0 ▶ if ($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) 0 0 0
92 0 $_on_gce = 1;
93 0 return $_on_gce;
94 }
95  
96 0 $host = '169.254.169.254';
97 0 $response = $ua->get("http://$host/computeMetadata/v1/instance/",
98 'Metadata-Flavor' => 'Google');
99  
100 0 ▶ $_on_gce = $response->is_success ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$response->is_success ? : 0 0 0
101 0 return $_on_gce;
102 }
103  
104 0 ▶sub fetch_access_token {
Subroutine fetch_access_token: not called CC=3 SCAR=24.8
Pod undocumented
105 0 my ($self, %options) = @_;
106  
107 0 my $ua = $self->ua;
108 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
109  
110 # Ensure no proxy for metadata server
111 0 $ua->no_proxy($host, '169.254.169.254');
112  
113 0 my $url =
114 "http://$host/computeMetadata/v1/instance/service-accounts/default/token";
115  
116 0 $log->infof("Fetching access token from GCE metadata server at $url");
117  
118 my $response = Google::Auth::RetryHelper->execute_with_retry(
119 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
120 0 my $res = $ua->get($url, 'Metadata-Flavor' => 'Google');
121 0 ▶ if (!$res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->is_success) 0 0 0
122 0 Google::Auth::Error->throw('HTTP request failed with status ' .
123 $res->code . ': ' . $res->decoded_content);
124 }
125 0 return $res;
126 },
127 0 %options
128 );
129  
130 0 my $res_data = decode_json($response->decoded_content);
131 0 my $token = $res_data->{access_token};
132 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
133  
134 0 $self->access_token($token);
135 0 $self->expires_at(time() + $expires);
136  
137 0 return $token;
138 }
139  
140 12 1;
141  
« blib/lib/Google/Auth/WebUserAuthorizer.pm ↑ Summary blib/lib/Google/Auth/UserAuthorizer.pm »