def auth_create(ctx, # pylint: disable=too-many-arguments authentication_provider, host, port, name, set_default, api_endpoint, user, password): """ command """ auth_info = { 'name': name, 'authentication-type': authentication_provider, 'default': set_default } if user: if password is None: password = click.prompt("Password", type=str, hide_input=True) auth_info.update({ 'user': user, 'password': password}) if authentication_provider == \ constants.AUTHENTICATION_PROVIDERS.get(constants.CLOUD_SERVICES_GROUP_NAME): if api_endpoint is not None: auth_info['api_endpoint'] = api_endpoint else: if host is not None: auth_info['host'] = host auth_info['port'] = port auth_client = AuthConfigurationClient(auth=auth_info) auth_client.store_auth('create') ctx.log('Authentication configured successfully')
def test_update_non_existing_account( mocker, yaml_load_fixture_auth, os_path_exists_fixture, # pylint: disable=unused-argument os_path_isfile_fixture): # pylint: disable=unused-argument """ Update non-existing account Given - Auth file containing multiple accounts When - store_auth('update') is invoked with auth details Then - store_auth raises an exception when trying to update a non existing account """ new_account = {'name': 'bigip4'} copy_typical_auth_contents = copy.deepcopy(TYPICAL_AUTH_CONTENTS) yaml_load_fixture_auth.return_value = copy_typical_auth_contents client = AuthConfigurationClient(auth=new_account) with pytest.raises(click.exceptions.ClickException) as error: with mocker.patch('f5cli.config.auth.open', new_callable=mocker.mock_open()): client.store_auth('update') assert error.value.args[0] == f"Update command failed." \ f" A account of bigip4 name does not exist."
def auth_update(ctx, # pylint: disable=too-many-arguments authentication_provider, host, port, name, set_default, api_endpoint, user, password): """ command """ auth_info = { 'name': name, 'authentication-type': authentication_provider, 'default': set_default, 'host': host, 'port': port, 'api-endpoint': api_endpoint } if user: if password is None: password = click.prompt("Password", type=str, hide_input=True) auth_info.update({ 'user': user, 'password': password}) auth_client = AuthConfigurationClient(auth=auth_info) auth_client.store_auth('update') ctx.log('Authentication updated successfully')
def test_create_pre_existing_account( mocker, yaml_load_fixture_auth, os_path_exists_fixture, # pylint: disable=unused-argument os_path_isfile_fixture): # pylint: disable=unused-argument """ Attempt to perform crate auth account when an auth account of the same name has already been configured Given - Auth file containing multiple accounts When - store_auth('create') is invoked with auth details Then - store_auth raises an exception when trying to create a pre-existing account """ new_account = {'name': 'cs1'} copy_typical_auth_contents = copy.deepcopy(TYPICAL_AUTH_CONTENTS) yaml_load_fixture_auth.return_value = copy_typical_auth_contents client = AuthConfigurationClient(auth=new_account) with pytest.raises(click.exceptions.ClickException) as error: with mocker.patch('f5cli.config.core.open', new_callable=mocker.mock_open()): client.store_auth('create') assert error.value.args[0] == f"Create command failed. " \ f"A account of cs1 name already exists."
def test_write_nonexist_config_directory(mocker, os_path_exists_fixture, os_path_isfile_fixture): """ Write Auth file into a non-exist directory Given - Config directory does not exist When - store_auth() is invoked Then - Config directory is created - Auth file is written to disk """ mock_path_exist = os_path_exists_fixture mock_path_exist.return_value = False mock_path_is_file = os_path_isfile_fixture mock_path_is_file.return_value = False mock_make_dir = mocker.patch("f5cli.config.core.os.makedirs") auth = {'name': 'blah', 'username': '******', 'password': '******'} client = AuthConfigurationClient(auth=auth) mock_yaml_safe_dump = mocker.patch("f5cli.config.core.yaml.safe_dump") with mocker.patch('f5cli.config.core.open', new_callable=mocker.mock_open()): client.store_auth('create') mock_yaml_safe_dump.assert_called_once() mock_make_dir.assert_called_once() assert mock_yaml_safe_dump.call_args_list[0][0][0][0] == auth
def cli(ctx, authentication_provider, host, port, api_endpoint, user, password): """ command """ if authentication_provider == \ constants.AUTHENTICATION_PROVIDERS.get(constants.BIGIP_GROUP_NAME): if host is None: host = click.prompt("Host", type=str) auth_info = { 'name': BIGIP_AUTH_ACCOUNT_NAME, 'authentication-type': authentication_provider, 'default': True, 'host': host, 'port': port } else: auth_info = { 'name': CS_AUTH_ACCOUNT_NAME, 'authentication-type': authentication_provider, 'default': True } if api_endpoint is not None: auth_info['api_endpoint'] = api_endpoint if user is None: user = click.prompt("User", type=str) auth_info['user'] = user if password is None: password = click.prompt("Password", type=str, hide_input=True) auth_info['password'] = password # Validate credentials if authentication_provider == \ constants.AUTHENTICATION_PROVIDERS.get(constants.BIGIP_GROUP_NAME): try: management_kwargs = dict(port=auth_info['port'], user=auth_info['user'], password=auth_info['password']) BigipManagementClient(auth_info['host'], **management_kwargs) except (DeviceReadyError, HTTPError) as error: raise click.ClickException(f"Failed to login to BIG-IP: {error}") else: try: CSManagementClient(user=auth_info['user'], password=auth_info['password'], api_endpoint=auth_info.pop( 'api_endpoint', None)) except HTTPError as error: raise click.ClickException( f"Failed to login to Cloud Services: {error}") # Store credentials in auth file auth_client = AuthConfigurationClient(auth=auth_info) try: auth_client.store_auth('create') except click.ClickException: auth_client.store_auth('update') ctx.log('Logged in successfully')
def test_write_to_existing_auth_file( mocker, yaml_load_fixture_auth, os_path_exists_fixture, # pylint: disable=unused-argument os_path_isfile_fixture): # pylint: disable=unused-argument """ Write credentials to an existing Auth file Given - Config directory exists - Auth file exists with contents - Create action is invoked When - store_auth('create') is invoked Then - The existing Auth file is read - The Auth file is overwritten, containing merged credentials """ bigip_auth = { 'name': 'bigip_auth', 'username': '******', 'password': '******', 'host': '1.2.3.4', 'type': 'BIGIP', 'default': 'true' } client = AuthConfigurationClient(auth=bigip_auth) cs_auth = { 'name': 'cs_auth', 'username': '******', 'password': '******', 'type': 'CS', 'default': 'true' } yaml_load_fixture_auth.return_value = [cs_auth] mock_yaml_safe_dump = mocker.patch("f5cli.config.core.yaml.safe_dump") with mocker.patch('f5cli.config.core.open', new_callable=mocker.mock_open()): client.store_auth('create') mock_yaml_safe_dump.assert_called_once() mock_yaml_dump_wrote = mock_yaml_safe_dump.call_args_list[0][0][0] assert mock_yaml_dump_wrote[0] == cs_auth assert mock_yaml_dump_wrote[1] == bigip_auth
def test_update_existing_account( mocker, yaml_load_fixture_auth, os_path_exists_fixture, # pylint: disable=unused-argument os_path_isfile_fixture): # pylint: disable=unused-argument """ Update an existing account Given - Auth file contains multiple accounts - The account name to the updated exists in the auth file When - store_auth('update') Then - Auth file is updated with the new account and the default account for the specific authentication provider is updated """ new_account = {'name': 'bigip3', 'host': 'host2', 'default': True} mock_yaml_safe_dump = mocker.patch("f5cli.config.core.yaml.safe_dump") copy_typical_auth_contents = copy.deepcopy(TYPICAL_AUTH_CONTENTS) yaml_load_fixture_auth.return_value = copy_typical_auth_contents client = AuthConfigurationClient(auth=new_account) with mocker.patch('f5cli.config.core.open', new_callable=mocker.mock_open()): client.store_auth('update') mock_yaml_safe_dump.assert_called_once() mock_yaml_dump_wrote = mock_yaml_safe_dump.call_args_list[0][0][0] assert not mock_yaml_dump_wrote[3].get('default') returned_account = mock_yaml_dump_wrote[4] expected_result = TYPICAL_AUTH_CONTENTS[4] assert returned_account.get('host') == new_account.get('host') assert returned_account.get('default') == new_account.get('default') assert returned_account.get('user') == expected_result.get('user') assert returned_account.get('password') == expected_result.get( 'password') assert returned_account.get('type') == expected_result.get('type')
def test_write_exist_config_directory( mocker, os_path_exists_fixture, # pylint: disable=unused-argument os_path_isfile_fixture): """ Write credentials to Auth file in an existing directory Given - Config directory exist - Auth file does not exist When - store_auth() is invoked Then - The credentials are written to the Auth file """ client = AuthConfigurationClient(auth={'name': 'blah'}) mock_path_isfile = os_path_isfile_fixture mock_path_isfile.return_value = False mock_yaml_safe_dump = mocker.patch("f5cli.config.core.yaml.safe_dump") with mocker.patch('f5cli.config.core.open', new_callable=mocker.mock_open()): client.store_auth('create') mock_yaml_safe_dump.assert_called_once()
def test_update_no_auth_file( mocker, yaml_load_fixture_auth, os_path_exists_fixture, # pylint: disable=unused-argument os_path_isfile_fixture): # pylint: disable=unused-argument """ Attempt to perform update auth account when no auth accounts have been configured yet Given - No Auth file When - store_auth('update') is invoked with auth details Then - store_auth raises an exception """ new_account = {'name': 'bigip4'} yaml_load_fixture_auth.return_value = None client = AuthConfigurationClient(auth=new_account) with pytest.raises(click.exceptions.ClickException) as error: with mocker.patch('f5cli.config.auth.open', new_callable=mocker.mock_open()): client.store_auth('update') assert error.value.args[0] == f"Update command failed. " \ f"No accounts have been configured yet"