oncall-engine/engine/apps/api/views/maintenance.py
Joey Orlando 9e598385f4
Add RBAC Support (#777)
* Modify plugin.json to support RBAC role registration

* defines 26 new custom roles in plugin.json. The main roles are:

- Admin: read/write access to everything in OnCall
- Reader: read access to everything in OnCall
- OnCaller : read access to everything in OnCall + edit access to Alert Groups and Schedules
- <object-type> Editor: read/write access to everything related to <object-type>
- <object-type> Reader: read access for <object-type>
- User Settings Admin: read/write access to all user's settings, not just own settings. This is in comparison to User Settings Editor which can only read/write own settings

* update changelog and documentation (#686)

* implement RBAC for OnCall backend

This commit refactors backend authorization. It trys to use RBAC authorization if the org's grafana instance supports it, otherwise it falls back to basic role authorization.

* update RBAC backend tests

* add tests for RBAC changes
- run backend tests as matrix where RBAC is enabled/disabled. When RBAC is enabled, the permissions granted are read from the role grants in the frontend's plugin.json file (instead of relying what we specify in RBACPermission.Permissions)
- remove --reuse-db --nomigrations flags from engine/tox.ini
- minor autoformatting changes to docker-compose-developer.yml

* remove --ds=settings.ci-test from pytest CI command

DJANGO_SETTINGS_MODULE is already specified as an env var so this is just unecessary duplication

* update gitignore

* update github action job name for "test"

* RBAC frontend changes

* refactors the use of basic roles (ex. Viewer, Editor, Admin) use RBAC permissions (when supported), or falling back to basic roles when RBAC is not supported.

- updates the UserAction enum in grafana-plugin/src/state/userAction.ts. Previously this was hardcoded to a list of strings that were being returned by the OnCall API. Now the values here correspond to the permissions in plugin.json (plus a fallback role)

* changes per Gabriel's comments:
- get rid of group attribute in rbac roles
- remove displayName role attribute
- remove hidden role attribute
- add back role to includes section

* don't try to update user timezone if they don't have permission
2022-11-29 09:41:56 +01:00

129 lines
4.9 KiB
Python

from rest_framework import status
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView
from apps.alerts.models import AlertReceiveChannel
from apps.alerts.models.maintainable_object import MaintainableObject
from apps.api.permissions import RBACPermission
from apps.auth_token.auth import PluginAuthentication
from common.api_helpers.exceptions import BadRequest
from common.exceptions import MaintenanceCouldNotBeStartedError
class GetObjectMixin:
def get_object(self, request):
organization = request.auth.organization
type = request.data.get("type", None)
if type == "organization":
instance = organization
elif type == "alert_receive_channel":
pk = request.data.get("alert_receive_channel_id", None)
if pk is not None:
try:
instance = AlertReceiveChannel.objects.get(
public_primary_key=pk,
organization=organization,
team=request.user.current_team,
)
except AlertReceiveChannel.DoesNotExist:
raise BadRequest(detail={"alert_receive_channel_id": ["unknown id"]})
else:
raise BadRequest(detail={"alert_receive_channel_id": ["id is required"]})
else:
raise BadRequest(detail={"type": ["Unknown type"]})
return instance
class MaintenanceAPIView(APIView):
authentication_classes = (PluginAuthentication,)
permission_classes = (IsAuthenticated, RBACPermission)
rbac_permissions = {
"get": [RBACPermission.Permissions.MAINTENANCE_READ],
}
def get(self, request):
organization = self.request.auth.organization
team = self.request.user.current_team
response = []
integrations_under_maintenance = AlertReceiveChannel.objects.filter(
maintenance_mode__isnull=False, organization=organization, team=team
).order_by("maintenance_started_at")
if organization.maintenance_mode is not None:
response.append(
{
"organization_id": organization.public_primary_key,
"type": "organization",
"maintenance_mode": organization.maintenance_mode,
"maintenance_till_timestamp": organization.till_maintenance_timestamp,
"started_at_timestamp": organization.started_at_timestamp,
}
)
for i in integrations_under_maintenance:
response.append(
{
"alert_receive_channel_id": i.public_primary_key,
"type": "alert_receive_channel",
"maintenance_mode": i.maintenance_mode,
"maintenance_till_timestamp": i.till_maintenance_timestamp,
"started_at_timestamp": i.started_at_timestamp,
}
)
return Response(response, status=200)
class MaintenanceStartAPIView(GetObjectMixin, APIView):
authentication_classes = (PluginAuthentication,)
permission_classes = (IsAuthenticated, RBACPermission)
rbac_permissions = {
"post": [RBACPermission.Permissions.MAINTENANCE_WRITE],
}
def post(self, request):
mode = request.data.get("mode", None)
duration = request.data.get("duration", None)
try:
mode = int(mode)
except (ValueError, TypeError):
raise BadRequest(detail={"mode": ["Invalid mode"]})
if mode not in [MaintainableObject.DEBUG_MAINTENANCE, MaintainableObject.MAINTENANCE]:
raise BadRequest(detail={"mode": ["Unknown mode"]})
try:
duration = int(duration)
except (ValueError, TypeError):
raise BadRequest(detail={"duration": ["Invalid duration"]})
if duration not in MaintainableObject.maintenance_duration_options_in_seconds():
raise BadRequest(detail={"mode": ["Unknown duration"]})
instance = self.get_object(request)
try:
instance.start_maintenance(mode, duration, request.user)
except MaintenanceCouldNotBeStartedError as e:
if type(instance) == AlertReceiveChannel:
detail = {"alert_receive_channel_id": ["Already on maintenance"]}
else:
detail = str(e)
raise BadRequest(detail=detail)
return Response(status=status.HTTP_200_OK)
class MaintenanceStopAPIView(GetObjectMixin, APIView):
authentication_classes = (PluginAuthentication,)
permission_classes = (IsAuthenticated, RBACPermission)
rbac_permissions = {
"post": [RBACPermission.Permissions.MAINTENANCE_WRITE],
}
def post(self, request):
instance = self.get_object(request)
user = request.user
instance.force_disable_maintenance(user)
return Response(status=status.HTTP_200_OK)