45 lines
1.0 KiB
Markdown
45 lines
1.0 KiB
Markdown
|
# env.py
|
||
|
|
||
|
This describes how to use secrets in an environment.
|
||
|
|
||
|
It's nice to be able to share your source code without the passwords.
|
||
|
|
||
|
This can be done by configuring the passwords on your environment.
|
||
|
|
||
|
Just to prevent your password to show up in some log or so it's base64 encoded and uses the key 'SECRET' instead of password.
|
||
|
|
||
|
This is what I have made for that:
|
||
|
|
||
|
## Usage in other projects
|
||
|
```
|
||
|
import env
|
||
|
password = env.secret
|
||
|
|
||
|
# password now contains the base64 encoded secret.
|
||
|
```
|
||
|
|
||
|
## Create a secret variable
|
||
|
1. execute `python3 env.py`
|
||
|
2. enter password when asked
|
||
|
3. copy this source in your `~/.bashrc`
|
||
|
```bash
|
||
|
export SECRET="your-base64-encoded-password-made-with-env.py"
|
||
|
```
|
||
|
## Source
|
||
|
```python
|
||
|
# (C) Retoor
|
||
|
|
||
|
secret = None
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
import base64
|
||
|
secret = input("Type password: ")
|
||
|
print(base64.b64encode(secret.encode()).decode())
|
||
|
else:
|
||
|
import os
|
||
|
import base64
|
||
|
try:
|
||
|
secret = base64.b64decode(os.getenv("SECRET","").encode()).decode()
|
||
|
except:
|
||
|
pass
|
||
|
```
|