Skip to content

Commit db8d279

Browse files
committed
feat: migrate non-encrypted config
1 parent 97cc3da commit db8d279

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

nile/utils/config.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import logging
66
from typing import Union
77
from enum import Enum
8+
import hashlib
89
import nile.constants as constants
910
from Crypto.Cipher import AES
1011
from Crypto.Util.Padding import pad, unpad
@@ -21,10 +22,35 @@ class Config:
2122
Handles writing and reading from config with automatic in memory caching
2223
TODO: Make memory caching opt out in some cases
2324
"""
25+
migrations_ran = False
2426

2527
def __init__(self):
2628
self.logger = logging.getLogger("CONFIG")
2729
self.cache = {}
30+
try:
31+
self.migrate()
32+
except:
33+
self.logger.error("Failed to run config migrations")
34+
35+
def migrate(self):
36+
if Config.migrations_ran:
37+
return
38+
Config.migrations_ran = True
39+
40+
# Encrypted config
41+
user_data = self.get('user')
42+
if user_data:
43+
name = user_data['extensions']['customer_info']['given_name']
44+
uid = user_data['extensions']['customer_info']['user_id']
45+
current_user = {
46+
'name': name, 'user_id': uid
47+
}
48+
uid = uid.encode('utf-8')
49+
self.write("current_user", current_user)
50+
store = hashlib.md5(uid).hexdigest()
51+
enc_key = hashlib.sha256(uid).digest()
52+
self.write(store, user_data, cfg_type=ConfigType.JSONENC, enc_key=enc_key)
53+
self.remove('user')
2854

2955
def _join_path_name(self, name: str, cfg_type: ConfigType) -> str:
3056
extension = "json"
@@ -53,6 +79,8 @@ def remove(self, store, cfg_type=ConfigType.JSON):
5379
path = self._join_path_name(store, cfg_type)
5480
if os.path.exists(path):
5581
os.remove(path)
82+
if store in self.cache:
83+
del self.cache[store]
5684

5785
def write(self, store: str, data: any, cfg_type=ConfigType.JSON, enc_key=None):
5886
"""

0 commit comments

Comments
 (0)