Skip to content
This repository has been archived by the owner on Oct 11, 2024. It is now read-only.

Commit

Permalink
feat: Updates permissions & telemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
frgfm committed Oct 30, 2023
1 parent ea453d1 commit f3c7f79
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/app/api/api_v1/endpoints/compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async def analyze_snippet(
guidelines: GuidelineCRUD = Depends(get_guideline_crud),
user=Security(get_current_user, scopes=[UserScope.ADMIN, UserScope.USER]),
) -> List[ComplianceResult]:
telemetry_client.capture(user.id, event="snippet-analysis", properties={"repo_id": payload.repo_id})
telemetry_client.capture(user.id, event="compute-analysis", properties={"repo_id": payload.repo_id})

Check warning on line 27 in src/app/api/api_v1/endpoints/compute.py

View check run for this annotation

Codecov / codecov/patch

src/app/api/api_v1/endpoints/compute.py#L27

Added line #L27 was not covered by tests
# Check repo
await repos.get(payload.repo_id, strict=True)
# Fetch guidelines
Expand Down
13 changes: 8 additions & 5 deletions src/app/api/api_v1/endpoints/guidelines.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,28 @@ async def create_guideline(
guidelines: GuidelineCRUD = Depends(get_guideline_crud),
user=Security(get_current_user, scopes=[UserScope.USER, UserScope.ADMIN]),
) -> Guideline:
guideline = await guidelines.create(payload)
telemetry_client.capture(user.id, event="guideline-creation", properties={"repo_id": payload.repo_id})
guideline = await guidelines.create(payload)

Check warning on line 27 in src/app/api/api_v1/endpoints/guidelines.py

View check run for this annotation

Codecov / codecov/patch

src/app/api/api_v1/endpoints/guidelines.py#L27

Added line #L27 was not covered by tests
return guideline


@router.get("/{guideline_id}", status_code=status.HTTP_200_OK)
async def get_guideline(
guideline_id: int = Path(..., gt=0),
guidelines: GuidelineCRUD = Depends(get_guideline_crud),
_=Security(get_current_user, scopes=[UserScope.USER, UserScope.ADMIN]),
user=Security(get_current_user, scopes=[UserScope.USER, UserScope.ADMIN]),
) -> Guideline:
return cast(Guideline, await guidelines.get(guideline_id, strict=True))
guideline = cast(Guideline, await guidelines.get(guideline_id, strict=True))
telemetry_client.capture(user.id, event="guideline-get", properties={"repo_id": guideline.repo_id})
return guideline

Check warning on line 39 in src/app/api/api_v1/endpoints/guidelines.py

View check run for this annotation

Codecov / codecov/patch

src/app/api/api_v1/endpoints/guidelines.py#L37-L39

Added lines #L37 - L39 were not covered by tests


@router.get("/", status_code=status.HTTP_200_OK)
async def fetch_guidelines(
guidelines: GuidelineCRUD = Depends(get_guideline_crud),
_=Security(get_current_user, scopes=[UserScope.ADMIN]),
user=Security(get_current_user, scopes=[UserScope.ADMIN]),
) -> List[Guideline]:
telemetry_client.capture(user.id, event="guideline-fetch")

Check warning on line 47 in src/app/api/api_v1/endpoints/guidelines.py

View check run for this annotation

Codecov / codecov/patch

src/app/api/api_v1/endpoints/guidelines.py#L47

Added line #L47 was not covered by tests
return [elt for elt in await guidelines.fetch_all()]


Expand Down Expand Up @@ -76,5 +79,5 @@ async def delete_guideline(
user=Security(get_current_user, scopes=[UserScope.USER, UserScope.ADMIN]),
) -> None:
guideline = cast(Guideline, await guidelines.get(guideline_id, strict=True))
await guidelines.delete(guideline_id)
telemetry_client.capture(user.id, event="guideline-deletion", properties={"repo_id": guideline.repo_id})
await guidelines.delete(guideline_id)

Check warning on line 83 in src/app/api/api_v1/endpoints/guidelines.py

View check run for this annotation

Codecov / codecov/patch

src/app/api/api_v1/endpoints/guidelines.py#L83

