def test_access_token_config(self): config_dict = { POLYAXON_KEYS_AUTH_USERNAME: "******", POLYAXON_KEYS_AUTH_TOKEN: "sdfsdf098sdf80s9dSDF800", } config = AccessTokenConfig.from_dict(config_dict) assert config.to_dict() == config_dict
def patch_settings( set_auth: bool = True, set_client: bool = True, set_cli: bool = True, set_agent: bool = True, set_proxies: bool = True, ): settings.AUTH_CONFIG = None if set_auth: settings.AUTH_CONFIG = AccessTokenConfig() settings.CLIENT_CONFIG = None if set_client: settings.CLIENT_CONFIG = ClientConfig(host="1.2.3.4") settings.CLI_CONFIG = None if set_cli: settings.CLI_CONFIG = CliConfig() settings.AGENT_CONFIG = None if set_agent: settings.AGENT_CONFIG = AgentConfig() settings.PROXIES_CONFIG = None if set_proxies: settings.PROXIES_CONFIG = ProxiesConfig() settings.CLIENT_CONFIG.tracking_timeout = 0
def impersonate(owner, project, run_uuid): try: response = PolyaxonClient().runs_v1.impersonate_token( owner, project, run_uuid) polyaxon_client = PolyaxonClient(token=response.token) user = polyaxon_client.users_v1.get_user() access_token = AccessTokenConfig(username=user.username, token=response.token) create_context_auth(access_token) except (ApiException, HTTPError) as e: raise PolyaxonClientException( "This worker is not allowed to run this job %s." % e)
def test_create_context_auth(self): token = uuid.uuid4().hex context_mount = tempfile.mkdtemp() context_mount_auth = "{}/.auth".format(context_mount) # Login without updating the token and without persistence if os.path.exists(context_mount_auth): os.remove(context_mount_auth) assert os.path.exists(context_mount_auth) is False create_context_auth(AccessTokenConfig(token=token), context_mount_auth) assert os.path.exists(context_mount_auth) is True
def get_config_from_env(cls) -> AccessTokenConfig: tmp_path = os.path.join(CONTEXT_TMP_POLYAXON_PATH, cls.CONFIG_FILE_NAME) user_path = os.path.join(CONTEXT_USER_POLYAXON_PATH, cls.CONFIG_FILE_NAME) auth_config = ConfigManager.read_configs([ os.environ, ConfigSpec(tmp_path, config_type=".json", check_if_exists=False), ConfigSpec(user_path, config_type=".json", check_if_exists=False), ConfigSpec(CONTEXT_MOUNT_AUTH, config_type=".json", check_if_exists=False), { "dummy": "dummy" }, ]) return AccessTokenConfig.from_dict(auth_config.data)
def setUp(self): super().setUp() settings.AUTH_CONFIG = None if self.SET_AUTH_SETTINGS: settings.AUTH_CONFIG = AccessTokenConfig() settings.CLIENT_CONFIG = None if self.SET_CLIENT_SETTINGS: settings.CLIENT_CONFIG = ClientConfig() settings.AGENT_CONFIG = None if self.SET_AGENT_SETTINGS: settings.AGENT_CONFIG = AgentConfig() settings.PROXIES_CONFIG = None if self.SET_PROXIES_SETTINGS: settings.PROXIES_CONFIG = ProxiesConfig() fpath = tempfile.mkdtemp() settings.CLIENT_CONFIG.agent_path = fpath
def login(token, username, password): """Login to Polyaxon.""" polyaxon_client = PolyaxonClient() if username and not token: # Use user or email / password login if not password: password = click.prompt( "Please enter your password", type=str, hide_input=True ) password = password.strip() if not password: logger.info( "You entered an empty string. " "Please make sure you enter your password correctly." ) sys.exit(1) try: body = V1Credentials(username=username, password=password) access_auth = polyaxon_client.auth_v1.login(body=body) except (ApiException, HTTPError) as e: AuthConfigManager.purge() CliConfigManager.purge() handle_cli_error(e, message="Could not login.") sys.exit(1) if not access_auth.token: Printer.print_error("Failed to login") return else: if not token: token_url = "{}/profile/token".format( clean_host(polyaxon_client.config.host) ) click.confirm( "Authentication token page will now open in your browser. Continue?", abort=True, default=True, ) click.launch(token_url) logger.info("Please copy and paste the authentication token.") token = click.prompt( "This is an invisible field. Paste token and press ENTER", type=str, hide_input=True, ) if not token: logger.info( "Empty token received. " "Make sure your shell is handling the token appropriately." ) logger.info( "See docs for help: http://polyaxon.com/docs/polyaxon_cli/commands/auth" ) return access_auth = polyaxon_sdk.models.V1Auth(token=token.strip(" ")) # Set user try: AuthConfigManager.purge() polyaxon_client = PolyaxonClient(token=access_auth.token) user = polyaxon_client.users_v1.get_user() except (ApiException, HTTPError) as e: handle_cli_error(e, message="Could not load user info.") sys.exit(1) access_token = AccessTokenConfig(username=user.username, token=access_auth.token) AuthConfigManager.set_config(access_token) polyaxon_client.config.token = access_auth.token Printer.print_success("Login successful") # Reset current cli server_versions = get_server_versions(polyaxon_client=polyaxon_client) current_version = get_current_version() log_handler = get_log_handler(polyaxon_client=polyaxon_client) CliConfigManager.reset( check_count=0, current_version=current_version, server_versions=server_versions.to_dict(), log_handler=log_handler, )
{"dummy": "dummy"}, ] ) config = rhea.Rhea.read_configs( [ os.environ, rhea.ConfigSpec( TMP_CLIENT_CONFIG_PATH, config_type=".json", check_if_exists=False ), rhea.ConfigSpec( USER_CLIENT_CONFIG_PATH, config_type=".json", check_if_exists=False ), ] ) AUTH_CONFIG = AccessTokenConfig.from_dict(auth_config.data) CLIENT_CONFIG = ClientConfig.from_dict(config.data) HASH_LENGTH = config.get_int(POLYAXON_KEYS_HASH_LENGTH, is_optional=True, default=12) HEALTH_CHECK_URL = config.get_string(POLYAXON_KEYS_HEALTH_CHECK_URL, is_optional=True) RECONCILE_URL = config.get_string(POLYAXON_KEYS_RECONCILE_URL, is_optional=True) MIN_TIMEOUT = config.get_int("POLYAXON_MIN_TIMEOUT", is_optional=True, default=1) REQUEST_TIMEOUT = config.get_int( "POLYAXON_REQUEST_TIMEOUT", is_optional=True, default=25 ) LONG_REQUEST_TIMEOUT = config.get_int( "POLYAXON_LONG_REQUEST_TIMEOUT", is_optional=True, default=3600 ) HEALTH_CHECK_INTERVAL = config.get_int( "HEALTH_CHECK_INTERVAL", is_optional=True, default=60