Skip to content

Commit 1ca452c

Browse files
author
Bokan Mohammad Assad
committed
change: linting all changed files in this branch
1 parent 6090e65 commit 1ca452c

File tree

5 files changed

+86
-42
lines changed

5 files changed

+86
-42
lines changed

README.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ configure aetos in `settings.py`:
2626

2727
.. code-block:: python
2828
29-
# on enabled ip allowlist with empty list, requests are denied
29+
# on enabled ip allowlist with empty list, requests are denied
3030
AETOS_ENABLE_IP_ALLOWLIST = True
3131
AETOS_IP_ALLOWLIST = ["127.0.0.1"]
3232

example_project/example_project/settings.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,4 @@
125125

126126
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
127127

128-
# WARNING: Do not set AETOS_* settings, otherwise tests fail.
128+
# WARNING: Do not set AETOS_* settings, otherwise tests fail.

example_project/tests.py

+65-26
Original file line numberDiff line numberDiff line change
@@ -9,106 +9,145 @@
99
universes_count 1
1010
"""
1111

12+
1213
@pytest.mark.django_db
1314
def test_e2e(client):
1415
resp = client.get("/metrics")
15-
assert (
16-
resp.content.decode()
17-
== expected_output
18-
)
16+
assert resp.content.decode() == expected_output
17+
1918

20-
@override_settings(AETOS_ENABLE_IP_ALLOWLIST=True, AETOS_IP_ALLOWLIST=["127.0.0.1"], AETOS_ENABLE_AUTH=True, AETOS_AUTH_TOKENLIST=["AhGei6ohghooDae"])
19+
@override_settings(
20+
AETOS_ENABLE_IP_ALLOWLIST=True,
21+
AETOS_IP_ALLOWLIST=["127.0.0.1"],
22+
AETOS_ENABLE_AUTH=True,
23+
AETOS_AUTH_TOKENLIST=["AhGei6ohghooDae"],
24+
)
2125
def test_settings():
2226
from django_aetos import app_settings
27+
2328
assert app_settings.ENABLE_IP_ALLOWLIST == True
2429
assert app_settings.IP_ALLOWLIST == ["127.0.0.1"]
2530
assert app_settings.ENABLE_AUTH == True
2631
assert app_settings.AUTH_TOKENLIST == ["AhGei6ohghooDae"]
27-
32+
33+
2834
def test_settings_defaults():
2935
from django_aetos import app_settings
36+
3037
assert app_settings.ENABLE_IP_ALLOWLIST == False
3138
assert app_settings.IP_ALLOWLIST == []
3239
assert app_settings.ENABLE_AUTH == False
3340
assert app_settings.AUTH_TOKENLIST == []
3441

42+
3543
@pytest.mark.django_db
3644
@override_settings(AETOS_ENABLE_IP_ALLOWLIST=True, AETOS_IP_ALLOWLIST=["127.0.0.1"])
3745
def test_enable_allowed_ips(client):
3846
resp = client.get("/metrics")
39-
assert (
40-
resp.content.decode()
41-
== expected_output
42-
)
47+
assert resp.content.decode() == expected_output
48+
4349

4450
@pytest.mark.django_db
4551
@override_settings(AETOS_ENABLE_IP_ALLOWLIST=True, AETOS_IP_ALLOWLIST=["255.0.0.1"])
4652
def test_enable_allowed_ips_not_allowed(client):
4753
resp = client.get("/metrics")
4854
assert resp.status_code == 401
4955

56+
5057
@pytest.mark.django_db
5158
@override_settings(AETOS_ENABLE_AUTH=True, AETOS_AUTH_TOKENLIST=["aquee4ro4Theeth"])
5259
def test_enable_auth(client):
5360
resp = client.get("/metrics", headers={"Authorization": "Bearer aquee4ro4Theeth"})
54-
assert (
55-
resp.content.decode()
56-
== expected_output
57-
)
61+
assert resp.content.decode() == expected_output
62+
5863

5964
@pytest.mark.django_db
6065
@override_settings(AETOS_ENABLE_AUTH=True, AETOS_AUTH_TOKENLIST=["aquee4ro4Theeth"])
6166
def test_enable_auth_token_not_allowed(client):
6267
resp = client.get("/metrics", headers={"Authorization": "Bearer wr0ngt0kenf"})
6368
assert resp.status_code == 401
6469

70+
6571
@pytest.mark.django_db
66-
@override_settings(AETOS_ENABLE_IP_ALLOWLIST=True, AETOS_IP_ALLOWLIST=["127.0.0.1"], AETOS_ENABLE_AUTH=True, AETOS_AUTH_TOKENLIST=["aquee4ro4Theeth"])
72+
@override_settings(
73+
AETOS_ENABLE_IP_ALLOWLIST=True,
74+
AETOS_IP_ALLOWLIST=["127.0.0.1"],
75+
AETOS_ENABLE_AUTH=True,
76+
AETOS_AUTH_TOKENLIST=["aquee4ro4Theeth"],
77+
)
6778
def test_enable_all(client):
6879
resp = client.get("/metrics", headers={"Authorization": "Bearer aquee4ro4Theeth"})
69-
assert (
70-
resp.content.decode()
71-
== expected_output
72-
)
80+
assert resp.content.decode() == expected_output
81+
7382

7483
@pytest.mark.django_db
75-
@override_settings(AETOS_ENABLE_IP_ALLOWLIST=True, AETOS_IP_ALLOWLIST=["255.0.0.1"], AETOS_ENABLE_AUTH=True, AETOS_AUTH_TOKENLIST=["aquee4ro4Theeth"])
84+
@override_settings(
85+
AETOS_ENABLE_IP_ALLOWLIST=True,
86+
AETOS_IP_ALLOWLIST=["255.0.0.1"],
87+
AETOS_ENABLE_AUTH=True,
88+
AETOS_AUTH_TOKENLIST=["aquee4ro4Theeth"],
89+
)
7690
def test_enable_all_wrong_ip(client):
7791
resp = client.get("/metrics", headers={"Authorization": "Bearer aquee4ro4Theeth"})
7892
assert resp.status_code == 401
7993

94+
8095
@pytest.mark.django_db
81-
@override_settings(AETOS_ENABLE_IP_ALLOWLIST=True, AETOS_IP_ALLOWLIST=["127.0.0.1"], AETOS_ENABLE_AUTH=True, AETOS_AUTH_TOKENLIST=["aquee4ro4Theeth"])
96+
@override_settings(
97+
AETOS_ENABLE_IP_ALLOWLIST=True,
98+
AETOS_IP_ALLOWLIST=["127.0.0.1"],
99+
AETOS_ENABLE_AUTH=True,
100+
AETOS_AUTH_TOKENLIST=["aquee4ro4Theeth"],
101+
)
82102
def test_enable_all_wrong_token(client):
83103
resp = client.get("/metrics", headers={"Authorization": "Bearer wr0ngt0ken"})
84104
assert resp.status_code == 401
85105

106+
86107
@pytest.mark.django_db
87-
@override_settings(AETOS_ENABLE_IP_ALLOWLIST=True, AETOS_IP_ALLOWLIST=["255.0.0.1"], AETOS_ENABLE_AUTH=True, AETOS_AUTH_TOKENLIST=["aquee4ro4Theeth"])
108+
@override_settings(
109+
AETOS_ENABLE_IP_ALLOWLIST=True,
110+
AETOS_IP_ALLOWLIST=["255.0.0.1"],
111+
AETOS_ENABLE_AUTH=True,
112+
AETOS_AUTH_TOKENLIST=["aquee4ro4Theeth"],
113+
)
88114
def test_enable_all_wrong_token_ip(client):
89115
resp = client.get("/metrics", headers={"Authorization": "Bearer wr0ngt0ken"})
90116
assert resp.status_code == 401
91117

118+
92119
@pytest.mark.django_db
93-
@override_settings(AETOS_ENABLE_IP_ALLOWLIST=True, AETOS_ENABLE_AUTH=True, AETOS_AUTH_TOKENLIST=["aquee4ro4Theeth"])
120+
@override_settings(
121+
AETOS_ENABLE_IP_ALLOWLIST=True, AETOS_ENABLE_AUTH=True, AETOS_AUTH_TOKENLIST=["aquee4ro4Theeth"]
122+
)
94123
def test_enable_all_empty_ip(client):
95124
resp = client.get("/metrics", headers={"Authorization": "Bearer aquee4ro4Theeth"})
96125
assert resp.status_code == 401
97126

127+
98128
@pytest.mark.django_db
99-
@override_settings(AETOS_ENABLE_IP_ALLOWLIST=True, AETOS_IP_ALLOWLIST=["127.0.0.1"], AETOS_ENABLE_AUTH=True)
129+
@override_settings(
130+
AETOS_ENABLE_IP_ALLOWLIST=True, AETOS_IP_ALLOWLIST=["127.0.0.1"], AETOS_ENABLE_AUTH=True
131+
)
100132
def test_enable_all_empty_token(client):
101133
resp = client.get("/metrics", headers={"Authorization": "Bearer aquee4ro4Theeth"})
102134
assert resp.status_code == 401
103135

136+
104137
@pytest.mark.django_db
105138
@override_settings(AETOS_ENABLE_IP_ALLOWLIST=True, AETOS_ENABLE_AUTH=True)
106139
def test_enable_all_empty_token_ip(client):
107140
resp = client.get("/metrics", headers={"Authorization": "Bearer aquee4ro4Theeth"})
108141
assert resp.status_code == 401
109142

143+
110144
@pytest.mark.django_db
111-
@override_settings(AETOS_ENABLE_IP_ALLOWLIST=True, AETOS_IP_ALLOWLIST=["127.0.0.1"], AETOS_ENABLE_AUTH=True, AETOS_AUTH_TOKENLIST=["aquee4ro4Theeth"])
145+
@override_settings(
146+
AETOS_ENABLE_IP_ALLOWLIST=True,
147+
AETOS_IP_ALLOWLIST=["127.0.0.1"],
148+
AETOS_ENABLE_AUTH=True,
149+
AETOS_AUTH_TOKENLIST=["aquee4ro4Theeth"],
150+
)
112151
def test_enable_all_wrong_auth_header(client):
113152
resp = client.get("/metrics", headers={"Authorization": "Basic aquee4ro4Theeth"})
114-
assert resp.status_code == 401
153+
assert resp.status_code == 401

src/django_aetos/app_settings.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,32 @@
1-
21
class AppSettings:
32
def __init__(self, prefix):
43
self.prefix = prefix
5-
4+
65
def _setting(self, name, dflt):
76
from django.conf import settings
8-
7+
98
return getattr(settings, self.prefix + name, dflt)
10-
9+
1110
@property
1211
def ENABLE_IP_ALLOWLIST(self):
1312
return self._setting("ENABLE_IP_ALLOWLIST", False)
1413

1514
@property
1615
def IP_ALLOWLIST(self):
1716
return self._setting("IP_ALLOWLIST", [])
18-
17+
1918
@property
2019
def ENABLE_AUTH(self):
2120
return self._setting("ENABLE_AUTH", False)
22-
21+
2322
@property
2423
def AUTH_TOKENLIST(self):
2524
return self._setting("AUTH_TOKENLIST", [])
2625

26+
2727
_app_settings = AppSettings("AETOS_")
2828

29+
2930
def __getattr__(name):
3031
# See https://peps.python.org/pep-0562/
31-
return getattr(_app_settings, name)
32+
return getattr(_app_settings, name)

src/django_aetos/views.py

+11-7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from .signals import collect_metrics
88

9+
910
def collect_response():
1011
metrics = []
1112
known_metrics = set()
@@ -19,35 +20,38 @@ def collect_response():
1920
metric["add_help"] = True
2021
known_metrics.add(metric["name_without_labels"])
2122
metrics.append(metric)
22-
23+
2324
return metrics
2425

26+
2527
def check_auth(request):
26-
ip_address = request.META['REMOTE_ADDR']
28+
ip_address = request.META["REMOTE_ADDR"]
2729
allowed_ips = app_settings.IP_ALLOWLIST
28-
30+
2931
if app_settings.ENABLE_IP_ALLOWLIST and ip_address in allowed_ips:
3032
return True
3133
elif not app_settings.ENABLE_IP_ALLOWLIST:
3234
return True
3335
else:
3436
return False
3537

38+
3639
def check_ips(request):
3740
try:
38-
authorization_header = request.META['HTTP_AUTHORIZATION']
41+
authorization_header = request.META["HTTP_AUTHORIZATION"]
3942
except KeyError:
4043
authorization_header = ""
41-
type, sep, token = authorization_header.partition(' ')
44+
type, sep, token = authorization_header.partition(" ")
4245
allowed_tokens = app_settings.AUTH_TOKENLIST
43-
46+
4447
if app_settings.ENABLE_AUTH and type == "Bearer" and token in allowed_tokens:
4548
return True
4649
elif not app_settings.ENABLE_AUTH:
4750
return True
4851
else:
4952
return False
5053

54+
5155
def export_metrics(request):
5256
if check_auth(request) and check_ips(request):
5357
response = render(
@@ -57,7 +61,7 @@ def export_metrics(request):
5761
content_type="text/plain",
5862
)
5963
response.content = re.sub(b"\n+", b"\n", response.content)
60-
64+
6165
return response
6266
else:
6367
return HttpResponse(status=401)

0 commit comments

Comments
 (0)