From 11d473e536e080a539ab738db0dd93c335dfae2c Mon Sep 17 00:00:00 2001 From: Ben Muthalaly Date: Sat, 14 Oct 2023 00:38:04 -0500 Subject: [PATCH 1/5] Add config options to add admin user on first run --- archivebox/config.py | 2 ++ archivebox/main.py | 7 ++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/archivebox/config.py b/archivebox/config.py index 795b98e9..6fd6621c 100644 --- a/archivebox/config.py +++ b/archivebox/config.py @@ -91,6 +91,8 @@ CONFIG_SCHEMA: Dict[str, ConfigDefaultDict] = { 'OUTPUT_PERMISSIONS': {'type': str, 'default': '644'}, 'RESTRICT_FILE_NAMES': {'type': str, 'default': 'windows'}, 'URL_BLACKLIST': {'type': str, 'default': r'\.(css|js|otf|ttf|woff|woff2|gstatic\.com|googleapis\.com/css)(\?.*)?$'}, # to avoid downloading code assets as their own pages + 'ARCHIVEBOX_USERNAME': {'type': str, 'default': None}, + 'ARCHIVEBOX_PASSWORD': {'type': str, 'default': None}, 'URL_WHITELIST': {'type': str, 'default': None}, 'ENFORCE_ATOMIC_WRITES': {'type': bool, 'default': True}, 'TAG_SEPARATOR_PATTERN': {'type': str, 'default': r'[,]'}, diff --git a/archivebox/main.py b/archivebox/main.py index 5878185c..87dd8899 100755 --- a/archivebox/main.py +++ b/archivebox/main.py @@ -112,6 +112,8 @@ from .config import ( load_all_config, CONFIG, USER_CONFIG, + ARCHIVEBOX_USERNAME, + ARCHIVEBOX_PASSWORD, get_real_name, setup_django, ) @@ -422,7 +424,10 @@ def init(force: bool=False, quick: bool=False, setup: bool=False, out_dir: Path= if existing_index: print('{green}[√] Done. Verified and updated the existing ArchiveBox collection.{reset}'.format(**ANSI)) else: - # TODO: allow creating new supersuer via env vars on first init + if ARCHIVEBOX_USERNAME and ARCHIVEBOX_PASSWORD: + print('{green}[+] ARCHIVEBOX_USERNAME and ARCHIVEBOX_PASSWORD configuration options found. Creating new admin user with username {} and password {}.{reset}'.format(ARCHIVEBOX_USERNAME, ARCHIVEBOX_PASSWORD, **ANSI)) + from django.contrib.auth.models import User + User.objects.create_superuser(username=ARCHIVEBOX_USERNAME, password=ARCHIVEBOX_PASSWORD) # if config.HTTP_USER and config.HTTP_PASS: # from django.contrib.auth.models import User # User.objects.create_superuser(HTTP_USER, '', HTTP_PASS) From 44a94157beaa319f04efacd06a7a20daf7443e6f Mon Sep 17 00:00:00 2001 From: Ben Muthalaly Date: Sun, 15 Oct 2023 23:36:47 -0500 Subject: [PATCH 2/5] Remove logging of configured username and password --- archivebox/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/archivebox/main.py b/archivebox/main.py index 87dd8899..5c6a3fd7 100755 --- a/archivebox/main.py +++ b/archivebox/main.py @@ -425,7 +425,7 @@ def init(force: bool=False, quick: bool=False, setup: bool=False, out_dir: Path= print('{green}[√] Done. Verified and updated the existing ArchiveBox collection.{reset}'.format(**ANSI)) else: if ARCHIVEBOX_USERNAME and ARCHIVEBOX_PASSWORD: - print('{green}[+] ARCHIVEBOX_USERNAME and ARCHIVEBOX_PASSWORD configuration options found. Creating new admin user with username {} and password {}.{reset}'.format(ARCHIVEBOX_USERNAME, ARCHIVEBOX_PASSWORD, **ANSI)) + print('{green}[+] ARCHIVEBOX_USERNAME and ARCHIVEBOX_PASSWORD configuration options found. Creating new admin user.{reset}'.format(**ANSI)) from django.contrib.auth.models import User User.objects.create_superuser(username=ARCHIVEBOX_USERNAME, password=ARCHIVEBOX_PASSWORD) # if config.HTTP_USER and config.HTTP_PASS: From 521ea70e0cdf209dde850661e17a6bb3de3e9d34 Mon Sep 17 00:00:00 2001 From: Ben Muthalaly Date: Wed, 18 Oct 2023 03:07:54 -0500 Subject: [PATCH 3/5] Add check for existing user, change varable names --- archivebox/config.py | 4 ++-- archivebox/main.py | 19 +++++++++---------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/archivebox/config.py b/archivebox/config.py index 6fd6621c..719ee34a 100644 --- a/archivebox/config.py +++ b/archivebox/config.py @@ -91,8 +91,8 @@ CONFIG_SCHEMA: Dict[str, ConfigDefaultDict] = { 'OUTPUT_PERMISSIONS': {'type': str, 'default': '644'}, 'RESTRICT_FILE_NAMES': {'type': str, 'default': 'windows'}, 'URL_BLACKLIST': {'type': str, 'default': r'\.(css|js|otf|ttf|woff|woff2|gstatic\.com|googleapis\.com/css)(\?.*)?$'}, # to avoid downloading code assets as their own pages - 'ARCHIVEBOX_USERNAME': {'type': str, 'default': None}, - 'ARCHIVEBOX_PASSWORD': {'type': str, 'default': None}, + 'ADMIN_USERNAME': {'type': str, 'default': None}, + 'ADMIN_PASSWORD': {'type': str, 'default': None}, 'URL_WHITELIST': {'type': str, 'default': None}, 'ENFORCE_ATOMIC_WRITES': {'type': bool, 'default': True}, 'TAG_SEPARATOR_PATTERN': {'type': str, 'default': r'[,]'}, diff --git a/archivebox/main.py b/archivebox/main.py index 5c6a3fd7..e811a496 100755 --- a/archivebox/main.py +++ b/archivebox/main.py @@ -112,8 +112,8 @@ from .config import ( load_all_config, CONFIG, USER_CONFIG, - ARCHIVEBOX_USERNAME, - ARCHIVEBOX_PASSWORD, + ADMIN_USERNAME, + ADMIN_PASSWORD, get_real_name, setup_django, ) @@ -421,17 +421,16 @@ def init(force: bool=False, quick: bool=False, setup: bool=False, out_dir: Path= write_main_index(list(pending_links.values()), out_dir=out_dir) print('\n{green}----------------------------------------------------------------------{reset}'.format(**ANSI)) + + from django.contrib.auth.models import User + + if (ADMIN_USERNAME and ADMIN_PASSWORD) and not User.objects.filter(username=ADMIN_USERNAME, is_superuser=True).exists(): + User.objects.create_superuser(username=ADMIN_USERNAME, password=ADMIN_PASSWORD) + print('{green}[+] New ADMIN_USERNAME and ADMIN_PASSWORD configuration options found. Creating new admin user.{reset}'.format(ADMIN_USERNAME, ADMIN_PASSWORD, **ANSI)) + if existing_index: print('{green}[√] Done. Verified and updated the existing ArchiveBox collection.{reset}'.format(**ANSI)) else: - if ARCHIVEBOX_USERNAME and ARCHIVEBOX_PASSWORD: - print('{green}[+] ARCHIVEBOX_USERNAME and ARCHIVEBOX_PASSWORD configuration options found. Creating new admin user.{reset}'.format(**ANSI)) - from django.contrib.auth.models import User - User.objects.create_superuser(username=ARCHIVEBOX_USERNAME, password=ARCHIVEBOX_PASSWORD) - # if config.HTTP_USER and config.HTTP_PASS: - # from django.contrib.auth.models import User - # User.objects.create_superuser(HTTP_USER, '', HTTP_PASS) - print('{green}[√] Done. A new ArchiveBox collection was initialized ({} links).{reset}'.format(len(all_links) + len(pending_links), **ANSI)) json_index = out_dir / JSON_INDEX_FILENAME From 9e6a87114b4b550e814856a00d1ab38bb030b2cc Mon Sep 17 00:00:00 2001 From: Ben Muthalaly Date: Wed, 18 Oct 2023 12:07:36 -0500 Subject: [PATCH 4/5] Fix formatting, logging, logic issues --- archivebox/config.py | 4 ++-- archivebox/main.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/archivebox/config.py b/archivebox/config.py index 719ee34a..64d3d0a2 100644 --- a/archivebox/config.py +++ b/archivebox/config.py @@ -91,8 +91,8 @@ CONFIG_SCHEMA: Dict[str, ConfigDefaultDict] = { 'OUTPUT_PERMISSIONS': {'type': str, 'default': '644'}, 'RESTRICT_FILE_NAMES': {'type': str, 'default': 'windows'}, 'URL_BLACKLIST': {'type': str, 'default': r'\.(css|js|otf|ttf|woff|woff2|gstatic\.com|googleapis\.com/css)(\?.*)?$'}, # to avoid downloading code assets as their own pages - 'ADMIN_USERNAME': {'type': str, 'default': None}, - 'ADMIN_PASSWORD': {'type': str, 'default': None}, + 'ADMIN_USERNAME': {'type': str, 'default': None}, + 'ADMIN_PASSWORD': {'type': str, 'default': None}, 'URL_WHITELIST': {'type': str, 'default': None}, 'ENFORCE_ATOMIC_WRITES': {'type': bool, 'default': True}, 'TAG_SEPARATOR_PATTERN': {'type': str, 'default': r'[,]'}, diff --git a/archivebox/main.py b/archivebox/main.py index e811a496..b3c59633 100755 --- a/archivebox/main.py +++ b/archivebox/main.py @@ -424,9 +424,9 @@ def init(force: bool=False, quick: bool=False, setup: bool=False, out_dir: Path= from django.contrib.auth.models import User - if (ADMIN_USERNAME and ADMIN_PASSWORD) and not User.objects.filter(username=ADMIN_USERNAME, is_superuser=True).exists(): + if (ADMIN_USERNAME and ADMIN_PASSWORD) and not User.objects.filter(username=ADMIN_USERNAME).exists(): User.objects.create_superuser(username=ADMIN_USERNAME, password=ADMIN_PASSWORD) - print('{green}[+] New ADMIN_USERNAME and ADMIN_PASSWORD configuration options found. Creating new admin user.{reset}'.format(ADMIN_USERNAME, ADMIN_PASSWORD, **ANSI)) + print('{green}[+] Found ADMIN_USERNAME and ADMIN_PASSWORD configuration options, creating new admin user.{reset}'.format(**ANSI)) if existing_index: print('{green}[√] Done. Verified and updated the existing ArchiveBox collection.{reset}'.format(**ANSI)) From d286dca925fc0f96ba1d7fbb04961b2783283022 Mon Sep 17 00:00:00 2001 From: Nick Sweeting Date: Wed, 18 Oct 2023 11:47:55 -0700 Subject: [PATCH 5/5] better to log before doing a thing than after --- archivebox/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/archivebox/main.py b/archivebox/main.py index b3c59633..5268691b 100755 --- a/archivebox/main.py +++ b/archivebox/main.py @@ -425,8 +425,8 @@ def init(force: bool=False, quick: bool=False, setup: bool=False, out_dir: Path= from django.contrib.auth.models import User if (ADMIN_USERNAME and ADMIN_PASSWORD) and not User.objects.filter(username=ADMIN_USERNAME).exists(): - User.objects.create_superuser(username=ADMIN_USERNAME, password=ADMIN_PASSWORD) print('{green}[+] Found ADMIN_USERNAME and ADMIN_PASSWORD configuration options, creating new admin user.{reset}'.format(**ANSI)) + User.objects.create_superuser(username=ADMIN_USERNAME, password=ADMIN_PASSWORD) if existing_index: print('{green}[√] Done. Verified and updated the existing ArchiveBox collection.{reset}'.format(**ANSI))