Skip to content

Commit 1c9cbe5

Browse files
authored
fix: EncryptField unable to process values that starts with special header (#237)
1 parent 0155a60 commit 1c9cbe5

14 files changed

Lines changed: 194 additions & 20 deletions

File tree

.github/workflows/blue-krill.yml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,19 @@ jobs:
1919
steps:
2020
- uses: actions/checkout@v2
2121
- name: Set up Python
22-
uses: actions/setup-python@v2
22+
uses: actions/setup-python@v5
2323
with:
2424
python-version: 3.11
25-
- name: Check with ruff
26-
working-directory: sdks/blue-krill
27-
run: |
28-
pip install ruff==0.9.6
29-
ruff check --config ../../pyproject.toml
25+
- name: Setup uv
26+
uses: astral-sh/setup-uv@v5
27+
- name: Install Poetry system-wide
28+
# Use uv install to install poetry directly instead github action for simplicity
29+
run: uv pip install --system poetry==2.1.3
30+
- name: Ruff check
31+
run: poetry install && poetry run ruff check ./sdks/blue-krill --config ./pyproject.toml
3032
- name: Lint with mypy
3133
working-directory: sdks/blue-krill
32-
run: |
33-
pip install mypy==0.910 types-requests==2.31.0.2 types-setuptools==57.4.18 types-dataclasses==0.1.7 types-redis==3.5.18 types-PyMySQL==1.1.0.1 types-six==0.1.9 types-toml==0.1.5
34-
mypy . --config-file=pyproject.toml
34+
run: poetry install && poetry run mypy . --config-file=./pyproject.toml
3535
test:
3636
strategy:
3737
fail-fast: false

pyproject.toml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
[tool.poetry]
2-
name = "蓝鲸 PaaS 平台 Python 工具集"
3-
version = "0.0.1"
4-
description = "BlueKing PaaS Python SDK"
5-
authors = ["blueking <blueking@tencent.com>"]
62
package-mode = false
73

4+
[project]
5+
name = "bkpaas-python-sdk"
6+
description = "BlueKing PaaS Python SDK"
7+
version = "0.0.1"
8+
authors = [{ name = "blueking", email = "blueking@tencent.com" }]
9+
requires-python = '>=3.8.1,<3.12'
10+
811
[tool.poetry.dependencies]
912
python = ">=3.8.1,<3.12"
1013

sdks/blue-krill/blue_krill/encrypt/handler.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,6 @@ def encrypt_cipher_type(self):
5656

5757
def encrypt(self, text: str) -> str:
5858
"""根据指定加密算法,加密字段"""
59-
# 已加密则不处理
60-
for cls in self.cipher_classes.values():
61-
if cls.header.contain_header(text):
62-
return text
63-
6459
# 根据加密类型配置选择不同的加密算法
6560
try:
6661
cipher_class = self.cipher_classes[self.encrypt_cipher_type]

sdks/blue-krill/blue_krill/models/fields.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222
from blue_krill.encrypt.handler import EncryptHandler
2323

2424

25+
class _EncryptedString(str):
26+
"""A string that is encrypted, this type is used to distinguish between normal
27+
strings and encrypted ones."""
28+
29+
2530
class EncryptField(models.TextField):
2631
"""a field which will be encrypted via cryptography/fernet"""
2732

@@ -34,7 +39,10 @@ def __init__(self, encrypt_cipher_type: Optional[str] = None, secret_key: Option
3439
def get_prep_value(self, value):
3540
if value is None:
3641
return value
37-
return self.handler.encrypt(value)
42+
# 如果值已经是 _EncryptedString 实例,则直接返回,避免重复触发加密逻辑
43+
if isinstance(value, _EncryptedString):
44+
return value
45+
return _EncryptedString(self.handler.encrypt(value))
3846

3947
def get_db_prep_value(self, value, connection, prepared=False):
4048
return self.get_prep_value(value)

sdks/blue-krill/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,4 @@ build-backend = "poetry.core.masonry.api"
7272
ignore_missing_imports = true
7373
show_error_codes = true
7474
check_untyped_defs = true
75+
exclude = ['^tests/models/apps/.*/migrations/.*$']

sdks/blue-krill/tests/conftest.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818

1919
def pytest_configure():
20+
from cryptography.fernet import Fernet
2021
from django.conf import settings
2122

2223
settings.configure(
@@ -28,12 +29,21 @@ def pytest_configure():
2829
TIME_ZONE="Asia/Shanghai",
2930
MIDDLEWARE=[],
3031
MIDDLEWARE_CLASSES=[],
31-
INSTALLED_APPS=(),
32+
# If the models of these apps have been updated, remember to remove all migration files so
33+
# that the migrations could stay minimal because the test suite will create them from scratch.
34+
INSTALLED_APPS=("tests.models.apps.test_encrypt",),
35+
# For testing encrypt module
36+
BKKRILL_ENCRYPT_SECRET_KEY=Fernet.generate_key(),
3237
)
3338

3439
try:
3540
import django
41+
from django.core.management import execute_from_command_line
3642

3743
django.setup()
44+
45+
# Create migrations for test apps
46+
execute_from_command_line(["manage.py", "makemigrations"])
47+
3848
except AttributeError:
3949
pass

sdks/blue-krill/tests/models/__init__.py

Whitespace-only changes.

sdks/blue-krill/tests/models/apps/__init__.py

Whitespace-only changes.

sdks/blue-krill/tests/models/apps/test_encrypt/__init__.py

Whitespace-only changes.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from django.apps import AppConfig
2+
3+
4+
class TestEncryptConfig(AppConfig):
5+
default_auto_field = "django.db.models.BigAutoField"
6+
name = "tests.models.apps.test_encrypt"

0 commit comments

Comments
 (0)