-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.py
310 lines (260 loc) · 10.2 KB
/
auth.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
"""
Flask Blueprint containing all behaviour related to the authentication and
authorization of users/clients
Copyright 2022 Tom Brouwer
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
from pathlib import Path
import ipaddress
import datetime
import logging
from flask import Blueprint, request, abort, g, current_app
from flask.blueprints import BlueprintSetupState
import fastjsonschema
from . import yaml, tools
bp = Blueprint('auth', __name__)
_ERRORS = [
(
429, 'auth-rate-limited',
'Rate limited because of too many false auth attempts from this ip',
),
(
403, 'unauthorized-ip',
'You are not authorized to access the server from this IP',
),
(
403, 'unauthorized-path',
'You are not authorized to access this path on the server',
),
]
_AUDIT_EVENTS = [
(
'unauthenticated', logging.WARNING,
'Unauthenticated request to {path} from {ip}',
),
(
'rate-limited', logging.WARNING,
'Requests from {ip} are rate limited by the auth module',
),
(
'unauthorized-ip', logging.WARNING,
"User '{user}' connected from unauthorized IP {ip}",
),
(
'unauthorized-path', logging.WARNING,
"User '{user}' tried to access unauthorized path: {path}",
),
]
# Load the schema to validate the users file against
module_dir = Path(__file__).absolute().parent
_users_schema = yaml.safe_load_file(
Path(module_dir, 'schemas/scs-users.yaml'),
)
validate_user_configuration = fastjsonschema.compile(_users_schema)
class _RateLimiter:
"""
Tracks invalid authentication attempts for the current 15 minute interval
and blacklists IPs when too many invalid authentication attempts were made.
"""
def __init__(self, *, max_auth_fails_per_15_min: int):
self._current_window = self._get_window_id()
self._invalid_auth_attempts = {}
self.max_auth_fails = max_auth_fails_per_15_min
def register_attempt(self, ip: str):
invalid_auth_attempts = self.invalid_auth_attempts
if ip not in invalid_auth_attempts:
invalid_auth_attempts[ip] = 0
invalid_auth_attempts[ip] += 1
def is_limited(self, ip: str) -> bool:
return self.invalid_auth_attempts.get(ip, 0) >= self.max_auth_fails
@property
def invalid_auth_attempts(self) -> dict:
window_id = self._get_window_id()
# Reset attempts if a new window is reached
if self._current_window != window_id:
self._current_window = window_id
self._invalid_auth_attempts = {}
return self._invalid_auth_attempts
def _get_window_id(self) -> str:
"""
Get the id of the current 15 minute window
"""
now = datetime.datetime.utcnow()
minute_rounded = now.minute - (now.minute % 15)
return f'{now.year}{now.month}{now.day}{now.hour}{minute_rounded}'
class NetworkWhitelist:
"""
A network whitelist with a method to check if another network is included
in the whitelist
"""
def __init__(self, networks: list[str], private_only: bool = False):
"""
Initialzes the NetworkWhitelist instance
Args:
networks:
List with specific IPs, Subnet CIDR notations or both. All
items are passed to the ipaddress.ip_network() function.
private_only:
If True, the networks must contain only private subnets
"""
self.ipv4_networks = []
self.ipv6_networks = []
for network in networks:
ip_network = ipaddress.ip_network(network)
if private_only and not ip_network.is_private:
raise ValueError(
f'private_only enabled, but network {network} is not private!' # noqa:E501
)
if isinstance(ip_network, ipaddress.IPv4Network):
self.ipv4_networks.append(ip_network)
else:
self.ipv6_networks.append(ip_network)
def contains(self, network: str) -> bool:
"""
Check if the whitelist contains the provided network
Args:
network:
A specific IP or CIDR notation
"""
ip_network = ipaddress.ip_network(network)
if isinstance(ip_network, ipaddress.IPv4Network):
whitelist = self.ipv4_networks
else:
whitelist = self.ipv6_networks
for whitelisted_network in whitelist:
if ip_network.subnet_of(whitelisted_network):
return True
return False
def issubset(self, other_network_whitelist) -> bool:
"""
Checks if this whitelist is fully covered by another whitelist
Args:
network_whitelist:
Another NetworkWhitelist instance
"""
network_types = ['ipv4_networks', 'ipv6_networks']
for network_type in network_types:
networks = getattr(self, network_type)
other_networks = getattr(other_network_whitelist, network_type)
for network in networks:
is_subnet_of_other = False
for other_network in other_networks:
if network.subnet_of(other_network):
is_subnet_of_other = True
break
if not is_subnet_of_other:
return False
return True
@bp.record
def init(setup_state: BlueprintSetupState):
"""
Initializes the authentication module and loads the user data
Args:
setup_state:
The .options attribute (options parameter passed to
register_blueprint) should be a dict with the key/value pairs:
users_file: pathlib.Path
directories: dict
secrets: string
networks: dict
private_only: bool
whitelist: list[str]
max_auth_fails_per_15_min: bool
"""
options = setup_state.options
users_file_path = Path(options['users_file'])
private_networks_only = options['networks']['private_only']
network_whitelist = options['networks']['whitelist']
secrets_dir = options['directories']['secrets']
max_auth_fails_per_15_min = options['max_auth_fails_per_15_min']
validate_dots = setup_state.app.config['SCS']['environments'][
'reject_keys_containing_dots'
]
setup_state.app.scs._auth_rate_limiter = _RateLimiter(
max_auth_fails_per_15_min=max_auth_fails_per_15_min
)
secrets_constructor = yaml.SCSSecretConstructor(
secrets_dir=secrets_dir,
validate_dots=validate_dots
)
class SCSUsersFileLoader(yaml.SCSYamlLoader):
"""
Loader for the user configuration file
Local class used to prevent re-use accross multiple apps
"""
pass
SCSUsersFileLoader.add_constructor(
secrets_constructor.tag, secrets_constructor.construct
)
scs_users = yaml.SecretsSerializedList(
yaml.load_file(users_file_path, loader=SCSUsersFileLoader)
)
scs_users = validate_user_configuration(scs_users.data)
global_whitelist = NetworkWhitelist(
network_whitelist, private_only=private_networks_only,
)
setup_state.app.scs._auth_global_whitelist = global_whitelist
_auth_mapping = {}
for user in scs_users:
_auth_mapping[user.pop('token')] = user
parsed_whitelist = NetworkWhitelist(
user['has_access']['from_networks']
)
if not parsed_whitelist.issubset(global_whitelist):
raise ValueError(
f'Network whitelist of user {user["id"]} not fully covered'
' by global whitelist'
)
user['has_access']['from_networks'] = parsed_whitelist
user['has_access']['to_paths'] = [
tools.build_pattern_from_path(p)
for p in user['has_access']['to_paths']
]
setup_state.app.scs._auth_mapping = _auth_mapping
for error_args in _ERRORS:
setup_state.app.scs.register_error(*error_args)
for audit_event_args in _AUDIT_EVENTS:
setup_state.app.scs.register_audit_event(*audit_event_args)
@bp.before_app_request
def check_auth():
"""
Validates if user authentication credentials are valid, and if the given
user is authorized to make the request.
"""
if not current_app.scs._auth_global_whitelist.contains(request.remote_addr): # noqa:E501
# Check the global whitelist before hitting the rate-limiter, so
# non-whitelisted IPs cannot be used to reduce the changes of rate
# limiting. The failed attempt is not logs, since otherwise attackers
# could flood the logs
abort(403, description={'id': 'unauthorized-ip'})
if current_app.scs._auth_rate_limiter.is_limited(request.remote_addr):
g.add_audit_event(event_type='rate-limited')
abort(429, description={'id': 'auth-rate-limited'})
try:
auth_header = request.headers['Authorization']
token = auth_header.removeprefix('Bearer ')
user = current_app.scs._auth_mapping[token]
g.user_id = user['id']
except KeyError:
current_app.scs._auth_rate_limiter.register_attempt(
request.remote_addr
)
g.add_audit_event(event_type='unauthenticated')
abort(401)
if not user['has_access']['from_networks'].contains(request.remote_addr):
g.add_audit_event(event_type='unauthorized-ip')
abort(403, description={'id': 'unauthorized-ip'})
for pattern in user['has_access']['to_paths']:
if pattern.match(request.path):
break
else:
g.add_audit_event(event_type='unauthorized-path')
abort(403, description={'id': 'unauthorized-path'})