Skip to content

Commit

Permalink
feat(admin): implement basic admin user scopes
Browse files Browse the repository at this point in the history
  • Loading branch information
MeredithAnya committed Dec 15, 2022
1 parent 53d6f3e commit d8cbe6d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
11 changes: 10 additions & 1 deletion snuba/admin/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from flask import request

from snuba import settings
from snuba.admin.auth_scopes import ADMIN_SCOPES
from snuba.admin.jwt import validate_assertion
from snuba.admin.user import AdminUser

Expand All @@ -28,7 +29,15 @@ def authorize_request() -> AdminUser:
provider = AUTH_PROVIDERS.get(provider_id)
if provider is None:
raise ValueError("Invalid authorization provider")
return provider()

return _set_scopes(provider())


def _set_scopes(user: AdminUser) -> AdminUser:
# todo: depending on provider convert user email
# to subset of ADMIN_SCOPES based on IAM roles
user.scopes = ADMIN_SCOPES
return user


def passthrough_authorize() -> AdminUser:
Expand Down
43 changes: 43 additions & 0 deletions snuba/admin/auth_scopes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from dataclasses import dataclass
from enum import Enum
from typing import Set


class AdminRole(Enum):
ADMIN = "admin"
MEMBER = "member"
MEMBER_READ = "member_read"


ADMIN_CATEGORY_RESOURCES = {
# clickhouse migrations
"migrations.all",
"migrations.system",
"migrations.generic_metrics",
"migrations.profiles",
"migrations.functions",
"migrations.replays",
"migrations.querylog",
"migrations.test_migration",
}


@dataclass(frozen=True)
class AuthScope:
category: str
resource: str
role: AdminRole

def to_str(self) -> str:
return f"{self.category}.{self.resource}.{self.role.value}"


def scopes() -> Set[AuthScope]:
scopes = set()
for item in ADMIN_CATEGORY_RESOURCES:
category, resource = item.split(".")
scopes.update([AuthScope(category, resource, role) for role in AdminRole])
return scopes


ADMIN_SCOPES = scopes()
6 changes: 5 additions & 1 deletion snuba/admin/user.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from dataclasses import dataclass
from dataclasses import dataclass, field
from typing import Set

from snuba.admin.auth_scopes import AuthScope


@dataclass
Expand All @@ -10,3 +13,4 @@ class AdminUser:

email: str
id: str
scopes: Set[AuthScope] = field(default_factory=set)

0 comments on commit d8cbe6d

Please sign in to comment.