|
| 1 | +from typing import Optional |
| 2 | + |
| 3 | +import click |
| 4 | + |
| 5 | +from cycode.cli import config, consts |
| 6 | +from cycode.cli.user_settings.configuration_manager import ConfigurationManager |
| 7 | +from cycode.cli.user_settings.credentials_manager import CredentialsManager |
| 8 | +from cycode.cli.utils.string_utils import obfuscate_text |
| 9 | + |
| 10 | +_URLS_UPDATED_SUCCESSFULLY_MESSAGE = 'Successfully configured Cycode URLs! Saved to: {filename}' |
| 11 | +_URLS_ARE_SET_IN_ENVIRONMENT_VARIABLES_MESSAGE = ( |
| 12 | + 'Note that the URLs (APP and API) that already exist in environment variables ' |
| 13 | + f'({consts.CYCODE_API_URL_ENV_VAR_NAME} and {consts.CYCODE_APP_URL_ENV_VAR_NAME}) ' |
| 14 | + 'take precedent over these URLs; either update or remove the environment variables.' |
| 15 | +) |
| 16 | +_CREDENTIALS_UPDATED_SUCCESSFULLY_MESSAGE = 'Successfully configured CLI credentials! Saved to: {filename}' |
| 17 | +_CREDENTIALS_ARE_SET_IN_ENVIRONMENT_VARIABLES_MESSAGE = ( |
| 18 | + 'Note that the credentials that already exist in environment variables ' |
| 19 | + f'({config.CYCODE_CLIENT_ID_ENV_VAR_NAME} and {config.CYCODE_CLIENT_SECRET_ENV_VAR_NAME}) ' |
| 20 | + 'take precedent over these credentials; either update or remove the environment variables.' |
| 21 | +) |
| 22 | +_CREDENTIALS_MANAGER = CredentialsManager() |
| 23 | +_CONFIGURATION_MANAGER = ConfigurationManager() |
| 24 | + |
| 25 | + |
| 26 | +@click.command(short_help='Initial command to configure your CLI client authentication.') |
| 27 | +def configure_command() -> None: |
| 28 | + """Configure your CLI client authentication manually.""" |
| 29 | + global_config_manager = _CONFIGURATION_MANAGER.global_config_file_manager |
| 30 | + |
| 31 | + current_api_url = global_config_manager.get_api_url() |
| 32 | + current_app_url = global_config_manager.get_app_url() |
| 33 | + api_url = _get_api_url_input(current_api_url) |
| 34 | + app_url = _get_app_url_input(current_app_url) |
| 35 | + |
| 36 | + config_updated = False |
| 37 | + if _should_update_value(current_api_url, api_url): |
| 38 | + global_config_manager.update_api_base_url(api_url) |
| 39 | + config_updated = True |
| 40 | + if _should_update_value(current_app_url, app_url): |
| 41 | + global_config_manager.update_app_base_url(app_url) |
| 42 | + config_updated = True |
| 43 | + |
| 44 | + current_client_id, current_client_secret = _CREDENTIALS_MANAGER.get_credentials_from_file() |
| 45 | + client_id = _get_client_id_input(current_client_id) |
| 46 | + client_secret = _get_client_secret_input(current_client_secret) |
| 47 | + |
| 48 | + credentials_updated = False |
| 49 | + if _should_update_value(current_client_id, client_id) or _should_update_value(current_client_secret, client_secret): |
| 50 | + credentials_updated = True |
| 51 | + _CREDENTIALS_MANAGER.update_credentials_file(client_id, client_secret) |
| 52 | + |
| 53 | + if config_updated: |
| 54 | + click.echo(_get_urls_update_result_message()) |
| 55 | + if credentials_updated: |
| 56 | + click.echo(_get_credentials_update_result_message()) |
| 57 | + |
| 58 | + |
| 59 | +def _get_client_id_input(current_client_id: Optional[str]) -> Optional[str]: |
| 60 | + prompt_text = 'Cycode Client ID' |
| 61 | + |
| 62 | + prompt_suffix = ' []: ' |
| 63 | + if current_client_id: |
| 64 | + prompt_suffix = f' [{obfuscate_text(current_client_id)}]: ' |
| 65 | + |
| 66 | + new_client_id = click.prompt(text=prompt_text, prompt_suffix=prompt_suffix, default='', show_default=False) |
| 67 | + return new_client_id or current_client_id |
| 68 | + |
| 69 | + |
| 70 | +def _get_client_secret_input(current_client_secret: Optional[str]) -> Optional[str]: |
| 71 | + prompt_text = 'Cycode Client Secret' |
| 72 | + |
| 73 | + prompt_suffix = ' []: ' |
| 74 | + if current_client_secret: |
| 75 | + prompt_suffix = f' [{obfuscate_text(current_client_secret)}]: ' |
| 76 | + |
| 77 | + new_client_secret = click.prompt(text=prompt_text, prompt_suffix=prompt_suffix, default='', show_default=False) |
| 78 | + return new_client_secret or current_client_secret |
| 79 | + |
| 80 | + |
| 81 | +def _get_app_url_input(current_app_url: Optional[str]) -> str: |
| 82 | + prompt_text = 'Cycode APP URL' |
| 83 | + |
| 84 | + default = consts.DEFAULT_CYCODE_APP_URL |
| 85 | + if current_app_url: |
| 86 | + default = current_app_url |
| 87 | + |
| 88 | + return click.prompt(text=prompt_text, default=default, type=click.STRING) |
| 89 | + |
| 90 | + |
| 91 | +def _get_api_url_input(current_api_url: Optional[str]) -> str: |
| 92 | + prompt_text = 'Cycode API URL' |
| 93 | + |
| 94 | + default = consts.DEFAULT_CYCODE_API_URL |
| 95 | + if current_api_url: |
| 96 | + default = current_api_url |
| 97 | + |
| 98 | + return click.prompt(text=prompt_text, default=default, type=click.STRING) |
| 99 | + |
| 100 | + |
| 101 | +def _get_credentials_update_result_message() -> str: |
| 102 | + success_message = _CREDENTIALS_UPDATED_SUCCESSFULLY_MESSAGE.format(filename=_CREDENTIALS_MANAGER.get_filename()) |
| 103 | + if _are_credentials_exist_in_environment_variables(): |
| 104 | + return f'{success_message}. {_CREDENTIALS_ARE_SET_IN_ENVIRONMENT_VARIABLES_MESSAGE}' |
| 105 | + |
| 106 | + return success_message |
| 107 | + |
| 108 | + |
| 109 | +def _are_credentials_exist_in_environment_variables() -> bool: |
| 110 | + client_id, client_secret = _CREDENTIALS_MANAGER.get_credentials_from_environment_variables() |
| 111 | + return any([client_id, client_secret]) |
| 112 | + |
| 113 | + |
| 114 | +def _get_urls_update_result_message() -> str: |
| 115 | + success_message = _URLS_UPDATED_SUCCESSFULLY_MESSAGE.format( |
| 116 | + filename=_CONFIGURATION_MANAGER.global_config_file_manager.get_filename() |
| 117 | + ) |
| 118 | + if _are_urls_exist_in_environment_variables(): |
| 119 | + return f'{success_message}. {_URLS_ARE_SET_IN_ENVIRONMENT_VARIABLES_MESSAGE}' |
| 120 | + |
| 121 | + return success_message |
| 122 | + |
| 123 | + |
| 124 | +def _are_urls_exist_in_environment_variables() -> bool: |
| 125 | + api_url = _CONFIGURATION_MANAGER.get_api_url_from_environment_variables() |
| 126 | + app_url = _CONFIGURATION_MANAGER.get_app_url_from_environment_variables() |
| 127 | + return any([api_url, app_url]) |
| 128 | + |
| 129 | + |
| 130 | +def _should_update_value( |
| 131 | + old_value: Optional[str], |
| 132 | + new_value: Optional[str], |
| 133 | +) -> bool: |
| 134 | + if not new_value: |
| 135 | + return False |
| 136 | + |
| 137 | + return old_value != new_value |
0 commit comments