-
Notifications
You must be signed in to change notification settings - Fork 46
/
export.py
83 lines (63 loc) · 2.77 KB
/
export.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from .config import Config
import tempfile
import base64
import json
from keeper_secrets_manager_core.utils import base64_to_bytes
from keeper_secrets_manager_core.keeper_globals import keeper_servers
class Export:
"""
Export a specific profile config
"""
def __init__(self, config, file_format=None, plain=False):
# If the JSON dictionary is passed in convert it to a Config
if isinstance(config, dict) is True:
config = Config.create_from_json(config).get_profile(Config.default_profile)
self.config = config
self.file_format = file_format
self.plain = plain
def run(self):
config_str = None
if self.file_format == "ini":
config_str = self._format_ini()
elif self.file_format == "json":
config_str = self._format_json()
if self.plain is False:
if type(config_str) is str:
config_str = config_str.encode()
config_str = base64.urlsafe_b64encode(config_str)
return config_str
def _format_ini(self):
from .profile import Profile
# Apparently the config parser doesn't like temp files. So create a
# temp file, then open a file for writing and use that to write
# the config. Then read the temp file to get our new config.
with tempfile.NamedTemporaryFile() as tf:
config = Config(ini_file=tf.name)
config.set_profile(Profile.default_profile,
client_id=self.config.client_id,
private_key=self.config.private_key,
app_key=self.config.app_key,
hostname=self.config.hostname,
app_owner_public_key=self.config.app_owner_public_key,
server_public_key_id=self.config.server_public_key_id)
config.config.active_profile = Profile.default_profile
config.save()
tf.seek(0)
config_str = tf.read()
tf.close()
return config_str
def _format_json(self):
def _base64(value):
if value is not None:
value_bytes = base64_to_bytes(value)
value = base64.b64encode(value_bytes).decode()
return value
config_dict = {
"clientId": _base64(self.config.client_id),
"privateKey": _base64(self.config.private_key),
"appKey": _base64(self.config.app_key),
"hostname": keeper_servers.get(self.config.hostname, self.config.hostname),
"serverPublicKeyId": self.config.server_public_key_id,
"appOwnerPublicKey": self.config.app_owner_public_key
}
return json.dumps(config_dict, indent=4)