blib/lib/Google/Auth/ExternalAccountCredentials/Pluggable.pm

filter: Statementstmt 78.2% 90 / 115 Branchbran 63.8% 23 / 36 Conditioncond 62.0% 18 / 29 MC/DCmcdc 37.5% 6 / 16 Subroutinesub 91.6% 11 / 12 Podpod 0.0% 0 / 1 totaltotal 70.8% 148 / 209 CC 32 SCAR 39.1
CC
32
Cov
73%
retrieve_subject_token
67.0
__ANON__
1.1
BEGIN
1.0
CRAP 50.1
« blib/script/gcloud-auth.pl ↑ Summary blib/lib/Google/Auth/IAMCredentials.pm »
1 # Copyright 2026 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::ExternalAccountCredentials::Pluggable;
16  
17 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 extends 'Google::Auth::ExternalAccountCredentials';
22  
23 2 ▶use JSON::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use Google::Auth::Exceptions;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶use Capture::Tiny qw(capture);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 ▶use Text::ParseWords qw(shellwords);
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 7 ▶sub retrieve_subject_token {
Subroutine retrieve_subject_token: called CC=32 SCAR=42.0
Pod undocumented
31 7 my ($self) = @_;
32  
33 7 ▶ if (($ENV{GOOGLE_EXTERNAL_ACCOUNT_ALLOW_EXECUTABLES} // '0') ne '1') {
Branch: 1 branch
BranchTrueFalseTotal
if (($ENV{"GOOGLE_EXTERNAL_ACCOUNT_ALLOW_EXECUTABLES"} // 0) ne 1) 1 6 7
Condition50%1/2
$ENV{"GOOGLE_EXTERNAL_ACCOUNT_ALLOW_EXECUTABLES"} // 0
l!l
70
MC/DC0%0/1
$ENV{"GOOGLE_EXTERNAL_ACCOUNT_ALLOW_EXECUTABLES"} // 0
$ENV{"GOOGLE_EXTERNAL_ACCOUNT_ALLOW_EXECUTABLES"}
Truth table50%1/2
A
A= $ENV{"GOOGLE_EXTERNAL_ACCOUNT_ALLOW_EXECUTABLES"}
Aresult
11
00
34 1 $log->errorf(
35 'Pluggable credentials are not enabled. Set GOOGLE_EXTERNAL_ACCOUNT_ALLOW_EXECUTABLES=1 to enable.'
36 );
37 1 Google::Auth::Error->throw(
38 'Pluggable credentials are not enabled. Set GOOGLE_EXTERNAL_ACCOUNT_ALLOW_EXECUTABLES=1 to enable.'
39 );
40 }
41  
42 6 my $source = $self->credential_source;
43 6 my $exec = $source->{executable};
44 6 ▶ if (!defined $exec) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $exec) 0 6 6
45 0 $log->errorf(
46 'Missing executable configuration in Pluggable credential_source');
47 0 Google::Auth::Error->throw(
48 'Missing executable configuration in credential_source');
49 }
50  
51 6 my $command = $exec->{command};
52 6 ▶ if (!defined $command) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $command) 0 6 6
53 0 $log->errorf('Missing command in Pluggable executable configuration');
54 0 Google::Auth::Error->throw('Missing command in executable configuration');
55 }
56  
57 6 ▶ my $env_vars = $exec->{environment_variables} // {};
Condition100%2/2
$exec->{"environment_variables"} // {}
l!l
15
MC/DC100%1/1
$exec->{"environment_variables"} // {}
$exec->{"environment_variables"}
Truth table100%2/2
A
A= $exec->{"environment_variables"}
Aresult
11
00
58 6 local %ENV = %ENV;
59 6 ▶ while (my ($k, $v) = each %$env_vars) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$env_vars) 1 6 7
60 1 $ENV{$k} = $v;
61 }
62  
63 # Parse command into words, respecting quotes and escapes
64 6 my @words = shellwords($command);
65 6 my $executable = $words[0];
66  
67 6 ▶ if (!defined $executable) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $executable) 0 6 6
68 0 $log->errorf(
69 'Invalid command format in Pluggable executable configuration');
70 0 Google::Auth::Error->throw(
71 'Invalid command format in executable configuration');
72 }
73  
74 6 my $resolved_executable;
75 6 ▶ if (-f $executable && -x _ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $executable and -x _) { } 5 1 6
Condition66%2/3
-f $executable and -x _
!ll&&!rl&&r
105
MC/DC50%1/2
-f $executable and -x _
-f $executable -x _
Truth table66%2/3
A and B
A= -f $executableB= -x _
ABresult
0X0
100
111
76 5 $resolved_executable = $executable;
77 } else {
78 # Search PATH
79 1 ▶ my $path_sep = $Config{path_sep} // ':';
Condition50%1/2
$Google::Auth::ExternalAccountCredentials::Pluggable::Config{"path_sep"} // ":"
l!l
10
MC/DC0%0/1
$Google::Auth::ExternalAccountCredentials::Pluggable::Config{"path_sep"} // ":"
$Google::Auth::ExternalAccountCredentials::Pluggable::Config{"path_sep"}
Truth table50%1/2
A
A= $Google::Auth::ExternalAccountCredentials::Pluggable::Config{"path_sep"}
Aresult
11
00
80 1 ▶ for my $dir (split /\Q$path_sep\E/, ($ENV{PATH} // '')) {
Condition50%1/2
$ENV{"PATH"} // ""
l!l
10
MC/DC0%0/1
$ENV{"PATH"} // ""
$ENV{"PATH"}
Truth table50%1/2
A
A= $ENV{"PATH"}
Aresult
11
00
81 10 my $path = "$dir/$executable";
82 10 ▶ if (-f $path && -x _ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $path and -x _) 0 10 10
Condition33%1/3
-f $path and -x _
!ll&&!rl&&r
1000
MC/DC0%0/2
-f $path and -x _
-f $path -x _
Truth table33%1/3
A and B
A= -f $pathB= -x _
ABresult
0X0
100
111
83 0 $resolved_executable = $path;
84 0 last;
85 }
86 }
87 }
88  
89 6 ▶ if (!defined $resolved_executable) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $resolved_executable) 1 5 6
90 1 $log->errorf('Executable not found or not executable: %s', $executable);
91 1 Google::Auth::Error->throw(
92 "Executable not found or not executable: $executable");
93 }
94  
95 # `@words` are automatically de-tainted by shellwords.
96 # We use indirect object syntax with system to avoid invoking the shell,
97 # preventing shell injection even if arguments contain metacharacters.
98  
99 5 $log->infof('Executing Pluggable credential command: %s', $command);
100  
101 5 ▶ my $timeout = $exec->{timeout_millis} ? $exec->{timeout_millis} / 1000 : 30;
Branch: 1 branch
BranchTrueFalseTotal
$exec->{'timeout_millis'} ? : 0 5 5
102 5 my ($stdout, $stderr, $exit);
103  
104 5 eval {
105 0 ▶ local $SIG{ALRM} = sub { die "Timeout\n" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
106 5 alarm($timeout);
107 5 ▶ ($stdout, $stderr, $exit) = capture {
Subroutine __ANON__: called CC=1 SCAR=0.0
108 5 system {$resolved_executable} @words;
109 5 };
110 5 alarm(0);
111 };
112 5 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 5 5
113 0 alarm(0);
114 0 ▶ if ($@ eq "Timeout\n") {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ eq "Timeout\n") { } 0 0 0
115 0 $log->errorf('Pluggable command timed out after %d seconds', $timeout);
116 0 Google::Auth::Error->throw(
117 "Pluggable command timed out after $timeout seconds");
118 } else {
119 0 die $@; # Re-throw other errors
120 }
121 }
122  
123 5 ▶ if ($exit != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit != 0) 0 5 5
124 0 my $exit_code = $exit >> 8;
125 0 $log->errorf('Pluggable command failed with exit code %d: %s',
126 $exit_code, $stderr);
127 0 Google::Auth::Error->throw(
128 'Pluggable credential command failed with exit code ' .
129 $exit_code . ': ' .
130 $stderr);
131 }
132  
133 5 ▶ my $format = $source->{format} // {};
Condition100%2/2
$source->{"format"} // {}
l!l
23
MC/DC100%1/1
$source->{"format"} // {}
$source->{"format"}
Truth table100%2/2
A
A= $source->{"format"}
Aresult
11
00
134 5 ▶ my $format_type = $format->{type} // 'json';
Condition100%2/2
$format->{"type"} // "json"
l!l
23
MC/DC100%1/1
$format->{"type"} // "json"
$format->{"type"}
Truth table100%2/2
A
A= $format->{"type"}
Aresult
11
00
135  
136 5 my $token;
137 5 ▶ if ($format_type eq 'json') {
Branch: 2 branches
BranchTrueFalseTotal
if ($format_type eq 'json') { } 4 1 5
elsif ($format_type eq 'text') { } 1 0 1
138 4 $log->tracef('Parsing JSON output from Pluggable command...');
139 4 my $clean_stdout = $stdout;
140 4 $clean_stdout =~ s/^\s*['"]?|['"]?\s*$//g;
141 my $data =
142 4 ▶ eval { decode_json($stdout) } || eval { decode_json($clean_stdout) };
Condition66%2/3
eval { do { decode_json($stdout) } } || eval { do { decode_json($clean_stdout) } }
l!l&&r!l&&!r
301
MC/DC50%1/2
eval { do { decode_json($stdout) } } || eval { do { decode_json($clean_stdout) } }
eval { do { decode_json($stdout) } } eval { do { decode_json($clean_stdout) } }
Truth table66%2/3
A || B
A= eval { do { decode_json($stdout) } }B= eval { do { decode_json($clean_stdout) } }
ABresult
1X1
011
000
143 4 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 3 4
144 1 $log->errorf('Pluggable JSON parsing failed: %s', $@);
145 1 Google::Auth::Error->throw(
146 'Failed to parse JSON from pluggable command output: ' . $@);
147 }
148  
149 # Schema Validation
150 3 ▶ unless (defined $data->{version}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $data->{"version"} and defined $data->{"success"} and defined $data->{"expiration_time"}) 0 3 3
Condition33%2/6
defined $data->{"version"} and defined $data->{"success"} and defined $data->{"expiration_time"}
!ll&&!rl&&r
003
defined $data->{"version"} and defined $data->{"success"}
!ll&&!rl&&r
003
MC/DC0%0/3
defined $data->{"version"} and defined $data->{"success"} and defined $data->{"expiration_time"}
defined $data->{"version"} defined $data->{"success"} defined $data->{"expiration_time"}
Truth table25%1/4
A and B and C
A= defined $data->{"version"}B= defined $data->{"success"}C= defined $data->{"expiration_time"}
ABCresult
0XX0
10X0
1100
1111
151 && defined $data->{success}
152 && defined $data->{expiration_time})
153 {
154 0 $log->errorf(
155 'Pluggable command output missing required schema fields (version, success, expiration_time)'
156 );
157 0 Google::Auth::Error->throw(
158 'Pluggable command output missing required schema fields (version, success, expiration_time)'
159 );
160 }
161 3 ▶ unless ($data->{success}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data->{"success"}) 0 3 3
162 0 $log->errorf('Pluggable command reported failure (success=false)');
163 0 Google::Auth::Error->throw(
164 'Pluggable command reported failure (success=false)');
165 }
166 3 ▶ my $field = $format->{subject_token_field_name} // 'id_token';
Condition100%2/2
$format->{"subject_token_field_name"} // "id_token"
l!l
12
MC/DC100%1/1
$format->{"subject_token_field_name"} // "id_token"
$format->{"subject_token_field_name"}
Truth table100%2/2
A
A= $format->{"subject_token_field_name"}
Aresult
11
00
167 3 $token = $data->{$field};
168 } elsif ($format_type eq 'text') {
169 1 $log->tracef('Using raw text output from Pluggable command...');
170 1 $token = $stdout;
171 1 $token =~ s/\r?\n$//;
172 } else {
173 0 $log->errorf('Invalid Pluggable credential_source format: %s',
174 $format_type);
175 0 Google::Auth::Error->throw(
176 'Invalid credential_source format: ' . $format_type);
177 }
178  
179 4 ▶ if (!defined $token) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $token) 0 4 4
180 0 $log->errorf('Pluggable command returned empty subject token.');
181 0 Google::Auth::Error->throw(
182 'Pluggable credential command did not return a valid subject token');
183 }
184  
185 4 $log->tracef('Pluggable subject token retrieved successfully.');
186 4 return $token;
187 }
188  
189 2 1;
« blib/script/gcloud-auth.pl ↑ Summary blib/lib/Google/Auth/IAMCredentials.pm »