Add Key to Dokku - digital-ocean

I am trying to add a key like this one:
-----BEGIN PRIVATE KEY-----
MIIEuwIBADANBgkqhkiG9w0BAQEFAASCBKUwggShAgEAAoIBAQCslQfgfW3L+/Kx
beM8LnJ2ocbX/YdS43ry69e5nmK2RM/GuJqRMZ0JrMfEtQ0FlKGxS9+cBhOV6Yt3
fCx8LcgWZY5mhpSrB4r90d5q93HrR/uVGGuu0QOf5+eq+YK2vUVCIer8Yxpg+wGf
4DyW/aWo6qriy7sPApAtuqiAkjLwKENVe9qsrXsNgG9+YTpgKXhQ4abTbSN55K0W
6TBNqA8MyKe3buPQ2GspjieotyZVGGBXASMKPLRuhuOEuRl2GbenYigfMoth5t3Q
8xQhphR88gcdiTdb1mvesB23DfHb5pPR+14hebH1AjbDEpxYSiNMGcQUa3yJ8b3H
0Z/6xIllAgMBAAECgf9Yffygmg+GKFBBrIJUsV3oB+cGp3gwI3asAP8XJup9NsT4
RFcom/0li0qrSc+WgQb3DH67fsXyIlhXQ/ZGCL5sVWNkPggQsyD2vZf/VLR1pVYF
bHZvnxX6h/RMJJUqgBi0C/fY9pQxt37IDhWFhk9hlnkCsT4tcOMFrhMV/MK7wsIO
dz+6FnLgscBL6ocugwsO8vP/VI9rP7BdtYx8YNqG1qOeh3oQkP4/jU5p1FkCrNrS
NldVOnzdftfpZ//DVMcjMrMU6rlBX9ChN5weapbyLjlZ/2qlvGJky3I05ifFB55B
VrIfTx0X8kdnRR71hz9fQq1NFfSIKQPSwLC3OnECgYEA7oruyXl9rxYFDLEIdXEg
gFFHlcN9sZNejMxRM1Y/oZs5C1tYjavllP3pHfEsZWzjI4b/XwHHCxlicY0/ggyz
rK9NY3lzDpyNgpFLGr+afrIjaXFI5jp1vq+sDy07h3Nbj4oUUggOdy2CqFEc9KWY
jMZbdqnZHxAou25RVyP/brasdfgsuTZV3X+dGLI55Jvmu+aefaiBCKZFyXjTDH2l
od4bM9/BFYvUI5X8UV8QZocPCHwsefqfw7apj1WOIJGZ6Tjw5mn1xEaBXnAXiAvf
AXIf9BWlDo7WjmUl6NWNIf7qJTXD8vELce8mKh6jHaQ/ayyloeOceXUMz+E8a0n/
RUXTuvUCgYAnrXpcjtXGCr6aRtQmXMPQNDTZB2Rf5ihi+RVEmcMJyXzqk64eGoqc
hsOA1H3S/RKm9SyDDBOkl/tU8e6vXheA0MPpmOj3dDjhj/NTJq23B0B2qH48tVqi
diwDdj2hoSGItan9nxNw5WQHRT6wvt6qA9Bw432W+iQcURQ8j2NMcQKBgGzk6W8l
jLPsdeVXVD29xgmTfOoc+IMQKuX5Kd0VvWCuedO1NrEFiig9gGMHnWsbtvqwxCZJ
yz+eycWUVa2t8NVID1urKHzh4rP2ZtNuyiK0Zys8CCIIjlVq2WPXgMsH5YF8/4Gp
XgL9qGHRJAhbY7bfICy5CSNgBin+g7uy5l3RAoGBAIIUHR66LjzVSCsvhlafuYrK
k4mRpGKMyEknat9SWCrmDY+gqkLzTOQx/EKaTvWD41UV6LSGFIp0y3WICvl/VlON
uimrsFTLOByhIVDdgOy1kHgzgIMiGhVYXQe04hFUNrSq0KMcDQ5q73LrktL3wnMS
hDVAh9SSDAe+xULorSPK
-----END PRIVATE KEY-----
Don't worry it has been created for the example.
I want to add it as an env variable. But in this key, the newline is important, so I was looking around and found that if you search for the env file, you can modify it directly.
I did that but every time I restart the app, the file erase all my env vars and only keep some that dokku makes.
Could anyone tell me why, or how I could add this key to my env vars.
The file I am modifying is this one: nano /home/dokku/chatbot-test/ENV
Thank you,
Jonathan Prieto

