55from base64 import b64encode , b64decode
66
77APERTUREDB_CLOUD = ".cloud.aperturedata.io"
8- APERTUREDB_KEY_VERSION = 1
8+ APERTUREDB_KEY_VERSION = 2
9+ APERTUREDB_OLD_KEY_VERSION = 1
910FLAG_USE_COMPRESSED_HOST = 4
1011FLAG_USE_REST = 2
1112FLAG_USE_SSL = 1
@@ -33,6 +34,7 @@ class Configuration:
3334 retry_max_attempts : int = 3
3435 token : str = None
3536 user_keys : dict = None
37+ ca_cert : str = None
3638
3739 def __repr__ (self ) -> str :
3840 mode = "REST" if self .use_rest else "TCP"
@@ -41,7 +43,7 @@ def __repr__(self) -> str:
4143
4244 def deflate (self ) -> list :
4345 return self .create_aperturedb_key (self .host , self .port , self .token ,
44- self .use_rest , self .use_ssl , self .username , self .password )
46+ self .use_rest , self .use_ssl , self .ca_cert , self . username , self .password )
4547
4648 def has_user_keys (self ) -> bool :
4749 return self .user_keys is not None
@@ -82,8 +84,10 @@ def config_default_port(cls, use_rest: bool, use_ssl: bool):
8284 return DEFAULT_TCP_PORT
8385
8486 @classmethod
85- def create_aperturedb_key (cls , host : str , port : int , token_string : str ,
86- use_rest : bool , use_ssl : bool , username : str = None , password : str = None ) -> None :
87+ def create_aperturedb_key (
88+ cls , host : str , port : int , token_string : str ,
89+ use_rest : bool , use_ssl : bool , ca_cert : str = None ,
90+ username : str = None , password : str = None ) -> None :
8791 compressed = False
8892 if token_string is not None and token_string .startswith ("adbp_" ):
8993 token_string = token_string [5 :]
@@ -100,10 +104,11 @@ def create_aperturedb_key(cls, host: str, port: int, token_string: str,
100104 if port != default_port :
101105 host = f"{ host } :{ port } "
102106 if token_string is not None :
103- key_json = [APERTUREDB_KEY_VERSION , key_type , host , token_string ]
107+ key_json = [APERTUREDB_KEY_VERSION ,
108+ key_type , host , ca_cert , token_string ]
104109 else :
105110 key_json = [APERTUREDB_KEY_VERSION ,
106- key_type , host , username , password ]
111+ key_type , host , ca_cert , username , password ]
107112 simplified = json .dumps (key_json , separators = (',' , ':' ))
108113 encoded = b64encode (simplified .encode ('utf-8' )).decode ('utf-8' )
109114 return encoded
@@ -118,16 +123,25 @@ def reinflate(cls, encoded_str: list) -> object:
118123 raise Exception (
119124 "Unable to make configuration from the provided string" )
120125 version = as_list [0 ]
121- if version not in (APERTUREDB_KEY_VERSION ,):
126+ if version not in (APERTUREDB_KEY_VERSION , APERTUREDB_OLD_KEY_VERSION ):
122127 raise ValueError ("version identifier of configuration was"
123128 f"{ version } , which is not supported" )
124129 is_compressed , use_rest , use_ssl = cls .key_type_to_config (as_list [1 ])
125130 host = as_list [2 ]
131+ pem = None
132+
133+ if version == APERTUREDB_KEY_VERSION :
134+ pem = as_list [3 ]
135+ list_offset = 4
136+ else :
137+ list_offset = 3
126138 token = username = password = None
127- if len (as_list ) == 4 :
128- token = "adbp_" + as_list [3 ]
129- elif len (as_list ) == 5 :
130- username , password = as_list [3 :]
139+ if len (as_list ) == list_offset + 1 :
140+ token = "adbp_" + as_list [list_offset ]
141+ elif len (as_list ) == list_offset + 2 :
142+ username , password = as_list [list_offset :]
143+ else :
144+ raise ValueError ("Bad format for key list" )
131145
132146 port_match = re .match (".*:(\d+)$" , host )
133147 if port_match is not None :
@@ -146,7 +160,7 @@ def reinflate(cls, encoded_str: list) -> object:
146160 f"Unable to parse compressed host: { host } Error: { e } " )
147161
148162 c = Configuration (
149- host , port , username , password , name , use_ssl , use_rest )
163+ host , port , username , password , name , use_ssl , use_rest , ca_cert = pem )
150164 if token :
151165 c .token = token
152166 return c
0 commit comments