Returning Environmental Variables - django

I am working on Corey Schafer's blog tutorial and now setting up 'password reset email' using my gmail account as an email client. I want to store my email and password in an environmental variable but when I verify that the information was properly stored it returns none
In the terminal I used (nano .bash_profie)
export EMAIL_USER='email#gmail.com'
export EMAIL_PASS='password'
Then I verified
import os
user = os.environ.get('EMAIL_USER')
password = os.environ.get('EMAIL_PASS')
print(user)
print(password)
Other commenters on the tutorial suggested...
-restart editor and terminal
-Use .zshrc instead of .bash_profile
I have tried these other recommendations but still it returns none. I am still new to programming so any suggestion is appreciated.

without '
export EMAIL_USER=email#gmail.com
export EMAIL_PASS=password

Set env variables like:
os.environ['EMAIL_USER']
os.environ['EMAIL_PASSWORD']
and then use with:
os.environ.get('EMAIL_USER')
Then could you try sourcing your .bash_profie and then run your python file, you must run both commands within the same shell.
source .bash_profie
python yourpythonfile

If you are using ubuntu, try nano .bashrc instead of nano .bash_profile then follow other instructions as you described.

Related

Invalid Password when logging into Jupyter in AWS

I'm just starting to work with AWS and I'm working on getting Anaconda set up on my AWS EC2 Instance. The entire set up process worked well but i'm getting stuck with inputting my password in jupyter.
FYI, here is the tutorial I have been using: https://chrisalbon.com/jupyter/run_project_jupyter_on_amazon_ec2.html
I manually changed the password in the ipython config file:
Even when inputting testing123 I get an invalid error message.
Have you guys dealt with this? Any help would be much appreciated.
I think you have understood the whole process but got confused in password section so I am only gonna point out what you did wrong.
Assuming You followed below blog as you said:
https://chrisalbon.com/jupyter/run_project_jupyter_on_amazon_ec2.html
In the section
Create a password for jupyter notebook
1 ipython
2 from IPython.lib import passwd
3 passwd()
4 'sha1:98ff0e580111:12798c72623a6eecd54b51c006b1050f0ac1a62d'
5 exit
I have added the line numbers for the sake of better explanation:
when you write passwd() which is in 3rd line
The AWS expects you to input a password and again ask you to verify the password you have given and then it gives you a SHA version of your password. like on 4th line: 'sha1:98ff0e580111:12798c72623a6eecd54b51c006b1050f0ac1a62d'
Now you have to copy this and keep it with yourself to paste it later in ipython config file.
so you should use below lines in your config to make it work for you:
c = get_config()
# Kernel config
c.IPKernelApp.pylab = 'inline' # if you want plotting support always in your notebook
# Notebook config
c.NotebookApp.certfile = u'/home/ubuntu/certs/mycert.pem' #location of your certificate file
c.NotebookApp.ip = '*'
c.NotebookApp.open_browser = False #so that the ipython notebook does not opens up a browser by default
c.NotebookApp.password = u'sha1:98ff0e580111:12798c72623a6eecd54b51c006b1050f0ac1a62d' #the encrypted password we generated above
# It is a good idea to put it on a known, fixed port
c.NotebookApp.port = 8888

New ubuntu user gets "mkvirtualenv: command not found" error

I was able to run a test Django application after installing all the required software.
I was able to use mkvirtualenv and create two test apps that worked as required.
I then decided to create another user in Ubuntu. This user does not possess Sudo privileges because I wanted to secure the environment.
With this newly created user I get an error stating "mkvirtualenv: command not found".
My bashrc file has the following commits:-
export WORKON_HOME=$HOME/.virtualenvs
export PATH=/usr/local/bin:$PATH
export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python
export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/bin/virtualenv
source /usr/local/bin/virtualenvwrapper.sh
Running echo $WORKON_HOME results in the following:-
/home/ubuntu/.virtualenvs
I'm not particularly sure what I need to do to have the ability to use mkvirtualenv with the new user.
Any help is appreciated.
Thanks in advance!

Rails doesn't pick up SECRET_KEY_BASE environment variable

Here's what I'm working with right now:
Ubuntu Trusty 14.04
Rails 4.2.6
Ruby 2.2.3
Passenger
Nginx
When I try to visit the IP I get this message:
Incomplete response received from application
When I look at nginx/error.log I see:
Missing `secret_token` and `secret_key_base` for 'production' environment, set these values in `config/secrets.yml`
On the server I did:
RAILS_ENV=production bundle exec rake secret
I placed that result into each of these files for good measure:
~/.bashrc
~/.bash_profile
~/.profile
/app/shared/config/local_env.yml
For all shell scripts the format is:
export SECRET_KEY_BASE="[key]"
For the local_env.yml I used just:
SECRET_KEY_BASE="[key]"
I've also tried entering it without quotation marks.
I've restarted the server each time I made a change. No cigar.
What else might be the issue?
-- UPDATE
I've even added the secret key to the secrets.yml file directly. So now I'm thinking my issue is either something to do with passenger/nginx or with a typo somewhere.
It is more likely that the environment variables are not actually set rather than Rails is not picking them up. You're raking secrets, which I don't do. I set them up manually in the Unix etc/environment, and do not check any secrets into source control. But the following are a few steps that should help you either resolve or hone in on the problem.
On your Ubuntu server for system wide environment variables
1- $env
Look for your SECRET_TOKEN and SECRET_KEY_BASE. The error tells you that these are not set, this is just a technique to check env. (RAILS_ENV will also be shown in the list if it is set.)
2- $sudo nano /etc/environment
Add the following lines -- use your actual values between double quotes. Do not use a [key] or any programmatic replacement.
export SECRET_TOKEN="T99ABC..."
export SECRET_KEY_BASE="99ABC..."
3- $logout / $login to reload environment vars
4- $env - Check the environment again
Look for your SECRET_TOKEN and SECRET_KEY_BASE to be set.
5- Try deploying again. If it fails, check the environment vars using $env again. It will tell you if something in your deploy is smashing your SECRET_* env vars.

Change jupyter notebook server password

I would like to change my jupyter notebook server's password to something else. I see on Jupyter's documentation (http://jupyter-notebook.readthedocs.org/en/latest/public_server.html) how to prepare a password for ostensibly a new server but not how to change an existing one.
Can I simply:
Generate a new SHA hash for a password as the documentation above specifies
Replace the password in the PWDFILE environment variable (located in jupyter_notebook_config.py)
Restart the jupyter server
Basically, I'm asking because I don't want to confuse and/or mess up the authentication system by just changing things.
From version 5.0, you can easily change current password with jupyter notebook password command.
Note : I tested it on windows environment.
Start -> Anaconda Powershell Prompt
It will display you the prompt like below:
(base) PS C:\yourName >
Enter below command :
(base) PS C:\yourName > jupyter notebook password
It will ask you for new password
Currently its 2022 and the jupyter notebook password still works
Reposting as an answer:
The process to change the password should be just the same as setting it in the first place. Jupyter hashes the password you enter, and compares it with the hash it loaded from the config file. It doesn't copy the hash to any other persistent storage (though it does store it in memory, so you will need to restart the notebook server).
I'm not sure how environment variables are involved - I don't think the instructions use any for the password.

Django: environment variable for SECRET_KEY not working

I have SECRET_KEY = os.environ['SECRET_KEY'] in my prod.py, and SECRET_KEY=secret_string in my .bashrc
This will cause 502 error but if I set SECRET_KEY="secret_string", it is working. How can I use environment variable to do this?
I'm starting gunicorn via sudo service gunicorn restart and I have a upstart script.
Here is the output of cat /proc/<PID>/environ:
PATH=/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/sbin:/sbin:/bin^#TERM=linux^#UPSTART_JOB=gunicorn^#UPSTART_INSTANCE=^#
You need to do:
export SECRET_KEY=secret_string
in your .bashrc. If you just do:
SECRET_KEY=secret_string
It's only available in current process, but when you run django server/shell, the subprocess has no idea of this variable. export make the variable available in subprocesses as well.
.bashrc only affects bash login shells. Init scripts are not affected in any way by it.
You should copy the export SECRET_KEY=... line to the top of your init script.