Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions alibabacloud_credentials/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
from urllib.parse import urlparse, parse_qs

from Tea.core import TeaCore
from Tea.request import TeaRequest

from alibabacloud_credentials.utils import auth_constant as ac
from alibabacloud_credentials.utils import parameter_helper as ph
from alibabacloud_credentials.exceptions import CredentialException
from alibabacloud_credentials.models import CredentialModel

Expand Down Expand Up @@ -351,7 +351,7 @@ async def _ensure_credential_async(self):

def _get_new_credential(self):
r = urlparse(self.credentials_uri)
tea_request = TeaRequest()
tea_request = ph.get_new_request()
tea_request.headers['host'] = r.hostname
tea_request.port = r.port
tea_request.method = 'GET'
Expand Down Expand Up @@ -387,7 +387,7 @@ def _get_new_credential(self):

async def _get_new_credential_async(self):
r = urlparse(self.credentials_uri)
tea_request = TeaRequest()
tea_request = ph.get_new_request()
tea_request.headers['host'] = r.netloc
tea_request.method = 'GET'
tea_request.pathname = r.path
Expand Down
23 changes: 11 additions & 12 deletions alibabacloud_credentials/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import requests
from Tea.core import TeaCore
from Tea.request import TeaRequest

from alibabacloud_credentials import credentials
from alibabacloud_credentials.exceptions import CredentialException
Expand Down Expand Up @@ -132,7 +131,7 @@ def _get_role_name(self, url=None):
self.role_name = response.text

async def _get_role_name_async(self, url=None):
tea_request = TeaRequest()
tea_request = ph.get_new_request()
tea_request.headers['host'] = url if url else self.__metadata_service_host
if not url:
tea_request.pathname = self.__url_in_ecs_metadata
Expand All @@ -147,7 +146,7 @@ def _need_to_refresh_token(self):
def _get_metadata_token(self, url=None):
if self._need_to_refresh_token():
tmp_time = int(time.mktime(time.localtime())) + self.metadata_token_duration
tea_request = TeaRequest()
tea_request = ph.get_new_request()
tea_request.method = 'PUT'
tea_request.headers['host'] = url if url else self.__metadata_service_host
tea_request.headers['X-aliyun-ecs-metadata-token-ttl-seconds'] = str(self.metadata_token_duration)
Expand All @@ -163,7 +162,7 @@ def _get_metadata_token(self, url=None):
async def _get_metadata_token_async(self, url=None):
if self._need_to_refresh_token():
tmp_time = int(time.mktime(time.localtime())) + self.metadata_token_duration
tea_request = TeaRequest()
tea_request = ph.get_new_request()
tea_request.method = 'PUT'
tea_request.headers['host'] = url if url else self.__metadata_service_host
tea_request.headers['X-aliyun-ecs-metadata-token-ttl-seconds'] = str(self.metadata_token_duration)
Expand All @@ -177,7 +176,7 @@ async def _get_metadata_token_async(self, url=None):
self.__metadata_token = response.body.decode('utf-8')

def _create_credential(self, url=None, metadata_token=None):
tea_request = TeaRequest()
tea_request = ph.get_new_request()
tea_request.headers['host'] = url if url else self.__metadata_service_host
if metadata_token:
tea_request.headers['X-aliyun-ecs-metadata-token'] = metadata_token
Expand Down Expand Up @@ -215,7 +214,7 @@ def get_credentials(self):
return self._create_credential()

async def _create_credential_async(self, url=None, metadata_token=None):
tea_request = TeaRequest()
tea_request = ph.get_new_request()
tea_request.headers['host'] = url if url else self.__metadata_service_host
if metadata_token:
tea_request.headers['X-aliyun-ecs-metadata-token'] = metadata_token
Expand Down Expand Up @@ -274,7 +273,7 @@ def get_credentials(self):

