blib/lib/Amazon/Credentials/Resolver/Profile.pm

filter: Statementstmt 99.1% 122 / 123 Branchbran 85.9% 55 / 64 Conditioncond 80.0% 16 / 20 MC/DCmcdc 63.6% 7 / 11 Subroutinesub 100% 18 / 18 Podpod 0.0% 0 / 2 totaltotal 91.6% 218 / 238 CC 40 SCAR 24.3
CC
40
Cov
93%
_resolve_profile
18.6
_resolve_assume_role
16.0
_resolve_credential_source
6.0
CRAP 11.4
« blib/lib/Amazon/Credentials/Role/SSOCache.pm ↑ Summary blib/lib/Amazon/Credentials/Provider/WebIdentity.pm »
1 package Amazon::Credentials::Resolver::Profile;
2  
3 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 9 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 9 ▶use Amazon::Credentials::Provider::AssumeRole;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use Amazon::Credentials::Provider::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use Amazon::Credentials::Provider::Process;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use Amazon::Credentials::Provider::Container;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use Amazon::Credentials::Provider::Env;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use Amazon::Credentials::Provider::InstanceRole;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 ▶use Amazon::Credentials::Provider::WebIdentity;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 9 ▶use Amazon::Credentials::Provider::SSO;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 9 ▶use Role::Tiny::With;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 9 with 'Amazon::Credentials::Role::File';
21  
22 ########################################################################
23 37 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
24 ########################################################################
25 37 my ( $class, @args ) = @_;
26  
27 37 ▶ my $options = ref $args[0] ? { %{ $args[0] } } : {@args};
Branch: 1 branch
BranchTrueFalseTotal
ref $args[0] ? : 10 27 37
28  
29 return bless {
30 user_agent => $options->{user_agent},
31 imdsv2 => $options->{imdsv2},
32 37 }, $class;
33 }
34  
35 ########################################################################
36 37 ▶sub resolve {
Subroutine resolve: called CC=2 SCAR=7.1
Pod undocumented
37 ########################################################################
38 37 my ( $self, $profile ) = @_;
39  
40 37 ▶ $profile ||= 'default';
Condition50%1/2
$profile ||= "default"
l!l
370
MC/DC0%0/1
$profile ||= "default"
$profile
Truth table50%1/2
A
A= $profile
Aresult
11
00
41  
42 37 return $self->_resolve_profile(
43 profile => $profile,
44 seen => {},
45 );
46 }
47  
48 ########################################################################
49 51 ▶sub _resolve_profile {
Subroutine _resolve_profile: called CC=18 SCAR=29.2
50 ########################################################################
51 51 my ( $self, @args ) = @_;
52  
53 51 ▶ my $options = ref $args[0] ? $args[0] : {@args};
Branch: 1 branch
BranchTrueFalseTotal
ref $args[0] ? : 0 51 51
54  
55 51 my $profile = $options->{profile};
56  
57 51 my $seen = $options->{seen};
58  
59 croak "credential profile cycle detected at '$profile'"
60 51 ▶ if $seen->{$profile};
Branch: 1 branch
BranchTrueFalseTotal
if $seen->{$profile} 2 49 51
61  
62 49 my %seen = %{$seen};
63  
64 49 $seen{$profile} = 1;
65  
66 49 my $config = $self->_get_profile($profile);
67  
68 return
69 49 ▶ if !$config || !keys %{$config};
Branch: 1 branch
BranchTrueFalseTotal
unless $config and keys %{$config;} 6 43 49
Condition66%2/3
$config and keys %{$config;}
!ll&&!rl&&r
6043
MC/DC50%1/2
$config and keys %{$config;}
$config keys %{$config;}
Truth table66%2/3
A and B
A= $configB= keys %{$config;}
ABresult
0X0
100
111
70  
71 43 ▶ if ( $config->{web_identity_token_file} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"web_identity_token_file"}) 1 42 43
72 croak "profile '$profile' has web_identity_token_file but no role_arn"
73 1 ▶ if !$config->{role_arn};
Branch: 1 branch
BranchTrueFalseTotal
unless $config->{"role_arn"} 0 1 1
74  
75 my %provider_options = (
76 role_arn => $config->{role_arn},
77 token_file => $config->{web_identity_token_file},
78 1 );
79  
80 1 ▶ if ( defined $config->{role_session_name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config->{"role_session_name"}) 0 1 1
81 0 $provider_options{role_session_name} = $config->{role_session_name};
82 }
83  
84 1 ▶ if ( defined $config->{region} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config->{"region"}) 1 0 1
85 1 $provider_options{region} = $config->{region};
86 }
87  
88 1 ▶ if ( $self->{user_agent} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"user_agent"}) 1 0 1
89 1 $provider_options{user_agent} = $self->{user_agent};
90 }
91  
92 1 return Amazon::Credentials::Provider::WebIdentity->new(%provider_options);
93 }
94  
95 42 ▶ if ( $config->{role_arn} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"role_arn"}) 24 18 42
96 24 return $self->_resolve_assume_role(
97 config => $config,
98 profile => $profile,
99 seen => \%seen,
100 );
101 }
102 18 ▶ if ( $config->{sso_session}
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"sso_session"} or $config->{"sso_start_url"} or $config->{"sso_account_id"} or $config->{"sso_role_name"}) 3 15 18
Condition77%7/9
$config->{"sso_session"} or $config->{"sso_start_url"} or $config->{"sso_account_id"} or $config->{"sso_role_name"}
l!l&&r!l&&!r
3015
$config->{"sso_session"} or $config->{"sso_start_url"} or $config->{"sso_account_id"}
l!l&&r!l&&!r
3015
$config->{"sso_session"} or $config->{"sso_start_url"}
l!l&&r!l&&!r
1215
MC/DC50%2/4
$config->{"sso_session"} or $config->{"sso_start_url"} or $config->{"sso_account_id"} or $config->{"sso_role_name"}
$config->{"sso_session"} $config->{"sso_start_url"} $config->{"sso_account_id"} $config->{"sso_role_name"}
Truth table60%3/5
A or B or C or D
A= $config->{"sso_session"}B= $config->{"sso_start_url"}C= $config->{"sso_account_id"}D= $config->{"sso_role_name"}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
103 || $config->{sso_start_url}
104 || $config->{sso_account_id}
105 || $config->{sso_role_name} ) {
106 3 my %provider_options = ( profile => $profile, );
107  
108 3 ▶ if ( $self->{user_agent} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"user_agent"}) 2 1 3
109 2 $provider_options{user_agent} = $self->{user_agent};
110 }
111  
112 3 return Amazon::Credentials::Provider::SSO->new(%provider_options);
113 }
114  
115 15 ▶ if ( $config->{credential_process} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"credential_process"}) 4 11 15
116 4 my %provider_options = ( profile => $profile, );
117  
118 4 ▶ if ( $self->{user_agent} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"user_agent"}) 3 1 4
119 3 $provider_options{user_agent} = $self->{user_agent};
120 }
121  
122 4 return Amazon::Credentials::Provider::Process->new(%provider_options);
123 }
124  
125 11 return Amazon::Credentials::Provider::Config->new( profile => $profile, );
126 }
127  
128 ########################################################################
129 24 ▶sub _resolve_assume_role {
Subroutine _resolve_assume_role: called CC=16 SCAR=27.7
130 ########################################################################
131 24 my ( $self, @args ) = @_;
132  
133 24 ▶ my $options = ref $args[0] ? $args[0] : {@args};
Branch: 1 branch
BranchTrueFalseTotal
ref $args[0] ? : 0 24 24
134  
135 24 my $config = $options->{config};
136  
137 24 my $profile = $options->{profile};
138  
139 24 my $source_profile = $config->{source_profile};
140  
141 24 my $credential_source = $config->{credential_source};
142  
143 24 ▶ croak "profile '$profile' cannot specify both source_profile and credential_source"
Branch: 1 branch
BranchTrueFalseTotal
if $source_profile and $credential_source 1 23 24
Condition100%3/3
$source_profile and $credential_source
!ll&&!rl&&r
9141
MC/DC100%2/2
$source_profile and $credential_source
$source_profile $credential_source
Truth table100%3/3
A and B
A= $source_profileB= $credential_source
ABresult
0X0
100
111
144 if $source_profile && $credential_source;
145  
146 23 ▶ croak "profile '$profile' has role_arn but no credential source"
Branch: 1 branch
BranchTrueFalseTotal
unless $source_profile or $credential_source 1 22 23
Condition100%3/3
$source_profile or $credential_source
l!l&&r!l&&!r
1481
MC/DC100%2/2
$source_profile or $credential_source
$source_profile $credential_source
Truth table100%3/3
A or B
A= $source_profileB= $credential_source
ABresult
1X1
011
000
147 if !$source_profile && !$credential_source;
148  
149 22 my $source_provider;
150  
151 22 ▶ if ($source_profile) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source_profile) { } 14 8 22
152 $source_provider = $self->_resolve_profile(
153 profile => $source_profile,
154 seen => $options->{seen},
155 14 );
156  
157 11 ▶ croak "unable to resolve source_profile '$source_profile' for profile '$profile'"
Branch: 1 branch
BranchTrueFalseTotal
unless $source_provider 1 10 11
158 if !$source_provider;
159 }
160 else {
161 8 $source_provider = $self->_resolve_credential_source($credential_source);
162  
163 6 ▶ croak "unable to resolve credential_source '$credential_source' for profile '$profile'"
Branch: 1 branch
BranchTrueFalseTotal
unless $source_provider 1 5 6
164 if !$source_provider;
165 }
166  
167 my %provider_options = (
168 role_arn => $config->{role_arn},
169 15 source_provider => $source_provider,
170 );
171  
172 15 ▶ if ( defined $config->{mfa_serial} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config->{"mfa_serial"}) 2 13 15
173 croak "profile '$profile' requires MFA but AMAZON_CREDENTIALS_MFA_TOKEN is not set"
174 2 ▶ if !defined $ENV{AMAZON_CREDENTIALS_MFA_TOKEN};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ENV{"AMAZON_CREDENTIALS_MFA_TOKEN"} 1 1 2
175  
176 1 $provider_options{mfa_serial} = $config->{mfa_serial};
177 1 $provider_options{mfa_token} = $ENV{AMAZON_CREDENTIALS_MFA_TOKEN};
178 }
179  
180 14 ▶ if ( defined $config->{role_session_name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config->{"role_session_name"}) 1 13 14
181 1 $provider_options{role_session_name} = $config->{role_session_name};
182 }
183  
184 14 ▶ if ( defined $config->{external_id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config->{"external_id"}) 1 13 14
185 1 $provider_options{external_id} = $config->{external_id};
186 }
187  
188 14 ▶ if ( defined $config->{duration_seconds} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config->{"duration_seconds"}) 1 13 14
189 1 $provider_options{duration_seconds} = $config->{duration_seconds};
190 }
191  
192 14 ▶ if ( defined $config->{region} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config->{"region"}) 8 6 14
193 8 $provider_options{region} = $config->{region};
194 }
195  
196 14 ▶ if ( $self->{user_agent} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"user_agent"}) 14 0 14
197 14 $provider_options{user_agent} = $self->{user_agent};
198 }
199  
200 14 return Amazon::Credentials::Provider::AssumeRole->new(%provider_options);
201 }
202  
203 ########################################################################
204 8 ▶sub _resolve_credential_source {
Subroutine _resolve_credential_source: called CC=6 SCAR=18.0
205 ########################################################################
206 8 my ( $self, $source ) = @_;
207  
208 8 ▶ if ( $source eq 'Environment' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($source eq 'Environment') { } 2 6 8
elsif ($source eq 'EcsContainer') { } 1 5 6
elsif ($source eq 'Ec2InstanceMetadata') { } 4 1 5
209 2 return Amazon::Credentials::Provider::Env->new;
210 }
211 elsif ( $source eq 'EcsContainer' ) {
212 1 my %options;
213  
214 1 ▶ if ( $self->{user_agent} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"user_agent"}) 1 0 1
215 1 $options{user_agent} = $self->{user_agent};
216 }
217  
218 1 return Amazon::Credentials::Provider::Container->new(%options);
219 }
220 elsif ( $source eq 'Ec2InstanceMetadata' ) {
221 4 my %options = ( imdsv2 => $self->{imdsv2}, );
222  
223 4 ▶ if ( $self->{user_agent} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"user_agent"}) 4 0 4
224 4 $options{user_agent} = $self->{user_agent};
225 }
226  
227 4 return Amazon::Credentials::Provider::InstanceRole->new(%options);
228 }
229  
230 1 croak "unsupported credential_source '$source'";
231 }
232  
233 9 1;
« blib/lib/Amazon/Credentials/Role/SSOCache.pm ↑ Summary blib/lib/Amazon/Credentials/Provider/WebIdentity.pm »