|
| 1 | +import base64 |
1 | 2 | import configparser
|
2 | 3 | import os
|
| 4 | +import json |
3 | 5 | from pathlib import Path
|
| 6 | +from typing import Optional |
4 | 7 |
|
5 | 8 | from boxsdk.auth.jwt_auth import JWTAuth
|
6 | 9 | from boxsdk.client import Client
|
7 | 10 |
|
8 | 11 |
|
9 |
| -def read_jwt_path_from_config(config_path: str): |
| 12 | +JWT_CONFIG_ENV_VAR_NAME = 'JWT_CONFIG_BASE_64' |
| 13 | +CURRENT_DIR_PATH = str(Path(os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__))))) |
| 14 | +CONFIG_PATH = f'{CURRENT_DIR_PATH}/integration_tests.cfg' |
| 15 | + |
| 16 | + |
| 17 | +def get_jwt_config() -> JWTAuth: |
| 18 | + jwt_config = read_jwt_config_from_env_var() or read_jwt_config_from_file() |
| 19 | + |
| 20 | + if not jwt_config: |
| 21 | + raise RuntimeError( |
| 22 | + f'JWT config cannot be loaded. Missing environment variable: {JWT_CONFIG_ENV_VAR_NAME} or JWT config path.' |
| 23 | + ) |
| 24 | + return jwt_config |
| 25 | + |
| 26 | + |
| 27 | +def read_jwt_config_from_env_var() -> Optional[JWTAuth]: |
| 28 | + |
| 29 | + jwt_config_base64 = os.getenv(JWT_CONFIG_ENV_VAR_NAME) |
| 30 | + if not jwt_config_base64: |
| 31 | + return None |
| 32 | + jwt_config_str = base64.b64decode(jwt_config_base64) |
| 33 | + jwt_config_json = json.loads(jwt_config_str) |
| 34 | + return JWTAuth.from_settings_dictionary(jwt_config_json) |
| 35 | + |
| 36 | + |
| 37 | +def read_jwt_config_from_file() -> Optional[JWTAuth]: |
10 | 38 | config_parser = configparser.ConfigParser()
|
11 |
| - config_parser.read(config_path) |
12 |
| - return config_parser["JWT"].get('SettingsFilePath') |
| 39 | + config_parser.read(CONFIG_PATH) |
| 40 | + jwt_config_file_path = config_parser["JWT"].get('ConfigFilePath') |
| 41 | + if not jwt_config_file_path: |
| 42 | + return None |
| 43 | + return JWTAuth.from_settings_file(jwt_config_file_path) |
13 | 44 |
|
14 | 45 |
|
15 |
| -CURRENT_DIR_PATH = str(Path(os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__))))) |
16 |
| -jwt_config_path = read_jwt_path_from_config(config_path=f'{CURRENT_DIR_PATH}/integration_tests.cfg') |
17 |
| -jwt_config = JWTAuth.from_settings_file(jwt_config_path) |
18 |
| -CLIENT = Client(jwt_config) |
| 46 | +CLIENT = Client(get_jwt_config()) |
0 commit comments