def _create_credentials(self, turl=None):
# 获取credential 先实现签名用工具类
tea_request = TeaRequest()
tea_request = ph.get_new_request()
tea_request.query = {
'Action': 'AssumeRole',
'Format': 'JSON',
Expand Down Expand Up @@ -315,7 +314,7 @@ async def get_credentials_async(self):

async def _create_credentials_async(self, turl=None):
# 获取credential 先实现签名用工具类
tea_request = TeaRequest()
tea_request = ph.get_new_request()
tea_request.query = {
'Action': 'AssumeRole',
'Format': 'JSON',
Expand Down Expand Up @@ -383,7 +382,7 @@ def get_credentials(self):
def _create_credentials(self, turl=None):
# 获取credential 先实现签名用工具类
oidc_token = au.get_private_key(self.oidc_token_file_path)
tea_request = TeaRequest()
tea_request = ph.get_new_request()
tea_request.query = {
'Action': 'AssumeRoleWithOIDC',
'Format': 'JSON',
Expand Down Expand Up @@ -421,7 +420,7 @@ async def get_credentials_async(self):
async def _create_credentials_async(self, turl=None):
# 获取credential 先实现签名用工具类
oidc_token = au.get_private_key(self.oidc_token_file_path)
tea_request = TeaRequest()
tea_request = ph.get_new_request()
tea_request.query = {
'Action': 'AssumeRoleWithOIDC',
'Format': 'JSON',
Expand Down Expand Up @@ -467,7 +466,7 @@ async def get_credentials_async(self):
return await self._create_credential_async()

async def _create_credential_async(self, turl=None):
tea_request = TeaRequest()
tea_request = ph.get_new_request()
tea_request.query = {
'Action': 'GenerateSessionAccessKey',
'Format': 'JSON',
Expand Down Expand Up @@ -503,7 +502,7 @@ def get_credentials(self):
return self._create_credential()

def _create_credential(self, turl=None):
tea_request = TeaRequest()
tea_request = ph.get_new_request()
tea_request.query = {
'Action': 'GenerateSessionAccessKey',
'Format': 'JSON',
Expand Down
13 changes: 13 additions & 0 deletions alibabacloud_credentials/utils/parameter_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
import socket
import uuid
import datetime
import platform

import alibabacloud_credentials
from Tea.request import TeaRequest

TIME_ZONE = "UTC"
FORMAT_ISO_8601 = "yyyy-MM-dd'T'HH:mm:ss'Z'"
Expand All @@ -15,6 +19,15 @@
ALGORITHM_NAME = "HmacSHA1"


def get_new_request():
request = TeaRequest()
request.headers['user-agent'] = f'AlibabaCloud ({platform.system()}; {platform.machine()}) ' \
f'Python/{platform.python_version()} ' \
f'Credentials/{alibabacloud_credentials.__version__} ' \
f'TeaDSL/1'
return request


def get_uuid():
name = socket.gethostname() + str(uuid.uuid1())
namespace = uuid.NAMESPACE_URL
Expand Down
15 changes: 15 additions & 0 deletions tests/test_util.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from alibabacloud_credentials.utils import auth_util, parameter_helper

import unittest
import re
from . import txt_file


Expand Down Expand Up @@ -32,8 +33,22 @@ def test_compose_url(test):
res = parameter_helper.compose_url(endpoint, queries, protocol)
test.assertEqual('https://aliyun.com/?tests=test', res)

def test_get_new_request(test):
request = parameter_helper.get_new_request()
test.assertEqual({}, request.query)
test.assertEqual('http', request.protocol)
test.assertEqual(80, request.port)
test.assertEqual('GET', request.method)
test.assertEqual('', request.pathname)
test.assertIsNone(request.body)
test.assertEqual('', request.pathname)
test.assertIsNotNone(
re.match('AlibabaCloud (.+; .+) Python/.+ Credentials/.+ TeaDSL/1',
request.headers.get('user-agent')))

test_get_uuid(self)
test_get_iso_8061_date(self)
test_compose_string_to_sign(self)
test_sign_string(self)
test_compose_url(self)
test_get_new_request(self)