Skip to content

Commit

Permalink
Issue 8704: improve env config handling (apache#8709)
Browse files Browse the repository at this point in the history
There are three issues addressed in this PR.
1) script files that contain lines other than comments and value
assignments cause the script to abort not re-writing the file at all.
These lines should be tolerated if they cannot be parsed as value
assignments and flow through to the edited results.

2) Redaction of passwords in environment variables. Environment
variables with a name containing password will automatically be
redacted and not printed in the log messages.

3) values that contain spaces as in many java args that contain
stringed together options like "-Xmx ... -Xmn ..." are read from the
environment without the quotes. These quotes need to be added back
when writing back to the shell script to ensure that these values are
processed as a group.
  • Loading branch information
klwilson227 authored Dec 3, 2020
1 parent 85f3ff4 commit c02e880
Showing 1 changed file with 33 additions and 6 deletions.
39 changes: 33 additions & 6 deletions docker/pulsar/scripts/apply-config-from-env.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,30 @@
if not line or line.startswith('#'):
continue

k,v = line.split('=', 1)
keys[k] = len(lines) - 1
try:
k,v = line.split('=', 1)
keys[k] = len(lines) - 1
except:
print("[%s] skip Processing %s" % (conf_filename, line))

# Update values from Env
for k in sorted(os.environ.keys()):
v = os.environ[k]
v = os.environ[k].strip()

# Quote the value if it contains a space.
if v.find(" ") >= 0:
v = '\"%s\"' % v

# Hide the value in logs if is password.
if "password" in k:
displayValue = "********"
else:
displayValue = v

if k.startswith(PF_ENV_PREFIX):
k = k[len(PF_ENV_PREFIX):]
if k in keys:
print('[%s] Applying config %s = %s' % (conf_filename, k, v))
print('[%s] Applying config %s = %s' % (conf_filename, k, displayValue))
idx = keys[k]
lines[idx] = '%s=%s\n' % (k, v)

Expand All @@ -66,16 +80,29 @@
v = os.environ[k]
if not k.startswith(PF_ENV_PREFIX):
continue

# Quote the value if it contains a space.
if v.find(" ") >= 0:
v = '\"%s\"' % v

# Hide the value in logs if is password.
if "password" in k:
displayValue = "********"
else:
displayValue = v

k = k[len(PF_ENV_PREFIX):]
if k not in keys:
print('[%s] Adding config %s = %s' % (conf_filename, k, v))
print('[%s] Adding config %s = %s' % (conf_filename, k, displayValue))
lines.append('%s=%s\n' % (k, v))
else:
print('[%s] Updating config %s = %s' %(conf_filename, k, v))
print('[%s] Updating config %s = %s' % (conf_filename, k, displayValue))
lines[keys[k]] = '%s=%s\n' % (k, v)


# Store back the updated config in the same file
f = open(conf_filename, 'w')
for line in lines:
f.write(line)
f.close()

0 comments on commit c02e880

Please sign in to comment.