Skip to content

Commit

Permalink
qbittorrentvpn: generate default credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
saltydk committed Dec 17, 2023
1 parent 586b4ea commit 4917904
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
41 changes: 41 additions & 0 deletions library/qbittorrent_passwd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/python
from ansible.module_utils.basic import AnsibleModule
import base64
import hashlib
import os

def qbittorrent_passwd(plain_passwd):
try:
ITERATIONS = 100_000
SALT_SIZE = 16

salt = os.urandom(SALT_SIZE)
h = hashlib.pbkdf2_hmac("sha512", plain_passwd.encode(), salt, ITERATIONS)
return "@ByteArray({}:{})".format(base64.b64encode(salt).decode(), base64.b64encode(h).decode())
except Exception as e:
raise ValueError(f"Error generating password hash: {str(e)}")

def main():
module_args = dict(
password=dict(type='str', required=True, no_log=True)
)

result = dict(
changed=False,
msg=''
)

module = AnsibleModule(
argument_spec=module_args,
supports_check_mode=True
)

try:
result['msg'] = qbittorrent_passwd(module.params['password'])
except ValueError as err:
module.fail_json(msg=str(err))

module.exit_json(**result)

if __name__ == '__main__':
main()
27 changes: 27 additions & 0 deletions roles/qbittorrentvpn/tasks/subtasks/post-install/settings/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,30 @@
owner: "{{ user.name }}"
group: "{{ user.name }}"
mode: "0775"

- name: Post-Install | Settings | Generate Password Hash
qbittorrent_passwd:
password: "{{ user.pass }}"
register: qbittorrent_hashed_passwd

- name: Post-Install | Settings | Set qBittorrent 'WebUI\Username'
community.general.ini_file:
section: Preferences
option: WebUI\Username
value: '{{ user.name }}'
path: "{{ qbittorrentvpn_paths_conf }}"
state: present
owner: "{{ user.name }}"
group: "{{ user.name }}"
mode: "0775"

- name: Post-Install | Settings | Set qBittorrent 'WebUI\Password_PBKDF2'
community.general.ini_file:
section: Preferences
option: WebUI\Password_PBKDF2
value: '{{ qbittorrent_hashed_passwd.msg }}'
path: "{{ qbittorrentvpn_paths_conf }}"
state: present
owner: "{{ user.name }}"
group: "{{ user.name }}"
mode: "0775"

0 comments on commit 4917904

Please sign in to comment.