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
45 changes: 43 additions & 2 deletions .github/workflows/testPython.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ permissions:
id-token: write

jobs:
build:
build-linux:
runs-on: ubuntu-22.04
strategy:
matrix:
Expand Down Expand Up @@ -49,4 +49,45 @@ jobs:
- name: Upload Coverage Report
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }} # required
token: ${{ secrets.CODECOV_TOKEN }} # required

build-win:
runs-on: windows-2019
strategy:
matrix:
python-version: [ "3.8", "3.9", "3.10", "3.11", "3.12" ]
fail-fast: false
steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: pip install alibabacloud-tea coverage pytest alibabacloud_credentials_api APScheduler aiofiles
- name: Setup OIDC
run: npm install @actions/core@1.6.0 @actions/http-client
- name: Get Id Token
uses: actions/github-script@v6
id: idtoken
with:
script: |
const coreDemo = require('@actions/core');
const idToken = await coreDemo.getIDToken('sts.aliyuncs.com');
const fsx = require('fs/promises');
await fsx.writeFile('D:\\oidc_token', idToken);
- name: Test with unittest
run: |
coverage run -m unittest discover
env:
SUB_ALIBABA_CLOUD_ACCESS_KEY: ${{ secrets.SUB_ALIBABA_CLOUD_ACCESS_KEY }}
SUB_ALIBABA_CLOUD_SECRET_KEY: ${{ secrets.SUB_ALIBABA_CLOUD_SECRET_KEY }}
SUB_ALIBABA_CLOUD_ROLE_ARN: ${{ secrets.ALIBABA_CLOUD_ROLE_ARN }}
ALIBABA_CLOUD_ROLE_ARN: ${{ secrets.OIDC_ROLE_ARN }}
ALIBABA_CLOUD_ROLE_SESSION_NAME: ${{ secrets.ALIBABA_CLOUD_ROLE_SESSION_NAME }}
ALIBABA_CLOUD_OIDC_TOKEN_FILE: "D:\\oidc_token"
ALIBABA_CLOUD_OIDC_PROVIDER_ARN: ${{ secrets.OIDC_PROVIDER_ARN }}
- name: Upload Coverage Report
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }} # required
5 changes: 3 additions & 2 deletions alibabacloud_credentials/utils/auth_constant.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
from alibabacloud_credentials.utils import auth_util

HOME = auth_util.get_home()

HOME = os.getenv('HOME') if os.getenv('HOME') else os.getenv('HOMEPATH')
INI_ACCESS_KEY_ID = "access_key_id"
INI_ACCESS_KEY_IDSECRET = "access_key_secret"
INI_TYPE = "type"
Expand Down
18 changes: 18 additions & 0 deletions alibabacloud_credentials/utils/auth_util.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import os
import platform
import re

client_type = os.environ.get('ALIBABA_CLOUD_PROFILE', 'default')

Expand Down Expand Up @@ -35,3 +37,19 @@ def get_private_key(file_path):
with open(file_path, encoding='utf-8') as f:
key = f.read()
return key


def get_home():
if platform.system() == 'Windows':
home = os.getenv('HOME')
home_path = os.getenv('HOMEPATH')
home_drive = os.getenv('HOMEDRIVE')
if home:
return home
elif home_path:
has_drive_in_home_path = bool(re.match(r'^[A-Za-z]:', home_path))
return home_path if has_drive_in_home_path else os.path.join(home_drive or '', home_path)
else:
return os.path.expanduser("~")
else:
return os.getenv('HOME') or os.getenv('HOMEPATH') or os.path.expanduser("~")
48 changes: 47 additions & 1 deletion tests/test_util.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from alibabacloud_credentials.utils import auth_util, parameter_helper
from alibabacloud_credentials.utils import auth_util, parameter_helper, auth_constant

import os
import unittest
from unittest.mock import patch
import re
from . import txt_file
import platform


class TestUtil(unittest.TestCase):
Expand Down Expand Up @@ -52,3 +55,46 @@ def test_get_new_request(test):
test_sign_string(self)
test_compose_url(self)
test_get_new_request(self)

def test_home(self):
@patch.dict(os.environ, {'HOME': '/mock/home/linux'})
def test_home_exists():
"""case1:HOME exists"""
assert auth_util.get_home() == '/mock/home/linux'

@patch.dict(os.environ, {'HOME': ''})
@patch.dict(os.environ, {'HOMEPATH': ''})
def test_home_empty():
"""case2:HOME exists but empty"""
with patch('os.path.expanduser', return_value='/fallback'):
assert auth_util.get_home() == '/fallback'

@patch.dict(os.environ, {'HOME': ''})
@patch.dict(os.environ, {'HOMEPATH': '\\Users\\mockuser'})
@patch.dict(os.environ, {'HOMEDRIVE': 'C:'})
def test_home_path_and_drive_windows():
"""case3:Windows HOMEPATH exists and HOMEDRIVE exists"""
if platform.system() == 'Windows':
assert auth_util.get_home() == 'C:\\Users\\mockuser'
else:
assert auth_util.get_home() == '\\Users\\mockuser'

@patch.dict(os.environ, {'HOME': ''})
@patch.dict(os.environ, {'HOMEPATH': 'D:\\Users\\mockuser'})
@patch.dict(os.environ, {'HOMEDRIVE': 'C:'})
def test_home_path_windows():
"""case4:Windows HOMEPATH exists"""
assert auth_util.get_home() == 'D:\\Users\\mockuser'

def test_real_system():
"""case5:test real system"""
assert auth_constant.HOME
assert os.path.exists(auth_constant.HOME)
assert auth_constant.HOME == os.path.expanduser('~')
assert auth_util.get_home() == os.path.expanduser('~')

test_home_exists()
test_home_empty()
test_home_path_and_drive_windows()
test_home_path_windows()
test_real_system()