Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading in quoted environment variable new line

Tags:

python

I need to read in an env variable which must be quoted when exported

export ENV_QUOTES="cat\nhat" # This is actually a rsa private key
export ENV_NO_QUOTES=cat\nhat

When I read in the quoted env var it does not register the escaped newline:

import os
env_var_no_quotes = os.environ['ENV_NO_QUOTES']
env_var = os.environ['ENV_QUOTES']
for idx, v in enumerate(env_var_no_quotes):
    print(idx, v, env_var[idx])

Output:

0 c c
1 a a
2 t t
3 n \
4 h n
5 a h
6 t a

How do I get python to recognize newlines for env vars that are quoted? The actual output print the private keys looks like this:

# Non quoted
-----BEGIN PRIVATE KEY-----
ffaffdsafsdeeekqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCltThZlt88aAn8
f2MlQsV4Nv9TEhBj8WUypfGc148FedpTRraNyE3f9bZ4u4MJC34vI9zuqf5hNrX+
+w+eYrPJwbS4CvA3Ww2znjCfKK4u3n3P522LTGNxAoGBAK5AA0twCSifwIPIlIsZ
psGtFIEJpc6sJ4+LgVzTW/5EwreMHMnx3tQTaVi6pVIJTqjaMJp5SAAUlPrl5zZM
gsVgtwyXMMqlKNt9wMdBH+ir7oJsppC+DjIRbpCa8U6nEG4Wij5qVeRJ8DVV8NAM
C5Tc+6qgzVZRaXXkCStZA/PK
    -----END PRIVATE KEY-----

    # Quoted
-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCltThZlt88aAn8\nf2MlQsV4Nv9TEhBj8WUypfGc148FedpTRraNyE3f9bZ4u4MJC34vI9zuqf5hNrX+\nW+UfXJkPLfemupraI5sTWyFCwX7ZYOd5I7vyRoqSii4MHCXlg8zWySrjwirwou87\nn18zSyA30fu3kuHBDlHFnRVw+Ag+M/XjesRcz5Q+KQ9o/MnpcZcYQ7sZU5GntqCw\n3ZNgb68Cla87jbhvlkjfdN77IcLJypuwCMZeyHeQYNtgxLyE0o2Te5ddAjvD59uS\nML69iuQqpvPjpuBgZnReKX3oZusU/o6qgzVZRaXXkCStZA/PK\n-----END PRIVATE KEY-----\n
like image 436
newToScala Avatar asked Jan 28 '23 18:01

newToScala


1 Answers

If you can't change the shell script assigning your input to correctly contain a newline literal (which is the better approach!), you can simply use string.sub() in Python to replace r'\n' (which is what the variable actually contains) with '\n' (which is what you want):

env_var = os.environ['ENV_QUOTES'].replace(r'\n', '\n')

That said, on the shell side, consider:

# in bash or ksh, not POSIX sh
ENV_QUOTES=$'cat\nhat'    # literal newline, not '\' followed by 'n'

...or...

# in any POSIX shell
orig_shell_var='cat\nhat'                 # two characters, '\' then 'n'
ENV_QUOTES=$(printf %b "$orig_shell_var") # evaluated as format string, now has newline
export ENV_QUOTES

...or...

# in any POSIX shell
ENV_QUOTES='cat
hat'
like image 135
Charles Duffy Avatar answered Jan 31 '23 09:01

Charles Duffy