Skip to content

Commit 2334b11

Browse files
Merge pull request #2 from LozanoMatheus/feature/auto-add-mfa-cli
Add support for AWS MFA
2 parents 4283a15 + 856212b commit 2334b11

File tree

6 files changed

+261
-24
lines changed

6 files changed

+261
-24
lines changed

.github/FUNDING.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# see @ https://docs.github.com/en/repositories/managing-your-repositorys-settings-and-features/customizing-your-repository/displaying-a-sponsor-button-in-your-repository
2+
github: LozanoMatheus
3+
custom: ['https://www.paypal.com/donate/?hosted_button_id=GP3EDMH3CY9Y2']

README.md

Lines changed: 142 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ _Hoping these will become a native features_
2929
* [AWS CLI](https://aws.amazon.com/cli/)
3030
* [realpath](https://github.com/coreutils/coreutils)
3131

32-
## AWS Access Keys auto-rotation
32+
## AWS Access Keys autorotation
3333

3434
The AWSecure CLI can autorotate the AWS Access Keys based on the profile that the user is currently using or via cronjob.
3535

@@ -41,11 +41,12 @@ For example, if you configured to only use in the user request and there are mul
4141

4242
The AWS Access Keys auto-rotation works transparently for the users, when the user executes an AWS command (e.g. `aws lambda list-functions`), it will check if needs to rotate the AWS Access Keys for the current AWS Profile. Once the AWS Access Keys auto-rotation steps are done, it will run the command requested by the user (e.g. `aws lambda list-functions`).
4343

44-
### How it works - cronjob
45-
4644
This allows users to add the AWS Access Keys auto-rotation as a cronjob (e.g. on crontab), so they can disable it when running any AWS command. It's also possible to add multiple entries, one per AWS profile.
4745

46+
This is helpful in case you have a profile that you barely use or you want to eliminate the extra ~3 seconds on each command or ~25 seconds when the keys needs to be rotated.
47+
4848
For example:
49+
4950
Configure the `~/.awsecure-cli` to never run the AWS Access Keys auto-rotation.
5051

5152
```bash
@@ -69,6 +70,12 @@ PATH=/usr/local/bin:...
6970

7071
> Make sure you have the environment variable PATH configure and pointing to AWSecure CLI.
7172
73+
## AWSecure CLI and AWS MFA
74+
75+
The AWSecure CLI makes easier to use AWS MFA in the terminal, specially when you have multiple profiles and/or using other tools such as kubectl.
76+
77+
The AWSecure CLI can automatically gets the first MFA device configured in your user, request the MFA code and then temporarily stores the session token for the time you define in the `AWSECURE_CLI_MFA_TOKEN_DURATION`. Once the MFA token duration is reached, it will automatically ask you again for the MFA code and renew the session token.
78+
7279
## Instalation
7380

7481
There are two ways you can install the wrapper. You can create a symbolic link to `/usr/local/bin` (or another place of your choice) or by setting the `awsecure-cli/bin/<INTERPRETER>`.
@@ -91,7 +98,9 @@ echo 'PATH=/usr/local/bin:${PATH}' >> ~/.zshrc
9198

9299
## Configuring
93100

94-
These are the configurations that you can define in your `~/.awsecure-cli`.
101+
These are the configurations that you can define in your `~/.awsecure-cli` or via environment variables.
102+
103+
> The exported environment variable has high priority over the file `~/.awsecure-cli`.
95104
96105
| PARAMETER | DEFAULT | ACCEPTED<br> VALUES | COMMENT |
97106
|:---------------------------------------:|:-----------:|:----------------------------:|:-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------:|
@@ -100,40 +109,98 @@ These are the configurations that you can define in your `~/.awsecure-cli`.
100109
| AWSECURE_CLI_LOG_TO_FILE | false | true<br>false | This will send the logs to a file `/tmp/awsecure-cli.log.<Ymd>` (e.g. `/tmp/awsecure-cli.log.20220215`) |
101110
| AWSECURE_CLI_AUTOROTATE_AWS_ACCESS_KEYS | true | true<br>false | Enable the AWS Access Keys autorotation |
102111
| AWSECURE_CLI_AUTOROTATE_PERIOD | 168 | 1 to ... | This value is based on hours and once your AWS Access Keys are older than this, it will autorotate (168 hours == 7 days) |
103-
| AWSECURE_CLI_AUTOROTATE_CHECK | daily | daily<br>on-reboot<br>always | This is when the autorotate will be executed.<br>If you're using in the user request, this will only be triggered if you run the AWS CLI |
104-
| AWSECURE_CLI_AUTOROTATE_ONLY | not defined | true<br>false | This trigger the only the AWS Access Keys auto-rotation, any AWS command (e.g. `aws lambda list-functions`) will be ignored |
112+
| AWSECURE_CLI_AUTOROTATE_CHECK | daily | daily<br>on-reboot<br>always | This is when the autorotate will be executed.<br>If you're using in the user request, this will only be triggered if you run the AWS CLI. This check is based on your AWSecure CLI utilization. If you don't set it on cronjob or use it, then it will never autorotate your AWS access keys. |
113+
| AWSECURE_CLI_AUTOROTATE_ONLY | not defined | true<br>false | This trigger only the AWS Access Keys auto-rotation, any AWS command (e.g. `aws lambda list-functions`) will be ignored |
114+
| AWSECURE_CLI_MFA_ON | false | true<br>false | This will add the AWS_SESSION_TOKEN on (almost) all AWS CLI request. You need to set AWSECURE_CLI_MFA_AUTO_GET_DEVICE or AWSECURE_CLI_MFA_AWS_ARN |
115+
| AWSECURE_CLI_MFA_AUTO_GET_DEVICE | true | true<br>false | This will automatically get the first AWS MFA device configured in your user and set the AWSECURE_CLI_MFA_AWS_ARN |
116+
| AWSECURE_CLI_MFA_AWS_ARN | false | string | This is the AWS ARN for the MFA device configured in your user. The ARN starts with `arn:aws:iam::<AWS_Account_ID>:sms-mfa/` or `arn:aws:iam::<AWS_Account_ID>:mfa`. Please, check the "[Checking MFA status](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_mfa_checking-status.html) official documentation." |
117+
| AWSECURE_CLI_MFA_TOKEN_DURATION | 900 | int | This is how long the token will be valid. The token will be temporarly stored locally and renewed once is reaches the time informed. Valid range: Minimum value of 900 (15 minutes). Maximum value of 129600 (36 hours) - Please, check the [AWS official documentation - AWS STS API Reference](https://docs.aws.amazon.com/STS/latest/APIReference/API_GetSessionToken.html#API_GetSessionToken_RequestParameters) |
105118

106119
> \* mandatory parameter
107120
108-
### Example
121+
## Configuration examples
122+
123+
These are some of the configuration examples you can have in your AWSecure CLI.
124+
125+
### Minimal (inc. Access keys autorotation)
126+
127+
The `AWSECURE_CLI_AWS_BIN_FILEPATH` is the only thing you have to define. All the other configurations already have a pre-defined value or it's not mandatory.
109128

110-
You can configure via `~/.awsecure-cli`.
129+
This will autorotate your AWS Access keys every 168 hours (7 days) and checking if it's needed to rotate every day.
130+
131+
Defining it via `~/.awsecure-cli`.
111132

112133
```bash
113134
AWSECURE_CLI_AWS_BIN_FILEPATH=~/.asdf/shims/aws
114-
AWSECURE_CLI_MUTED="false"
115-
AWSECURE_CLI_AUTOROTATE_AWS_ACCESS_KEYS="true"
116-
AWSECURE_CLI_AUTOROTATE_PERIOD="24"
117-
AWSECURE_CLI_AUTOROTATE_CHECK="always"
118-
AWSECURE_CLI_AUTOROTATE_ONLY="true"
119-
AWSECURE_CLI_LOG_TO_FILE="true"
120135
```
121136

122-
Or export the environment variables, like:
137+
Or via environment variables:
123138

124139
```bash
125140
export AWSECURE_CLI_AWS_BIN_FILEPATH=~/.asdf/shims/aws
126-
export AWSECURE_CLI_MUTED="false"
127-
export AWSECURE_CLI_AUTOROTATE_AWS_ACCESS_KEYS="true"
128-
export AWSECURE_CLI_AUTOROTATE_PERIOD="24"
129-
export AWSECURE_CLI_AUTOROTATE_CHECK="always"
130-
export AWSECURE_CLI_AUTOROTATE_ONLY="true"
131-
export AWSECURE_CLI_LOG_TO_FILE="true"
132141
```
133142

134-
> The exported environment variable has high priority over the file `~/.awsecure-cli`.
143+
### Custom AWS Access keys autorotation
144+
145+
AWS access keys autorotation every 336 hours (14 days).
146+
147+
Defining it via `~/.awsecure-cli`.
148+
149+
```bash
150+
AWSECURE_CLI_AWS_BIN_FILEPATH=~/.asdf/shims/aws
151+
AWSECURE_CLI_AUTOROTATE_PERIOD="336"
152+
```
153+
154+
Or via environment variables:
155+
156+
```bash
157+
export AWSECURE_CLI_AWS_BIN_FILEPATH=~/.asdf/shims/aws
158+
export AWSECURE_CLI_AUTOROTATE_PERIOD="336"
159+
```
160+
161+
### MFA + Access keys autorotation
162+
163+
Enable MFA and setting its session token duration time for 14400 (4 hours). Also, autorotating your AWS access keys every 168 hours (7 days).
164+
165+
Defining it via `~/.awsecure-cli`.
166+
167+
```bash
168+
export AWSECURE_CLI_AWS_BIN_FILEPATH=~/.asdf/shims/aws
169+
export AWSECURE_CLI_AUTOROTATE_PERIOD="168"
170+
export AWSECURE_CLI_MFA_ON="on"
171+
export AWSECURE_CLI_MFA_TOKEN_DURATION="14400"
172+
```
173+
174+
Or via environment variables:
175+
176+
```bash
177+
AWSECURE_CLI_AWS_BIN_FILEPATH=~/.asdf/shims/aws
178+
AWSECURE_CLI_AUTOROTATE_PERIOD="168"
179+
AWSECURE_CLI_MFA_ON="on"
180+
AWSECURE_CLI_MFA_TOKEN_DURATION="14400"
181+
```
182+
183+
### Minimal for kubectl without kubeconfig
184+
185+
AWSecure CLI can also be integrated with kubectl. The AWSCLI is used to get the session-token and autheticate to your EKS cluster.
186+
187+
The only mandatory for this, is the `AWSECURE_CLI_MUTED="false"`. This is because kubectl only accepts a specific JSON return, anything more than that will cause an error and prevent you from using the kubectl.
188+
189+
Defining it via `~/.awsecure-cli`.
190+
191+
```bash
192+
AWSECURE_CLI_AWS_BIN_FILEPATH=~/.asdf/shims/aws
193+
AWSECURE_CLI_MUTED="true"
194+
```
195+
196+
Or via environment variables:
197+
198+
```bash
199+
export AWSECURE_CLI_AWS_BIN_FILEPATH=~/.asdf/shims/aws
200+
export AWSECURE_CLI_MUTED="true"
201+
```
135202

136-
## Integrating with kubectl
203+
### Integrating with kubectl with kubeconfig
137204

138205
The integration with `kubectl` will be done thanks to the integration between AWS STS and Kubernetes.
139206

@@ -145,6 +212,8 @@ aws eks update-kubeconfig --name <EKS_CLUSTER_NAME> --role-arn <ROLE_ARN> --alia
145212

146213
Make sure you have, at least, the `AWS_PROFILE` and `AWSECURE_CLI_MUTED` defined in your `~/.kube/config`.
147214

215+
> In case you define the `AWSECURE_CLI_AUTOROTATE_PERIOD` via `~/.kube/config`, make sure it's the same as the one defined via `~/.awsecure-cli` and/or environment variables.
216+
148217
```yaml
149218
- name: arn:aws:eks:<REGION>:<ACCOUNT_ID>:cluster/<CLUSTER_NAME>
150219
user:
@@ -173,3 +242,53 @@ For more information, please check the AWS official documentation.
173242
174243
* [Create a kubeconfig for Amazon EKS](https://docs.aws.amazon.com/eks/latest/userguide/create-kubeconfig.html)
175244
* [AWS CLI - aws eks update-kubeconfig](https://docs.aws.amazon.com/cli/latest/reference/eks/update-kubeconfig.html)
245+
246+
### Kubectl + MFA
247+
248+
This will use the MFA for every kubectl request you make and it will automatically gets your first MFA device.
249+
250+
To use the MFA is relative simply, but since kubectl will isn't interactive, you can't pass the MFA code. So, in order to fix it, you have to first run an AWSCLI command (e.g. `aws s3 ls`) to create the session token and then you can use the kubectl. You can also disable the MFA only for kubectl.
251+
252+
Defining it via `~/.awsecure-cli`.
253+
254+
```bash
255+
AWSECURE_CLI_AWS_BIN_FILEPATH=~/.asdf/shims/aws
256+
AWSECURE_CLI_MFA_ON="on"
257+
AWSECURE_CLI_MFA_TOKEN_DURATION="14400"
258+
```
259+
260+
Or via environment variables:
261+
262+
```bash
263+
export AWSECURE_CLI_AWS_BIN_FILEPATH=~/.asdf/shims/aws
264+
export AWSECURE_CLI_MFA_ON="on"
265+
export AWSECURE_CLI_MFA_TOKEN_DURATION="14400"
266+
```
267+
268+
Or via `~/.kube/config`:
269+
270+
> In case you define the `AWSECURE_CLI_MFA_TOKEN_DURATION` via `~/.kube/config`, make sure it's the same as the one defined via `~/.awsecure-cli` and/or environment variables.
271+
272+
```yaml
273+
- name: arn:aws:eks:<REGION>:<ACCOUNT_ID>:cluster/<CLUSTER_NAME>
274+
user:
275+
exec:
276+
apiVersion: <API_VERSION>
277+
args:
278+
- --region
279+
- <REGION>
280+
- eks
281+
- get-token
282+
- --cluster-name
283+
- <CLUSTER_NAME>
284+
- --role
285+
- arn:aws:iam::<ACCOUNT_ID>:role/<ROLE_NAME>
286+
command: aws
287+
env:
288+
- name: AWS_PROFILE
289+
value: <AWS_PROFILE>
290+
- name: AWSECURE_CLI_MFA_ON
291+
value: "true" ## or false, in case you want to disable it only for kubectl
292+
- name: AWSECURE_CLI_MUTED
293+
value: "true"
294+
```

bin/bash/aws

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ set -eo pipefail
77
[[ ! -z "${AWSECURE_CLI_AUTOROTATE_AWS_ACCESS_KEYS}" ]] && declare -lx AWSECURE_CLI_AUTOROTATE_AWS_ACCESS_KEYS_TMP="${AWSECURE_CLI_AUTOROTATE_AWS_ACCESS_KEYS}"
88
[[ ! -z "${AWSECURE_CLI_AUTOROTATE_PERIOD}" ]] && declare -lx AWSECURE_CLI_AUTOROTATE_PERIOD_TMP="${AWSECURE_CLI_AUTOROTATE_PERIOD}"
99
[[ ! -z "${AWSECURE_CLI_AUTOROTATE_CHECK}" ]] && declare -lx AWSECURE_CLI_AUTOROTATE_CHECK_TMP="${AWSECURE_CLI_AUTOROTATE_CHECK}"
10+
[[ ! -z "${AWSECURE_CLI_MFA_ON}" ]] && declare -lx AWSECURE_CLI_MFA_ON_TMP="${AWSECURE_CLI_MFA_ON}"
1011

1112
. ~/.awsecure-cli
1213

@@ -15,6 +16,7 @@ set -eo pipefail
1516
[[ ! -z "${AWSECURE_CLI_AUTOROTATE_AWS_ACCESS_KEYS_TMP}" ]] && declare -glx AWSECURE_CLI_AUTOROTATE_AWS_ACCESS_KEYS="${AWSECURE_CLI_AUTOROTATE_AWS_ACCESS_KEYS_TMP:-$AWSECURE_CLI_AUTOROTATE_AWS_ACCESS_KEYS}"
1617
[[ ! -z "${AWSECURE_CLI_AUTOROTATE_PERIOD_TMP}" ]] && declare -glx AWSECURE_CLI_AUTOROTATE_PERIOD="${AWSECURE_CLI_AUTOROTATE_PERIOD_TMP:-$AWSECURE_CLI_AUTOROTATE_PERIOD}"
1718
[[ ! -z "${AWSECURE_CLI_AUTOROTATE_CHECK_TMP}" ]] && declare -glx AWSECURE_CLI_AUTOROTATE_CHECK="${AWSECURE_CLI_AUTOROTATE_CHECK_TMP:-$AWSECURE_CLI_AUTOROTATE_CHECK}"
19+
[[ ! -z "${AWSECURE_CLI_MFA_ON_TMP}" ]] && declare -glx AWSECURE_CLI_MFA_ON="${AWSECURE_CLI_MFA_ON_TMP:-$AWSECURE_CLI_MFA_ON}"
1820

1921
if [[ $(type awsecure_cli_log_info 2> /dev/null) == "" || -z "${AWSECURE_CLI_SRC_DIRECTORY// /}" ]]; then
2022
[[ -L ${0} ]] && declare -gr AWSECURE_CLI_SRC_DIRECTORY="$(realpath $(readlink ${0}) | xargs dirname)/../../src" || declare -gr AWSECURE_CLI_SRC_DIRECTORY="$(realpath ${0} | xargs dirname)/../../src"

src/common/add_mfa_on_cli.sh

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#!/usr/bin/env bash
2+
3+
set -eo pipefail
4+
5+
[[ ! -z "${AWSECURE_CLI_AWS_BIN_FILEPATH}" ]] && declare -x AWSECURE_CLI_AWS_BIN_FILEPATH_TMP="${AWSECURE_CLI_AWS_BIN_FILEPATH}"
6+
[[ ! -z "${AWSECURE_CLI_MUTED}" ]] && declare -lx AWSECURE_CLI_MUTED_TMP="${AWSECURE_CLI_MUTED}"
7+
8+
. ~/.awsecure-cli
9+
10+
[[ ! -z "${AWSECURE_CLI_AWS_BIN_FILEPATH_TMP}" ]] && declare -gx AWSECURE_CLI_AWS_BIN_FILEPATH="${AWSECURE_CLI_AWS_BIN_FILEPATH_TMP:-$AWSECURE_CLI_AWS_BIN_FILEPATH}"
11+
[[ ! -z "${AWSECURE_CLI_MUTED_TMP}" ]] && declare -glx AWSECURE_CLI_MUTED="${AWSECURE_CLI_MUTED_TMP:-$AWSECURE_CLI_MUTED}"
12+
13+
if [[ $(type awsecure_cli_log_info 2> /dev/null) == "" || -z "${AWSECURE_CLI_SRC_DIRECTORY// /}" ]]; then
14+
[[ -L ${0} ]] && declare -gr AWSECURE_CLI_SRC_DIRECTORY="$(realpath $(readlink ${0}) | xargs dirname)/../../src" || declare -gr AWSECURE_CLI_SRC_DIRECTORY="$(realpath ${0} | xargs dirname)/../../src"
15+
. ${AWSECURE_CLI_SRC_DIRECTORY}/common/logging.shinc
16+
fi
17+
18+
if [[ ! -z "${ZSH_NAME}" ]]; then
19+
declare -lr AWSECURE_CLI_SH_INTERPRETER="zsh"
20+
elif [[ ! -z "${BASH}" ]]; then
21+
declare -lr AWSECURE_CLI_SH_INTERPRETER="bash"
22+
else
23+
awsecure_cli_log_error "SH Interpreter not supported or not defined"
24+
fi
25+
26+
function awsecure_cli_auto_get_first_mfa_device() {
27+
awsecure_cli_get_user
28+
${AWSECURE_CLI_AWS_BIN_FILEPATH} iam list-mfa-devices --user-name "${AWSECURE_CLI_USERNAME}" | jq -r '.MFADevices[0].SerialNumber'
29+
}
30+
31+
function awsecure_cli_get_user() {
32+
local -r AWSECURE_CLI_USER_ARN="$(${AWSECURE_CLI_AWS_BIN_FILEPATH} sts get-caller-identity | jq -r '.Arn')"
33+
local -rg AWSECURE_CLI_USERNAME="${AWSECURE_CLI_USER_ARN//*\/}"
34+
}
35+
36+
function awsecure_cli_set_mfa_session_token() {
37+
local -r AWSECURE_CLI_MFA_TOKEN_FILE=~/.awsecure-cli-mfa-session-token-${AWS_PROFILE// /}
38+
local -i AWSECURE_CLI_MFA_TOKEN_DURATION="${AWSECURE_CLI_MFA_TOKEN_DURATION:-"900"}"
39+
40+
awsecure_cli_mfa_check_session_age
41+
case "${AWSECURE_CLI_MFA_CHECK_SESSION_AGE// /}" in
42+
older|none)
43+
awsecure_cli_log_info "Your MFA session token is older than ${AWSECURE_CLI_MFA_TOKEN_DURATION}, renewing it."
44+
echo "Please, inform your MFA code (e.g. 123 456): "
45+
read -t 30 -r AWSECURE_CLI_MFA_CODE_TMP
46+
local -r AWSECURE_CLI_MFA_CODE="${AWSECURE_CLI_MFA_CODE_TMP// /}"
47+
48+
local -r AWS_SESSION_TOKEN="$(${AWSECURE_CLI_AWS_BIN_FILEPATH} sts get-session-token --serial-number "${AWSECURE_CLI_MFA_AWS_ARN}" --token-code ${AWSECURE_CLI_MFA_CODE} --duration-second "${AWSECURE_CLI_MFA_TOKEN_DURATION}" | jq -r '.Credentials.SessionToken')"
49+
: "${AWS_SESSION_TOKEN:?"Variable not set or empty"}"
50+
51+
rm -f ${AWSECURE_CLI_MFA_TOKEN_FILE}
52+
echo "AWS_SESSION_TOKEN=${AWS_SESSION_TOKEN}" > ${AWSECURE_CLI_MFA_TOKEN_FILE}
53+
chmod 0400 ${AWSECURE_CLI_MFA_TOKEN_FILE}
54+
;;
55+
newer)
56+
awsecure_cli_log_info "Your MFA session token is newer than ${AWSECURE_CLI_MFA_TOKEN_DURATION}, reusing it."
57+
. ${AWSECURE_CLI_MFA_TOKEN_FILE}
58+
;;
59+
esac
60+
}
61+
62+
function awsecure_cli_mfa_check_session_age() {
63+
[[ -f ${AWSECURE_CLI_MFA_TOKEN_FILE} ]] && local -lrg AWSECURE_CLI_MFA_CHECK_SESSION_AGE="$(find ${AWSECURE_CLI_MFA_TOKEN_FILE} -type f -newermt "-${AWSECURE_CLI_MFA_TOKEN_DURATION} seconds" | grep . > /dev/null 2>&1 && echo "newer" || echo "older")" || local -lrg AWSECURE_CLI_MFA_CHECK_SESSION_AGE="none"
64+
}
65+
66+
function awsecure_cli_mfa_session_token() {
67+
awsecure_cli_mfa_check_session_age
68+
}
69+
70+
function awsecure_cli_add_mfa_check() {
71+
local -r AWSECURE_CLI_MFA_AUTO_GET_DEVICE="${AWSECURE_CLI_MFA_AUTO_GET_DEVICE:-true}"
72+
73+
case "${AWSECURE_CLI_MFA_AUTO_GET_DEVICE// /}-${AWSECURE_CLI_MFA_AWS_ARN// /}" in
74+
true-*)
75+
local -r AWSECURE_CLI_MFA_AWS_ARN="$(awsecure_cli_auto_get_first_mfa_device)"
76+
awsecure_cli_set_mfa_session_token
77+
;;
78+
false-arn:aws:iam*)
79+
awsecure_cli_set_mfa_session_token
80+
;;
81+
*)
82+
awsecure_cli_log_error "Invalid values for AWSECURE_CLI_MFA_AUTO_GET_DEVICE and/or AWSECURE_CLI_MFA_AWS_ARN"
83+
;;
84+
esac
85+
}
86+
awsecure_cli_add_mfa_check

