Skip to content

Commit 7734ac6

Browse files
committed
Merge branch 'main' of https://github.com/zecrypt-io/zecrypt-server into feature/shivil/modules-UI-Fixing
2 parents c80e9f5 + 9483334 commit 7734ac6

13 files changed

Lines changed: 148 additions & 273 deletions

File tree

packages/backend-server/app/api/v1/web/dashboard/services.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ async def get_dashboard_overview(request, user):
1818
data = {}
1919
for key, value in features.items():
2020
if value.get("enabled"):
21-
data[key] = secret_manager.get_project_secrets_count(
22-
db, key, project_id
23-
)
21+
data[key] = secret_manager.get_project_secrets_count(db, key, project_id)
2422

2523
return response_helper(200, "Dashboard overview loaded successfully", data)
2624

packages/backend-server/app/api/v1/web/projects/services.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
secrets as secrets_manager,
77
)
88

9-
from app.framework.encryption.service import get_project_key
109
from app.utils.i8ns import translate
1110

1211

@@ -133,10 +132,8 @@ def get_tags(db, project_id):
133132
return response_helper(200, translate("project.tags"), data=unique_tags)
134133

135134

136-
def add_project_key(db, user_id, project_id, workspace_id, project_key=None):
137-
if not project_key:
138-
project_key = get_project_key(db, user_id)
139-
135+
def add_project_key(db, user_id, project_id, workspace_id, project_key):
136+
140137
project_keys_manager.insert_one(
141138
db,
142139
{

packages/backend-server/app/api/v1/web/secrets/services.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,23 @@
44
from app.utils.i8ns import translate
55
from fastapi import BackgroundTasks
66
from app.api.v1.web.project_activity.services import add_recent_activity
7+
from app.framework.valkey import services as valkey_services
78

8-
background_tasks = BackgroundTasks()
9-
10-
11-
async def get_secret_details(db, data_type, doc_id):
12-
secret = secrets_manager.find_one(
13-
db, {"doc_id": doc_id, "secret_type": data_type}, {"_id": False}
14-
)
15-
16-
if not secret:
17-
return response_helper(404, translate(f"{data_type}.not_found"))
18-
19-
return response_helper(200, translate(f"{data_type}.details"), data=secret)
209

2110

2211
async def get_secrets(request, user, data_type):
2312
db = user.get("db")
2413
page = request.query_params.get("page", None)
2514
limit = request.query_params.get("limit", None)
26-
15+
project_id = request.path_params.get("project_id")
2716
query = {
2817
"secret_type": data_type,
29-
"project_id": request.path_params.get("project_id"),
18+
"project_id": project_id,
3019
}
20+
if valkey_services.check_secret_exists(project_id, data_type, query):
21+
data = valkey_services.get_secret(project_id, data_type, query)
22+
return response_helper(200, translate(f"{data_type}.list"), data=data, count=len(data))
23+
3124
if page and limit:
3225
skip = (page - 1) * limit
3326
secrets = secrets_manager.find(db, query, skip=skip, limit=limit)
@@ -76,6 +69,7 @@ async def add_secret(request, user, data_type, payload, background_tasks):
7669
payload.get("doc_id"),
7770
"create",
7871
)
72+
background_tasks.add_task(valkey_services.delete_secret, project_id, data_type)
7973
return response_helper(201, translate(f"{data_type}.add"), data=payload)
8074

8175

@@ -113,6 +107,7 @@ async def update_secret(request, user, data_type, payload, background_tasks):
113107
background_tasks.add_task(
114108
add_recent_activity, user, project_id, data_type, doc_id, "update"
115109
)
110+
background_tasks.add_task(valkey_services.delete_secret, project_id, data_type)
116111
return response_helper(200, translate(f"{data_type}.update"), data=payload)
117112

118113

@@ -128,4 +123,5 @@ async def delete_secret(request, user, data_type, background_tasks):
128123
background_tasks.add_task(
129124
add_recent_activity, user, project_id, data_type, doc_id, "delete"
130125
)
126+
background_tasks.add_task(valkey_services.delete_secret, project_id, data_type)
131127
return response_helper(200, translate(f"{data_type}.delete"), data={})

packages/backend-server/app/core/config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ class Settings(BaseSettings):
1313
STACK_AUTH_CLIENT_SECRET: str
1414
TOTP_SECRET: str
1515

16+
VALKEY_URL: str
17+
1618
class Config:
1719
case_sensitive = True
1820
env_file = ".env"

packages/backend-server/app/framework/encryption/service.py

Lines changed: 0 additions & 25 deletions
This file was deleted.

packages/backend-server/app/framework/permission_services/permissions.py

Lines changed: 0 additions & 107 deletions
This file was deleted.
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import json
2+
3+
import valkey
4+
5+
from app.core.config import settings
6+
7+
valkey_client = valkey.Valkey.from_url(url=settings.VALKEY_URL)
8+
9+
10+
11+
class ValkeyClient:
12+
def __init__(self):
13+
"""
14+
Initialize a new instance of redis client
15+
"""
16+
self.client = valkey_client
17+
18+
19+
def add_key_to_hash(self, name, key, value):
20+
"""
21+
Add a new key-value pair to a hash
22+
23+
:param name: The name of the hash.
24+
:param key: The key of the item.
25+
:param value: The value to be stored.
26+
"""
27+
if self.client.hexists(name, key):
28+
self.client.hdel(name, key)
29+
self.client.hset(name, key, json.dumps(value, default=str))
30+
31+
def remove_key_from_hash(self, name, key):
32+
"""
33+
Remove a key-value pair from a hash
34+
35+
:param name: The name of the hash.
36+
:param key: The key of the item to remove.
37+
"""
38+
try:
39+
self.client.hdel(name, key)
40+
except Exception:
41+
pass
42+
43+
44+
def get_from_hash(self, name, key):
45+
"""
46+
Get a value from a hash
47+
:param name: The name of the hash.
48+
:param key: The key of the item to get.
49+
:returns: The value of the item.
50+
"""
51+
try:
52+
return json.loads(self.client.hget(name, key))
53+
except Exception:
54+
return None
55+
56+
def field_exists_in_hash(self, name, key):
57+
"""
58+
Check if a field exists in a hash
59+
:param name: The name of the hash.
60+
:param key: The key of the item to get.
61+
:returns: True if the field exists, False otherwise.
62+
"""
63+
return self.client.hexists(name, key)
64+
65+
66+
def get_all_keys_from_hash(self, name):
67+
"""
68+
Get all keys from a hash
69+
:param name: The name of the hash.
70+
:returns: A list of all keys in the hash.
71+
"""
72+
keys = list(self.client.hkeys(name))
73+
return [key.decode("utf-8") for key in keys]
74+
75+
76+
77+
def delete_hash(self, hash):
78+
try:
79+
if isinstance(hash, str):
80+
self.client.delete(hash)
81+
else:
82+
self.client.delete(*hash)
83+
except Exception:
84+
pass
85+
86+
def check_hash_exists(self, hash_key):
87+
if self.client.exists(hash_key) and self.client.type(hash_key) == b"hash":
88+
return True
89+
return False
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from app.framework.valkey.client import ValkeyClient
2+
from app.utils.utils import generate_query_hash
3+
4+
valkey_client = ValkeyClient()
5+
6+
7+
8+
def add_secret( project_id, secret_type, query, secret_value):
9+
query_hash = generate_query_hash(query)
10+
valkey_client.add_key_to_hash(
11+
f"{project_id}:{secret_type}", query_hash, secret_value
12+
)
13+
14+
15+
def get_secret( project_id, secret_type, query):
16+
query_hash = generate_query_hash(query)
17+
return valkey_client.get_from_hash(
18+
f"{project_id}:{secret_type}", query_hash
19+
)
20+
21+
22+
def delete_secret( project_id, secret_type):
23+
valkey_client.delete_hash(f"{project_id}:{secret_type}")
24+
25+
def check_secret_exists( project_id, secret_type, query):
26+
query_hash = generate_query_hash(query)
27+
return valkey_client.field_exists_in_hash(
28+
f"{project_id}:{secret_type}", query_hash
29+
)

packages/backend-server/app/utils/security/__init__.py

Whitespace-only changes.

packages/backend-server/app/utils/security/encryption_utils.py

Lines changed: 0 additions & 43 deletions
This file was deleted.

0 commit comments

Comments
 (0)