Allow dashboard import to specify if api encryption key should be generated (#4393)

This commit is contained in:
Jesse Hills 2023-02-07 12:27:07 +13:00 committed by GitHub
parent 93ddce2e79
commit 3facfa5c21
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 8 deletions

View file

@ -1,3 +1,5 @@
import base64
import secrets
from pathlib import Path
from typing import Optional
@ -73,6 +75,7 @@ def import_config(
project_name: str,
import_url: str,
network: str = CONF_WIFI,
encryption: bool = False,
) -> None:
p = Path(path)
@ -80,15 +83,21 @@ def import_config(
raise FileExistsError
if project_name == "esphome.web":
kwargs = {
"name": name,
"friendly_name": friendly_name,
"platform": "ESP32" if "esp32" in import_url else "ESP8266",
"board": "esp32dev" if "esp32" in import_url else "esp01_1m",
"ssid": "!secret wifi_ssid",
"psk": "!secret wifi_password",
}
if encryption:
noise_psk = secrets.token_bytes(32)
key = base64.b64encode(noise_psk).decode()
kwargs["api_encryption_key"] = key
p.write_text(
wizard_file(
name=name,
friendly_name=friendly_name,
platform="ESP32" if "esp32" in import_url else "ESP8266",
board="esp32dev" if "esp32" in import_url else "esp01_1m",
ssid="!secret wifi_ssid",
psk="!secret wifi_password",
),
wizard_file(**kwargs),
encoding="utf8",
)
else:
@ -115,6 +124,11 @@ def import_config(
"packages": {project_name: import_url},
"esphome": esphome_core,
}
if encryption:
noise_psk = secrets.token_bytes(32)
key = base64.b64encode(noise_psk).decode()
config["api"] = {"encryption": {"key": key}}
output = dump(config)
if network == CONF_WIFI:

View file

@ -433,6 +433,7 @@ class ImportRequestHandler(BaseHandler):
try:
name = args["name"]
friendly_name = args.get("friendly_name")
encryption = args.get("encryption", False)
imported_device = next(
(res for res in IMPORT_RESULT.values() if res.device_name == name), None
@ -452,6 +453,7 @@ class ImportRequestHandler(BaseHandler):
args["project_name"],
args["package_import_url"],
network,
encryption,
)
except FileExistsError:
self.set_status(500)