Skip to content

Instantly share code, notes, and snippets.

@muhqu
Created April 9, 2015 20:52
Show Gist options
  • Save muhqu/d688a6dbd6b93e3808e5 to your computer and use it in GitHub Desktop.
Save muhqu/d688a6dbd6b93e3808e5 to your computer and use it in GitHub Desktop.
Mask ENV Vars
#!/bin/bash
#
# The MIT License (MIT)
#
# Copyright (c) 2015 Mathias Leppich <[email protected]>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
SELF="$0"
usage() {
cat <<-USAGE
Usage: ${SELF} "ENV_VAR1 ENV_VAR2 .." PROGRAM [params]
Example:
# asume some ENV vars that should not be printed to stdout/stderr
export ACCESS_TOKEN="I6k6oHehDyENhjvyCJoURypXu+sHR2Hc+0lSnt7/AoGATKCYg5sTOk10pEqV1438"
export SECURE_KEY="96O6iCBrvtyM7J3/Lj44jW4mrJdF+IMKKJ4E"
$SELF "ACCESS_TOKEN SECURE_KEY" /bin/bash <<SCRIPT
echo "Using ACCESS_TOKEN: \$ACCESS_TOKEN" # stdout example
echo >&2 "Error: The SECURE_KEY is invalid: \$SECURE_KEY" # stderr example
SCRIPT
> Using ACCESS_TOKEN: ********************************
> Error: The SECURE_KEY is invalid: ********************************
USAGE
}
maskEnvs() {
python -c '
import os,sys,re;
p=re.compile("|".join([re.escape(os.getenv(arg,"*****")) for arg in os.getenv("MASK_ENVS","").split(" ")]),re.M);
[sys.stdout.write(p.sub(lambda x:min(len(x.group(0)),32)*"*",line)) for line in iter(sys.stdin.readline,"")];
';
}
maskEnvsWrap() {
local ME="$1"; shift
"$@" 2> >(MASK_ENVS="$ME" maskEnvs >&2) > >(MASK_ENVS="$ME" maskEnvs)
}
if [ -z "$1" ]; then
usage
exit 1
else
maskEnvsWrap "$@"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment