Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get htpaswd credentials from ansible variable #9

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions ansible/inventory/host_files/192.168.56.10/pandda.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,3 @@
protected_prefixes:
- 192.168.100.0/24
- 192.168.101.0/24
users:
- username: admin
password: admin
- username: local
password: local
2 changes: 2 additions & 0 deletions ansible/inventory/host_vars/192.168.56.10/pandda_vars.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
adict_gui_user: username
adict_gui_pass: password
6 changes: 5 additions & 1 deletion ansible/roles/nginx/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@

- name: Execute basic HTTP auth configuration generator
ansible.builtin.command:
cmd: "{{ pandda_conf_executable }} {{ pandda_conf_opt }}/adict_basic_auth_conf.py -f {{ pandda_conf_path }}adict.htpasswd"
cmd: >
{{ pandda_conf_executable }} {{ pandda_conf_opt }}/adict_basic_auth_conf.py
--config '[{username: {{ adict_gui_user }}, password: {{ adict_gui_pass }}}]'
--file {{ pandda_conf_path }}adict.htpasswd
no_log: true
changed_when: true
tags: configure

Expand Down
29 changes: 8 additions & 21 deletions ansible/roles/pandda_conf/files/adict_basic_auth_conf.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import sys
import os
import typing

import bcrypt
Expand All @@ -11,10 +10,10 @@ def generate_htpasswd_content(config: dict, output_file: typing.TextIO):
"""Generates htpasswd file content based on supplied central configuration file

Args:
config (dict): Central configuration file
config (dict): User configuration
output_file (TextIO): Output file handle
"""
for user in config["users"]:
for user in config:
# Data from config
username = user["username"]
password = user["password"]
Expand All @@ -32,8 +31,9 @@ def generate_htpasswd_content(config: dict, output_file: typing.TextIO):
"-c",
"--config",
dest="config",
default="/etc/pandda.d/pandda.yaml",
help="configuration file containing user settings",
help="configuration string containing user settings",
required=True,
type=str,
)
parser.add_argument(
"-f",
Expand All @@ -43,26 +43,13 @@ def generate_htpasswd_content(config: dict, output_file: typing.TextIO):
)
args = parser.parse_args()

# Load central config
adict_config = None
if os.path.exists(args.config):
with open(args.config, "r") as f:
pandda_config = yaml.safe_load(f)
for sub_config in pandda_config:
if "adict" in sub_config:
adict_config = sub_config["adict"]
break
else:
print("ADiCT section missing in the configuration file", file=sys.stderr)
exit(1)
else:
print(f"Configuration file {args.config} not found", file=sys.stderr)
exit(1)
# Load configuration
users_config = yaml.safe_load(args.config)

# Generate htpasswd
with open(args.file, "w") as f:
try:
htpasswd_content = generate_htpasswd_content(adict_config, f)
htpasswd_content = generate_htpasswd_content(users_config, f)
except KeyError as e:
print(f"Key missing in the configuration file: {e}", file=sys.stderr)
exit(1)