Added line #L83 was not covered by tests
2 changes: 1 addition & 1 deletion src/app/api/api_v1/endpoints/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ async def login_with_creds(
user = await users.get_by_login(form_data.username)
if user is None or not await verify_password(form_data.password, user.hashed_password):
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid credentials.")
telemetry_client.capture(user.id, event="user-login", properties={"login": user.login})

Check warning on line 72 in src/app/api/api_v1/endpoints/login.py

View check run for this annotation

Codecov / codecov/patch

src/app/api/api_v1/endpoints/login.py#L72

Added line #L72 was not covered by tests
# create access token using user user_id/user_scopes
token_data = {"sub": str(user.id), "scopes": user.scope.split()}
token = await create_access_token(token_data, settings.ACCESS_TOKEN_UNLIMITED_MINUTES)
telemetry_client.capture(user.id, event="user-login", properties={"login": user.login})

return Token(access_token=token, token_type="bearer") # nosec B106 # noqa S106

Expand Down
19 changes: 9 additions & 10 deletions src/app/api/api_v1/endpoints/repos.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ async def create_repo(
async def get_repo(
repo_id: int = Path(..., gt=0),
repos: RepositoryCRUD = Depends(get_repo_crud),
_=Security(get_current_user, scopes=[UserScope.USER, UserScope.ADMIN]),
user=Security(get_current_user, scopes=[UserScope.USER, UserScope.ADMIN]),
) -> Repository:
telemetry_client.capture(user.id, event="repo-get", properties={"repo_id": repo_id})

Check warning on line 51 in src/app/api/api_v1/endpoints/repos.py

View check run for this annotation

Codecov / codecov/patch

src/app/api/api_v1/endpoints/repos.py#L51

Added line #L51 was not covered by tests
return cast(Repository, await repos.get(repo_id, strict=True))


Expand All @@ -56,8 +57,8 @@ async def fetch_repos(
repos: RepositoryCRUD = Depends(get_repo_crud),
user=Security(get_current_user, scopes=[UserScope.USER, UserScope.ADMIN]),
) -> List[Repository]:
entries = await repos.fetch_all() if user.scope == UserScope.ADMIN else await repos.fetch_all(("owner_id", user.id))
telemetry_client.capture(user.id, event="repo-fetch")
entries = await repos.fetch_all() if user.scope == UserScope.ADMIN else await repos.fetch_all(("owner_id", user.id))

Check warning on line 61 in src/app/api/api_v1/endpoints/repos.py

View check run for this annotation

Codecov / codecov/patch

src/app/api/api_v1/endpoints/repos.py#L61

Added line #L61 was not covered by tests
return [elt for elt in entries]


Expand All @@ -68,6 +69,7 @@ async def reorder_guidelines(
guidelines: GuidelineCRUD = Depends(get_guideline_crud),
user=Security(get_current_user, scopes=[UserScope.USER, UserScope.ADMIN]),
) -> List[Guideline]:
telemetry_client.capture(user.id, event="guideline-order", properties={"repo_id": repo_id})

Check warning on line 72 in src/app/api/api_v1/endpoints/repos.py

View check run for this annotation

Codecov / codecov/patch

src/app/api/api_v1/endpoints/repos.py#L72

Added line #L72 was not covered by tests
# Ensure all IDs are unique
if len(payload.guideline_ids) != len(set(payload.guideline_ids)):
raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail="Duplicate IDs were passed.")
Expand All @@ -78,12 +80,10 @@ async def reorder_guidelines(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail="Guideline IDs for that repo don't match."
)
# Update all order
guideline_list = [
return [

Check warning on line 83 in src/app/api/api_v1/endpoints/repos.py

View check run for this annotation

Codecov / codecov/patch

src/app/api/api_v1/endpoints/repos.py#L83

Added line #L83 was not covered by tests
await guidelines.update(guideline_id, OrderUpdate(order=order_idx, updated_at=datetime.utcnow()))
for order_idx, guideline_id in enumerate(payload.guideline_ids)
]
telemetry_client.capture(user.id, event="guideline-order", properties={"repo_id": repo_id})
return guideline_list


@router.put("/{repo_id}/disable", status_code=status.HTTP_200_OK)
Expand All @@ -92,9 +92,8 @@ async def disable_repo(
repos: RepositoryCRUD = Depends(get_repo_crud),
user=Security(get_current_user, scopes=[UserScope.USER, UserScope.ADMIN]),
) -> Repository:
repo = await repos.update(repo_id, RepoUpdate(removed_at=datetime.utcnow()))
telemetry_client.capture(user.id, event="repo-disable", properties={"repo_id": repo_id})
return repo
return await repos.update(repo_id, RepoUpdate(removed_at=datetime.utcnow()))

Check warning on line 96 in src/app/api/api_v1/endpoints/repos.py

View check run for this annotation

Codecov / codecov/patch

src/app/api/api_v1/endpoints/repos.py#L96

Added line #L96 was not covered by tests


@router.put("/{repo_id}/enable", status_code=status.HTTP_200_OK)
Expand All @@ -103,9 +102,8 @@ async def enable_repo(
repos: RepositoryCRUD = Depends(get_repo_crud),
user=Security(get_current_user, scopes=[UserScope.USER, UserScope.ADMIN]),
) -> Repository:
repo = await repos.update(repo_id, RepoUpdate(removed_at=None))
telemetry_client.capture(user.id, event="repo-enable", properties={"repo_id": repo_id})
return repo
return await repos.update(repo_id, RepoUpdate(removed_at=None))

Check warning on line 106 in src/app/api/api_v1/endpoints/repos.py

View check run for this annotation

Codecov / codecov/patch

src/app/api/api_v1/endpoints/repos.py#L106

Added line #L106 was not covered by tests


@router.delete("/{repo_id}", status_code=status.HTTP_200_OK)
Expand All @@ -114,8 +112,8 @@ async def delete_repo(
repos: RepositoryCRUD = Depends(get_repo_crud),
user=Security(get_current_user, scopes=[UserScope.ADMIN]),
) -> None:
await repos.delete(repo_id)
telemetry_client.capture(user.id, event="repo-delete", properties={"repo_id": repo_id})
await repos.delete(repo_id)

Check warning on line 116 in src/app/api/api_v1/endpoints/repos.py

View check run for this annotation

Codecov / codecov/patch

src/app/api/api_v1/endpoints/repos.py#L116

Added line #L116 was not covered by tests


@router.get("/{repo_id}/guidelines", status_code=status.HTTP_200_OK)
Expand All @@ -125,4 +123,5 @@ async def fetch_guidelines_from_repo(
repos: RepositoryCRUD = Depends(get_repo_crud),
user=Security(get_current_user, scopes=[UserScope.ADMIN, UserScope.USER]),
) -> List[Guideline]:
telemetry_client.capture(user.id, event="repo-fetch-guidelines", properties={"repo_id": repo_id})

Check warning on line 126 in src/app/api/api_v1/endpoints/repos.py

View check run for this annotation

Codecov / codecov/patch

src/app/api/api_v1/endpoints/repos.py#L126

Added line #L126 was not covered by tests
return [elt for elt in await guidelines.fetch_all(("repo_id", repo_id))]
13 changes: 8 additions & 5 deletions src/app/api/api_v1/endpoints/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,32 @@ async def create_user(
users: UserCRUD = Depends(get_user_crud),
_=Security(get_current_user, scopes=[UserScope.ADMIN]),
) -> User:
telemetry_client.capture(payload.id, event="user-creation", properties={"login": payload.login})

Check warning on line 26 in src/app/api/api_v1/endpoints/users.py

View check run for this annotation

Codecov / codecov/patch

src/app/api/api_v1/endpoints/users.py#L26

Added line #L26 was not covered by tests
# Hash the password
pwd = await hash_password(payload.password)

user = await users.create(
UserCreation(id=payload.id, login=payload.login, hashed_password=pwd, scope=payload.scope)
)
telemetry_client.capture(payload.id, event="user-creation", properties={"login": payload.login})
return user


@router.get("/{user_id}", status_code=status.HTTP_200_OK)
async def get_user(
user_id: int = Path(..., gt=0),
users: UserCRUD = Depends(get_user_crud),
_=Security(get_current_user, scopes=[UserScope.ADMIN]),
user=Security(get_current_user, scopes=[UserScope.ADMIN]),
) -> User:
telemetry_client.capture(user.id, event="user-get", properties={"user_id": user_id})

Check warning on line 42 in src/app/api/api_v1/endpoints/users.py

View check run for this annotation

Codecov / codecov/patch

src/app/api/api_v1/endpoints/users.py#L42

Added line #L42 was not covered by tests
return cast(User, await users.get(user_id, strict=True))


@router.get("/", status_code=status.HTTP_200_OK)
async def fetch_users(
users: UserCRUD = Depends(get_user_crud),
_=Security(get_current_user, scopes=[UserScope.ADMIN]),
user=Security(get_current_user, scopes=[UserScope.ADMIN]),
) -> List[User]:
telemetry_client.capture(user.id, event="user-fetch")

Check warning on line 51 in src/app/api/api_v1/endpoints/users.py

View check run for this annotation

Codecov / codecov/patch

src/app/api/api_v1/endpoints/users.py#L51

Added line #L51 was not covered by tests
return [elt for elt in await users.fetch_all()]


Expand All @@ -55,8 +57,9 @@ async def update_user_password(
payload: Cred,
user_id: int = Path(..., gt=0),
users: UserCRUD = Depends(get_user_crud),
_=Security(get_current_user, scopes=[UserScope.ADMIN]),
user=Security(get_current_user, scopes=[UserScope.ADMIN]),
) -> User:
telemetry_client.capture(user.id, event="user-pwd", properties={"user_id": user_id})

Check warning on line 62 in src/app/api/api_v1/endpoints/users.py

View check run for this annotation

Codecov / codecov/patch

src/app/api/api_v1/endpoints/users.py#L62

Added line #L62 was not covered by tests
pwd = await hash_password(payload.password)
return await users.update(user_id, CredHash(hashed_password=pwd))

Expand All @@ -67,5 +70,5 @@ async def delete_user(
users: UserCRUD = Depends(get_user_crud),
user=Security(get_current_user, scopes=[UserScope.ADMIN]),
) -> None:
telemetry_client.capture(user_id, event="user-deletion", properties={"user_id": user_id})

Check warning on line 73 in src/app/api/api_v1/endpoints/users.py

View check run for this annotation

Codecov / codecov/patch

src/app/api/api_v1/endpoints/users.py#L73

Added line #L73 was not covered by tests
await users.delete(user_id)
telemetry_client.capture(user_id, event="user-deletion", properties={"login": user.login})

0 comments on commit f3c7f79

Please sign in to comment.