src/common/logging.shinc

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
declare -lr AWSECURE_CLI_LOG_FILEPATH=/tmp/awsecure-cli.log.$(date +'%Y%m%d')
2-
[[ ! -f "${AWSECURE_CLI_LOG_FILEPATH// /}" ]] && mktemp ${AWSECURE_CLI_LOG_FILEPATH} &> /dev/null
32
declare -l AWSECURE_CLI_MUTED="${AWSECURE_CLI_MUTED:-"false"}"
43
declare -l AWSECURE_CLI_LOG_TO_FILE="${AWSECURE_CLI_LOG_TO_FILE:-"false"}"
54

5+
function awsecure_cli_create_log_file () {
6+
touch ${AWSECURE_CLI_LOG_FILEPATH}
7+
}
8+
69
function awsecure_cli_log_info() {
710
case "${AWSECURE_CLI_MUTED// /}-${AWSECURE_CLI_LOG_TO_FILE// /}" in
11+
true-true)
12+
awsecure_cli_create_log_file
13+
echo "$(date +'%Y-%m-%d %T')Z INFO ${@}" ${AWSECURE_CLI_LOG_TO_FILE_CMD} >> ${AWSECURE_CLI_LOG_FILEPATH} 2>&1
14+
;;
815
false-true)
16+
awsecure_cli_create_log_file
917
echo "$(date +'%Y-%m-%d %T')Z INFO ${@}" ${AWSECURE_CLI_LOG_TO_FILE_CMD} | tee -a ${AWSECURE_CLI_LOG_FILEPATH}
1018
;;
1119
false-false)
@@ -16,7 +24,12 @@ function awsecure_cli_log_info() {
1624

1725
function awsecure_cli_log_error() {
1826
case "${AWSECURE_CLI_MUTED// /}-${AWSECURE_CLI_LOG_TO_FILE// /}" in
27+
true-true)
28+
awsecure_cli_create_log_file
29+
echo "$(date +'%Y-%m-%d %T')Z ERROR ${@}" ${AWSECURE_CLI_LOG_TO_FILE_CMD} >> ${AWSECURE_CLI_LOG_FILEPATH} 2>&1
30+
;;
1931
false-true)
32+
awsecure_cli_create_log_file
2033
echo "$(date +'%Y-%m-%d %T')Z ERROR ${@}" ${AWSECURE_CLI_LOG_TO_FILE_CMD} | tee -a ${AWSECURE_CLI_LOG_FILEPATH}
2134
;;
2235
false-false)

