Skip to content

Commit

Permalink
Allow hostname and password override from balena device variables
Browse files Browse the repository at this point in the history
Relates to #679
  • Loading branch information
shawaj committed Oct 20, 2024
1 parent 44facbe commit f404541
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
11 changes: 10 additions & 1 deletion hw_diag/utilities/auth.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os
import logging
import datetime
import bcrypt

Expand Down Expand Up @@ -68,12 +70,19 @@ def write_password(password):


def read_password():
PASSWORD_OVERRIDE = os.getenv('PASSWORD_OVERRIDE', 'false')
try:
password_row = g.db.query(AuthKeyValue). \
filter(AuthKeyValue.key == 'password_hash'). \
one()
except NoResultFound:
default_password = generate_default_password()
if PASSWORD_OVERRIDE != "false": # nosec
default_password = PASSWORD_OVERRIDE
logging.info("Using password from override env var!")
else:
default_password = generate_default_password()
logging.info("No password override. Generating default!")

password_row = write_password(default_password)
return password_row

Expand Down
11 changes: 10 additions & 1 deletion hw_diag/utilities/network.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import logging
import subprocess # nosec

Expand Down Expand Up @@ -54,6 +55,7 @@ def setup_hostname():
# This runs before the Flask app is really fully running, so we do not have the
# global "g" object with the db session, so we must spawn our own.
db = get_db_session()
HOSTNAME_OVERRIDE = os.getenv('HOSTNAME_OVERRIDE', 'false')
HOSTNAME_SET_KEY = "hostname_set"
try:
try:
Expand All @@ -74,7 +76,14 @@ def setup_hostname():
# Set hostname via Balena supervisor...
default_password = generate_default_password()
hostname_suffix = default_password[6:]
hostname = "nebra-%s.local" % hostname_suffix

if HOSTNAME_OVERRIDE != "false":
hostname = HOSTNAME_OVERRIDE
logging.info("Using hostname override from env var!")
else:
hostname = "nebra-%s.local" % hostname_suffix
logging.info("No hostname override, using default!")

balena_supervisor = BalenaSupervisor.new_from_env()
balena_supervisor.set_hostname(hostname)
hostname_set_row.value = "true"
Expand Down
12 changes: 10 additions & 2 deletions hw_diag/views/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,17 @@ def get_password_change_form():
@authenticate
def handle_password_change():
current_password = request.form.get('txtOriginalPassword')
new_password = request.form.get('txtNewPassword')
confirm_password = request.form.get('txtConfirmPassword')
PASSWORD_OVERRIDE = os.getenv('PASSWORD_OVERRIDE', 'false')

if PASSWORD_OVERRIDE != "false": # nosec
new_password = PASSWORD_OVERRIDE
confirm_password = new_password
logging.info("Password overriden env var - not updating!")
else:
new_password = request.form.get('txtNewPassword')
confirm_password = request.form.get('txtConfirmPassword')
logging.info("No password override. Saving new password!")

result = update_password(
current_password,
new_password,
Expand Down
9 changes: 8 additions & 1 deletion hw_diag/views/diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,8 +376,15 @@ def shutdown():
@authenticate
def handle_hostname_update():
req_body = request.get_json()
new_hostname = req_body.get('hostname')
HOSTNAME_OVERRIDE = os.getenv('HOSTNAME_OVERRIDE', 'false')

if HOSTNAME_OVERRIDE != "false":
new_hostname = HOSTNAME_OVERRIDE
logging.info("Hostname overriden from env var. Not updating!")
else:
new_hostname = req_body.get('hostname')
logging.info("No hostname override, updating as per request!")

try:
balena_supervisor = BalenaSupervisor.new_from_env()
balena_supervisor.set_hostname(new_hostname)
Expand Down

0 comments on commit f404541

Please sign in to comment.