2121except :
2222 import http .client as httplib
2323
24+ from qingcloud .misc .json_tool import json_load
25+ from qingcloud .conn .auth import QuerySignatureAuthHandler
26+
2427
2528class ConnectionQueue (object ):
2629 """ Http connection queue
@@ -148,7 +151,11 @@ def __str__(self):
148151
149152 def authorize (self , connection , ** kwargs ):
150153 # add authorize information to request
151- if connection ._auth_handler :
154+ if connection .iam_access_key :
155+ kwargs .update ({'access_key' : connection .iam_access_key ,
156+ 'token' : connection ._token ,
157+ 'signature_version' : 2 })
158+ if connection ._auth_handler and (connection .qy_access_key_id or connection .iam_access_key ):
152159 connection ._auth_handler .add_auth (self , ** kwargs )
153160
154161
@@ -180,7 +187,7 @@ class HttpConnection(object):
180187
181188 def __init__ (self , qy_access_key_id , qy_secret_access_key , host = None ,
182189 port = 443 , protocol = "https" , pool = None , expires = None ,
183- http_socket_timeout = 10 , debug = False ):
190+ http_socket_timeout = 10 , debug = False , credential_proxy_host = None , credential_proxy_port = 80 ):
184191 """
185192 @param qy_access_key_id - the access key id
186193 @param qy_secret_access_key - the secret access key
@@ -204,6 +211,12 @@ def __init__(self, qy_access_key_id, qy_secret_access_key, host=None,
204211 self ._proxy_port = None
205212 self ._proxy_headers = None
206213 self ._proxy_protocol = None
214+ self ._token = ''
215+ self ._token_exp = None
216+ self .credential_proxy_host = credential_proxy_host
217+ self .credential_proxy_port = credential_proxy_port
218+ self .iam_access_key = None
219+ self .iam_secret_key = None
207220
208221 def set_proxy (self , host , port = None , headers = None , protocol = "http" ):
209222 """ set http (https) proxy
@@ -263,6 +276,10 @@ def send(self, method, path, params=None, headers=None, host=None,
263276 if not host :
264277 host = self .host
265278
279+ if not self .qy_access_key_id and not self .qy_secret_access_key :
280+ if self ._token :
281+ path = '/iam/'
282+
266283 # Build the http request
267284 request = self .build_http_request (method , path , params , auth_path ,
268285 headers , host , data )
@@ -299,3 +316,31 @@ def send(self, method, path, params=None, headers=None, host=None,
299316 self ._set_conn (conn )
300317
301318 return response
319+
320+ def _check_token (self ):
321+ if not self ._token or not self ._token_exp or time .time () >= self ._token_exp :
322+ try :
323+ conn = httplib .HTTPConnection (self .credential_proxy_host , self .credential_proxy_port , timeout = 1 )
324+ conn .request ("GET" , "/latest/meta-data/security-credentials" , headers = {"Accept" : "application/json" })
325+ response = conn .getresponse ()
326+ # Reuse the connection
327+ if response .status == 200 :
328+ r = response .read ()
329+ if r :
330+ # first reverse escape, then json_load
331+ r = json_load (eval (r ))
332+ self ._token = r .get ('id_token' )
333+ self ._token_exp = r .get ('expiration' )
334+ self .iam_access_key = r .get ('access_key' )
335+ self .iam_secret_key = r .get ('secret_key' )
336+
337+ self ._auth_handler = QuerySignatureAuthHandler (self .host ,
338+ str (self .iam_access_key ),
339+ str (self .iam_secret_key ))
340+
341+ elif response .status == 404 :
342+ print ("The current instance has no credentials" )
343+ pass
344+ except Exception as e :
345+ print ("Failed to get credentials due to error: %s" % e )
346+ pass
0 commit comments