• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mozilla / fx-private-relay / aa5dd6a6-0e37-47d5-bd00-91a02cab55fb

13 May 2025 04:17PM UTC coverage: 85.204% (-0.04%) from 85.243%
aa5dd6a6-0e37-47d5-bd00-91a02cab55fb

Pull #5550

circleci

groovecoder
for MPP-3957: start update_fxrelay_allowlist_collection command
Pull Request #5550: for MPP-3957: start update_fxrelay_allowlist_collection command

2467 of 3607 branches covered (68.39%)

Branch coverage included in aggregate %.

67 of 86 new or added lines in 3 files covered. (77.91%)

1 existing line in 1 file now uncovered.

17383 of 19690 relevant lines covered (88.28%)

9.65 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

69.57
/privaterelay/management/commands/update_fxrelay_allowlist_collection.py
1
from django.conf import settings
1✔
2
from django.core.management.base import BaseCommand
1✔
3

4
from kinto_http import Client, KintoException
1✔
5
import requests
1✔
6

7
BUCKET = settings.KINTO_BUCKET
1✔
8
COLLECTION = settings.KINTO_COLLECTION
1✔
9

10

11
class Command(BaseCommand):
1✔
12
    help = "Updates the Firefox Relay allowlist Kinto collection from a JSON source."
1✔
13

14
    def handle(self, *args, **options):
1✔
15
        print(f"Connecting to ☁️ {settings.KINTO_SERVER}...")
1✔
16
        client = Client(
1✔
17
            server_url=settings.KINTO_SERVER, auth=settings.KINTO_AUTH_TOKEN
18
        )
19
        try:
1✔
20
            client.server_info()
1✔
21
            print("Connected to Kinto server successfully.")
1✔
NEW
22
        except Exception as e:
×
NEW
23
            self.stderr.write(f"❌ Failed to connect to Kinto server: {e}")
×
NEW
24
            return
×
25

26
        print(f"Ensuring 🪣 bucket {BUCKET} exists.")
1✔
27
        try:
1✔
28
            client.get_bucket(id=BUCKET)
1✔
29
            print(f"Bucket {BUCKET} already exists.")
1✔
NEW
30
        except KintoException:
×
NEW
31
            print(f"Bucket {BUCKET} not found. Creating...")
×
NEW
32
            try:
×
NEW
33
                client.create_bucket(id=BUCKET)
×
NEW
34
                print(f"Bucket {BUCKET} created.")
×
NEW
35
            except KintoException as e:
×
NEW
36
                self.stderr.write(f"❌ Failed to find or create bucket: {e}")
×
NEW
37
                return
×
38

39
        print(f"Ensuring 📁 collection {COLLECTION} exists.")
1✔
40
        try:
1✔
41
            client.get_collection(id=COLLECTION, bucket=BUCKET)
1✔
42
            print(f"Collection {COLLECTION} already exists.")
1✔
NEW
43
        except KintoException:
×
NEW
44
            print(f"Collection {COLLECTION} not found. Creating...")
×
NEW
45
            try:
×
NEW
46
                client.create_collection(id=COLLECTION, bucket=BUCKET)
×
NEW
47
                print(f"Collection {COLLECTION} created.")
×
NEW
48
            except KintoException as e:
×
NEW
49
                self.stderr.write(f"❌ Failed to find or create collection: {e}")
×
NEW
50
                return
×
51

52
        print(f"Loading new allowlist from 🌐 {settings.ALLOWLIST_JSON_INPUT_URL}.")
1✔
53
        response = requests.get(settings.ALLOWLIST_JSON_INPUT_URL)
1✔
54
        response.raise_for_status()
1✔
55
        new_allowlist = response.content.decode()
1✔
56

57
        new_domains = set(filter(None, new_allowlist.split("\n")))
1✔
58
        print(f"Parsed {len(new_domains)} from {settings.ALLOWLIST_JSON_INPUT_URL}.")
1✔
59

60
        print(
1✔
61
            f"Getting existing domain records from ☁️ {settings.KINTO_SERVER}, 🪣 bucket {BUCKET}, 📁 collection {COLLECTION}"
62
        )
63
        existing_records = client.get_records(bucket=BUCKET, collection=COLLECTION)
1✔
64
        existing_domains = {rec["domain"] for rec in existing_records}
1✔
65
        print(f"Found {len(existing_domains)} existing domains.")
1✔
66

67
        # Delete records:
68
        # 1. no longer in the domain allowlist
69
        # 2. where id does not match domain
70
        for existing_record in existing_records:
1✔
71
            if (
1!
72
                existing_record["domain"] not in new_domains
73
                or existing_record["domain"] != existing_record["id"]
74
            ):
75
                print(f'🗑 removed domain: {existing_record["domain"]}')
1✔
76
                client.delete_record(
1✔
77
                    id=existing_record["id"], bucket=BUCKET, collection=COLLECTION
78
                )
79

80
        # Add new records for new domains in allowlist
81
        for domain in new_domains:
1✔
82
            if domain not in existing_domains:
1!
83
                print(f"✚ new domain: {domain}")
1✔
84
                record = {
1✔
85
                    "domain": domain,
86
                }
87
                client.create_record(
1✔
88
                    id=domain, data=record, bucket=BUCKET, collection=COLLECTION
89
                )
90

91
        print("Allowlist synchronized 🔄 successfully. ✅")
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc