http://hg.code.sf.net:8000/p/roundup/code/atom-log/tip/roundup/rate_limit.py Mercurial Repository: p/roundup/code: roundup/rate_limit.py history 2025-11-03T00:13:04-05:00 refactor: change some classes to use __slots__ http://hg.code.sf.net:8000/p/roundup/code/#changeset-224ccb8b49ca54569350edb5905b19185a97b092 John Rouillard rouilj@ieee.org 2025-11-03T00:13:04-05:00 2025-11-03T00:13:04-05:00
changeset 224ccb8b49ca
branch
bookmark
tag
user John Rouillard <rouilj@ieee.org>
description refactor: change some classes to use __slots__

Speed up access to and reduce size of some low level classes. A few
classes in security.py, rest.py are heavily used. But for all, it
prevents adding random properties to lower level classes that people
shouldn't be mucking with. While doing this I found some test cases
accessing an invalid property name and this change caused the cases to
crash.

admin.py:

Use new method Role.props_dict() and Permission.props_dict() where
original code just referenced __dict__ when printing Role/Permission.

mlink_expr.py:

Add slots to multiple classes.

Classes Binary and Unary set real properties/attributes. Classes that
inherit from them (Equals, Empty, Not, Or, And) define empty slots
tuple to eliminate need for __dict__.

Class Expression also gets a slot.

rate_limit.py:

RateLimit and Gcra classes get slots.

A couple of pep8 fixes: sort imports, remove trailing spaces on a
line, remove unused noqa comment.

rest.py:

Add slots to class SimulateFieldStorageFromJson and FsValue
classes. The memory savings from this could be useful as well as
speedier access to the attributes.

security.py:

Add slots to Permission class. To prevent conflict between slot
limit_perm_to_props_only and the class variable of the same name,
rename the class variable to limit_perm_to_props_only_default.
Also define method props_dict() to allow other code to get a dict to
iterate over when checking permissions.

Add slots to class Role along with props_dict() method.

Add slots to class Security. Also have to add explicit __dict__ slot
to support test override of the hasPermission() method. Add
props_dict() method, currently unused, but added for symmetry.

support.py:

TruthDict and PrioList gets slots.

test/test_cgi.py:

Fix incorrect setting of permission property. Was setting
permissions. So testing may not have been doing what we thought it
was. Multiple places found with this typo.

Remove setting of permissions in some places where it should
have no effect on the test and looks like it was just copypasta.

test/test_xmlrpc.py

Remove setting of permissions in some places where it should
have no effect on the test and looks like it was just copypasta.
files
fix: issue2551278 - datetime.datetime.utcnow deprecation. http://hg.code.sf.net:8000/p/roundup/code/#changeset-5fbd3af526bd12136554052d2ceacd1efb8f4cd6 John Rouillard rouilj@ieee.org 2023-07-27T00:53:36-04:00 2023-07-27T00:53:36-04:00
changeset 5fbd3af526bd
branch
bookmark
tag
user John Rouillard <rouilj@ieee.org>
description fix: issue2551278 - datetime.datetime.utcnow deprecation.

We now use the timezone aware utc dates for python 3.11+.

But we have to make all the rest of the dates (datetime.min, unix
epoch date) timezon aware so we can subtract them. Also need to
marshall/unmarshall timezone aware iso formatted date strings.
files
fix: issue2551278 - datetime.datetime.utcnow deprecation. http://hg.code.sf.net:8000/p/roundup/code/#changeset-8f29e4ea05ce7ac8e19f5a1e93856878b5e1fd21 John Rouillard rouilj@ieee.org 2023-07-25T16:30:10-04:00 2023-07-25T16:30:10-04:00
changeset 8f29e4ea05ce
branch
bookmark
tag
user John Rouillard <rouilj@ieee.org>
description fix: issue2551278 - datetime.datetime.utcnow deprecation.

Replace calls with equivalent that produces timezone aware dates
rather than naive dates.

Also some flake8 fixes for test/rest_common.py.
files
Make rate_limit.py pass flake8. http://hg.code.sf.net:8000/p/roundup/code/#changeset-69a35d164a6921ac52e5523061f326f6b93e8f91 John Rouillard rouilj@ieee.org 2019-12-25T20:00:04-05:00 2019-12-25T20:00:04-05:00
changeset 69a35d164a69
branch
bookmark
tag
user John Rouillard <rouilj@ieee.org>
description Make rate_limit.py pass flake8.

Fix up some formatting and other issues. I did noqa a line wrap as
breaking the line into multiple parts with proper indenting would make
it unintelligable.
files
fix rate limit headers - were ints/floats need to be strings http://hg.code.sf.net:8000/p/roundup/code/#changeset-5d0873a4de4a9a5e59dc7e95115a4330951cad2b John Rouillard rouilj@ieee.org 2019-10-20T20:56:56-04:00 2019-10-20T20:56:56-04:00
changeset 5d0873a4de4a
branch
bookmark
tag
user John Rouillard <rouilj@ieee.org>
description fix rate limit headers - were ints/floats need to be strings

Running under gunicorn rest requests were crashing. Not all of the
values for the rate limit headers were strings. Some were
numbers. This caused the header generation for wsgi to fail. Now the
values are all strings.
files
Run pylint and clean up it's issues. Also fix comment. http://hg.code.sf.net:8000/p/roundup/code/#changeset-e225f403cc35c0d774bc2f6ad8923bea4eb030f3 John Rouillard rouilj@ieee.org 2019-05-28T18:10:30-04:00 2019-05-28T18:10:30-04:00
changeset e225f403cc35
branch
bookmark
tag
user John Rouillard <rouilj@ieee.org>
description Run pylint and clean up it's issues. Also fix comment.
files
Rename Store class in rate_limit.py to Gcra. The name Store makes no http://hg.code.sf.net:8000/p/roundup/code/#changeset-2f116ba7e7cf933908259f7b0f0e0edd21e0c6a9 John Rouillard rouilj@ieee.org 2019-05-17T19:45:15-04:00 2019-05-17T19:45:15-04:00
changeset 2f116ba7e7cf
branch
bookmark
tag
user John Rouillard <rouilj@ieee.org>
description Rename Store class in rate_limit.py to Gcra. The name Store makes no
sense since the class implements a Gcra, I'll call it Gcra....
files
issue2550949: Rate limit password guesses/login attempts. http://hg.code.sf.net:8000/p/roundup/code/#changeset-cad18de2b9887b18e004eb0dcab1716d33d6f6a8 John Rouillard rouilj@ieee.org 2019-05-11T17:24:58-04:00 2019-05-11T17:24:58-04:00
changeset cad18de2b988
branch
bookmark
tag
user John Rouillard <rouilj@ieee.org>
description issue2550949: Rate limit password guesses/login attempts.

Generic rate limit mechanism added. Deployed for web page
logins. Default is 3 login attempts/minute for a user. After which one
login attempt every 20 seconds can be done.

Uses gcra algorithm so all I need to store is a username and timestamp
in the one time key database. This does mean I don't have a list of
all failed login attempts as part of the rate limiter.

Set up config setting as well so admin can tune the rate. Maybe 1
every 10 seconds is ok at a site with poor typists who need 6 attempts
to get the password right 8-).

The gcra method can also be used to limit the rest and xmlrpc
interfaces if needed. The mechanism I added also supplies a status
method that calculates the expected values for http headers returned
as part of rate limiting.

Also tests added to test all code paths I hope.
files