def test_build_client_token_auth(self): config = TNCOEnvironment( address='https://testing', secure=True, auth_mode='token', token='123' ) client = config.build_client() self.assertIsInstance(client, TNCOClient) self.assertIsInstance(client.auth_type, JwtTokenAuth) self.assertEqual(client.auth_type.token, '123')
def test_build_client_credentials_auth(self): config = TNCOEnvironment( address='https://testing', secure=True, client_id='TNCOClient', client_secret='sosecret' ) client = config.build_client() self.assertIsInstance(client, TNCOClient) self.assertIsInstance(client.auth_type, ClientCredentialsAuth) self.assertEqual(client.auth_type.client_id, 'TNCOClient') self.assertEqual(client.auth_type.client_secret, 'sosecret')
def test_build_client_user_pass_auth(self): config = TNCOEnvironment( address='https://testing', secure=True, client_id='TNCOClient', client_secret='sosecret', username='******', password='******' ) client = config.build_client() self.assertIsInstance(client, TNCOClient) self.assertIsInstance(client.auth_type, UserPassAuth) self.assertEqual(client.auth_type.username, 'user') self.assertEqual(client.auth_type.password, 'secret') self.assertEqual(client.auth_type.client_id, 'TNCOClient') self.assertEqual(client.auth_type.client_secret, 'sosecret')
def test_build_client_legacy_auth(self): config = TNCOEnvironment( address='https://testing', secure=True, username='******', password='******', auth_host='auth', auth_port=81, auth_protocol='https' ) client = config.build_client() self.assertIsInstance(client, TNCOClient) self.assertIsInstance(client.auth_type, LegacyUserPassAuth) self.assertEqual(client.auth_type.username, 'user') self.assertEqual(client.auth_type.password, 'secret') self.assertEqual(client.auth_type.legacy_auth_address, 'https://auth:81')
def test_build_client_zen_auth(self): config = TNCOEnvironment( address='https://testing', secure=True, auth_mode='zen', username='******', api_key='secret', auth_host='auth', auth_port=81, auth_protocol='https' ) client = config.build_client() self.assertIsInstance(client, TNCOClient) self.assertIsInstance(client.auth_type, ZenAPIKeyAuth) self.assertEqual(client.auth_type.username, 'user') self.assertEqual(client.auth_type.api_key, 'secret') self.assertEqual(client.auth_type.zen_auth_address, 'https://auth:81')
def test_build_client_sets_address(self): config = TNCOEnvironment( host='test', port=80, protocol='http', path='gateway', secure=True, username='******', password='******', auth_host='auth', auth_port=81, auth_protocol='https' ) client = config.build_client() self.assertIsInstance(client, TNCOClient) self.assertEqual(client.address, 'http://test:80/gateway') self.assertEqual(client.kami_address, 'http://test:31289')
def login(ctx: click.Context, address: str, username: str = None, pwd: str = None, api_key: str = None, client_id: str = None, client_secret: str = None, token: str = None, name: str = None, auth_address: str = None, save_creds: bool = False, yes_to_prompts: bool = False, print_token: bool = False, is_zen: bool = False): # Support missing config file by pre-creating one path = find_config_location(ignore_not_found=True) if not os.path.exists(path): with open(path, 'w') as f: f.write('environments: {}') ctl = get_global_controller(override_config_path=path) _error_if_set(ctx, '--token', token, '--username', username) _error_if_set(ctx, '--token', token, '-p, --pwd, --password', pwd) _error_if_set(ctx, '--token', token, '--client', client_id) _error_if_set(ctx, '--token', token, '--client-secret', client_secret) _error_if_set(ctx, '--token', token, '--auth-address', auth_address) _error_if_set(ctx, '--token', token, '--api-key', api_key) if is_zen: # Only test "is_zen" when set to True _error_if_set(ctx, '--token', token, '--zen', is_zen) if token is None and client_id is None and username is None: # No credentials passed, must prompt if is_zen: auth_address = _prompt_if_not_set(ctl, 'Auth Address', auth_address) username = _prompt_if_not_set(ctl, 'Username', username) api_key = _prompt_if_not_set(ctl, 'API Key', api_key, secret=True) else: # If auth address is not set then we must prompt for client credentials in addition to username/password if auth_address is None: client_id = _prompt_if_not_set(ctl, 'Client ID', client_id) client_secret = _prompt_if_not_set(ctl, 'Client Secret', client_secret, secret=True) username = _prompt_if_not_set(ctl, 'Username', username) pwd = _prompt_if_not_set(ctl, 'Password', pwd, secret=True) elif token is None: if client_id is not None: client_secret = _prompt_if_not_set(ctl, 'Client Secret', client_secret, secret=True) if username is not None: if client_id is None and auth_address is None: raise click.BadArgumentUsage( message= f'Must specify "--auth-address" option when attempting to authenticate with username/password/api_key but without client/client-secret', ctx=ctx) if is_zen: api_key = _prompt_if_not_set(ctl, 'API Key', api_key, secret=True) else: pwd = _prompt_if_not_set(ctl, 'Password', pwd, secret=True) auth_mode = OAUTH_MODE if token is not None: auth_mode = TOKEN_AUTH_MODE elif is_zen: auth_mode = ZEN_AUTH_MODE tnco_env = TNCOEnvironment(address=address, secure=True, client_id=client_id, client_secret=client_secret, username=username, password=pwd, api_key=api_key, token=token, auth_mode=auth_mode, auth_address=auth_address) client = tnco_env.build_client() access_token = client.get_access_token() if print_token: ctl.io.print(access_token) else: ctl.io.print('Login success') name = _ensure_name(ctl, name, yes_to_prompts) if not save_creds or token is not None: tnco_env_dict = { 'address': address, 'secure': True, 'token': access_token, 'auth_mode': TOKEN_AUTH_MODE } else: tnco_env_dict = { 'address': address, 'secure': True, 'auth_mode': auth_mode } if client_id is not None: tnco_env_dict['client_id'] = client_id if client_secret is not None: tnco_env_dict['client_secret'] = client_secret if username is not None: tnco_env_dict['username'] = username if pwd is not None: tnco_env_dict['password'] = pwd if auth_address is not None: tnco_env_dict['auth_address'] = auth_address if api_key is not None: tnco_env_dict['api_key'] = api_key tnco_env = TNCOEnvironment(**tnco_env_dict) # Write if name not in ctl.config.environments: ctl.config.environments[name] = EnvironmentGroup(name=name) ctl.config.environments[name].tnco = tnco_env ctl.config.active_environment = name ctl.io.print(f'Updating config at: {ctl.config_path}') write_config(ctl.config, override_config_path=ctl.config_path)