Skip to content
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

Add permissions checks to assignments #173

Merged
Merged
Changes from all commits
Commits
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
69 changes: 63 additions & 6 deletions internal/api/assignments.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,40 @@ func (r *Router) assignmentCreate(c echo.Context) error {
return echo.NewHTTPError(http.StatusBadRequest, "error parsing request body").SetInternal(err)
}

subjID, err := gidx.Parse(reqBody.SubjectID)
assigneeID, err := gidx.Parse(reqBody.SubjectID)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "error parsing subject ID").SetInternal(err)
}

subjResource, err := r.engine.NewResourceFromID(subjID)
assigneeResource, err := r.engine.NewResourceFromID(assigneeID)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "error creating resource").SetInternal(err)
}

subjectResource, err := r.currentSubject(c)
if err != nil {
return err
}

roleResource, err := r.engine.NewResourceFromID(roleID)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "error getting resource").SetInternal(err)
}

resource, err := r.engine.GetRoleResource(ctx, roleResource, "")
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "error getting resource").SetInternal(err)
}

if err := r.checkActionWithResponse(ctx, subjectResource, actionRoleUpdate, resource); err != nil {
return err
}

role := types.Role{
ID: roleID,
}

_, err = r.engine.AssignSubjectRole(ctx, subjResource, role)
_, err = r.engine.AssignSubjectRole(ctx, assigneeResource, role)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "error creating resource").SetInternal(err)
}
Expand All @@ -66,6 +85,25 @@ func (r *Router) assignmentsList(c echo.Context) error {
ctx, span := tracer.Start(c.Request().Context(), "api.assignmentCreate", trace.WithAttributes(attribute.String("role_id", roleIDStr)))
defer span.End()

subjectResource, err := r.currentSubject(c)
if err != nil {
return err
}

roleResource, err := r.engine.NewResourceFromID(roleID)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "error getting resource").SetInternal(err)
}

resource, err := r.engine.GetRoleResource(ctx, roleResource, "")
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "error getting resource").SetInternal(err)
}

if err := r.checkActionWithResponse(ctx, subjectResource, actionRoleGet, resource); err != nil {
return err
}

role := types.Role{
ID: roleID,
}
Expand Down Expand Up @@ -110,21 +148,40 @@ func (r *Router) assignmentDelete(c echo.Context) error {
return echo.NewHTTPError(http.StatusBadRequest, "error parsing request body").SetInternal(err)
}

subjID, err := gidx.Parse(reqBody.SubjectID)
assigneeID, err := gidx.Parse(reqBody.SubjectID)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "error parsing subject ID").SetInternal(err)
}

subjResource, err := r.engine.NewResourceFromID(subjID)
assigneeResource, err := r.engine.NewResourceFromID(assigneeID)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "error parsing resource type from subject").SetInternal(err)
}

subjectResource, err := r.currentSubject(c)
if err != nil {
return err
}

roleResource, err := r.engine.NewResourceFromID(roleID)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "error getting resource").SetInternal(err)
}

resource, err := r.engine.GetRoleResource(ctx, roleResource, "")
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "error getting resource").SetInternal(err)
}

if err := r.checkActionWithResponse(ctx, subjectResource, actionRoleUpdate, resource); err != nil {
mikemrm marked this conversation as resolved.
Show resolved Hide resolved
return err
}

role := types.Role{
ID: roleID,
}

_, err = r.engine.UnassignSubjectRole(ctx, subjResource, role)
_, err = r.engine.UnassignSubjectRole(ctx, assigneeResource, role)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "error deleting assignment").SetInternal(err)
}
Expand Down
Loading