env-alias 0.3.7__tar.gz → 0.5.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- env_alias-0.5.1/PKG-INFO +133 -0
- env_alias-0.5.1/README.md +106 -0
- env_alias-0.5.1/pyproject.toml +101 -0
- env_alias-0.5.1/src/env_alias/__init__.py +16 -0
- env_alias-0.5.1/src/env_alias/exceptions.py +20 -0
- env_alias-0.5.1/src/env_alias/lib/definitions.py +61 -0
- env_alias-0.5.1/src/env_alias/lib/generator.py +146 -0
- env_alias-0.5.1/src/env_alias/lib/logger.py +110 -0
- env_alias-0.5.1/src/env_alias/lib/selector.py +84 -0
- env_alias-0.5.1/src/env_alias/lib/source.py +213 -0
- env_alias-0.5.1/src/env_alias/main.py +71 -0
- env_alias-0.5.1/src/env_alias/models/envalias_definition.py +79 -0
- env_alias-0.5.1/src/env_alias/models/sourced_content.py +13 -0
- env-alias-0.3.7/MANIFEST.in +0 -5
- env-alias-0.3.7/PKG-INFO +0 -81
- env-alias-0.3.7/README.md +0 -57
- env-alias-0.3.7/package.yml +0 -45
- env-alias-0.3.7/setup.cfg +0 -4
- env-alias-0.3.7/setup.py +0 -50
- env-alias-0.3.7/src/env_alias/__init__.py +0 -10
- env-alias-0.3.7/src/env_alias/cli/entrypoints.py +0 -72
- env-alias-0.3.7/src/env_alias/env_alias.py +0 -11
- env-alias-0.3.7/src/env_alias/env_alias_generator.py +0 -74
- env-alias-0.3.7/src/env_alias/exceptions/__init__.py +0 -2
- env-alias-0.3.7/src/env_alias/utils/config.py +0 -68
- env-alias-0.3.7/src/env_alias/utils/content.py +0 -70
- env-alias-0.3.7/src/env_alias/utils/logger.py +0 -111
- env-alias-0.3.7/src/env_alias/utils/selector.py +0 -81
- env-alias-0.3.7/src/env_alias.egg-info/PKG-INFO +0 -81
- env-alias-0.3.7/src/env_alias.egg-info/SOURCES.txt +0 -23
- env-alias-0.3.7/src/env_alias.egg-info/dependency_links.txt +0 -1
- env-alias-0.3.7/src/env_alias.egg-info/entry_points.txt +0 -4
- env-alias-0.3.7/src/env_alias.egg-info/requires.txt +0 -1
- env-alias-0.3.7/src/env_alias.egg-info/top_level.txt +0 -1
- env-alias-0.3.7/src/env_alias.egg-info/zip-safe +0 -1
- {env-alias-0.3.7 → env_alias-0.5.1}/LICENSE +0 -0
- {env-alias-0.3.7/src/env_alias/cli → env_alias-0.5.1/src/env_alias/lib}/__init__.py +0 -0
- /env-alias-0.3.7/src/env_alias/utils/__init__.py → /env_alias-0.5.1/src/env_alias/py.typed +0 -0
env_alias-0.5.1/PKG-INFO
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: env-alias
|
|
3
|
+
Version: 0.5.1
|
|
4
|
+
Summary: Powerful helper utility to create shell alias commands to easily set collections of environment variables often with secret values from a variety of data-sources and data-formats.
|
|
5
|
+
License: BSD-2-Clause
|
|
6
|
+
Keywords: env-alias,shell,env,alias,bash
|
|
7
|
+
Author: Nicholas de Jong
|
|
8
|
+
Author-email: contact@nicholasdejong.com
|
|
9
|
+
Requires-Python: >=3.8,<4.0
|
|
10
|
+
Classifier: Environment :: Console
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Intended Audience :: Information Technology
|
|
13
|
+
Classifier: License :: OSI Approved :: BSD License
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Requires-Dist: pyyaml (>=5.0)
|
|
21
|
+
Project-URL: Bug Tracker, https://github.com/ndejong/env-alias/issues
|
|
22
|
+
Project-URL: Documentation, https://env-alias.readthedocs.io/
|
|
23
|
+
Project-URL: Homepage, https://github.com/ndejong/env-alias
|
|
24
|
+
Project-URL: Repository, https://github.com/ndejong/env-alias
|
|
25
|
+
Description-Content-Type: text/markdown
|
|
26
|
+
|
|
27
|
+
# Env Alias
|
|
28
|
+
|
|
29
|
+
[](https://pypi.python.org/pypi/env-alias/)
|
|
30
|
+
[](https://github.com/ndejong/env-alias/)
|
|
31
|
+
[](https://github.com/ndejong/env-alias/actions/workflows/build-tests.yml)
|
|
32
|
+
[](https://env-alias.readthedocs.io)
|
|
33
|
+

|
|
34
|
+
|
|
35
|
+
Env Alias is an environment variable swiss-army-knife that enables loading complex collections
|
|
36
|
+
of environment variables from a variety of sources only when you require them, thus reducing risks
|
|
37
|
+
in working with sensitive environment values.
|
|
38
|
+
|
|
39
|
+
A variety of data-formats are supported including **JSON**, **YAML**, **Keepass**, **Ansible Vault**,
|
|
40
|
+
**Plaintext** and **Ini**-config where these formats can be sourced from the local-filesystem,
|
|
41
|
+
http-remote or generated through shell-command exec output.
|
|
42
|
+
|
|
43
|
+
For example setting an Ansible-vault password file and loading AWS access credentials from values stored
|
|
44
|
+
in a git project based Keepass file:
|
|
45
|
+
```yaml
|
|
46
|
+
env-alias:
|
|
47
|
+
|
|
48
|
+
MYPROJECT_KEEPASS_FILE:
|
|
49
|
+
name: null # prevents this value being assigned into env
|
|
50
|
+
exec: 'echo "$(git rev-parse --show-toplevel)/secrets/myproject-keepass.kdbx"'
|
|
51
|
+
|
|
52
|
+
MYPROJECT_KEEPASS_PASSPHRASE:
|
|
53
|
+
source: "<getpass>" # obtain value from user-input using getpass method
|
|
54
|
+
override: false # if this env-value exists then skip setting again
|
|
55
|
+
|
|
56
|
+
MYPROJECT_ANSIBLE_VAULT_PASSWORD:
|
|
57
|
+
name: null # prevents this value being assigned into env
|
|
58
|
+
source: "env:MYPROJECT_KEEPASS_FILE"
|
|
59
|
+
selector: "myproject-name/ansible-vault-entry-name:Password" # select an item from Keepass file
|
|
60
|
+
keepass_password: "env:MYPROJECT_KEEPASS_PASSPHRASE"
|
|
61
|
+
|
|
62
|
+
ANSIBLE_VAULT_PASSWORD_FILE:
|
|
63
|
+
ansible_vault_password: "env:MYPROJECT_ANSIBLE_VAULT_PASSWORD" # NB: see docs how this gets managed
|
|
64
|
+
ansible_vault_password_file: true # invoke special helper that renders an Ansible Vault password file
|
|
65
|
+
|
|
66
|
+
AWS_SECRET_ACCESS_KEY:
|
|
67
|
+
source: "env:MYPROJECT_KEEPASS_FILE"
|
|
68
|
+
selector: "myproject-name/aws-entry-name:Password"
|
|
69
|
+
keepass_password: "env:MYPROJECT_KEEPASS_PASSPHRASE"
|
|
70
|
+
|
|
71
|
+
AWS_ACCESS_KEY_ID:
|
|
72
|
+
source: "env:MYPROJECT_KEEPASS_FILE"
|
|
73
|
+
selector: "myproject-name/aws-entry-name:Username"
|
|
74
|
+
keepass_password: "env:MYPROJECT_KEEPASS_PASSPHRASE"
|
|
75
|
+
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
The above example sets the environment variable `MYPROJECT_KEEPASS_PASSPHRASE` with user input using
|
|
79
|
+
the `getpass` Python module only if not already set (`override=false`). This environment value is then
|
|
80
|
+
used as the `keepass` passphrase to open a Keepass file where values are then selected and exported
|
|
81
|
+
into the shell environment.
|
|
82
|
+
|
|
83
|
+
Substantially more complex env-alias definitions can be created.
|
|
84
|
+
|
|
85
|
+
By naming your env-aliases with an easy to remember prefix such as `env-` it is also possible to
|
|
86
|
+
leverage shell **tab-completion** thus making it easier to find the env-alias definitions created
|
|
87
|
+
for your project or other use-case situation.
|
|
88
|
+
|
|
89
|
+
## Features
|
|
90
|
+
Env Alias is enormously useful in working with large sets of environment variables from remote, encrypted
|
|
91
|
+
or otherwise secured data-sources.
|
|
92
|
+
|
|
93
|
+
* Data sources: **local-files**, **http-remote** and stdout from an **exec** command-line.
|
|
94
|
+
* Source formats supported: **JSON**, **YAML**, **Keepass**, **Ansible Vault**, **Plaintext** and **Ini**-config.
|
|
95
|
+
* Select values using **jq** style selectors, **xpath** style selectors or **line-numbers**.
|
|
96
|
+
* 💥 Additional special handling for **Ansible Vault Password Files** that makes credential handling for **Ansible Vault** files substantially easier with reduced exposure risks. 💥
|
|
97
|
+
* Self reference environment values in the definition file or from the existing system environment.
|
|
98
|
+
* Define variables with a `null` name to prevent them being exported into the system environment while still being available for self-reference within the env-alias definition; this is helpful when working with sensitive values that should not be available through the system environment.
|
|
99
|
+
* Ability to use `exec` commands to setup other project prerequisites or other project start conditions.
|
|
100
|
+
* Debug mode output to STDERR.
|
|
101
|
+
* Easy installation from PyPI.
|
|
102
|
+
* Plenty of documentation and examples - [https://env-alias.readthedocs.io](https://env-alias.readthedocs.io)
|
|
103
|
+
|
|
104
|
+
## Installation
|
|
105
|
+
Pip or pipx should be fine, we prefer pipx these days.
|
|
106
|
+
```shell
|
|
107
|
+
pipx install env-alias
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
## Usage
|
|
111
|
+
This tool is typically invoked using an entry in `.bash_aliases` with an entry of the form:-
|
|
112
|
+
```shell
|
|
113
|
+
source <(env-alias ~/projects/awesome/env-awesome-vars.yml)
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
This simple `.bash_aliases` one-line entry creates the alias `env-awesome-project` by inferring the
|
|
117
|
+
alias-name from the filename, where this alias then invokes env-alias to set environment values
|
|
118
|
+
defined in `env-awesome-project.yml`
|
|
119
|
+
|
|
120
|
+
Alternatively, you might want to create the alias `awesome-envvars` which you could do as per -
|
|
121
|
+
```shell
|
|
122
|
+
source <(env-alias awesome-envvars ~/projects/awesome/env-awesome-vars.yml)
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
## Project
|
|
127
|
+
* Github - [github.com/ndejong/env-alias](https://github.com/ndejong/env-alias)
|
|
128
|
+
* PyPI - [pypi.python.org/pypi/env-alias](https://pypi.python.org/pypi/env-alias/)
|
|
129
|
+
* ReadTheDocs - [env-alias.readthedocs.io](https://env-alias.readthedocs.io)
|
|
130
|
+
|
|
131
|
+
---
|
|
132
|
+
Copyright © (2020-2024) Nicholas de Jong
|
|
133
|
+
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
# Env Alias
|
|
2
|
+
|
|
3
|
+
[](https://pypi.python.org/pypi/env-alias/)
|
|
4
|
+
[](https://github.com/ndejong/env-alias/)
|
|
5
|
+
[](https://github.com/ndejong/env-alias/actions/workflows/build-tests.yml)
|
|
6
|
+
[](https://env-alias.readthedocs.io)
|
|
7
|
+

|
|
8
|
+
|
|
9
|
+
Env Alias is an environment variable swiss-army-knife that enables loading complex collections
|
|
10
|
+
of environment variables from a variety of sources only when you require them, thus reducing risks
|
|
11
|
+
in working with sensitive environment values.
|
|
12
|
+
|
|
13
|
+
A variety of data-formats are supported including **JSON**, **YAML**, **Keepass**, **Ansible Vault**,
|
|
14
|
+
**Plaintext** and **Ini**-config where these formats can be sourced from the local-filesystem,
|
|
15
|
+
http-remote or generated through shell-command exec output.
|
|
16
|
+
|
|
17
|
+
For example setting an Ansible-vault password file and loading AWS access credentials from values stored
|
|
18
|
+
in a git project based Keepass file:
|
|
19
|
+
```yaml
|
|
20
|
+
env-alias:
|
|
21
|
+
|
|
22
|
+
MYPROJECT_KEEPASS_FILE:
|
|
23
|
+
name: null # prevents this value being assigned into env
|
|
24
|
+
exec: 'echo "$(git rev-parse --show-toplevel)/secrets/myproject-keepass.kdbx"'
|
|
25
|
+
|
|
26
|
+
MYPROJECT_KEEPASS_PASSPHRASE:
|
|
27
|
+
source: "<getpass>" # obtain value from user-input using getpass method
|
|
28
|
+
override: false # if this env-value exists then skip setting again
|
|
29
|
+
|
|
30
|
+
MYPROJECT_ANSIBLE_VAULT_PASSWORD:
|
|
31
|
+
name: null # prevents this value being assigned into env
|
|
32
|
+
source: "env:MYPROJECT_KEEPASS_FILE"
|
|
33
|
+
selector: "myproject-name/ansible-vault-entry-name:Password" # select an item from Keepass file
|
|
34
|
+
keepass_password: "env:MYPROJECT_KEEPASS_PASSPHRASE"
|
|
35
|
+
|
|
36
|
+
ANSIBLE_VAULT_PASSWORD_FILE:
|
|
37
|
+
ansible_vault_password: "env:MYPROJECT_ANSIBLE_VAULT_PASSWORD" # NB: see docs how this gets managed
|
|
38
|
+
ansible_vault_password_file: true # invoke special helper that renders an Ansible Vault password file
|
|
39
|
+
|
|
40
|
+
AWS_SECRET_ACCESS_KEY:
|
|
41
|
+
source: "env:MYPROJECT_KEEPASS_FILE"
|
|
42
|
+
selector: "myproject-name/aws-entry-name:Password"
|
|
43
|
+
keepass_password: "env:MYPROJECT_KEEPASS_PASSPHRASE"
|
|
44
|
+
|
|
45
|
+
AWS_ACCESS_KEY_ID:
|
|
46
|
+
source: "env:MYPROJECT_KEEPASS_FILE"
|
|
47
|
+
selector: "myproject-name/aws-entry-name:Username"
|
|
48
|
+
keepass_password: "env:MYPROJECT_KEEPASS_PASSPHRASE"
|
|
49
|
+
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
The above example sets the environment variable `MYPROJECT_KEEPASS_PASSPHRASE` with user input using
|
|
53
|
+
the `getpass` Python module only if not already set (`override=false`). This environment value is then
|
|
54
|
+
used as the `keepass` passphrase to open a Keepass file where values are then selected and exported
|
|
55
|
+
into the shell environment.
|
|
56
|
+
|
|
57
|
+
Substantially more complex env-alias definitions can be created.
|
|
58
|
+
|
|
59
|
+
By naming your env-aliases with an easy to remember prefix such as `env-` it is also possible to
|
|
60
|
+
leverage shell **tab-completion** thus making it easier to find the env-alias definitions created
|
|
61
|
+
for your project or other use-case situation.
|
|
62
|
+
|
|
63
|
+
## Features
|
|
64
|
+
Env Alias is enormously useful in working with large sets of environment variables from remote, encrypted
|
|
65
|
+
or otherwise secured data-sources.
|
|
66
|
+
|
|
67
|
+
* Data sources: **local-files**, **http-remote** and stdout from an **exec** command-line.
|
|
68
|
+
* Source formats supported: **JSON**, **YAML**, **Keepass**, **Ansible Vault**, **Plaintext** and **Ini**-config.
|
|
69
|
+
* Select values using **jq** style selectors, **xpath** style selectors or **line-numbers**.
|
|
70
|
+
* 💥 Additional special handling for **Ansible Vault Password Files** that makes credential handling for **Ansible Vault** files substantially easier with reduced exposure risks. 💥
|
|
71
|
+
* Self reference environment values in the definition file or from the existing system environment.
|
|
72
|
+
* Define variables with a `null` name to prevent them being exported into the system environment while still being available for self-reference within the env-alias definition; this is helpful when working with sensitive values that should not be available through the system environment.
|
|
73
|
+
* Ability to use `exec` commands to setup other project prerequisites or other project start conditions.
|
|
74
|
+
* Debug mode output to STDERR.
|
|
75
|
+
* Easy installation from PyPI.
|
|
76
|
+
* Plenty of documentation and examples - [https://env-alias.readthedocs.io](https://env-alias.readthedocs.io)
|
|
77
|
+
|
|
78
|
+
## Installation
|
|
79
|
+
Pip or pipx should be fine, we prefer pipx these days.
|
|
80
|
+
```shell
|
|
81
|
+
pipx install env-alias
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## Usage
|
|
85
|
+
This tool is typically invoked using an entry in `.bash_aliases` with an entry of the form:-
|
|
86
|
+
```shell
|
|
87
|
+
source <(env-alias ~/projects/awesome/env-awesome-vars.yml)
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
This simple `.bash_aliases` one-line entry creates the alias `env-awesome-project` by inferring the
|
|
91
|
+
alias-name from the filename, where this alias then invokes env-alias to set environment values
|
|
92
|
+
defined in `env-awesome-project.yml`
|
|
93
|
+
|
|
94
|
+
Alternatively, you might want to create the alias `awesome-envvars` which you could do as per -
|
|
95
|
+
```shell
|
|
96
|
+
source <(env-alias awesome-envvars ~/projects/awesome/env-awesome-vars.yml)
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
## Project
|
|
101
|
+
* Github - [github.com/ndejong/env-alias](https://github.com/ndejong/env-alias)
|
|
102
|
+
* PyPI - [pypi.python.org/pypi/env-alias](https://pypi.python.org/pypi/env-alias/)
|
|
103
|
+
* ReadTheDocs - [env-alias.readthedocs.io](https://env-alias.readthedocs.io)
|
|
104
|
+
|
|
105
|
+
---
|
|
106
|
+
Copyright © (2020-2024) Nicholas de Jong
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["poetry-core"]
|
|
3
|
+
build-backend = "poetry.core.masonry.api"
|
|
4
|
+
|
|
5
|
+
[tool.poetry]
|
|
6
|
+
name = "env-alias"
|
|
7
|
+
version = "0.5.1"
|
|
8
|
+
description = "Powerful helper utility to create shell alias commands to easily set collections of environment variables often with secret values from a variety of data-sources and data-formats."
|
|
9
|
+
authors = ["Nicholas de Jong <contact@nicholasdejong.com>"]
|
|
10
|
+
license = "BSD-2-Clause"
|
|
11
|
+
readme = "README.md"
|
|
12
|
+
packages = [{ include = "env_alias", from = "src" }]
|
|
13
|
+
classifiers = [
|
|
14
|
+
"Environment :: Console",
|
|
15
|
+
"Intended Audience :: Developers",
|
|
16
|
+
"Intended Audience :: Information Technology",
|
|
17
|
+
"Programming Language :: Python :: 3.8",
|
|
18
|
+
"Programming Language :: Python :: 3.9",
|
|
19
|
+
"Programming Language :: Python :: 3.10",
|
|
20
|
+
"Programming Language :: Python :: 3.11",
|
|
21
|
+
"Programming Language :: Python :: 3.12"
|
|
22
|
+
]
|
|
23
|
+
keywords = ["env-alias", "shell", "env", "alias", "bash"]
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
[tool.poetry.urls]
|
|
27
|
+
Documentation = "https://env-alias.readthedocs.io/"
|
|
28
|
+
Homepage = "https://github.com/ndejong/env-alias"
|
|
29
|
+
Repository = "https://github.com/ndejong/env-alias"
|
|
30
|
+
"Bug Tracker" = "https://github.com/ndejong/env-alias/issues"
|
|
31
|
+
|
|
32
|
+
[tool.poetry.scripts]
|
|
33
|
+
env-alias = "env_alias.main:entrypoint"
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
[tool.poetry.dependencies]
|
|
37
|
+
python = "^3.8"
|
|
38
|
+
pyyaml = ">=5.0" # https://pypi.org/project/pyyaml/#history
|
|
39
|
+
|
|
40
|
+
[tool.poetry.dev-dependencies]
|
|
41
|
+
black = ">=23.0" # https://pypi.org/project/black/#history
|
|
42
|
+
flake8 = ">=7.1" # https://pypi.org/project/flake8/#history
|
|
43
|
+
isort = ">=5.13" # https://pypi.org/project/isort/#history
|
|
44
|
+
mypy = ">=1.10" # https://pypi.org/project/mypy/#history
|
|
45
|
+
pycln = ">=2.4" # https://pypi.org/project/pycln/#history
|
|
46
|
+
pytest = ">=8.2" # https://pypi.org/project/pytest/#history
|
|
47
|
+
safety = ">=3.2" # https://pypi.org/project/safety/#history
|
|
48
|
+
types-aiofiles = ">=23.2" # https://pypi.org/project/types-aiofiles/#history
|
|
49
|
+
types-PyYAML = ">=6.0" # https://pypi.org/project/types-PyYAML/#history
|
|
50
|
+
# NB: pip installs in .readthedocs.yml need to be kept up-to-date manually
|
|
51
|
+
novella = ">=0.2" # https://pypi.org/project/novella/#history
|
|
52
|
+
pydoc-markdown = ">=4.8" # https://pypi.org/project/pydoc-markdown/#history
|
|
53
|
+
mkdocs-material = ">=9.5" # https://pypi.org/project/mkdocs-material/#history
|
|
54
|
+
|
|
55
|
+
[tool.poetry.plugins."slap.plugins.check"]
|
|
56
|
+
changelog = "slap.ext.checks.changelog:ChangelogValidationCheckPlugin"
|
|
57
|
+
general = "slap.ext.checks.general:GeneralChecksPlugin"
|
|
58
|
+
poetry = "slap.ext.checks.poetry:PoetryChecksPlugin"
|
|
59
|
+
release = "slap.ext.checks.release:ReleaseChecksPlugin"
|
|
60
|
+
|
|
61
|
+
[tool.slap]
|
|
62
|
+
typed = true
|
|
63
|
+
release.branch = "dev"
|
|
64
|
+
|
|
65
|
+
[tool.slap.test]
|
|
66
|
+
check = "slap check"
|
|
67
|
+
black = "black --check src/ tests/"
|
|
68
|
+
flake8 = "flake8 src/ tests/"
|
|
69
|
+
isort = "isort --check-only src/ tests/"
|
|
70
|
+
mypy = "dmypy run src/"
|
|
71
|
+
pycln = "pycln src/ tests/ --check"
|
|
72
|
+
safety = "pip freeze | safety check --stdin --short-report --output text"
|
|
73
|
+
pytest = "pytest tests/ -vv"
|
|
74
|
+
|
|
75
|
+
[tool.slap.run]
|
|
76
|
+
format = "black src/ tests/ && isort src/ tests/"
|
|
77
|
+
docs-build = "cd docs && novella --base-url env-alias/"
|
|
78
|
+
docs-server = "cd docs && novella --serve"
|
|
79
|
+
|
|
80
|
+
[tool.mypy]
|
|
81
|
+
explicit_package_bases = true
|
|
82
|
+
ignore_missing_imports = true
|
|
83
|
+
mypy_path = ["src"]
|
|
84
|
+
namespace_packages = true
|
|
85
|
+
pretty = true
|
|
86
|
+
python_version = "3.8"
|
|
87
|
+
show_error_codes = true
|
|
88
|
+
show_error_context = true
|
|
89
|
+
strict = true
|
|
90
|
+
warn_no_return = true
|
|
91
|
+
warn_redundant_casts = true
|
|
92
|
+
warn_unreachable = true
|
|
93
|
+
warn_unused_ignores = true
|
|
94
|
+
|
|
95
|
+
[tool.isort]
|
|
96
|
+
profile = "black"
|
|
97
|
+
line_length = 120
|
|
98
|
+
combine_as_imports = true
|
|
99
|
+
|
|
100
|
+
[tool.black]
|
|
101
|
+
line-length = 120
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
#
|
|
2
|
+
# Copyright [2020] Nicholas de Jong (https://www.nicholasdejong.com)
|
|
3
|
+
#
|
|
4
|
+
|
|
5
|
+
from os import getenv
|
|
6
|
+
from sys import argv
|
|
7
|
+
|
|
8
|
+
__title__ = "Env Alias"
|
|
9
|
+
__version__ = "0.5.1"
|
|
10
|
+
|
|
11
|
+
LOGGER_LEVEL = "info"
|
|
12
|
+
if "--debug" in argv or getenv("ENVALIAS_DEBUG", "").lower().startswith(("true", "yes", "enable", "on")):
|
|
13
|
+
LOGGER_LEVEL = "debug"
|
|
14
|
+
|
|
15
|
+
LOGGER_NAME = "env-alias"
|
|
16
|
+
DEFINITIONS_ROOT = "env-alias"
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
from typing import Any, List, Union
|
|
2
|
+
|
|
3
|
+
from . import LOGGER_LEVEL, LOGGER_NAME
|
|
4
|
+
from .lib.logger import logger_get
|
|
5
|
+
|
|
6
|
+
logger = logger_get(name=LOGGER_NAME, loglevel=LOGGER_LEVEL)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class EnvAliasBaseException(Exception):
|
|
10
|
+
def __init__(self, *args: Union[str, List[Any]], **kwargs: Any) -> None:
|
|
11
|
+
log_message = " ".join([str(x) for x in args]).strip()
|
|
12
|
+
if log_message:
|
|
13
|
+
logger.error(f"{log_message}")
|
|
14
|
+
if "detail" in kwargs and LOGGER_LEVEL == "debug":
|
|
15
|
+
logger.error(f"{kwargs['detail']}".strip())
|
|
16
|
+
super().__init__(*args)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class EnvAliasException(EnvAliasBaseException):
|
|
20
|
+
pass
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import os
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
from typing import List, Optional
|
|
4
|
+
|
|
5
|
+
import yaml
|
|
6
|
+
|
|
7
|
+
from .. import DEFINITIONS_ROOT, LOGGER_LEVEL, LOGGER_NAME
|
|
8
|
+
from ..exceptions import EnvAliasException
|
|
9
|
+
from ..lib.logger import logger_get
|
|
10
|
+
from ..models.envalias_definition import EnvAliasDefinition
|
|
11
|
+
|
|
12
|
+
logger = logger_get(name=LOGGER_NAME, loglevel=LOGGER_LEVEL)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class EnvAliasDefinitions:
|
|
16
|
+
definitions_root: str = DEFINITIONS_ROOT
|
|
17
|
+
|
|
18
|
+
def __init__(self, definitions_root: Optional[str] = None):
|
|
19
|
+
if definitions_root:
|
|
20
|
+
self.definitions_root = definitions_root
|
|
21
|
+
|
|
22
|
+
def load_definitions(self, definitions_file: Path) -> List[EnvAliasDefinition]:
|
|
23
|
+
logger.debug(f"EnvAliasConfig.load_definitions(definitions_file={str(definitions_file)!r})")
|
|
24
|
+
|
|
25
|
+
if not os.path.isfile(definitions_file):
|
|
26
|
+
raise EnvAliasException(f"Unable to locate definitions_file={str(definitions_file)!r}")
|
|
27
|
+
|
|
28
|
+
loaded_definitions = {}
|
|
29
|
+
|
|
30
|
+
with open(definitions_file, "r") as f:
|
|
31
|
+
try:
|
|
32
|
+
loaded_definitions = yaml.safe_load(f.read())
|
|
33
|
+
except yaml.YAMLError as e:
|
|
34
|
+
raise EnvAliasException(f"Failed to load definitions_file={str(definitions_file)!r}", detail=e)
|
|
35
|
+
|
|
36
|
+
if not isinstance(loaded_definitions, dict) or self.definitions_root not in loaded_definitions.keys():
|
|
37
|
+
raise EnvAliasException(f"Unable to locate top-level definitions root {self.definitions_root!r}")
|
|
38
|
+
logger.debug(f"Definitions loaded from definitions_file={str(definitions_file)!r}")
|
|
39
|
+
|
|
40
|
+
definitions: List[EnvAliasDefinition] = []
|
|
41
|
+
for definition_key, definition_item in loaded_definitions[self.definitions_root].items():
|
|
42
|
+
if not isinstance(definition_item, dict):
|
|
43
|
+
raise EnvAliasException(f"Definition item {definition_key!r} is not dict type")
|
|
44
|
+
|
|
45
|
+
definition_item["_filename"] = definitions_file
|
|
46
|
+
|
|
47
|
+
if "name" in definition_item.keys() and definition_item["name"] is None:
|
|
48
|
+
definition_item["_is_internal_only"] = True
|
|
49
|
+
|
|
50
|
+
if not definition_item.get("name"):
|
|
51
|
+
definition_item["name"] = definition_key
|
|
52
|
+
|
|
53
|
+
try:
|
|
54
|
+
definitions.append(EnvAliasDefinition(**definition_item))
|
|
55
|
+
except TypeError as e:
|
|
56
|
+
raise EnvAliasException(str(e))
|
|
57
|
+
|
|
58
|
+
logger.debug(f"Loaded environment definition for {definition_item['name']!r}")
|
|
59
|
+
|
|
60
|
+
logger.debug(f"Total {len(definitions)} definitions in {definitions_file=}")
|
|
61
|
+
return definitions
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import sys
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
from typing import Dict, Union
|
|
5
|
+
|
|
6
|
+
from .. import DEFINITIONS_ROOT, LOGGER_LEVEL, LOGGER_NAME
|
|
7
|
+
from ..exceptions import EnvAliasException
|
|
8
|
+
from ..lib.definitions import EnvAliasDefinitions
|
|
9
|
+
from ..lib.logger import logger_get
|
|
10
|
+
from ..lib.selector import EnvAliasSelector
|
|
11
|
+
from ..lib.source import EnvAliasSource
|
|
12
|
+
from ..models.envalias_definition import EnvAliasDefinition
|
|
13
|
+
from ..models.sourced_content import SourcedContent
|
|
14
|
+
|
|
15
|
+
logger = logger_get(name=LOGGER_NAME, loglevel=LOGGER_LEVEL)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class EnvAliasGenerator:
|
|
19
|
+
definitions_file: Path
|
|
20
|
+
values_generated: Dict[str, str] = {}
|
|
21
|
+
|
|
22
|
+
def __init__(self, config_file: Path):
|
|
23
|
+
self.definitions_file = config_file
|
|
24
|
+
|
|
25
|
+
def generate(self) -> None:
|
|
26
|
+
logger.debug("EnvAliasGenerator.generate()")
|
|
27
|
+
|
|
28
|
+
definitions = EnvAliasDefinitions(definitions_root=DEFINITIONS_ROOT).load_definitions(self.definitions_file)
|
|
29
|
+
if not definitions:
|
|
30
|
+
raise EnvAliasException(f"Empty or malformed {self.definitions_file=}")
|
|
31
|
+
|
|
32
|
+
for definition in definitions:
|
|
33
|
+
is_existing_setting = os.getenv(definition.name) or self.values_generated.get(definition.name)
|
|
34
|
+
if definition.override is False and is_existing_setting:
|
|
35
|
+
logger.debug(f"Skipping {definition.name!r} because already set and definition.override=False.")
|
|
36
|
+
continue
|
|
37
|
+
|
|
38
|
+
value = self.get_definition_value(definition=self.update_env_replacement_attributes(definition))
|
|
39
|
+
|
|
40
|
+
if value is not None: # NB: not just "if value" because value could be a valid empty string
|
|
41
|
+
self.values_generated[definition.name] = value
|
|
42
|
+
if definition._is_internal_only is True:
|
|
43
|
+
logger.debug(
|
|
44
|
+
f"Definition for {definition.name!r} defines a 'null' name for env-alias internal "
|
|
45
|
+
f"only use, skipping generated output."
|
|
46
|
+
)
|
|
47
|
+
continue
|
|
48
|
+
|
|
49
|
+
self.output_export(env_name=definition.name, env_value=value)
|
|
50
|
+
|
|
51
|
+
if definition.ansible_vault_password_file: # special additional output case
|
|
52
|
+
self.output_export(
|
|
53
|
+
env_name=self.values_generated[value], env_value=definition.ansible_vault_password
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
def output_export(self, env_name: str, env_value: Union[str, None] = "", output_prefix: str = " ") -> None:
|
|
57
|
+
output = f'{output_prefix}export "{env_name}"="{env_value}"'
|
|
58
|
+
logger.debug(f"output={output!r}")
|
|
59
|
+
print(output, file=sys.stdout) # NB: force stdout
|
|
60
|
+
|
|
61
|
+
def get_definition_value(self, definition: EnvAliasDefinition) -> Union[str, None]:
|
|
62
|
+
logger.debug(f"EnvAliasGenerator.get_definition_value({definition.name=}, ...)")
|
|
63
|
+
|
|
64
|
+
if definition.value:
|
|
65
|
+
return definition.value
|
|
66
|
+
|
|
67
|
+
sourced_content = self.get_content_from_source(definition=definition)
|
|
68
|
+
|
|
69
|
+
parser = sourced_content.content_type
|
|
70
|
+
if definition.parser:
|
|
71
|
+
parser = definition.parser
|
|
72
|
+
|
|
73
|
+
if sourced_content.source_method in ("getpass", "stdin", "keepass") or parser == "none":
|
|
74
|
+
return sourced_content.content
|
|
75
|
+
|
|
76
|
+
if definition.selector == "none":
|
|
77
|
+
return None
|
|
78
|
+
|
|
79
|
+
if parser == "ini":
|
|
80
|
+
return EnvAliasSelector.ini_content(sourced_content.content, definition.selector)
|
|
81
|
+
elif parser == "json":
|
|
82
|
+
return EnvAliasSelector.json_content(sourced_content.content, definition.selector)
|
|
83
|
+
elif parser in ["yaml", "yml"]:
|
|
84
|
+
return EnvAliasSelector.yaml_content(sourced_content.content, definition.selector)
|
|
85
|
+
|
|
86
|
+
if definition.selector and not str(definition.selector).isdigit():
|
|
87
|
+
raise EnvAliasException(f"Selector for plaintext content must be number {definition.selector=}")
|
|
88
|
+
elif definition.selector is None:
|
|
89
|
+
definition.selector = "1"
|
|
90
|
+
|
|
91
|
+
return EnvAliasSelector.text_content(sourced_content.content, selector=int(definition.selector))
|
|
92
|
+
|
|
93
|
+
def get_content_from_source(self, definition: EnvAliasDefinition) -> SourcedContent:
|
|
94
|
+
if definition.source and definition.source.startswith("http"):
|
|
95
|
+
sourced_content = EnvAliasSource.remote(url=definition.source)
|
|
96
|
+
|
|
97
|
+
elif definition.source and definition.source == "<stdin>":
|
|
98
|
+
sourced_content = EnvAliasSource.stdin(prompt=f"Enter {definition.name!r} value using <stdin>: ")
|
|
99
|
+
|
|
100
|
+
elif definition.source and definition.source == "<getpass>":
|
|
101
|
+
sourced_content = EnvAliasSource.getpass(prompt=f"Enter {definition.name!r} value using <getpass>: ")
|
|
102
|
+
|
|
103
|
+
elif definition.source and definition.keepass_password:
|
|
104
|
+
sourced_content = EnvAliasSource.keepass(
|
|
105
|
+
filename=Path(definition.source), password=definition.keepass_password, selector=definition.selector
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
elif definition.source and definition.ansible_vault_password:
|
|
109
|
+
sourced_content = EnvAliasSource.ansible_vault(
|
|
110
|
+
filename=Path(definition.source), password=definition.ansible_vault_password
|
|
111
|
+
)
|
|
112
|
+
|
|
113
|
+
elif definition.ansible_vault_password and definition.ansible_vault_password_file:
|
|
114
|
+
sourced_content = EnvAliasSource.ansible_vault_password_file(password=definition.ansible_vault_password)
|
|
115
|
+
self.values_generated[sourced_content.content] = sourced_content.source # for the special additional output
|
|
116
|
+
|
|
117
|
+
elif definition.source:
|
|
118
|
+
sourced_content = EnvAliasSource.local(filename=Path(definition.source))
|
|
119
|
+
|
|
120
|
+
elif definition.exec:
|
|
121
|
+
sourced_content = EnvAliasSource.execute(command_line=definition.exec)
|
|
122
|
+
|
|
123
|
+
else:
|
|
124
|
+
raise EnvAliasException(f"Definition for env-alias {definition.name!r} is malformed.")
|
|
125
|
+
|
|
126
|
+
return sourced_content
|
|
127
|
+
|
|
128
|
+
def update_env_replacement_attributes(self, definition: EnvAliasDefinition) -> EnvAliasDefinition:
|
|
129
|
+
attr_names = ["value", "source", "parser", "selector", "keepass_password", "ansible_vault_password"]
|
|
130
|
+
for attr_name in attr_names:
|
|
131
|
+
attr_value = getattr(definition, attr_name)
|
|
132
|
+
if attr_value and str(attr_value).startswith("env:"):
|
|
133
|
+
setattr(definition, attr_name, self.get_replacement_env_value(attr_value))
|
|
134
|
+
return definition
|
|
135
|
+
|
|
136
|
+
def get_replacement_env_value(self, value: str) -> str:
|
|
137
|
+
env_name = value[4:]
|
|
138
|
+
|
|
139
|
+
env_value = os.getenv(env_name, None)
|
|
140
|
+
if env_value is None or len(value) < 1:
|
|
141
|
+
env_value = self.values_generated.get(env_name)
|
|
142
|
+
|
|
143
|
+
if env_value is None:
|
|
144
|
+
raise EnvAliasException(f"Definition replacement using {value!r} environment value that is unset.")
|
|
145
|
+
|
|
146
|
+
return env_value
|