| 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::DefaultCredentials; | ||||||||||||||||||||||||||||
| 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 | 13 | ▶ | use JSON::PP; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 13 | ▶ | use File::Spec; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 13 | ▶ | use Google::Auth::EnvironmentVars; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 13 | ▶ | use Google::Auth::Exceptions; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | has environment => ( | ||||||||||||||||||||||||||||
| 27 | is => 'ro', | ||||||||||||||||||||||||||||
| 28 | 10 | ▶ | default => sub { Google::Auth::EnvironmentVars->new() }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 13 | ); | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 10 | ▶ | sub make_creds { | ||||||||||||||||||||||||||
|
Subroutine
make_creds: called CC=12 SCAR=26.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 32 | 10 | my ($self, %options) = @_; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | # Allow calling as class or instance method | ||||||||||||||||||||||||||||
| 35 | 10 | ▶ | $self = ref $self ? $self : $self->new(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 10 | my $json_key = $options{json_key}; | |||||||||||||||||||||||||||
| 38 | 10 | my $json_content = $options{json_content}; | |||||||||||||||||||||||||||
| 39 | 10 | my $json_path = $options{json_path}; | |||||||||||||||||||||||||||
| 40 | 10 | my $scopes = $options{scope}; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 10 | my $info; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 10 | eval { | |||||||||||||||||||||||||||
| 45 | 10 | ▶ | if ($json_key) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 46 | 6 | $info = $json_key; | |||||||||||||||||||||||||||
| 47 | } elsif ($json_content) { | ||||||||||||||||||||||||||||
| 48 | 0 | $info = decode_json($json_content); | |||||||||||||||||||||||||||
| 49 | } elsif ($json_path) { | ||||||||||||||||||||||||||||
| 50 | 4 | ▶ | if (-f $json_path) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 3 | my $content = $self->_read_file($json_path); | |||||||||||||||||||||||||||
| 52 | 3 | $info = decode_json($content); | |||||||||||||||||||||||||||
| 53 | } else { | ||||||||||||||||||||||||||||
| 54 | 1 | Google::Auth::DefaultCredentialsError->throw( | |||||||||||||||||||||||||||
| 55 | 'Credential file ' . $json_path . ' does not exist'); | ||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | }; | ||||||||||||||||||||||||||||
| 59 | 10 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 2 | Google::Auth::DefaultCredentialsError->throw( | |||||||||||||||||||||||||||
| 61 | 'Failed to parse credential JSON: ' . $@); | ||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 8 | ▶ | if ($info) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 8 | my $type = $info->{type}; | |||||||||||||||||||||||||||
| 66 | 8 | ▶ | if (!$type) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 1 | Google::Auth::DefaultCredentialsError->throw( | |||||||||||||||||||||||||||
| 68 | 'The json is missing the type field'); | ||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 7 | my $creds_class = $self->_determine_class_for_type($type); | |||||||||||||||||||||||||||
| 72 | 7 | ▶ | if ($creds_class->can('make_creds')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 2 | return $creds_class->make_creds( | |||||||||||||||||||||||||||
| 74 | json_key => $info, | ||||||||||||||||||||||||||||
| 75 | scope => $scopes, | ||||||||||||||||||||||||||||
| 76 | %options | ||||||||||||||||||||||||||||
| 77 | ); | ||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | 5 | return $creds_class->new( | |||||||||||||||||||||||||||
| 80 | json_key => $info, | ||||||||||||||||||||||||||||
| 81 | scope => $scopes, | ||||||||||||||||||||||||||||
| 82 | %options | ||||||||||||||||||||||||||||
| 83 | ); | ||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | # If no explicit json info was provided, fallback to checking environment variables | ||||||||||||||||||||||||||||
| 87 | 0 | my $env = $self->environment; | |||||||||||||||||||||||||||
| 88 | 0 | ▶ | if ($ENV{GOOGLE_PRIVATE_KEY} && $ENV{GOOGLE_CLIENT_EMAIL}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$ENV{"GOOGLE_PRIVATE_KEY"} and $ENV{"GOOGLE_CLIENT_EMAIL"}
MC/DC0%0/2
$ENV{"GOOGLE_PRIVATE_KEY"} and $ENV{"GOOGLE_CLIENT_EMAIL"}
$ENV{"GOOGLE_PRIVATE_KEY"}
$ENV{"GOOGLE_CLIENT_EMAIL"}
Truth table0%0/3
A and B
A= $ENV{"GOOGLE_PRIVATE_KEY"}B= $ENV{"GOOGLE_CLIENT_EMAIL"}
| |||||||||||||||||||||||||||||
| 89 | 0 | require Google::Auth::ServiceAccountCredentials; | |||||||||||||||||||||||||||
| 90 | return Google::Auth::ServiceAccountCredentials->new( | ||||||||||||||||||||||||||||
| 91 | private_key => $ENV{GOOGLE_PRIVATE_KEY}, | ||||||||||||||||||||||||||||
| 92 | client_email => $ENV{GOOGLE_CLIENT_EMAIL}, | ||||||||||||||||||||||||||||
| 93 | 0 | scope => $scopes, | |||||||||||||||||||||||||||
| 94 | %options | ||||||||||||||||||||||||||||
| 95 | ); | ||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 0 | return; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 4 | ▶ | sub from_env { | ||||||||||||||||||||||||||
|
Subroutine
from_env: called CC=4 SCAR=15.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 102 | 4 | my ($self, $scopes, %options) = @_; | |||||||||||||||||||||||||||
| 103 | 4 | ▶ | $self = ref $self ? $self : $self->new(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | my $credentials_path = | ||||||||||||||||||||||||||||
| 106 | 4 | ▶ | $ENV{GOOGLE_APPLICATION_CREDENTIALS} || $self->environment->CREDENTIALS; | ||||||||||||||||||||||||||
|
Condition33%1/3
$ENV{'GOOGLE_APPLICATION_CREDENTIALS'} || $self->environment->CREDENTIALS
MC/DC0%0/2
$ENV{'GOOGLE_APPLICATION_CREDENTIALS'} || $self->environment->CREDENTIALS
$ENV{'GOOGLE_APPLICATION_CREDENTIALS'}
$self->environment->CREDENTIALS
Truth table33%1/3
A || B
A= $ENV{'GOOGLE_APPLICATION_CREDENTIALS'}B= $self->environment->CREDENTIALS
| |||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 4 | ▶ | if ($credentials_path) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 4 | return $self->make_creds( | |||||||||||||||||||||||||||
| 110 | json_path => $credentials_path, | ||||||||||||||||||||||||||||
| 111 | scope => $scopes, | ||||||||||||||||||||||||||||
| 112 | %options | ||||||||||||||||||||||||||||
| 113 | ); | ||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | 0 | return; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 0 | ▶ | sub from_well_known_path { | ||||||||||||||||||||||||||
|
Subroutine
from_well_known_path: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 119 | 0 | my ($self, $scopes, %options) = @_; | |||||||||||||||||||||||||||
| 120 | 0 | ▶ | $self = ref $self ? $self : $self->new(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 0 | my $env = $self->environment; | |||||||||||||||||||||||||||
| 123 | 0 | my $home = $ENV{HOME}; | |||||||||||||||||||||||||||
| 124 | 0 | ▶ | if ($^O eq 'MSWin32') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 0 | $home = $ENV{APPDATA}; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 0 | ▶ | return unless $home; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 0 | my $well_known_file; | |||||||||||||||||||||||||||
| 131 | 0 | ▶ | if ($^O eq 'MSWin32') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 0 | $well_known_file = File::Spec->catfile($home, 'gcloud', | |||||||||||||||||||||||||||
| 133 | 'application_default_credentials.json'); | ||||||||||||||||||||||||||||
| 134 | } else { | ||||||||||||||||||||||||||||
| 135 | 0 | ▶ | my $config_dir = $env->CLOUD_SDK_CONFIG_DIR | ||||||||||||||||||||||||||
|
Condition0%0/3
$env->CLOUD_SDK_CONFIG_DIR || 'File::Spec'->catdir($home, '.config')
MC/DC0%0/2
$env->CLOUD_SDK_CONFIG_DIR || 'File::Spec'->catdir($home, '.config')
$env->CLOUD_SDK_CONFIG_DIR
'File::Spec'->catdir($home, '.config')
Truth table0%0/3
A || B
A= $env->CLOUD_SDK_CONFIG_DIRB= 'File::Spec'->catdir($home, '.config')
| |||||||||||||||||||||||||||||
| 136 | || File::Spec->catdir($home, '.config'); | ||||||||||||||||||||||||||||
| 137 | 0 | $well_known_file = File::Spec->catfile($config_dir, 'gcloud', | |||||||||||||||||||||||||||
| 138 | 'application_default_credentials.json'); | ||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 0 | ▶ | if (-f $well_known_file) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | 0 | return $self->make_creds( | |||||||||||||||||||||||||||
| 143 | json_path => $well_known_file, | ||||||||||||||||||||||||||||
| 144 | scope => $scopes, | ||||||||||||||||||||||||||||
| 145 | %options | ||||||||||||||||||||||||||||
| 146 | ); | ||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | 0 | return; | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 0 | ▶ | sub from_system_default_path { | ||||||||||||||||||||||||||
|
Subroutine
from_system_default_path: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 152 | 0 | my ($self, $scopes, %options) = @_; | |||||||||||||||||||||||||||
| 153 | 0 | ▶ | $self = ref $self ? $self : $self->new(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 0 | my $system_default_file; | |||||||||||||||||||||||||||
| 156 | 0 | ▶ | if ($^O eq 'MSWin32') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | 0 | ▶ | return unless $ENV{ProgramData}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | $system_default_file = | ||||||||||||||||||||||||||||
| 159 | 0 | File::Spec->catfile($ENV{ProgramData}, 'Google', 'Auth', | |||||||||||||||||||||||||||
| 160 | 'application_default_credentials.json'); | ||||||||||||||||||||||||||||
| 161 | } else { | ||||||||||||||||||||||||||||
| 162 | 0 | $system_default_file = | |||||||||||||||||||||||||||
| 163 | '/etc/google/auth/application_default_credentials.json'; | ||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 0 | ▶ | if (-f $system_default_file) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | 0 | return $self->make_creds( | |||||||||||||||||||||||||||
| 168 | json_path => $system_default_file, | ||||||||||||||||||||||||||||
| 169 | scope => $scopes, | ||||||||||||||||||||||||||||
| 170 | %options | ||||||||||||||||||||||||||||
| 171 | ); | ||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | 0 | return; | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 7 | ▶ | sub _determine_class_for_type { | ||||||||||||||||||||||||||
|
Subroutine
_determine_class_for_type: called CC=6 SCAR=20.2
| |||||||||||||||||||||||||||||
| 177 | 7 | my ($self, $type) = @_; | |||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 7 | ▶ | if ($type eq 'service_account') { | ||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||
| 180 | 4 | require Google::Auth::ServiceAccountCredentials; | |||||||||||||||||||||||||||
| 181 | 4 | return 'Google::Auth::ServiceAccountCredentials'; | |||||||||||||||||||||||||||
| 182 | } elsif ($type eq 'authorized_user') { | ||||||||||||||||||||||||||||
| 183 | 0 | require Google::Auth::UserRefreshCredentials; | |||||||||||||||||||||||||||
| 184 | 0 | return 'Google::Auth::UserRefreshCredentials'; | |||||||||||||||||||||||||||
| 185 | } elsif ($type eq 'compute_engine') { | ||||||||||||||||||||||||||||
| 186 | 0 | require Google::Auth::ComputeEngine; | |||||||||||||||||||||||||||
| 187 | 0 | return 'Google::Auth::ComputeEngine'; | |||||||||||||||||||||||||||
| 188 | } elsif ($type eq 'impersonated_service_account') { | ||||||||||||||||||||||||||||
| 189 | 1 | require Google::Auth::ImpersonatedServiceAccountCredentials; | |||||||||||||||||||||||||||
| 190 | 1 | return 'Google::Auth::ImpersonatedServiceAccountCredentials'; | |||||||||||||||||||||||||||
| 191 | } elsif ($type eq 'external_account') { | ||||||||||||||||||||||||||||
| 192 | 2 | require Google::Auth::ExternalAccountCredentials; | |||||||||||||||||||||||||||
| 193 | 2 | return 'Google::Auth::ExternalAccountCredentials'; | |||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | Google::Auth::DefaultCredentialsError->throw( | ||||||||||||||||||||||||||||
| 197 | 0 | 'Unsupported credential type: ' . $type); | |||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 3 | ▶ | sub _read_file { | ||||||||||||||||||||||||||
|
Subroutine
_read_file: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||||||
| 201 | 3 | my ($self, $path) = @_; | |||||||||||||||||||||||||||
| 202 | 3 | ▶ | open(my $fh, '<', $path) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 203 | or Google::Auth::DefaultCredentialsError->throw( | ||||||||||||||||||||||||||||
| 204 | 'Could not open file ' . $path . ': ' . $!); | ||||||||||||||||||||||||||||
| 205 | 3 | local $/; | |||||||||||||||||||||||||||
| 206 | 3 | my $content = <$fh>; | |||||||||||||||||||||||||||
| 207 | 3 | ▶ | close($fh) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 208 | or Google::Auth::DefaultCredentialsError->throw( | ||||||||||||||||||||||||||||
| 209 | 'Could not close file ' . $path . ': ' . $!); | ||||||||||||||||||||||||||||
| 210 | 3 | return $content; | |||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | 13 | 1; | |||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||