binbashar / le-docker-leverage-toolbox

Docker image for https://github.com/binbashar/leverage
3 stars 0 forks source link

Bug | date failing with new date format returned by AWS #19

Closed juanmatias closed 1 year ago

juanmatias commented 2 years ago

Describe the Bug

Alpine-based Terraform image used as the base for leverage-toolbox, contains a binary for date that doesn't include the -d flag.

Since AWS seems to be sending now MFA credentials in this format:

2022-11-01T10:00:00-00:00
# previous version seemed to be
2022-11-01T10:00:00Z

Inside the scripts, Toolbox is getting rid of the T and the Z generating this command:

date -d '2022-11-01 10:00:00' 

With the new format, the command is:

date -d '2022-11-01 10:00:00-00:00'

...raising an error.

Expected Behavior

Toolbox managing both cases.

Steps to Reproduce

Steps to reproduce the behavior:

  1. Run this:
    ╰─ docker run -it --rm --entrypoint /bin/sh binbash/leverage-toolbox:1.2.7-latest  -c 'DATE="2022-11-01T10:00:00+00:00" && DATE=$(echo $DATE | sed "s/T/ /" | sed "s/Z//") && echo $DATE && date -d "$DATE" +"%s"'
    2022-11-01 10:00:00+00:00
    date: invalid date '2022-11-01 10:00:00+00:00'
  2. See the error

Screenshots

n/a

Environment (please complete the following information):

Containers. The previous Leverage CLI version used terraform:1.1.9 as a base. The new toolbox uses terraform:1.2.7 and it seems to be some differences:

╰─ docker run -it --rm --entrypoint /bin/sh hashicorp/terraform:1.1.9  -c 'DATE="2022-11-01T10:00:00+00:00" && DATE=$(echo $DATE | sed "s/T/ /" | sed "s/Z//") && echo $DATE && date -d "$DATE" +"%s"'                        
2022-11-01 10:00:00+00:00
1667296800

╰─ docker run -it --rm --entrypoint /bin/sh hashicorp/terraform:1.2.7  -c 'DATE="2022-11-01T10:00:00+00:00" && DATE=$(echo $DATE | sed "s/T/ /" | sed "s/Z//") && echo $DATE && date -d "$DATE" +"%s"'                              
2022-11-01 10:00:00+00:00
date: invalid date '2022-11-01 10:00:00+00:00'

Additional Context

Solution

Removing the extra digits will solve the issue partially. (supposing always it will be UTC+0 timezone)

E.g.:

╰─ docker run -it --rm --entrypoint /bin/sh hashicorp/terraform:1.2.7  -c 'DATE="2022-11-01T10:00:00+00:00" && DATE=$(echo $DATE | sed "s/T/ /" | sed -E "s/(Z|\+[0-9]{2}:[0-9]{2})$//") && echo $DATE && date -d "$DATE" +"%s"' 
2022-11-01 10:00:00
1667296800

Regarding time zones

Due to the Time Zone issue (do we need to pay attention to the TZ or AWS will always send Zulu Time?) I'll keep this issue opened for further discussion.

Wider solution

As per this post, this scenario can be handled by date by installing coreutils:

apk add coreutils

Evidence:

╰─ pipenv run leverage tf shell
/leverage-immersion # DATE="2022-11-01T10:00:00+00:00" && DATE=$(echo $DATE | sed "s/T/ /" | sed "s/Z//") && echo $DATE && date -d "$DATE" +"%s"
2022-11-01 10:00:00+00:00
date: invalid date '2022-11-01 10:00:00+00:00'
/leverage-immersion # apk add coreutils
fetch https://dl-cdn.alpinelinux.org/alpine/v3.16/main/x86_64/APKINDEX.tar.gz
fetch https://dl-cdn.alpinelinux.org/alpine/v3.16/community/x86_64/APKINDEX.tar.gz
(1/6) Installing libacl (2.3.1-r0)
(2/6) Installing libattr (2.5.1-r1)
(3/6) Installing gmp (6.2.1-r2)
(4/6) Installing skalibs (2.11.2.0-r0)
(5/6) Installing utmps-libs (0.1.2.0-r0)
(6/6) Installing coreutils (9.1-r0)
Executing busybox-1.35.0-r17.trigger
Executing glibc-bin-2.34-r0.trigger
OK: 101 MiB in 60 packages
/leverage-immersion # DATE="2022-11-01T10:00:00+00:00" && DATE=$(echo $DATE | sed "s/T/ /" | sed "s/Z//") && echo $DATE && date -d "$DATE" +"%s"
2022-11-01 10:00:00+00:00
1667296800
juanmatias commented 2 years ago

Solution applied with https://github.com/binbashar/le-docker-leverage-toolbox/pull/20

juanmatias commented 1 year ago

Done