Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
adam.huang
Ohmyzsh
Commits
23f9348e
Unverified
Commit
23f9348e
authored
Jul 13, 2021
by
Sergei Shvetsov
Committed by
GitHub
Jul 13, 2021
Browse files
fix(aws): allow for profile switch w/o MFA configured (#9924)
parent
e4f6f169
Changes
1
Hide whitespace changes
Inline
Side-by-side
plugins/aws/aws.plugin.zsh
View file @
23f9348e
...
...
@@ -62,47 +62,47 @@ function acp() {
read
-r
sess_duration
fi
mfa_opt
=(
--serial-number
"
$mfa_serial
"
--token-code
"
$mfa_token
"
--duration-seconds
"
${
sess_duration
:-
3600
}
"
)
fi
# Now see whether we need to just MFA for the current role, or assume a different one
local
role_arn
=
"
$(
aws configure get role_arn
--profile
$profile
)
"
local
sess_name
=
"
$(
aws configure get role_session_name
--profile
$profile
)
"
if
[[
-n
"
$role_arn
"
]]
;
then
# Means we need to assume a specified role
aws_command
=(
aws sts assume-role
--role-arn
"
$role_arn
"
"
${
mfa_opt
[@]
}
"
)
# Now see whether we need to just MFA for the current role, or assume a different one
local
role_arn
=
"
$(
aws configure get role_arn
--profile
$profile
)
"
local
sess_name
=
"
$(
aws configure get role_session_name
--profile
$profile
)
"
# Check whether external_id is configured to use while assuming the role
local
external_id
=
"
$(
aws configure get external_id
--profile
$profile
)
"
if
[[
-n
"
$external_id
"
]]
;
then
aws_command+
=(
--external-id
"
$external_id
"
)
fi
if
[[
-n
"
$role_arn
"
]]
;
then
# Means we need to assume a specified role
aws_command
=(
aws sts assume-role
--role-arn
"
$role_arn
"
"
${
mfa_opt
[@]
}
"
)
# Get source profile to use to assume role
local
source_profile
=
"
$(
aws configure get source_profile
--profile
$profile
)
"
if
[[
-z
"
$sess_name
"
]]
;
then
sess_name
=
"
${
source_profile
:-
profile
}
"
fi
aws_command+
=(
--profile
=
"
${
source_profile
:-
profile
}
"
--role-session-name
"
${
sess_name
}
"
)
# Check whether external_id is configured to use while assuming the role
local
external_id
=
"
$(
aws configure get external_id
--profile
$profile
)
"
if
[[
-n
"
$external_id
"
]]
;
then
aws_command+
=(
--external-id
"
$external_id
"
)
fi
echo
"Assuming role
$role_arn
using profile
${
source_profile
:-
profile
}
"
else
# Means we only need to do MFA
aws_command
=(
aws sts get-session-token
--profile
=
"
$profile
"
"
${
mfa_opt
[@]
}
"
)
echo
"Obtaining session token for profile
$profile
"
# Get source profile to use to assume role
local
source_profile
=
"
$(
aws configure get source_profile
--profile
$profile
)
"
if
[[
-z
"
$sess_name
"
]]
;
then
sess_name
=
"
${
source_profile
:-
profile
}
"
fi
aws_command+
=(
--profile
=
"
${
source_profile
:-
profile
}
"
--role-session-name
"
${
sess_name
}
"
)
# Format output of aws command for easier processing
aws_command+
=(
--query
'[Credentials.AccessKeyId,Credentials.SecretAccessKey,Credentials.SessionToken]'
--output
text
)
echo
"Assuming role
$role_arn
using profile
${
source_profile
:-
profile
}
"
else
# Means we only need to do MFA
aws_command
=(
aws sts get-session-token
--profile
=
"
$profile
"
"
${
mfa_opt
[@]
}
"
)
echo
"Obtaining session token for profile
$profile
"
fi
# Run the aws command to obtain credentials
local
-a
credentials
credentials
=(
${
(ps
:
\t
:
)
"
$(
${
aws_command
[@]
}
)
"
}
)
# Format output of aws command for easier processing
aws_command+
=(
--query
'[Credentials.AccessKeyId,Credentials.SecretAccessKey,Credentials.SessionToken]'
--output
text
)
if
[[
-n
"
$credentials
"
]]
;
then
aws_access_key_id
=
"
${
credentials
[1]
}
"
aws_secret_access_key
=
"
${
credentials
[2]
}
"
aws_session_token
=
"
${
credentials
[3]
}
"
fi
# Run the aws command to obtain credentials
local
-a
credentials
credentials
=(
${
(ps
:
\t
:
)
"
$(
${
aws_command
[@]
}
)
"
}
)
if
[[
-n
"
$credentials
"
]]
;
then
aws_access_key_id
=
"
${
credentials
[1]
}
"
aws_secret_access_key
=
"
${
credentials
[2]
}
"
aws_session_token
=
"
${
credentials
[3]
}
"
fi
# Switch to AWS profile
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment