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

Add an all users option to list-domains action #68

Merged
merged 5 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions .trivyignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
CVE-2022-40897

Check notice on line 1 in .trivyignore

View workflow job for this annotation

GitHub Actions / integration-tests / Scan Image (ghcr.io-canonical-httprequest-lego-provider-51f1644caa97ab358d6a0412284c189f9bc14bf4-_0.1_amd64.tar)

CVE-2022-40897 not present anymore, can be safely removed.
CVE-2024-6345

Check notice on line 2 in .trivyignore

View workflow job for this annotation

GitHub Actions / integration-tests / Scan Image (ghcr.io-canonical-httprequest-lego-provider-51f1644caa97ab358d6a0412284c189f9bc14bf4-_0.1_amd64.tar)

CVE-2024-6345 not present anymore, can be safely removed.
CVE-2024-34156 # https://github.com/canonical/pebble/issues/498

Check notice on line 3 in .trivyignore

View workflow job for this annotation

GitHub Actions / integration-tests / Scan Image (ghcr.io-canonical-httprequest-lego-provider-51f1644caa97ab358d6a0412284c189f9bc14bf4-_0.1_amd64.tar)

CVE-2024-34156 # https://github.com/canonical/pebble/issues/498 not present anymore, can be safely removed.
26 changes: 17 additions & 9 deletions httprequest_lego_provider/management/commands/list_domains.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@


class Command(BaseCommand):
"""Command to list the domains an user has access to.
"""Command to list the domains a user has access to.

Attrs:
help: help message to display.
"""

help = "Create a user or update its password."
help = "List domains a user has access to. Use '*' for all users."

def add_arguments(self, parser):
"""Argument parser.
Expand All @@ -39,10 +39,18 @@ def handle(self, *args, **options):
CommandError: if the user is not found.
"""
username = options["username"]
try:
user = User.objects.get(username=username)
except User.DoesNotExist as exc:
raise CommandError(f'User "{username}" does not exist') from exc
dups = DomainUserPermission.objects.filter(user=user)

self.stdout.write(self.style.SUCCESS(", ".join([dup.domain.fqdn for dup in dups])))
if username == "*":
output = []
for user in User.objects.iterator():
output.append(f"{user}:")
dups = DomainUserPermission.objects.filter(user=user)
output.append(", ".join([dup.domain.fqdn for dup in dups]))
self.stdout.write(self.style.SUCCESS("\n".join(output)))
else:
try:
user = User.objects.get(username=username)
except User.DoesNotExist as exc:
raise CommandError(f'User "{username}" does not exist') from exc
dups = DomainUserPermission.objects.filter(user=user)

self.stdout.write(self.style.SUCCESS(", ".join([dup.domain.fqdn for dup in dups])))
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,26 @@ def test_list_domains(domain_user_permissions: list[DomainUserPermission]):
assert dup.domain.fqdn in out.getvalue()


@pytest.mark.django_db
def test_list_domains_all_users(domain_user_permissions: list[DomainUserPermission]):
"""
arrange: given existing domains allowed for all users.
act: call the list_domains command.
assert: the list of associated domains is returned in the stdout.
"""
out = StringIO()
call_command("list_domains", "*", stdout=out)
# Username on one line with a semi-colon followed by list of domains
# they have access to. Leading and trailing control characters for terminal
# output.
expected_output = (
"\x1b[32;1mtest_user:\n"
"_acme-challenge.some.com, _acme-challenge.example2.com, "
"_acme-challenge.example.es\x1b[0m\n"
)
assert out.getvalue() == expected_output


@pytest.mark.django_db
def test_list_domains_raises_exception(fqdns: list[str]):
"""
Expand Down
Loading