Allow self signed certs on octoprint server (#59213)

This commit is contained in:
Ryan Fleming 2022-01-10 07:15:38 -05:00 committed by GitHub
parent 69c8a02e16
commit 77d22c8542
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 33 additions and 19 deletions

View File

@ -18,6 +18,7 @@ from homeassistant.const import (
CONF_PORT,
CONF_SENSORS,
CONF_SSL,
CONF_VERIFY_SSL,
Platform,
)
from homeassistant.core import HomeAssistant
@ -154,7 +155,12 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
if DOMAIN not in hass.data:
hass.data[DOMAIN] = {}
websession = async_get_clientsession(hass)
if CONF_VERIFY_SSL not in entry.data:
data = {**entry.data, CONF_VERIFY_SSL: True}
hass.config_entries.async_update_entry(entry, data=data)
verify_ssl = entry.data[CONF_VERIFY_SSL]
websession = async_get_clientsession(hass, verify_ssl=verify_ssl)
client = OctoprintClient(
entry.data[CONF_HOST],
websession,

View File

@ -14,6 +14,7 @@ from homeassistant.const import (
CONF_PORT,
CONF_SSL,
CONF_USERNAME,
CONF_VERIFY_SSL,
)
from homeassistant.helpers.aiohttp_client import async_get_clientsession
import homeassistant.helpers.config_validation as cv
@ -23,7 +24,9 @@ from .const import DOMAIN
_LOGGER = logging.getLogger(__name__)
def _schema_with_defaults(username="", host="", port=80, path="/", ssl=False):
def _schema_with_defaults(
username="", host="", port=80, path="/", ssl=False, verify_ssl=True
):
return vol.Schema(
{
vol.Required(CONF_USERNAME, default=username): str,
@ -31,6 +34,7 @@ def _schema_with_defaults(username="", host="", port=80, path="/", ssl=False):
vol.Required(CONF_PORT, default=port): cv.port,
vol.Required(CONF_PATH, default=path): str,
vol.Required(CONF_SSL, default=ssl): bool,
vol.Required(CONF_VERIFY_SSL, default=verify_ssl): bool,
},
extra=vol.ALLOW_EXTRA,
)
@ -80,6 +84,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
user_input[CONF_PORT],
user_input[CONF_PATH],
user_input[CONF_SSL],
user_input[CONF_VERIFY_SSL],
),
)
@ -111,14 +116,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
async def _finish_config(self, user_input):
"""Finish the configuration setup."""
session = async_get_clientsession(self.hass)
octoprint = OctoprintClient(
user_input[CONF_HOST],
session,
user_input[CONF_PORT],
user_input[CONF_SSL],
user_input[CONF_PATH],
)
octoprint = self._get_octoprint_client(user_input)
octoprint.set_api_key(user_input[CONF_API_KEY])
try:
@ -183,14 +181,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
async def _async_get_auth_key(self, user_input: dict):
"""Get application api key."""
session = async_get_clientsession(self.hass)
octoprint = OctoprintClient(
user_input[CONF_HOST],
session,
user_input[CONF_PORT],
user_input[CONF_SSL],
user_input[CONF_PATH],
)
octoprint = self._get_octoprint_client(user_input)
try:
user_input[CONF_API_KEY] = await octoprint.request_app_key(
@ -204,6 +195,18 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
)
)
def _get_octoprint_client(self, user_input: dict) -> OctoprintClient:
"""Build an octoprint client from the user_input."""
verify_ssl = user_input.get(CONF_VERIFY_SSL, True)
session = async_get_clientsession(self.hass, verify_ssl=verify_ssl)
return OctoprintClient(
user_input[CONF_HOST],
session,
user_input[CONF_PORT],
user_input[CONF_SSL],
user_input[CONF_PATH],
)
class CannotConnect(exceptions.HomeAssistantError):
"""Error to indicate we cannot connect."""

View File

@ -8,6 +8,7 @@
"path": "Application Path",
"port": "Port Number",
"ssl": "Use SSL",
"verify_ssl": "[%key:common::config_flow::data::verify_ssl%]",
"username": "[%key:common::config_flow::data::username%]"
}
}

View File

@ -21,7 +21,8 @@
"path": "Application Path",
"port": "Port Number",
"ssl": "Use SSL",
"username": "Username"
"username": "Username",
"verify_ssl": "Verify SSL certificate"
}
}
}

View File

@ -63,6 +63,7 @@ async def test_form(hass):
"port": 81,
"ssl": True,
"path": "/",
"verify_ssl": True,
}
assert len(mock_setup.mock_calls) == 1
assert len(mock_setup_entry.mock_calls) == 1
@ -107,6 +108,7 @@ async def test_form_cannot_connect(hass):
"name": "Printer",
"port": 81,
"ssl": True,
"verify_ssl": True,
"path": "/",
"api_key": "test-key",
},
@ -157,6 +159,7 @@ async def test_form_unknown_exception(hass):
"ssl": True,
"path": "/",
"api_key": "test-key",
"verify_ssl": True,
},
)