Skip to content

Commit 7eb3726

Browse files
committed
feat: 🎸 encrypt support PYE_ENCRYPT_KEY envvar
1 parent 5c2b43d commit 7eb3726

File tree

5 files changed

+34
-4
lines changed

5 files changed

+34
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ Options:
6666
-i, --in-place make changes to files in place
6767
-k, --key 🔑 Your encryption key.If you don‘t specify
6868
key, pyencrypt will generate encryption key
69-
randomly.
69+
randomly. [env var: PYE_ENCRYPT_KEY]
7070
--with-license Add license to encrypted file
7171
-m, --bind-mac 01:23:45:67:89:AB
7272
Bind mac address to encrypted file

examples/django/Dockerfile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ COPY . /root/demo/
2020
RUN python manage.py collectstatic --noinput
2121

2222
# --- Encryption ---
23-
RUN pip install git+https://github.com/ZhaoQi99/pyencrypt-pye.git
23+
ARG ENCRYPT_KEY
24+
ENV PYE_ENCRYPT_KEY=$ENCRYPT_KEY
25+
26+
RUN pip install pyencrypt-pye
2427
RUN pyencrypt encrypt --in-place --yes .
2528
RUN cp encrypted/loader*.so .
2629
RUN rm -rf encrypted build/

examples/django/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ This example shows how to use `pyencrypt` with Django.
77
docker compose up -d
88
```
99

10+
## Build image
11+
```shell
12+
docker build -f Dockerfile -t demo:v1.0 .
13+
docker build -f Dockerfile -t demo:v1.0 --build-arg ENCRYPT_KEY=YOUR_FIXED_KEY .
14+
docker save demo:v1.0| gzip > demo:v1.0_v1.0.tar.gz
15+
```
16+
1017
## Test
1118
* runserver: `curl http://127.0.0.1:8001/account/login/?username=admin&password=admin`
1219
* gunicorn: `curl http://127.0.0.1:8002/account/login/?username=admin&password=admin`

pyencrypt/cli.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from pathlib import Path
88

99
import click
10+
from click.core import ParameterSource # click>=8
1011

1112
from pyencrypt import __description__, __version__
1213
from pyencrypt.decrypt import decrypt_file
@@ -69,6 +70,8 @@
6970

7071
DATETIME_FORMATS = ["%Y-%m-%dT%H:%M:%S %z", "%Y-%m-%d %H:%M:%S", "%Y-%m-%d"]
7172

73+
ENVVAR_PREFIX = "PYE_ENCRYPT"
74+
7275

7376
class KeyParamType(click.ParamType):
7477
name = "key"
@@ -77,6 +80,17 @@ def _check_key(self, key: str) -> bool:
7780
return not (len(key) % 4 or len(base64.b64decode(key)) % 16)
7881

7982
def convert(self, value, param, ctx) -> str:
83+
if ctx.get_parameter_source(param.name) == ParameterSource.ENVIRONMENT:
84+
visible_chars = 4
85+
masked = (
86+
value[:visible_chars]
87+
+ "*" * (len(value) - 2 * visible_chars)
88+
+ value[-visible_chars:]
89+
)
90+
click.echo(
91+
f'Using encryption key 🔑 {click.style(masked, fg="yellow")} from environment variable {click.style(param.envvar, fg="bright_cyan")}.'
92+
)
93+
8094
value = click.STRING.convert(value, param, ctx)
8195
if not self._check_key(value):
8296
self.fail(INVALID_KEY_MSG, param, ctx)
@@ -147,7 +161,13 @@ def cli():
147161
is_flag=True,
148162
)
149163
@click.option(
150-
"-k", "--key", default=None, help=KEY_OPTION_HELP, type=CustomParamType.KEY
164+
"-k",
165+
"--key",
166+
default=None,
167+
help=KEY_OPTION_HELP,
168+
type=CustomParamType.KEY,
169+
envvar=f"{ENVVAR_PREFIX}_KEY",
170+
show_envvar=True,
151171
)
152172
@click.option(
153173
"--with-license", default=False, help="Add license to encrypted file", is_flag=True

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ install_requires =
3535
Cython >= 0.29.30
3636
pycryptodome >= 3.14.1
3737
python-minifier >= 2.6.0; python_version < '3.14'
38-
click
38+
click >= 8.0.0
3939
setuptools >= 66.1.0; python_version >= '3.12'
4040
setuptools <= 60.9.0; python_version < '3.12'
4141
python_requires = >=3.6,<3.14

0 commit comments

Comments
 (0)