src/common/wrapper.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,20 @@ awsecure_cli_log_info "Getting the AWS profile in use"
3737
declare -rxg AWS_PROFILE="$(awsecure_cli_get_aws_profile_set "${@}")"
3838
awsecure_cli_log_info "The AWS profile in use is the ${AWS_PROFILE}"
3939

40+
function awsecure_cli_add_mfa_invoke() {
41+
local -l AWSECURE_CLI_MFA_ON="${AWSECURE_CLI_MFA_ON:-"false"}"
42+
case "${AWSECURE_CLI_MFA_ON// /}" in
43+
true)
44+
awsecure_cli_log_info "Setting up the AWS_SESSION_TOKEN."
45+
. ${AWSECURE_CLI_SRC_DIRECTORY}/common/add_mfa_on_cli.sh
46+
;;
47+
*)
48+
awsecure_cli_log_info "The AWSECURE_CLI_MFA_ON is not set to true. Skiping adding AWS MFA code to this session"
49+
;;
50+
esac
51+
}
52+
awsecure_cli_add_mfa_invoke
53+
4054
function awsecure_cli_autorotate_invoke() {
4155
local -l AWSECURE_CLI_AUTOROTATE_AWS_ACCESS_KEYS="${AWSECURE_CLI_AUTOROTATE_AWS_ACCESS_KEYS:-"true"}"
4256
case "${AWSECURE_CLI_AUTOROTATE_AWS_ACCESS_KEYS// /}" in

0 commit comments

Comments
 (0)