Well after two days trying to resolve this, I finally found out how.
First, you have to put a \n at the end of each line to represente the newline.
Second, you have to use a website as this one: One Line Converter
After that, when you read your env var in your project, you have to use reggex to convert your \n into a real newline with this: .replace(/\\n/gm, '\n')
This resolved my issue perfectly.

Related

Regex to extract similar config blocks throughout ini file

I want to extract the hostname and node id values from the [ndb_mgmd] blocks only, from the mysql ini config file. There are other blocks in the file which are not needed.
Is it possible to extract just the hostname and node id values from all the [ndb_mgmd] blocks?
Otherwise, how can I extract those whole blocks using regex (without the [ndb_mgmd] headers preferably)?
Config file example below.
Random unwanted text in config file
[unwanted_block]
hostname=0.0.0.0
NodeId=2
[ndb_mgmd]
hostname=2.2.2.2
NodeId=1
[mysql_unwanted]
hostname=3.3.3.3
NodeId=6
[ndb_mgmd]
hostname=2.2.2.2
NodeId=4
randomconfig
Thanks
You can extract what you need with capture groups:
Hostnames: ^hostname=(\d\.\d\.\d\.\d)$
NodeIds: ^NodeId=(\d)$
I've played around and the following regex seems to work in playground (may not be the most ideal solution), however it's not working in ServiceNow where I'm trying to use.
/^\[ndb_mgmd\][\r\n]?hostname=(.*)[\r\n]?NodeId=(.*)/gm

How to robustly set Django secret key as environment variable

My Django project's secret key contains special characters such as #, #, ^, * etc. I'm trying to set this as an env variable at /etc/environment.
I include the following in the file:
export SECRET_KEY='zotpbek!*t_abkrfdpo!*^##plg6qt-x6(%dg)9p(qoj_r45y8'
I.e. I included single quotes around the string since it contains special characters (also prescribed by this SO post). I exit the file and do source /etc/environment. Next I type env in the terminal: SECRET__KEY correctly shows.
I log out and log back in. I type env again.
This time SECRET_KEY still shows, but is cut off beyond the # character. It's excluding everything beyond (and including) the # character.
How do I fix this issue? Trying with double quotes didn't alleviate anything either. My OS is Ubuntu 14.04 LTS.
p.s. I'm aware environment variables don't support access control; there's a bunch of reasons not to set the Django secret key as an env var. For the purposes of this ques, let's put that on the back burner.
This isn't a Django problem per se. According to this question Escape hash mark (#) in /etc/environment you can't use a "#" in /etc/environment.
I would recommend that you keep regenerating your secret key until you get one without #s -- that should fix the problem. Django Extensions has a command generate_secret_key for this. The side effect of changing the secret key is that current sessions will become invalid, that is, unless you are using it other places in your application.
Easiest way is to generate one using python3 in you linux terminal with following inline script:
python3 -c 'import random; print("".join([random.choice("abcdefghijklmnopqrstuvwxyz0123456789!#%^&*-_") for i in range(50)]))'
this will generate secret key without unsafe characters
As per the django-environ documention you can use unsafe characters in .env file.
https://django-environ.readthedocs.io/en/latest/index.html#tips
To use unsafe characters you have to encode with urllib.parse.encode before you set into .env file.
Example:- admin#123 = admin%28123

Change WiFi WPA2 passkey from a script

