Skip to content

Permission refactor #4

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

Draft
wants to merge 27 commits into
base: api-auth
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
aa89acf
*/security_policy: remove `IAuthorizationPolicy`
tnytown May 25, 2023
58440fe
lints
tnytown May 25, 2023
d55b08a
test_security_policy: more robust multi tests
tnytown May 26, 2023
d2ac6c6
use the security policy that provided the identity for permits
dstufft Jun 3, 2023
30cfe8b
reimplement the 2FA mandate
dstufft Jun 3, 2023
ffeefdc
refactor common permits logic into a mixin
dstufft Jun 3, 2023
d5ad51e
function is just as good here and simpler
dstufft Jun 3, 2023
1ed7b63
fixes
dstufft Jun 3, 2023
1d74b53
we always have an active request now
dstufft Jun 3, 2023
46104e8
fix account tests
dstufft Jun 3, 2023
a0eb7b4
fix tests for macaroon policy
dstufft Jun 3, 2023
9fcc68a
fix tests
dstufft Jun 3, 2023
47f5e1d
more
dstufft Jun 3, 2023
7a50bc0
more
dstufft Jun 3, 2023
9183311
Merge branch 'main' into no-authz-policy
dstufft Jun 3, 2023
c2de958
more
dstufft Jun 3, 2023
d5b73e1
Merge branch 'main' into no-authz-policy
dstufft Jun 3, 2023
5657fd2
restore admin_dashboard_access
dstufft Jun 3, 2023
396d609
Restrict Security Policies based on route type
dstufft Jun 3, 2023
cbcaf85
setup our initial set of permissions
dstufft Jun 3, 2023
30939ad
refactor uploading to use new permissions
dstufft Jun 3, 2023
41dbda7
add another missing permission
dstufft Jun 3, 2023
397dcb5
refactor project permissions to use new perms
dstufft Jun 3, 2023
876224e
we do not expand an alias in an ACL
dstufft Jun 3, 2023
3e45a26
shift permission validation into caveats
dstufft Jun 3, 2023
69c739a
don't hardcode "upload" in macaroon caveat
dstufft Jun 3, 2023
d28dfc5
limit user created macaroons to upload only
dstufft Jun 4, 2023
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
24 changes: 6 additions & 18 deletions tests/unit/accounts/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,6 @@


class TestLogin:
def test_invalid_route(self, pyramid_request, pyramid_services):
service = pretend.stub(find_userid=pretend.call_recorder(lambda username: None))
pyramid_services.register_service(service, IUserService, None)
pyramid_services.register_service(
pretend.stub(), IPasswordBreachedService, None
)
pyramid_request.matched_route = pretend.stub(name="route_name")
assert _basic_auth_check("myuser", "mypass", pyramid_request) is False
assert service.find_userid.calls == []

def test_with_no_user(self, pyramid_request, pyramid_services):
service = pretend.stub(find_userid=pretend.call_recorder(lambda username: None))
pyramid_services.register_service(service, IUserService, None)
Expand Down Expand Up @@ -390,14 +380,8 @@ def test_unauthenticated_userid(self):


def test_includeme(monkeypatch):
authz_obj = pretend.stub()
authz_cls = pretend.call_recorder(lambda *a, **kw: authz_obj)
monkeypatch.setattr(accounts, "ACLAuthorizationPolicy", authz_cls)
monkeypatch.setattr(accounts, "MacaroonAuthorizationPolicy", authz_cls)
monkeypatch.setattr(accounts, "TwoFactorAuthorizationPolicy", authz_cls)

multi_policy_obj = pretend.stub()
multi_policy_cls = pretend.call_recorder(lambda ps, authz: multi_policy_obj)
multi_policy_cls = pretend.call_recorder(lambda ps: multi_policy_obj)
monkeypatch.setattr(accounts, "MultiSecurityPolicy", multi_policy_cls)

session_policy_obj = pretend.stub()
Expand Down Expand Up @@ -474,6 +458,10 @@ def test_includeme(monkeypatch):
assert config.set_security_policy.calls == [pretend.call(multi_policy_obj)]
assert multi_policy_cls.calls == [
pretend.call(
[session_policy_obj, basic_policy_obj, macaroon_policy_obj], authz_obj
[
session_policy_obj,
basic_policy_obj,
macaroon_policy_obj,
]
)
]
64 changes: 64 additions & 0 deletions tests/unit/accounts/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@
# limitations under the License.

import datetime
import uuid

import pytest

from pyramid.authorization import Authenticated

from warehouse.accounts.models import Email, RecoveryCode, User, UserFactory
from warehouse.utils.security_policy import principals_for

from ...common.db.accounts import (
EmailFactory as DBEmailFactory,
Expand Down Expand Up @@ -162,3 +166,63 @@ def test_acl(self, db_session):
("Allow", "group:admins", "admin"),
("Allow", "group:moderators", "moderator"),
]

@pytest.mark.parametrize(
(
"is_superuser",
"is_moderator",
"is_psf_staff",
"expected",
),
[
(False, False, False, []),
(
True,
False,
False,
["group:admins", "group:moderators", "group:psf_staff"],
),
(
False,
True,
False,
["group:moderators"],
),
(
True,
True,
False,
["group:admins", "group:moderators", "group:psf_staff"],
),
(
False,
False,
True,
["group:psf_staff"],
),
(
False,
True,
True,
["group:moderators", "group:psf_staff"],
),
],
)
def test_principals(
self,
# db_session,
is_superuser,
is_moderator,
is_psf_staff,
expected,
):
user = User(
id=uuid.uuid4(),
is_superuser=is_superuser,
is_moderator=is_moderator,
is_psf_staff=is_psf_staff,
)

expected = expected[:] + [f"user:{user.id}", Authenticated]

assert set(principals_for(user)) == set(expected)
Loading