mirror of
https://github.com/home-assistant/core.git
synced 2026-04-20 10:07:12 +00:00
Proxmox re-use sanitize UserID (#164303)
This commit is contained in:
16
homeassistant/components/proxmoxve/common.py
Normal file
16
homeassistant/components/proxmoxve/common.py
Normal file
@@ -0,0 +1,16 @@
|
||||
"""Common methods for Proxmox VE integration."""
|
||||
|
||||
from typing import Any
|
||||
|
||||
from homeassistant.const import CONF_USERNAME
|
||||
|
||||
from .const import CONF_REALM
|
||||
|
||||
|
||||
def sanitize_userid(data: dict[str, Any]) -> str:
|
||||
"""Sanitize the user ID."""
|
||||
return (
|
||||
data[CONF_USERNAME]
|
||||
if "@" in data[CONF_USERNAME]
|
||||
else f"{data[CONF_USERNAME]}@{data[CONF_REALM]}"
|
||||
)
|
||||
@@ -23,6 +23,7 @@ from homeassistant.const import (
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.helpers import config_validation as cv
|
||||
|
||||
from .common import sanitize_userid
|
||||
from .const import (
|
||||
CONF_CONTAINERS,
|
||||
CONF_NODE,
|
||||
@@ -48,22 +49,13 @@ CONFIG_SCHEMA = vol.Schema(
|
||||
)
|
||||
|
||||
|
||||
def _sanitize_userid(data: dict[str, Any]) -> str:
|
||||
"""Sanitize the user ID."""
|
||||
return (
|
||||
data[CONF_USERNAME]
|
||||
if "@" in data[CONF_USERNAME]
|
||||
else f"{data[CONF_USERNAME]}@{data[CONF_REALM]}"
|
||||
)
|
||||
|
||||
|
||||
def _get_nodes_data(data: dict[str, Any]) -> list[dict[str, Any]]:
|
||||
"""Validate the user input and fetch data (sync, for executor)."""
|
||||
try:
|
||||
client = ProxmoxAPI(
|
||||
data[CONF_HOST],
|
||||
port=data[CONF_PORT],
|
||||
user=_sanitize_userid(data),
|
||||
user=sanitize_userid(data),
|
||||
password=data[CONF_PASSWORD],
|
||||
verify_ssl=data.get(CONF_VERIFY_SSL, DEFAULT_VERIFY_SSL),
|
||||
)
|
||||
|
||||
@@ -14,13 +14,7 @@ import requests
|
||||
from requests.exceptions import ConnectTimeout, SSLError
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import (
|
||||
CONF_HOST,
|
||||
CONF_PASSWORD,
|
||||
CONF_PORT,
|
||||
CONF_USERNAME,
|
||||
CONF_VERIFY_SSL,
|
||||
)
|
||||
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_PORT, CONF_VERIFY_SSL
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import (
|
||||
ConfigEntryAuthFailed,
|
||||
@@ -29,7 +23,8 @@ from homeassistant.exceptions import (
|
||||
)
|
||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
||||
|
||||
from .const import CONF_NODE, CONF_REALM, DEFAULT_VERIFY_SSL, DOMAIN
|
||||
from .common import sanitize_userid
|
||||
from .const import CONF_NODE, DEFAULT_VERIFY_SSL, DOMAIN
|
||||
|
||||
type ProxmoxConfigEntry = ConfigEntry[ProxmoxCoordinator]
|
||||
|
||||
@@ -177,16 +172,10 @@ class ProxmoxCoordinator(DataUpdateCoordinator[dict[str, ProxmoxNodeData]]):
|
||||
|
||||
def _init_proxmox(self) -> None:
|
||||
"""Initialize ProxmoxAPI instance."""
|
||||
user_id = (
|
||||
self.config_entry.data[CONF_USERNAME]
|
||||
if "@" in self.config_entry.data[CONF_USERNAME]
|
||||
else f"{self.config_entry.data[CONF_USERNAME]}@{self.config_entry.data[CONF_REALM]}"
|
||||
)
|
||||
|
||||
self.proxmox = ProxmoxAPI(
|
||||
host=self.config_entry.data[CONF_HOST],
|
||||
port=self.config_entry.data[CONF_PORT],
|
||||
user=user_id,
|
||||
user=sanitize_userid(dict(self.config_entry.data)),
|
||||
password=self.config_entry.data[CONF_PASSWORD],
|
||||
verify_ssl=self.config_entry.data.get(CONF_VERIFY_SSL, DEFAULT_VERIFY_SSL),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user