I'm using Raspbian Wheezy, but this is not a Raspberry Pi specific question.
I am developing a C application, which allows the user to change their WiFi Password.
I did not find a ready script/command for this, so I'm trying to use sed.
I pass the SSID name and new key to a bash script, and the key is replaced for the that ssid block within *etc/wpa_supplicant/wpa_supplicant.conf.*.
My application runs as root.
A sample block is shown below.
network={
ssid="MY_SSID"
scan_ssid=1
psk="my_ssid_psk"
}
so far I've tried the following (I've copied the wpa_supplicant.conf to wpa.txt for trying) :
(1) This tries to do the replacement between a range, started when my SSID is detected, and ending when the closing brace, followed by a newline.
SSID="TRIMURTI"
PSK="12345678"
sed -n "1 !H;1 h;$ {x;/ssid=\"${SSID}\"/,/}\n/ s/[[:space:]]*psk=.*\n/\n psk=\"${PSK}\"\n/p;}" wpa.txt
and
(2) This tries to 'remember' the matched pattern, and reproduce it in the output, but with the new key.
SSID="TRIMURTI"
PSK="12345678"
sed -n "1 !H; 1 h;$ {x;s/\(ssid=\"${SSID}\".*psk=\).*\n/\1\"${PSK}\"/p;}" wpa.txt
I have used hold & pattern buffers as the pattern can span multiple lines.
Above, the first example seems to ignore the range & replaces the 1st instance, and then truncates the rest of the file.
The second example replaces the last found psk value & truncates the file thereafter.
So I need help in correcting the above code, or trying a different solution.
If we can assume the fields will always be in a strict order where the ssid= goes before psk=, all you really need is
sed "/^[[:space:]]*ssid=\"$SSID\"[[:space:]]*$/,/}/s/^\([[:space:]]*psk=\"\)[^\"]*/\1$PSK/" wpa.txt
This is fairly brittle, though. If the input is malformed, or if the ssid goes after the psk in your block, it will break. The proper solution (which however is severe overkill in this case) is to have a proper parser for the input format; while that is in theory possible in sed, it would be much simpler if you were to swtich a higher-level language like Python or Perl, or even Awk.
The most useful case is update a password or other value in configuration is to utilize wpa_cli. E.g.:
wpa_cli -i "wlan0" set_network "0" psk "\"Some5Strong1Pass"\"
wpa_cli -i "wlan0" save_config
The save_config method is required to update cfg file: /etc/wpa_supplicant/wpa_supplicant.conf

Fabric: Inserting the hostname into a config file

I have a Fabric script I'm in the process of creating, and I want it to change the following line in a config file:
Hostname=localhost
I want it to include the actual host name, for example:
Hostname=mypc
I tried to do so with the following line:
fabric.contrib.files.sed(
'/etc/zabbix/zabbix_agentd.conf',
before='Hostname=localhost',
after='Hostname='"$HOSTNAME",
use_sudo=True, backup='')
But unfortunately, it won't work, and I've tried various combinations of quotes and double quotes. It will literally put "Hostname=$HOSTNAME" into the config file.
It won't work like that. The sed stuff is all inside single quotes. Just bind a name to the hostname value, and use Python interpolation to send it to the sed function. You might even be able to use the env.host setting. But an example of using this would look like this:
hostname = run("hostname -f")
fabric.contrib.files.sed(
'/etc/zabbix/zabbix_agentd.conf',
before='Hostname=localhost',
after='Hostname=%s' % hostname,
use_sudo=True, backup='')

Use regex with grep to filter data from the output of a verbose command

I am working with a cloud environment and there is a command that will display all available information about VMs running. here is an example of some of the lines that pertain to one VM.
RESERVATION r-6D0F464B 170506678332 GroupD
INSTANCE i-E9B444A9 emi-376642D8 999.99.999.999 88.888.88.888 running lock_key 0 c1.xlarge 2013-06-17T18:40:56.270Z cluster01 eki-E7E242A3 monitoring-disabled 999.99.999.999 88.888.88.888 ebs
I need to be able to pull the i-********, emi-********, both IP address, its status, the lock_key, the c1.xlarge, and the monitoring-disabled/enabled.
I have been able to pull the whole line with some super simple regex but all of this is well beyond me. If there is another easier method of grabbing this data any suggestions are welcome.
Let's go by parts. Best way I can think of is redirecting the output to a file, in unix-like environments you do it like:
cat your-command > filename.txt
Second, you need to read the file line by line, I would use a python script or a perl script if you know any of those, or whatever language fits you.
Third, you can get values two different ways:
Read columns by position, you can get colums with a regex like: [^\s]+
Write regular expressions for every specific column, so for IP you could have something like this: ([0-9]{1,3}\.){4} for monitoring monitoring-([^\s]+) and so on.
As long as the fields will always be in the same order, all you need to is split on whitespace.
Pseudocode (well, it's ruby, but hopefully you get the idea):
vms = {}
File.open('vm-info').readlines.each do |line|
fields = line.split('\s+')
field_map = {}
vm_name = fields[<index_of_vm_name>]
field_map['emi'] = fields[<index_of_emi>]
field_map['ip_address'] = fields[<index_of_ip_address]
.
.
.
vms[vm_name] = field_map
end
After this, vms will be initialized to contain information about each vm. You can simply print them all out at this point, or continue running data manipulation on them.