Add endpoint to fetch secrets keys (#2873)

This commit is contained in:
Jesse Hills 2021-12-06 20:15:34 +13:00 committed by GitHub
parent 71fe2f7ed3
commit 55db190875
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 4 deletions

View File

@ -27,7 +27,7 @@ import tornado.process
import tornado.web
import tornado.websocket
from esphome import const, platformio_api, util
from esphome import const, platformio_api, util, yaml_util
from esphome.helpers import mkdir_p, get_bool_env, run_system_command
from esphome.storage_json import (
EsphomeStorageJSON,
@ -836,6 +836,28 @@ class LogoutHandler(BaseHandler):
self.redirect("./login")
class SecretKeysRequestHandler(BaseHandler):
@authenticated
def get(self):
filename = None
for secret_filename in const.SECRETS_FILES:
relative_filename = settings.rel_path(secret_filename)
if os.path.isfile(relative_filename):
filename = relative_filename
break
if filename is None:
self.send_error(404)
return
secret_keys = list(yaml_util.load_yaml(filename, clear_secrets=False))
self.set_header("content-type", "application/json")
self.write(json.dumps(secret_keys))
def get_base_frontend_path():
if ENV_DEV not in os.environ:
import esphome_dashboard
@ -939,6 +961,7 @@ def make_app(debug=get_bool_env(ENV_DEV)):
(f"{rel}static/(.*)", StaticFileHandler, {"path": get_static_path()}),
(f"{rel}devices", ListDevicesHandler),
(f"{rel}import", ImportRequestHandler),
(f"{rel}secret_keys", SecretKeysRequestHandler),
],
**app_settings,
)

View File

@ -329,9 +329,10 @@ ESPHomeLoader.add_constructor("!lambda", ESPHomeLoader.construct_lambda)
ESPHomeLoader.add_constructor("!force", ESPHomeLoader.construct_force)
def load_yaml(fname):
_SECRET_VALUES.clear()
_SECRET_CACHE.clear()
def load_yaml(fname, clear_secrets=True):
if clear_secrets:
_SECRET_VALUES.clear()
_SECRET_CACHE.clear()
return _load_yaml_internal(fname)