aws-secrets icon indicating copy to clipboard operation
aws-secrets copied to clipboard

base64 varies on different platforms

Open rzane opened this issue 8 years ago • 10 comments

The base64 binary that ships with alpine linux considers --decode an invalid option. Instead, it prefers the short flag: -d.

Simply changing to -d would make the script incompatible with Mac OS, which prefers --decode or -D.

rzane avatar Mar 06 '17 19:03 rzane

Since awscli is already a prerequisite, I suppose we can assume python is installed, so perhaps this could be replaced with python -m base64 -d

bduggan avatar Mar 07 '17 12:03 bduggan

Also, can we be certain that every OS will have base64?

nicholaides avatar Mar 07 '17 17:03 nicholaides

@nicholaides It's a builtin: https://docs.python.org/3/library/base64.html

di avatar Mar 07 '17 17:03 di

Sorry-- I meant, the shell command/executable base64. I was wondering how certain we can be that it's available on every OS. Brian's point that we can rely on Python being installed makes Python seem like a good solution.

nicholaides avatar Mar 07 '17 17:03 nicholaides

It's part of gnu coreutils, so it's on any GNU/linux based distro (just as cat is). Another option would be to require coreutils on OS/X to get the gnu version (brew install coreutils) e.g. on an ubuntu box:

$ dpkg -S /bin/cat
coreutils: /bin/cat
$ dpkg -S /usr/bin/base64
coreutils: /usr/bin/base64

I wouldn't recommend python for this if we didn't already have awscli as a requirement.

bduggan avatar Mar 07 '17 21:03 bduggan

What's also interesting is that OS/X (the only outlier) lists lowercase d in the first line of the help message, so this could be called a bug in the OS/X base64

$ /usr/bin/base64 --help
Usage:  /usr/bin/base64 [-dhvD] [-b num] [-i in_file] [-o out_file]
  -h, --help     display this message
  -D, --decode   decodes input
  -b, --break    break encoded string into num character lines
  -i, --input    input file (default: "-" for stdin)
  -o, --output   output file (default: "-" for stdout)

bduggan avatar Mar 07 '17 22:03 bduggan

On second thought, maybe we can just skip base64 encoding entirely.

bduggan avatar Mar 10 '17 15:03 bduggan

Newer versions of base64 on OS/X supports both -D and -d for decoding:

Usage:	base64 [-hvDd] [-b num] [-i in_file] [-o out_file]
  -h, --help     display this message
  -Dd, --decode   decodes input
  -b, --break    break encoded string into num character lines
  -i, --input    input file (default: "-" for stdin)
  -o, --output   output file (default: "-" for stdout)

fabriziofortino avatar Sep 29 '21 12:09 fabriziofortino

I can attest @fabriziofortino

Even on Big Sur man base64

     -d
     -D
     --decode             Decode incoming Base64 stream into binary data.

om-ha avatar Nov 01 '21 21:11 om-ha

This issue just bit me today on Ubuntu 22.04: base64: invalid option -- 'D'

The only supported options are:

base64 --help
Usage: base64 [OPTION]... [FILE]
Base64 encode or decode FILE, or standard input, to standard output.

With no FILE, or when FILE is -, read standard input.

Mandatory arguments to long options are mandatory for short options too.
  -d, --decode          decode data
  -i, --ignore-garbage  when decoding, ignore non-alphabet characters
  -w, --wrap=COLS       wrap encoded lines after COLS character (default 76).
                          Use 0 to disable line wrapping

      --help     display this help and exit
      --version  output version information and exit

The data are encoded as described for the base64 alphabet in RFC 4648.
When decoding, the input may contain newlines in addition to the bytes of
the formal base64 alphabet.  Use --ignore-garbage to attempt to recover
from any other non-alphabet bytes in the encoded stream.

GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Full documentation <https://www.gnu.org/software/coreutils/base64>
or available locally via: info '(coreutils) base64 invocation'

coding-red-panda avatar Sep 30 '22 05:09 coding-red-panda