def test_keyring_repository_password_del(keyring, config, repository, username, password): keyring.set_password(keyring_service_name(repository), username, password) config.merge({"http-basic": {repository: {"username": username}}}) keyring_repository_password_del(config, repository) assert keyring.get_password(keyring_service_name(repository), username) is None
def test_keyring_repository_password_del(keyring, config, repository, username, password): keyring.set_password(keyring_service_name(repository), username, password) config.add_property("http-basic.{}.username".format(repository), username) keyring_repository_password_del(config, repository) assert keyring.get_password(keyring_service_name(repository), username) is None
def handle(self): from poetry.config.file_config_source import FileConfigSource from poetry.locations import CONFIG_DIR from poetry.utils._compat import Path from poetry.utils._compat import basestring from poetry.utils.toml_file import TomlFile config = Factory.create_config(self.io) config_file = TomlFile(Path(CONFIG_DIR) / "config.toml") try: local_config_file = TomlFile(self.poetry.file.parent / "poetry.toml") if local_config_file.exists(): config.merge(local_config_file.read()) except RuntimeError: local_config_file = TomlFile(Path.cwd() / "poetry.toml") if self.option("local"): config.set_config_source(FileConfigSource(local_config_file)) if not config_file.exists(): config_file.path.parent.mkdir(parents=True, exist_ok=True) config_file.touch(mode=0o0600) if self.option("list"): self._list_configuration(config.all(), config.raw()) return 0 setting_key = self.argument("key") if not setting_key: return 0 if self.argument("value") and self.option("unset"): raise RuntimeError( "You can not combine a setting value with --unset") # show the value if no value is provided if not self.argument("value") and not self.option("unset"): m = re.match(r"^repos?(?:itories)?(?:\.(.+))?", self.argument("key")) if m: if not m.group(1): value = {} if config.get("repositories") is not None: value = config.get("repositories") else: repo = config.get("repositories.{}".format(m.group(1))) if repo is None: raise ValueError( "There is no {} repository defined".format( m.group(1))) value = repo self.line(str(value)) else: values = self.unique_config_values if setting_key not in values: raise ValueError( "There is no {} setting.".format(setting_key)) value = config.get(setting_key) if not isinstance(value, basestring): value = json.dumps(value) self.line(value) return 0 values = self.argument("value") unique_config_values = self.unique_config_values if setting_key in unique_config_values: if self.option("unset"): return config.config_source.remove_property(setting_key) return self._handle_single_value( config.config_source, setting_key, unique_config_values[setting_key], values, ) # handle repositories m = re.match(r"^repos?(?:itories)?(?:\.(.+))?", self.argument("key")) if m: if not m.group(1): raise ValueError( "You cannot remove the [repositories] section") if self.option("unset"): repo = config.get("repositories.{}".format(m.group(1))) if repo is None: raise ValueError( "There is no {} repository defined".format(m.group(1))) config.config_source.remove_property("repositories.{}".format( m.group(1))) return 0 if len(values) == 1: url = values[0] config.config_source.add_property( "repositories.{}.url".format(m.group(1)), url) return 0 raise ValueError( "You must pass the url. " "Example: poetry config repositories.foo https://bar.com") # handle auth m = re.match(r"^(http-basic|pypi-token)\.(.+)", self.argument("key")) if m: if self.option("unset"): keyring_repository_password_del(config, m.group(2)) config.auth_config_source.remove_property("{}.{}".format( m.group(1), m.group(2))) return 0 if m.group(1) == "http-basic": if len(values) == 1: username = values[0] # Only username, so we prompt for password password = self.secret("Password:"******"Expected one or two arguments " "(username, password), got {}".format( len(values))) else: username = values[0] password = values[1] property_value = dict(username=username) try: keyring_repository_password_set(m.group(2), username, password) except RuntimeError: property_value.update(password=password) config.auth_config_source.add_property( "{}.{}".format(m.group(1), m.group(2)), property_value) elif m.group(1) == "pypi-token": if len(values) != 1: raise ValueError( "Expected only one argument (token), got {}".format( len(values))) token = values[0] config.auth_config_source.add_property( "{}.{}".format(m.group(1), m.group(2)), token) return 0 # handle certs m = re.match(r"(?:certificates)\.([^.]+)\.(cert|client-cert)", self.argument("key")) if m: if self.option("unset"): config.auth_config_source.remove_property( "certificates.{}.{}".format(m.group(1), m.group(2))) return 0 if len(values) == 1: config.auth_config_source.add_property( "certificates.{}.{}".format(m.group(1), m.group(2)), values[0]) else: raise ValueError("You must pass exactly 1 value") return 0 raise ValueError("Setting {} does not exist".format( self.argument("key")))
def handle(self): if self.option("list"): self._list_configuration(self._settings_config.content) return 0 setting_key = self.argument("key") if not setting_key: return 0 if self.argument("value") and self.option("unset"): raise RuntimeError("You can not combine a setting value with --unset") # show the value if no value is provided if not self.argument("value") and not self.option("unset"): m = re.match(r"^repos?(?:itories)?(?:\.(.+))?", self.argument("key")) if m: if not m.group(1): value = {} if self._settings_config.setting("repositories") is not None: value = self._settings_config.setting("repositories") else: repo = self._settings_config.setting( "repositories.{}".format(m.group(1)) ) if repo is None: raise ValueError( "There is no {} repository defined".format(m.group(1)) ) value = repo self.line(str(value)) else: values = self.unique_config_values if setting_key not in values: raise ValueError("There is no {} setting.".format(setting_key)) values = self._get_setting( self._settings_config.content, setting_key, default=values[setting_key][-1], ) for value in values: self.line(value[1]) return 0 values = self.argument("value") unique_config_values = self.unique_config_values if setting_key in unique_config_values: if self.option("unset"): return self._remove_single_value(setting_key) return self._handle_single_value( setting_key, unique_config_values[setting_key], values ) # handle repositories m = re.match(r"^repos?(?:itories)?(?:\.(.+))?", self.argument("key")) if m: if not m.group(1): raise ValueError("You cannot remove the [repositories] section") if self.option("unset"): repo = self._settings_config.setting( "repositories.{}".format(m.group(1)) ) if repo is None: raise ValueError( "There is no {} repository defined".format(m.group(1)) ) self._settings_config.remove_property( "repositories.{}".format(m.group(1)) ) return 0 if len(values) == 1: url = values[0] self._settings_config.add_property( "repositories.{}.url".format(m.group(1)), url ) return 0 raise ValueError( "You must pass the url. " "Example: poetry config repositories.foo https://bar.com" ) # handle auth m = re.match(r"^(http-basic)\.(.+)", self.argument("key")) if m: if self.option("unset"): if not self._auth_config.setting( "{}.{}".format(m.group(1), m.group(2)) ): raise ValueError( "There is no {} {} defined".format(m.group(2), m.group(1)) ) keyring_repository_password_del(self._auth_config, m.group(2)) self._auth_config.remove_property( "{}.{}".format(m.group(1), m.group(2)) ) return 0 if m.group(1) == "http-basic": if len(values) == 1: username = values[0] # Only username, so we prompt for password password = self.secret("Password:"******"Expected one or two arguments " "(username, password), got {}".format(len(values)) ) else: username = values[0] password = values[1] property_value = dict(username=username) try: keyring_repository_password_set(m.group(2), username, password) except RuntimeError: property_value.update(password=password) self._auth_config.add_property( "{}.{}".format(m.group(1), m.group(2)), property_value ) return 0 raise ValueError("Setting {} does not exist".format(self.argument("key")))
def test_keyring_repository_password_del_broken(broken_keyring, config): assert get_keyring() == broken_keyring keyring_repository_password_del(config, "repository")