77APERTUREDB_CLOUD = ".cloud.aperturedata.io"
88APERTUREDB_KEY_VERSION = 2
99APERTUREDB_OLD_KEY_VERSION = 1
10+ FLAG_VERIFY_HOSTNAME = 8
1011FLAG_USE_COMPRESSED_HOST = 4
1112FLAG_USE_REST = 2
1213FLAG_USE_SSL = 1
@@ -35,15 +36,29 @@ class Configuration:
3536 token : str = None
3637 user_keys : dict = None
3738 ca_cert : str = None
39+ verify_hostname : bool = True
40+
41+ def __ssl_mode (self ) -> str :
42+ if not self .use_ssl :
43+ mode = "SSL_OFF" # with not use_ssl, we don't use SSL
44+ elif not self .verify_hostname :
45+ mode = "SSL_NO_VERIFY" # with verify_hostname=False, we don't verify the hostname
46+ elif self .ca_cert :
47+ # with verify_hostname=True and ca_cert is provided, we verify the hostname using the provided ca_cert
48+ mode = "SSL_WITH_CA"
49+ else :
50+ mode = "SSL_DEFAULT"
51+
52+ return mode
3853
3954 def __repr__ (self ) -> str :
4055 mode = "REST" if self .use_rest else "TCP"
4156 auth_mode = "token" if self .token is not None else "password"
42- return f"[{ self .host } :{ self .port } as { self .username } using { mode } with SSL={ self .use_ssl } auth={ auth_mode } ]"
57+ return f"[{ self .host } :{ self .port } as { self .username } using { mode } with SSL={ self .__ssl_mode () } auth={ auth_mode } ]"
4358
4459 def deflate (self ) -> list :
4560 return self .create_aperturedb_key (self .host , self .port , self .token ,
46- self .use_rest , self .use_ssl , self .ca_cert , self .username , self .password )
61+ self .use_rest , self .use_ssl , self .ca_cert , self .username , self .password , self . verify_hostname )
4762
4863 def has_user_keys (self ) -> bool :
4964 return self .user_keys is not None
@@ -65,16 +80,20 @@ def set_user_keys(self, keys: dict) -> None:
6580 self .user_keys = keys
6681
6782 @classmethod
68- def config_to_key_type (cls , compressed_host : bool , use_rest : bool , use_ssl : bool ):
69- return (FLAG_USE_COMPRESSED_HOST if compressed_host else 0 ) + \
70- (FLAG_USE_REST if use_rest else 0 ) + \
71- (FLAG_USE_SSL if use_ssl else 0 )
83+ def config_to_key_type (cls , compressed_host : bool , use_rest : bool , use_ssl : bool , verify_hostname : bool ):
84+ return (
85+ (FLAG_USE_COMPRESSED_HOST if compressed_host else 0 ) |
86+ (FLAG_USE_REST if use_rest else 0 ) |
87+ (FLAG_USE_SSL if use_ssl else 0 ) |
88+ (FLAG_VERIFY_HOSTNAME if verify_hostname else 0 )
89+ )
7290
7391 @classmethod
7492 def key_type_to_config (cls , key_type : int ): \
7593 return [bool (key_type & FLAG_USE_COMPRESSED_HOST ),
7694 bool (key_type & FLAG_USE_REST ),
77- bool (key_type & FLAG_USE_SSL )]
95+ bool (key_type & FLAG_USE_SSL ),
96+ bool (key_type & FLAG_VERIFY_HOSTNAME )]
7897
7998 @classmethod
8099 def config_default_port (cls , use_rest : bool , use_ssl : bool ):
@@ -87,7 +106,7 @@ def config_default_port(cls, use_rest: bool, use_ssl: bool):
87106 def create_aperturedb_key (
88107 cls , host : str , port : int , token_string : str ,
89108 use_rest : bool , use_ssl : bool , ca_cert : str = None ,
90- username : str = None , password : str = None ) -> None :
109+ username : str = None , password : str = None , verify_hostname : bool = True ) -> None :
91110 compressed = False
92111 if token_string is not None and token_string .startswith ("adbp_" ):
93112 token_string = token_string [5 :]
@@ -99,7 +118,8 @@ def create_aperturedb_key(
99118 host = "{}.{}" .format (m .group (1 ), int (m .group (2 )))
100119 compressed = True
101120
102- key_type = cls .config_to_key_type (compressed , use_rest , use_ssl )
121+ key_type = cls .config_to_key_type (
122+ compressed , use_rest , use_ssl , verify_hostname )
103123 default_port = cls .config_default_port (use_rest , use_ssl )
104124 if port != default_port :
105125 host = f"{ host } :{ port } "
@@ -126,10 +146,14 @@ def reinflate(cls, encoded_str: list) -> object:
126146 if version not in (APERTUREDB_KEY_VERSION , APERTUREDB_OLD_KEY_VERSION ):
127147 raise ValueError ("version identifier of configuration was"
128148 f"{ version } , which is not supported" )
129- is_compressed , use_rest , use_ssl = cls .key_type_to_config (as_list [1 ])
149+ is_compressed , use_rest , use_ssl , verify_hostname = cls .key_type_to_config (
150+ as_list [1 ])
130151 host = as_list [2 ]
131152 pem = None
132153
154+ if version == APERTUREDB_OLD_KEY_VERSION :
155+ verify_hostname = True
156+
133157 if version == APERTUREDB_KEY_VERSION :
134158 pem = as_list [3 ]
135159 list_offset = 4
@@ -160,7 +184,7 @@ def reinflate(cls, encoded_str: list) -> object:
160184 f"Unable to parse compressed host: { host } Error: { e } " )
161185
162186 c = Configuration (
163- host , port , username , password , name , use_ssl , use_rest , ca_cert = pem )
187+ host , port , username , password , name , use_ssl , use_rest , ca_cert = pem , verify_hostname = verify_hostname )
164188 if token :
165189 c .token = token
166190 return c
0 commit comments